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

Do not overwrite overridden fields and associations #389

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 2 additions & 2 deletions lib/blueprinter/reflection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ def fields
@fields ||= @view_collection.fields_for(name).each_with_object({}) do |field, obj|
next if field.options[:association]

obj[field.method] = Field.new(field.method, field.name, field.options)
obj[field.method] ||= Field.new(field.method, field.name, field.options)
end
end

Expand All @@ -63,7 +63,7 @@ def associations

blueprint = field.options.fetch(:blueprint)
view = field.options[:view] || :default
obj[field.method] = Association.new(field.method, field.name, blueprint, view, field.options)
obj[field.method] ||= Association.new(field.method, field.name, blueprint, view, field.options)
end
end
end
Expand Down
27 changes: 27 additions & 0 deletions spec/units/reflection_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@
view :legacy do
association :parts, blueprint: part_bp, name: :pieces
end

view :aliased_names do
field :name, name: :aliased_name
association :category, blueprint: cat_bp, name: :aliased_category
end
end
}

Expand All @@ -56,6 +61,7 @@
:extended_plus,
:extended_plus_plus,
:legacy,
:aliased_names
].sort
end

Expand All @@ -76,6 +82,17 @@
].sort
end

it 'should list overridden fields' do
fields = widget_blueprint.reflections.fetch(:aliased_names).fields
expect(fields.keys.sort).to eq [
:id,
:name,
].sort
name_field = fields[:name]
expect(name_field.name).to eq :name
expect(name_field.display_name).to eq :aliased_name
end

it 'should list associations' do
associations = widget_blueprint.reflections.fetch(:default).associations
expect(associations.keys).to eq [:category]
Expand All @@ -92,6 +109,16 @@
expect(associations[:parts].display_name).to eq :pieces
end

it 'should list overridden associations' do
associations = widget_blueprint.reflections.fetch(:aliased_names).associations
expect(associations.keys.sort).to eq [
:category
].sort
category_association = associations[:category]
expect(category_association.name).to eq :category
expect(category_association.display_name).to eq :aliased_category
end

it 'should get a blueprint and view from an association' do
assoc = widget_blueprint.reflections[:extended].associations[:parts]
expect(assoc.name).to eq :parts
Expand Down