Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support relationships between classes with a common module parent #576

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1329,6 +1329,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)
Expand Down
8 changes: 7 additions & 1 deletion lib/dynamoid/associations/belongs_to.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In every other file the pluralize comes before the underscore, so I changed this to be consistent

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
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we unconditionally demodulize a class name?


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
Expand Down
5 changes: 5 additions & 0 deletions lib/dynamoid/associations/has_and_belongs_to_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions lib/dynamoid/associations/has_many.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
5 changes: 5 additions & 0 deletions lib/dynamoid/associations/has_one.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down