From a7291f5a2eaf06be39d18d8970d4e69b316e0626 Mon Sep 17 00:00:00 2001 From: Josh Menden Date: Sun, 7 Aug 2022 21:17:28 -0600 Subject: [PATCH] Support relationships between classes with a common module parent --- README.md | 1 + lib/dynamoid/associations/belongs_to.rb | 8 +++++++- lib/dynamoid/associations/has_and_belongs_to_many.rb | 5 +++++ lib/dynamoid/associations/has_many.rb | 5 +++++ lib/dynamoid/associations/has_one.rb | 5 +++++ 5 files changed, 23 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index ad6906e4..d43a4544 100644 --- a/README.md +++ b/README.md @@ -1320,6 +1320,7 @@ just as accessible to the Ruby world as MongoDB. Also, without contributors the project wouldn't be nearly as awesome. So many thanks to: +* [Josh Menden](https://github.com/joshmenden) * [Logan Bowers](https://github.com/loganb) * [Lane LaRue](https://github.com/luxx) * [Craig Heneveld](https://github.com/cheneveld) diff --git a/lib/dynamoid/associations/belongs_to.rb b/lib/dynamoid/associations/belongs_to.rb index b7c6c76a..f01a3e60 100644 --- a/lib/dynamoid/associations/belongs_to.rb +++ b/lib/dynamoid/associations/belongs_to.rb @@ -39,8 +39,14 @@ def associate(hash_key) # # @since 0.2.0 def target_association - has_many_key_name = options[:inverse_of] || source.class.to_s.underscore.pluralize.to_sym + has_many_key_name = options[:inverse_of] || source.class.to_s.pluralize.underscore.to_sym has_one_key_name = options[:inverse_of] || source.class.to_s.underscore.to_sym + + if target_class.module_parent != Object && (target_class.module_parent == source.class.module_parent) + has_many_key_name = source.class.to_s.demodulize.pluralize.underscore.to_sym + has_one_key_name = source.class.to_s.demodulize.underscore.to_sym + end + unless target_class.associations[has_many_key_name].nil? return has_many_key_name if target_class.associations[has_many_key_name][:type] == :has_many end diff --git a/lib/dynamoid/associations/has_and_belongs_to_many.rb b/lib/dynamoid/associations/has_and_belongs_to_many.rb index ce57b0e8..50580104 100644 --- a/lib/dynamoid/associations/has_and_belongs_to_many.rb +++ b/lib/dynamoid/associations/has_and_belongs_to_many.rb @@ -15,6 +15,11 @@ class HasAndBelongsToMany # @since 0.2.0 def target_association key_name = options[:inverse_of] || source.class.to_s.pluralize.underscore.to_sym + + if target_class.module_parent != Object && (target_class.module_parent == source.class.module_parent) + key_name = source.class.to_s.demodulize.pluralize.underscore.to_sym + end + guess = target_class.associations[key_name] return nil if guess.nil? || guess[:type] != :has_and_belongs_to_many diff --git a/lib/dynamoid/associations/has_many.rb b/lib/dynamoid/associations/has_many.rb index b5068c1e..bc984fca 100644 --- a/lib/dynamoid/associations/has_many.rb +++ b/lib/dynamoid/associations/has_many.rb @@ -15,6 +15,11 @@ class HasMany # @since 0.2.0 def target_association key_name = options[:inverse_of] || source.class.to_s.singularize.underscore.to_sym + + if target_class.module_parent != Object && (target_class.module_parent == source.class.module_parent) + key_name = source.class.to_s.demodulize.singularize.underscore.to_sym + end + guess = target_class.associations[key_name] return nil if guess.nil? || guess[:type] != :belongs_to diff --git a/lib/dynamoid/associations/has_one.rb b/lib/dynamoid/associations/has_one.rb index 18c38e82..4beeff8b 100644 --- a/lib/dynamoid/associations/has_one.rb +++ b/lib/dynamoid/associations/has_one.rb @@ -16,6 +16,11 @@ class HasOne # @since 0.2.0 def target_association key_name = options[:inverse_of] || source.class.to_s.singularize.underscore.to_sym + + if target_class.module_parent != Object && (target_class.module_parent == source.class.module_parent) + key_name = source.class.to_s.demodulize.singularize.underscore.to_sym + end + guess = target_class.associations[key_name] return nil if guess.nil? || guess[:type] != :belongs_to