arel-helpers 2.0.2 → 2.1.0
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 +4 -4
- data/History.txt +4 -0
- data/README.md +1 -1
- data/lib/arel-helpers/join_association.rb +21 -2
- data/lib/arel-helpers/version.rb +1 -1
- data/spec/join_association_spec.rb +6 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA1:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 307887bb3095f9de4ea562feb2e36c96dc85f131
|
|
4
|
+
data.tar.gz: 6bc236ff8796f5c9e3aba3aa2dedb2c5fb83fe3b
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0d7370d6b5fbb42e29b352d4fc07e2dadf0b5d7b1e2e885ac2b85c236556074125eca920a4ab0c7befcb4de7d0a451a66e2f72334210c2804c50df6e3d5186a8
|
|
7
|
+
data.tar.gz: 1805436115bc5c944cd99d5b850a842f5e2f93825e9e0881d51dfdf2010bd3b0b4bfecd5fbb5d4b671d46a31efda36fb615921b7f3f651f288395de21eabb620
|
data/History.txt
CHANGED
data/README.md
CHANGED
|
@@ -155,7 +155,7 @@ PostQueryBuilder.new
|
|
|
155
155
|
|
|
156
156
|
## Requirements
|
|
157
157
|
|
|
158
|
-
Requires ActiveRecord >= 3.1.0, <= 4.
|
|
158
|
+
Requires ActiveRecord >= 3.1.0, <= 4.2.0, tested with Ruby 1.9.3, 2.0.0, and 2.1.0. Depends on SQLite for testing purposes.
|
|
159
159
|
|
|
160
160
|
## Running Tests
|
|
161
161
|
|
|
@@ -17,8 +17,10 @@ module ArelHelpers
|
|
|
17
17
|
# For example, for HABTM associations, two join statements are required.
|
|
18
18
|
# This method encapsulates that functionality and yields an intermediate object for chaining.
|
|
19
19
|
# It also allows you to use an outer join instead of the default inner via the join_type arg.
|
|
20
|
-
def join_association(table, association, join_type = Arel::InnerJoin, &block)
|
|
21
|
-
if ActiveRecord::VERSION::STRING >= '4.
|
|
20
|
+
def join_association(table, association, join_type = Arel::Nodes::InnerJoin, &block)
|
|
21
|
+
if ActiveRecord::VERSION::STRING >= '4.2.0'
|
|
22
|
+
join_association_4_2(table, association, join_type, &block)
|
|
23
|
+
elsif ActiveRecord::VERSION::STRING >= '4.1.0'
|
|
22
24
|
join_association_4_1(table, association, join_type, &block)
|
|
23
25
|
else
|
|
24
26
|
join_association_3_1(table, association, join_type, &block)
|
|
@@ -62,6 +64,23 @@ module ArelHelpers
|
|
|
62
64
|
join_type.new(constraint.left, right)
|
|
63
65
|
end
|
|
64
66
|
end
|
|
67
|
+
|
|
68
|
+
def join_association_4_2(table, association, join_type)
|
|
69
|
+
associations = association.is_a?(Array) ? association : [association]
|
|
70
|
+
join_dependency = ActiveRecord::Associations::JoinDependency.new(table, associations, [])
|
|
71
|
+
|
|
72
|
+
join_dependency.join_constraints([]).map do |constraint|
|
|
73
|
+
constraint.joins.map do |join|
|
|
74
|
+
right = if block_given?
|
|
75
|
+
yield join.left.name.to_sym, join.right
|
|
76
|
+
else
|
|
77
|
+
join.right
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
join_type.new(join.left, right)
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
65
84
|
end
|
|
66
85
|
end
|
|
67
86
|
end
|
data/lib/arel-helpers/version.rb
CHANGED
|
@@ -10,7 +10,7 @@ describe ArelHelpers do
|
|
|
10
10
|
end
|
|
11
11
|
|
|
12
12
|
it "should work with an outer join" do
|
|
13
|
-
Post.joins(ArelHelpers.join_association(Post, :comments, Arel::OuterJoin)).to_sql.should ==
|
|
13
|
+
Post.joins(ArelHelpers.join_association(Post, :comments, Arel::Nodes::OuterJoin)).to_sql.should ==
|
|
14
14
|
'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id"'
|
|
15
15
|
end
|
|
16
16
|
|
|
@@ -45,6 +45,11 @@ describe ArelHelpers do
|
|
|
45
45
|
end).to_sql.should ==
|
|
46
46
|
'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" AND "comments"."text" = \'Awesome post!\' INNER JOIN "favorites" (ON "favorites"."post_id" = "posts"."id" OR "favorites"."amount" = \'lots\')'
|
|
47
47
|
end
|
|
48
|
+
|
|
49
|
+
it 'should be able to handle has_and_belongs_to_many associations' do
|
|
50
|
+
CollabPost.joins(ArelHelpers.join_association(CollabPost, :authors)).to_sql.should ==
|
|
51
|
+
'SELECT "collab_posts".* FROM "collab_posts" INNER JOIN "authors_collab_posts" ON "authors_collab_posts"."collab_post_id" = "collab_posts"."id" INNER JOIN "authors" ON "authors"."id" = "authors_collab_posts"."author_id"'
|
|
52
|
+
end
|
|
48
53
|
end
|
|
49
54
|
end
|
|
50
55
|
|
metadata
CHANGED
|
@@ -1,14 +1,14 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: arel-helpers
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 2.0
|
|
4
|
+
version: 2.1.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Cameron Dutro
|
|
8
8
|
autorequire:
|
|
9
9
|
bindir: bin
|
|
10
10
|
cert_chain: []
|
|
11
|
-
date:
|
|
11
|
+
date: 2015-01-13 00:00:00.000000000 Z
|
|
12
12
|
dependencies:
|
|
13
13
|
- !ruby/object:Gem::Dependency
|
|
14
14
|
name: activerecord
|