activerecord_extras 0.1.1 → 0.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f55fba01009bad98653095ad9f8505617070666a46bd822c6794a486702f2031
|
|
4
|
+
data.tar.gz: 07b79c6a3a16b803e95cff57822240e09fcac28fee6d05c5609ffa9635611517
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 70d4a7a37f70d44e6c3156ced212b974739c5d4ce61e3d875d760b518f6cd15d2bfd8dba591ab8ed9cd8a77e7587b214393ddf52aaba7435d42a94836e238938
|
|
7
|
+
data.tar.gz: 6f12f6d7a9334baae2a86778c0e632929a82b90561f0654fc801f278da4eaa0d572db82dc4dec1f6b69c0ab740136bd88a857e895d14d6a659045e3eeb2cf276
|
|
@@ -14,21 +14,12 @@ module ActiveRecord
|
|
|
14
14
|
def association_subquery(association_name, mode: :exists, &extra_conditions_block)
|
|
15
15
|
reflection = reflections[association_name.to_s]
|
|
16
16
|
|
|
17
|
-
unless reflection
|
|
18
|
-
|
|
19
|
-
end
|
|
20
|
-
|
|
21
|
-
unless reflection.macro == :has_many
|
|
22
|
-
raise ArgumentError, "Only has_many associations are supported (got #{reflection.macro})"
|
|
23
|
-
end
|
|
17
|
+
raise ArgumentError, "Unknown association: #{association_name}" unless reflection
|
|
18
|
+
raise ArgumentError, "Only has_many associations are supported (got :#{reflection.macro})" unless reflection.macro == :has_many
|
|
24
19
|
|
|
25
20
|
foreign_keys = Array(reflection.foreign_key)
|
|
26
21
|
primary_keys = Array(reflection.active_record_primary_key)
|
|
27
22
|
|
|
28
|
-
unless foreign_keys.size == primary_keys.size
|
|
29
|
-
raise ArgumentError, "Mismatch in key counts: #{foreign_keys} vs #{primary_keys}"
|
|
30
|
-
end
|
|
31
|
-
|
|
32
23
|
source_table = arel_table
|
|
33
24
|
target_table = reflection.klass.arel_table
|
|
34
25
|
|
|
@@ -36,18 +27,24 @@ module ActiveRecord
|
|
|
36
27
|
target_table[fk].eq(source_table[pk])
|
|
37
28
|
end.reduce(&:and)
|
|
38
29
|
|
|
39
|
-
# Allow caller to modify the join condition via block
|
|
40
30
|
if extra_conditions_block
|
|
41
|
-
|
|
42
|
-
join_conditions = new_conditions if new_conditions
|
|
31
|
+
join_conditions = extra_conditions_block.call(join_conditions, target_table) || join_conditions
|
|
43
32
|
end
|
|
44
33
|
|
|
34
|
+
relation = reflection.klass.all
|
|
35
|
+
|
|
36
|
+
if reflection.scope
|
|
37
|
+
relation = relation.instance_exec(&reflection.scope)
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
relation = relation.where(join_conditions)
|
|
41
|
+
|
|
45
42
|
case mode
|
|
46
43
|
when :exists
|
|
47
|
-
|
|
44
|
+
relation.select(Arel.sql("1")).arel.exists
|
|
48
45
|
when :count
|
|
49
46
|
Arel::Nodes::Grouping.new(
|
|
50
|
-
|
|
47
|
+
relation.select(Arel.star.count).arel
|
|
51
48
|
)
|
|
52
49
|
else
|
|
53
50
|
raise ArgumentError, "Unknown mode: #{mode.inspect} (expected :exists or :count)"
|