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

support dynamic attributes & merging collection values #172

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion lib/grape_entity/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ def to_xml(options = {})

# All supported options.
OPTIONS = [
:rewrite, :as, :if, :unless, :using, :with, :proc, :documentation, :format_with, :safe, :attr_path, :if_extras, :unless_extras
:rewrite, :as, :if, :unless, :using, :with, :proc, :documentation, :format_with, :safe, :attr_path, :if_extras, :unless_extras, :combine
].to_set.freeze

# Merges the given options with current block options.
Expand Down
6 changes: 5 additions & 1 deletion lib/grape_entity/exposure/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ def serializable_value(entity, options)
if partial_output.respond_to?(:serializable_hash)
partial_output.serializable_hash(options)
elsif partial_output.is_a?(Array) && partial_output.all? { |o| o.respond_to?(:serializable_hash) }
partial_output.map(&:serializable_hash)
if @options[:combine] == :merge
partial_output.reduce({}) { |memo, output| memo.merge(output.serializable_hash) }
else
partial_output.map(&:serializable_hash)
end
elsif partial_output.is_a?(Hash)
partial_output.each do |key, value|
partial_output[key] = value.serializable_hash if value.respond_to?(:serializable_hash)
Expand Down
10 changes: 9 additions & 1 deletion lib/grape_entity/exposure/nesting_exposure.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,15 @@ def serializable_value(entity, options)
normalized_exposures(entity, new_options).each_with_object({}) do |exposure, output|
exposure.with_attr_path(entity, new_options) do
result = exposure.serializable_value(entity, new_options)
output[exposure.key] = result

exposure_key = exposure.key.to_s
actual_key = if exposure_key.start_with?('__')
entity.delegate_attribute(exposure_key[2..-1].to_sym)
else
exposure.key
end

output[actual_key] = result
end
end
end
Expand Down
28 changes: 28 additions & 0 deletions spec/grape_entity/entity_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,34 @@ class Parent < Person
expect(representation).to eq(id: nil, name: nil, user: { id: nil, name: nil, email: nil })
end
end

context 'dynamic attribute' do
it 'determines the attribute value during representation' do
element_entity = Class.new(Grape::Entity)
element_entity.expose(:__date) do
element_entity.expose :fname, as: :first_name
element_entity.expose :last_name
end

collection_entity = Class.new(Grape::Entity)
collection_entity.present_collection(true)
collection_entity.expose(:items, using: element_entity, combine: :merge)

values = [
OpenStruct.new(date: '2015-09-01', fname: 'First', last_name: 'L.'),
OpenStruct.new(date: '2015-08-01', fname: 'F.', last_name: 'Last')
]

serialized = collection_entity.represent(values).serializable_hash

expect(
serialized[:items]
).to eq({
'2015-09-01' => { first_name: 'First', last_name: 'L.' },
'2015-08-01' => { first_name: 'F.', last_name: 'Last' }
})
end
end
end
end

Expand Down