From f3bf3fc1464ca7ae5fe9b29529ca8c6f853f3a70 Mon Sep 17 00:00:00 2001 From: Daniel Turner Date: Thu, 26 Jun 2014 15:22:52 -0500 Subject: [PATCH 1/6] Include has_one attributes correctly when calling to_params --- lib/her/model/parse.rb | 28 +++++++++++++++++++++++++--- spec/model/associations_spec.rb | 6 ++++++ 2 files changed, 31 insertions(+), 3 deletions(-) diff --git a/lib/her/model/parse.rb b/lib/her/model/parse.rb index db43d98f..f011acc6 100644 --- a/lib/her/model/parse.rb +++ b/lib/her/model/parse.rb @@ -54,13 +54,29 @@ def to_params(attributes, changes={}) # @private - # TODO: Handle has_one def embeded_params(attributes) + has_many_params = has_many_embeded_params(attributes) || {} + has_one_params = has_one_embeded_params(attributes) || {} + has_many_params.merge(has_one_params) + end + + def has_one_embeded_params(attributes) + associations[:has_one].select { |a| attributes.include?(a[:data_key])}.compact.inject({}) do |hash, association| + params = attributes[association[:data_key]].to_params + next if params.empty? + klass = find_class(association[:class_name]) + hash[association[:data_key]] = klass.include_root_in_json? ? params[klass.root_element] : params + hash + end + end + + def has_many_embeded_params(attributes) associations[:has_many].select { |a| attributes.include?(a[:data_key])}.compact.inject({}) do |hash, association| 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 = find_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 @@ -69,6 +85,12 @@ def embeded_params(attributes) end end + def find_class(class_name) + if class_name.demodulize == class_name && !const_defined?(class_name) + class_name = "#{self.name.deconstantize}::#{class_name}" + end + class_name.constantize + end # Return or change the value of `include_root_in_json` # # @example diff --git a/spec/model/associations_spec.rb b/spec/model/associations_spec.rb index 8d293403..44aa25ce 100644 --- a/spec/model/associations_spec.rb +++ b/spec/model/associations_spec.rb @@ -228,6 +228,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}")} From cd6db7e9a46eeda0e60d709e84c1ca6b7238b2aa Mon Sep 17 00:00:00 2001 From: Daniel Turner Date: Thu, 26 Jun 2014 15:41:50 -0500 Subject: [PATCH 2/6] bump version # --- lib/her/version.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/her/version.rb b/lib/her/version.rb index b576f24f..00b91834 100644 --- a/lib/her/version.rb +++ b/lib/her/version.rb @@ -1,3 +1,3 @@ module Her - VERSION = "0.7.2" + VERSION = "0.7.3" end From 9149fdf12974f44d751fac936b26bcd086dabd7b Mon Sep 17 00:00:00 2001 From: Daniel Turner Date: Thu, 26 Jun 2014 15:55:07 -0500 Subject: [PATCH 3/6] use each_with_object over inject --- lib/her/model/parse.rb | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/her/model/parse.rb b/lib/her/model/parse.rb index f011acc6..539a1a3e 100644 --- a/lib/her/model/parse.rb +++ b/lib/her/model/parse.rb @@ -55,23 +55,24 @@ def to_params(attributes, changes={}) # @private def embeded_params(attributes) - has_many_params = has_many_embeded_params(attributes) || {} - has_one_params = has_one_embeded_params(attributes) || {} + has_many_params = has_many_embeded_params(attributes) + has_one_params = has_one_embeded_params(attributes) has_many_params.merge(has_one_params) end def has_one_embeded_params(attributes) - associations[:has_one].select { |a| attributes.include?(a[:data_key])}.compact.inject({}) do |hash, association| + 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 = find_class(association[:class_name]) hash[association[:data_key]] = klass.include_root_in_json? ? params[klass.root_element] : params - hash end end def has_many_embeded_params(attributes) - associations[:has_many].select { |a| attributes.include?(a[:data_key])}.compact.inject({}) do |hash, association| + 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? klass = find_class(association[:class_name]) @@ -81,7 +82,6 @@ def has_many_embeded_params(attributes) else hash[association[:data_key]] = params end - hash end end From a4450bc21d56e3959ec18e878323d18fbc794b9e Mon Sep 17 00:00:00 2001 From: Daniel Turner Date: Thu, 26 Jun 2014 17:02:55 -0500 Subject: [PATCH 4/6] fix spelling --- lib/her/model/parse.rb | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/lib/her/model/parse.rb b/lib/her/model/parse.rb index 539a1a3e..feaf630d 100644 --- a/lib/her/model/parse.rb +++ b/lib/her/model/parse.rb @@ -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] @@ -54,14 +54,14 @@ def to_params(attributes, changes={}) # @private - def embeded_params(attributes) - has_many_params = has_many_embeded_params(attributes) - has_one_params = has_one_embeded_params(attributes) + 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_embeded_params(attributes) - present_has_ones = associations[:has_one].select { |a| attributes.include?(a[:data_key])} + 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? @@ -70,8 +70,8 @@ def has_one_embeded_params(attributes) end end - def has_many_embeded_params(attributes) - present_has_many = associations[:has_many].select { |a| attributes.include?(a[:data_key])} + 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? From dc0eeecd314a5df4e7dbefe1215893d5caf77b06 Mon Sep 17 00:00:00 2001 From: Daniel Turner Date: Fri, 27 Jun 2014 13:50:39 -0500 Subject: [PATCH 5/6] use her_nearby_class to find class --- lib/her/model/parse.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/her/model/parse.rb b/lib/her/model/parse.rb index feaf630d..5fac22c1 100644 --- a/lib/her/model/parse.rb +++ b/lib/her/model/parse.rb @@ -65,7 +65,7 @@ def has_one_embedded_params(attributes) present_has_ones.compact.each_with_object({}) do |association, hash| params = attributes[association[:data_key]].to_params next if params.empty? - klass = find_class(association[:class_name]) + klass = her_nearby_class(association[:class_name]) hash[association[:data_key]] = klass.include_root_in_json? ? params[klass.root_element] : params end end @@ -75,7 +75,7 @@ def has_many_embedded_params(attributes) present_has_many.compact.each_with_object({}) do |association, hash| params = attributes[association[:data_key]].map(&:to_params) next if params.empty? - klass = find_class(association[:class_name]) + 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] } @@ -85,12 +85,6 @@ def has_many_embedded_params(attributes) end end - def find_class(class_name) - if class_name.demodulize == class_name && !const_defined?(class_name) - class_name = "#{self.name.deconstantize}::#{class_name}" - end - class_name.constantize - end # Return or change the value of `include_root_in_json` # # @example From ef99acce3c172f4eaac1253e5c7e6efd92af64fb Mon Sep 17 00:00:00 2001 From: Daniel Turner Date: Wed, 19 Aug 2015 11:02:08 -0500 Subject: [PATCH 6/6] has_one_embedded_params & has_many_embedded_params now protect against the case where associations[:has_one] or associations[:has_many] is nil --- lib/her/model/parse.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/her/model/parse.rb b/lib/her/model/parse.rb index 3f2502bb..1cee7ee1 100644 --- a/lib/her/model/parse.rb +++ b/lib/her/model/parse.rb @@ -61,7 +61,7 @@ def embedded_params(attributes) end def has_one_embedded_params(attributes) - present_has_ones = associations[:has_one].select { |a| attributes.include?(a[:data_key]) } + 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? @@ -71,7 +71,7 @@ def has_one_embedded_params(attributes) end def has_many_embedded_params(attributes) - present_has_many = associations[:has_many].select { |a| attributes.include?(a[:data_key]) } + 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?