Skip to content
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
30 changes: 23 additions & 7 deletions lib/her/model/parse.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def parse(data)
# @private
def to_params(attributes, changes={})
filtered_attributes = attributes.dup.symbolize_keys
filtered_attributes.merge!(embeded_params(attributes))
filtered_attributes.merge!(embedded_params(attributes))
if her_api.options[:send_only_modified_attributes]
filtered_attributes = changes.symbolize_keys.keys.inject({}) do |hash, attribute|
hash[attribute] = filtered_attributes[attribute]
Expand All @@ -54,18 +54,34 @@ def to_params(attributes, changes={})


# @private
# TODO: Handle has_one
def embeded_params(attributes)
associations[:has_many].select { |a| attributes.include?(a[:data_key])}.compact.inject({}) do |hash, association|
def embedded_params(attributes)
has_many_params = has_many_embedded_params(attributes)
has_one_params = has_one_embedded_params(attributes)
has_many_params.merge(has_one_params)
end

def has_one_embedded_params(attributes)
present_has_ones = (associations[:has_one] || []).select { |a| attributes.include?(a[:data_key]) }
present_has_ones.compact.each_with_object({}) do |association, hash|
params = attributes[association[:data_key]].to_params
next if params.empty?
klass = her_nearby_class(association[:class_name])
hash[association[:data_key]] = klass.include_root_in_json? ? params[klass.root_element] : params
end
end

def has_many_embedded_params(attributes)
present_has_many = (associations[:has_many] || []).select { |a| attributes.include?(a[:data_key]) }
present_has_many.compact.each_with_object({}) do |association, hash|
params = attributes[association[:data_key]].map(&:to_params)
next if params.empty?
if association[:class_name].constantize.include_root_in_json?
root = association[:class_name].constantize.root_element
klass = her_nearby_class(association[:class_name])
if klass.include_root_in_json?
root = klass.root_element
hash[association[:data_key]] = params.map { |n| n[root] }
else
hash[association[:data_key]] = params
end
hash
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/model/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@
params[:comments].length.should eq(2)
end

it 'includes has_one relationships in params by default' do
params = @user_with_included_data.to_params
params[:role].should be_kind_of(Hash)
params[:role][:body].should eq("Admin")
end

[:create, :save_existing, :destroy].each do |type|
context "after #{type}" do
let(:subject) { self.send("user_with_included_data_after_#{type}")}
Expand Down