Skip to content

Commit

Permalink
do not overwrite overridden fields and associations
Browse files Browse the repository at this point in the history
Signed-off-by: Elliot Hursh <[email protected]>
  • Loading branch information
elliothursh committed Jan 31, 2024
1 parent 5b7e343 commit a9c62f5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 2 deletions.
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

0 comments on commit a9c62f5

Please sign in to comment.