active_record_aliased_joins 0.1.1 → 0.1.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rubocop.yml +4 -1
- data/lib/active_record_aliased_joins/version.rb +1 -1
- data/lib/active_record_aliased_joins.rb +15 -11
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 98d045644c3e8aae0c9d63c1e85555a1cee684190313c190fc5cd091c99898f6
|
4
|
+
data.tar.gz: bd247d692d109a8a4140fa9d29efcea75b8dbbf512e7d7accf81f322ab69de22
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d6686baf5c1d6dec938a4fb8f77dd06df634e064f8eae9d5e2587b1ab3772bbb03f50e6d9809b7675ef4372024c822d0b58d0b45beafd3a4db581de7b411a9b9
|
7
|
+
data.tar.gz: 37025699218bbfce0319f6c2750731f4af082c83f7288f3acc2b97cc3887e6255b86b44ba6c908fa290f4a2434a22a6c1f2b9a0a2b9aad3bf5456330d95cd3e9
|
data/.rubocop.yml
CHANGED
@@ -20,18 +20,18 @@ module ActiveRecordAliasedJoins
|
|
20
20
|
|
21
21
|
# Module to add joins_with_alias support to ActiveRecord::Base
|
22
22
|
module JoinWithAlias
|
23
|
-
def joins_with_alias(association_name, alias_name)
|
23
|
+
def joins_with_alias(association_name, alias_name, join_klass = Arel::Nodes::InnerJoin)
|
24
24
|
reflection = _reflect_on_association(association_name.to_s)
|
25
25
|
raise ActiveRecord::AssociationNotFoundError unless reflection
|
26
26
|
|
27
27
|
source_joins =
|
28
28
|
case reflection
|
29
29
|
when ActiveRecord::Reflection::HasManyReflection, ActiveRecord::Reflection::HasOneReflection
|
30
|
-
join_with_alias_has_many_has_one(reflection, association_name, alias_name)
|
30
|
+
join_with_alias_has_many_has_one(reflection, association_name, alias_name, join_klass)
|
31
31
|
when ActiveRecord::Reflection::BelongsToReflection
|
32
|
-
join_with_alias_belongs_to(reflection, association_name, alias_name)
|
32
|
+
join_with_alias_belongs_to(reflection, association_name, alias_name, join_klass)
|
33
33
|
when ActiveRecord::Reflection::ThroughReflection, ActiveRecord::Reflection::HasAndBelongsToManyReflection
|
34
|
-
join_with_alias_through(reflection, association_name, alias_name)
|
34
|
+
join_with_alias_through(reflection, association_name, alias_name, join_klass)
|
35
35
|
else
|
36
36
|
raise UnsupportedReflectionError, reflection
|
37
37
|
end
|
@@ -39,23 +39,27 @@ module ActiveRecordAliasedJoins
|
|
39
39
|
joins(source_joins.join_sources)
|
40
40
|
end
|
41
41
|
|
42
|
+
def left_joins_with_alias(association_name, alias_name)
|
43
|
+
joins_with_alias(association_name, alias_name, Arel::Nodes::OuterJoin)
|
44
|
+
end
|
45
|
+
|
42
46
|
private
|
43
47
|
|
44
|
-
def join_with_alias_has_many_has_one(reflection, _association_name, alias_name)
|
48
|
+
def join_with_alias_has_many_has_one(reflection, _association_name, alias_name, join_klass = Nodes::InnerJoin)
|
45
49
|
s = arel_table
|
46
50
|
related_klass = reflection.klass
|
47
51
|
r = related_klass.arel_table.alias(alias_name)
|
48
|
-
s.join(r).on(s[primary_key.to_sym].eq(r[reflection.foreign_key.to_sym]))
|
52
|
+
s.join(r, join_klass).on(s[primary_key.to_sym].eq(r[reflection.foreign_key.to_sym]))
|
49
53
|
end
|
50
54
|
|
51
|
-
def join_with_alias_belongs_to(reflection, _association_name, alias_name)
|
55
|
+
def join_with_alias_belongs_to(reflection, _association_name, alias_name, join_klass = Nodes::InnerJoin)
|
52
56
|
s = arel_table
|
53
57
|
related_klass = reflection.klass
|
54
58
|
r = related_klass.arel_table.alias(alias_name)
|
55
|
-
s.join(r).on(s[reflection.foreign_key.to_sym].eq(r[primary_key.to_sym]))
|
59
|
+
s.join(r, join_klass).on(s[reflection.foreign_key.to_sym].eq(r[primary_key.to_sym]))
|
56
60
|
end
|
57
61
|
|
58
|
-
def join_with_alias_through(reflection, association_name, alias_name)
|
62
|
+
def join_with_alias_through(reflection, association_name, alias_name, join_klass = Nodes::InnerJoin)
|
59
63
|
s = arel_table
|
60
64
|
related_klass = reflection.klass
|
61
65
|
r = related_klass.arel_table.alias(alias_name)
|
@@ -65,8 +69,8 @@ module ActiveRecordAliasedJoins
|
|
65
69
|
t = t_klass.arel_table
|
66
70
|
t_r_reflection = t_klass._reflect_on_association(association_name.to_s.singularize)
|
67
71
|
|
68
|
-
s.join(t).on(s[primary_key.to_sym].eq(t[s_t_reflection.foreign_key.to_sym]))
|
69
|
-
.join(r).on(t[t_r_reflection.foreign_key.to_sym].eq(r[related_klass.primary_key.to_sym]))
|
72
|
+
s.join(t, join_klass).on(s[primary_key.to_sym].eq(t[s_t_reflection.foreign_key.to_sym]))
|
73
|
+
.join(r, join_klass).on(t[t_r_reflection.foreign_key.to_sym].eq(r[related_klass.primary_key.to_sym]))
|
70
74
|
end
|
71
75
|
end
|
72
76
|
|