-
Notifications
You must be signed in to change notification settings - Fork 109
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
Fix association #60
Fix association #60
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,8 @@ | ||
class Blueprinter::AssociationSerializer < Blueprinter::Serializer | ||
def serialize(association_name, object, local_options, options={}) | ||
if options[:blueprint] | ||
view = options[:view] || :default | ||
options[:blueprint].prepare(object.public_send(association_name), view_name: view, local_options: local_options) | ||
else | ||
object.public_send(association_name) | ||
end | ||
value = object.public_send(association_name) | ||
return value if value.nil? | ||
view = options[:view] || :default | ||
options[:blueprint].prepare(value, view_name: view, local_options: local_options) | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -67,6 +67,40 @@ | |
let(:vehicle) { create(:vehicle) } | ||
|
||
include_examples 'Base::render' | ||
|
||
context 'Given blueprint has ::association' do | ||
let(:result) do | ||
'{"id":' + obj_id + ',"vehicles":[{"make":"Super Car"}]}' | ||
end | ||
let(:blueprint_without_associated_blueprint) do | ||
Class.new(Blueprinter::Base) do | ||
identifier :id | ||
association :vehicles | ||
end | ||
end | ||
before { vehicle.update(user: obj) } | ||
context 'Given associated blueprint is given' do | ||
let(:blueprint) do | ||
vehicle_blueprint = Class.new(Blueprinter::Base) do | ||
fields :make | ||
end | ||
Class.new(Blueprinter::Base) do | ||
identifier :id | ||
association :vehicles, blueprint: vehicle_blueprint | ||
end | ||
end | ||
it('returns json with association') { should eq(result) } | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is so implicit and confusing. Maybe we should switch to MiniTest 😜 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ok i admin i over-utilized some of rspec's "features" that does make it quite implicit. I'll make more explicit specs going forward =D |
||
end | ||
context 'Given no associated blueprint is given' do | ||
let(:blueprint) do | ||
Class.new(Blueprinter::Base) do | ||
identifier :id | ||
association :vehicles | ||
end | ||
end | ||
it { expect{subject}.to raise_error(Blueprinter::BlueprinterError) } | ||
end | ||
end | ||
end | ||
end | ||
end |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So, I'm not sure if you intended this or not, but a side effect of this code is that it makes it so that you now always have to have a Blueprint for every association. I think that could be considered a reasonable tradeoff, but I want to make sure we're explicitly making that decision.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ya, so I do not understand how it would work if the associated object does not have a blueprint.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am for having the requirement that any object being serialized within this system must use a blueprint... I've personally been bitten by having both AMS and jbuilder for the same resource far too many times to try to fashion some coexistence between any of them.