Skip to content
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
1 change: 0 additions & 1 deletion lib/her/model/orm.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ def save

run_callbacks callback do
run_callbacks :save do
params = to_params
self.class.request(to_params.merge(:_method => method, :_path => request_path)) do |parsed_data, response|
assign_attributes(self.class.parse(parsed_data[:data])) if parsed_data[:data].any?
@metadata = parsed_data[:metadata]
Expand Down
29 changes: 22 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,33 @@ 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]].try(:to_params)
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
2 changes: 1 addition & 1 deletion lib/her/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Her
VERSION = "0.7.2"
VERSION = "0.7.3"
end
14 changes: 13 additions & 1 deletion spec/model/associations_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,18 @@
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

it 'accepts nil values for has_one association' do
@user_with_included_data.role = nil
params = @user_with_included_data.to_params
params[:role].should be_nil
end

[:create, :save_existing, :destroy].each do |type|
context "after #{type}" do
let(:subject) { self.send("user_with_included_data_after_#{type}")}
Expand Down Expand Up @@ -307,7 +319,7 @@
@user_without_included_data.company.name.should == "Bluth Company"
end

it "does not require foreugn key to have nested object" do
it "does not require foreign key to have nested object" do
@user_with_included_data_but_no_fk.company.name.should == "Bluth Company Inc."
end
end
Expand Down