diff --git a/lib/blueprinter/reflection.rb b/lib/blueprinter/reflection.rb index e3ce4216..146bfe82 100644 --- a/lib/blueprinter/reflection.rb +++ b/lib/blueprinter/reflection.rb @@ -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 @@ -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 diff --git a/spec/units/reflection_spec.rb b/spec/units/reflection_spec.rb index 86135068..31b444f3 100644 --- a/spec/units/reflection_spec.rb +++ b/spec/units/reflection_spec.rb @@ -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 } @@ -56,6 +61,7 @@ :extended_plus, :extended_plus_plus, :legacy, + :aliased_names ].sort end @@ -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] @@ -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