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

Add association_primary to has_and_belongs_to_many #307

Open
wants to merge 3 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
2 changes: 1 addition & 1 deletion spec/model/relation_definition_spec.cr
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ module Jennifer::Model
c = Factory.create_contact
q = c.facebook_many_profiles_query
select_query(q)
.should match(/JOIN contacts_profiles ON contacts_profiles\.profile_id = profiles\.id AND contacts_profiles\.contact_id = %s/)
.should match(/JOIN contacts_profiles ON contacts_profiles\.profile_id = profiles\.uid AND contacts_profiles\.contact_id = %s/)
select_query(q)
.should match(/profiles\.type = %s/)
q.sql_args.includes?("FacebookProfile").should be_true
Expand Down
2 changes: 1 addition & 1 deletion spec/models.cr
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ class Contact < ApplicationRecord
has_many :addresses, Address, inverse_of: :contact
has_many :facebook_profiles, FacebookProfile, inverse_of: :contact
has_and_belongs_to_many :countries, Country
has_and_belongs_to_many :facebook_many_profiles, FacebookProfile, association_foreign: :profile_id
has_and_belongs_to_many :facebook_many_profiles, FacebookProfile, association_foreign: :profile_id, association_primary: :uid
has_one :main_address, Address, {where { _main }}, inverse_of: :contact
has_one :passport, Passport, inverse_of: :contact
belongs_to :user, User
Expand Down
5 changes: 3 additions & 2 deletions src/jennifer/model/relation_definition.cr
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,7 @@ module Jennifer
# - *primary* - specify the name of the column to use as the primary key for the relation
# - *join_table* - specifies the name of the join table if the default based on lexical order isn't what you want
# - *association_foreign* - specifies the foreign key used for the association on the receiving side of the association
# - *association_primary* - specifies the primary key used for the association on the receiving side of the association
#
# The following methods for retrieval and query of a single associated object will be added:
#
Expand All @@ -238,11 +239,11 @@ module Jennifer
# - `#remove_association(rel)`
# - `#association_query`
# - `#association_reload`
macro has_and_belongs_to_many(name, klass, request = nil, foreign = nil, primary = nil, join_table = nil, association_foreign = nil)
macro has_and_belongs_to_many(name, klass, request = nil, foreign = nil, primary = nil, join_table = nil, association_foreign = nil, association_primary = nil)
{{"{% RELATION_NAMES << #{name.id.stringify} %}".id}}
RELATIONS["{{name.id}}"] =
::Jennifer::Relation::ManyToMany({{klass}}, {{@type}}).new("{{name.id}}", {{foreign}}, {{primary}},
{{klass}}.all{% if request %}.exec {{request}} {% end %}, {{join_table}}, {{association_foreign}})
{{klass}}.all{% if request %}.exec {{request}} {% end %}, {{join_table}}, {{association_foreign}}, {{association_primary}})

before_destroy :__{{name.id}}_clean

Expand Down
16 changes: 12 additions & 4 deletions src/jennifer/relation/many_to_many.cr
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Jennifer
module Relation
class ManyToMany(T, Q) < Base(T, Q)
getter join_table : String?, association_foreign : String?
getter join_table : String?, association_foreign : String?, association_primary : String?

def initialize(@name, foreign : String | Symbol?, primary : String | Symbol?, query, @join_table = nil, _join_foreign = nil)
def initialize(@name, foreign : String | Symbol?, primary : String | Symbol?, query, @join_table = nil, _join_foreign = nil, _join_primary = nil)
@association_foreign = _join_foreign.to_s if _join_foreign
@association_primary = _join_primary.to_s if _join_primary
@foreign = foreign.to_s if foreign
@primary = primary.to_s if primary
@join_query = query.tree
Expand Down Expand Up @@ -39,10 +40,11 @@ module Jennifer

def query(primary_value)
afk = association_foreign_key
apk = association_primary_key
_primary_value = primary_value
mfk = foreign_field
q = T.all.join(join_table!) do
(c(afk) == T.primary) &
(c(afk) == apk) &
(_primary_value.is_a?(Array) ? c(mfk).in(_primary_value) : c(mfk) == _primary_value)
end
if @join_query
Expand All @@ -58,6 +60,7 @@ module Jennifer
_primary = primary_field
jt = join_table!
q = query.join(jt, type: type) { Q.c(_primary) == c(_foreign) }.join(T, type: type) do
# TODO: Replace `T.primary` with `association_primary_key`?
Copy link
Owner

Choose a reason for hiding this comment

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

this is correct

T.primary == c(association_foreign_key, jt)
end
if @join_query
Expand All @@ -72,6 +75,10 @@ module Jennifer
@association_foreign || Inflector.foreign_key(T.to_s)
end

def association_primary_key
(ap = association_primary) ? T.c(ap) : T.primary
end

def preload_relation(collection, out_collection : Array(Model::Resource), pk_repo)
return if collection.empty?
_primary = primary_field
Expand Down Expand Up @@ -104,7 +111,8 @@ module Jennifer
join_table!,
{
foreign_field => obj.attribute(primary_field),
association_foreign_key => rel.primary,
association_foreign_key =>
Copy link
Owner

@imdrasil imdrasil Apr 15, 2020

Choose a reason for hiding this comment

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

association_primary ? rel.attribute(association_primary.not_nil!) : rel.primary

association_primary ? rel.to_h.transform_keys { |key| key.to_s }[association_primary] : rel.primary,
}
)
end
Expand Down