From 2bc6f66701f156d1d620517de8ca2225ed784e4e Mon Sep 17 00:00:00 2001 From: Andy Anastasiadis-Gray Date: Tue, 11 Dec 2018 15:44:56 +1300 Subject: [PATCH 1/2] fix class naming when models are in a module --- lib/contentful_model/associations/has_many_nested.rb | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/contentful_model/associations/has_many_nested.rb b/lib/contentful_model/associations/has_many_nested.rb index b0bf06d..2204b50 100644 --- a/lib/contentful_model/associations/has_many_nested.rb +++ b/lib/contentful_model/associations/has_many_nested.rb @@ -25,8 +25,9 @@ module ClassMethods # to write your own recursion for this, because it's probably an implementation-specific problem. # rubocop:disable Style/PredicateName def has_many_nested(association_name, options = {}) - has_many association_name, class_name: to_s, inverse_of: :"parent_#{to_s.underscore}" - belongs_to_many :"parent_#{to_s.underscore.pluralize}", class_name: to_s + my_name = to_s.demodulize.underscore + has_many association_name, class_name: to_s, inverse_of: :"parent_#{my_name}" + belongs_to_many :"parent_#{my_name.pluralize}", class_name: to_s root_method = options[:root] if options[:root].is_a?(Proc) # If there's a root method defined, set up a class method called root_[class name]. In our example this would be @@ -34,14 +35,14 @@ def has_many_nested(association_name, options = {}) # @return [Object] the root entity returned from the proc defined in has_many_nested if defined?(root_method) && root_method.is_a?(Proc) # @return [Object] the root entity - define_method :"root_#{to_s.underscore}" do + define_method :"root_#{my_name}" do root_method.call end end # A utility method which returns the parent object; saves passing around interpolated strings define_method :parent do - parents = send(:"parent_#{self.class.to_s.underscore.pluralize}") + parents = send(:"parent_#{self.class.my_name.pluralize}") parents.first unless parents.nil? end From 6e0c02ccf255226714065fc05095c43a50af843a Mon Sep 17 00:00:00 2001 From: Andy Anastasiadis-Gray Date: Tue, 11 Dec 2018 15:54:05 +1300 Subject: [PATCH 2/2] moved fixed name so it is available in defined methods --- .../associations/has_many_nested.rb | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/lib/contentful_model/associations/has_many_nested.rb b/lib/contentful_model/associations/has_many_nested.rb index 2204b50..13996c6 100644 --- a/lib/contentful_model/associations/has_many_nested.rb +++ b/lib/contentful_model/associations/has_many_nested.rb @@ -25,9 +25,8 @@ module ClassMethods # to write your own recursion for this, because it's probably an implementation-specific problem. # rubocop:disable Style/PredicateName def has_many_nested(association_name, options = {}) - my_name = to_s.demodulize.underscore - has_many association_name, class_name: to_s, inverse_of: :"parent_#{my_name}" - belongs_to_many :"parent_#{my_name.pluralize}", class_name: to_s + has_many association_name, class_name: to_s, inverse_of: :"parent_#{has_many_nested_name}" + belongs_to_many :"parent_#{has_many_nested_name.pluralize}", class_name: to_s root_method = options[:root] if options[:root].is_a?(Proc) # If there's a root method defined, set up a class method called root_[class name]. In our example this would be @@ -35,14 +34,14 @@ def has_many_nested(association_name, options = {}) # @return [Object] the root entity returned from the proc defined in has_many_nested if defined?(root_method) && root_method.is_a?(Proc) # @return [Object] the root entity - define_method :"root_#{my_name}" do + define_method :"root_#{has_many_nested_name}" do root_method.call end end # A utility method which returns the parent object; saves passing around interpolated strings define_method :parent do - parents = send(:"parent_#{self.class.my_name.pluralize}") + parents = send(:"parent_#{self.class.has_many_nested_name.pluralize}") parents.first unless parents.nil? end @@ -133,6 +132,10 @@ def has_many_nested(association_name, options = {}) send(:private, :flatten_hash) end # rubocop:enable Style/PredicateName + + def has_many_nested_name + to_s.demodulize.underscore + end end end end