Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Accept an option 'default' to allow setting default serialized value for association if null #78

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,20 @@ Output:
}
```

#### Default option
By default, an association that evaluates to `nil` is serialized as `nil`. A default serialized value can be
specified as option on the association for cases when the association could potentially evaluate to `nil`.
```ruby
class UserBlueprint < Blueprinter::Base
identifier :uuid

view :normal do
fields :first_name, :last_name
association :company, blueprint: CompanyBlueprint, default: {}
end
end
```

### Defining a field directly in the Blueprint

You can define a field directly in the Blueprint by passing it a block. This is especially useful if the object does not already have such an attribute or method defined, and you want to define it specifically for use with the Blueprint. For example:
Expand Down
4 changes: 2 additions & 2 deletions lib/blueprinter/extractors/association_extractor.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
module Blueprinter
class AssociationExtractor < Extractor
def extract(association_name, object, local_options, options={})
value = object.public_send(association_name)
return value if value.nil?
value = object.public_send(association_name) || options[:default]
return value if value.blank?
view = options[:view] || :default
options[:blueprint].prepare(value, view_name: view, local_options: local_options)
end
Expand Down
34 changes: 34 additions & 0 deletions spec/integrations/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,40 @@
it { expect{subject}.to raise_error(Blueprinter::BlueprinterError) }
end
end

context "Given association is nil" do
before do
expect(vehicle).to receive(:user).and_return(nil)
end

context "Given default association value is not provided" do
let(:blueprint) do
vehicle_blueprint = Class.new(Blueprinter::Base) do
fields :make
association :user, blueprint: Class.new(Blueprinter::Base) { identifier :id }
end
end

it "should render the association as nil" do
expect(JSON.parse(blueprint.render(vehicle))["user"]).to be_nil
end
end

context "Given default association value is provided" do
let(:blueprint) do
vehicle_blueprint = Class.new(Blueprinter::Base) do
fields :make
association :user,
blueprint: Class.new(Blueprinter::Base) { identifier :id },
default: {}
end
end

it "should render the default value provided for the association" do
expect(JSON.parse(blueprint.render(vehicle))["user"]).to eq({})
end
end
end
end
end
describe '::render_as_hash' do
Expand Down