arel-helpers 2.3.0 → 2.4.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 +17 -0
- data/lib/arel-helpers/join_association.rb +1 -1
- data/lib/arel-helpers/version.rb +1 -1
- data/spec/env/migrations.rb +11 -9
- data/spec/join_association_spec.rb +14 -0
- 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: 6356cdb2e00758c2e17276118d916f727a1be308
|
4
|
+
data.tar.gz: 26faaf1834fd66939b376ef0caa88f7e85d6bc41
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e07f4c676161cd241f861b48aef16411f262f09fc53d9773432e185804e062c41d117d196f553b2fb3fff834c42ec7fbd3e081ad42ed741a6ced87f66d7c1a73
|
7
|
+
data.tar.gz: 9a72da308ac57e7df7dab0fce3c2909d6478f8c96170ede5c98fcf940f51f415c529838150dca14fd83ced259846c84601d312fb406b59a1545d120c43fa46c1
|
data/History.txt
CHANGED
data/README.md
CHANGED
@@ -96,6 +96,23 @@ Post.joins(
|
|
96
96
|
|
97
97
|
Easy peasy.
|
98
98
|
|
99
|
+
Note that pretty much anything you can pass to ActiveRecord's `#join` method you can also pass to `#join_association`'s second argument. For example, you can pass a hash to indicate a set of nested associations:
|
100
|
+
|
101
|
+
```ruby
|
102
|
+
Post.joins(
|
103
|
+
ArelHelpers.join_association(Post, { comments: :author })
|
104
|
+
)
|
105
|
+
```
|
106
|
+
|
107
|
+
This might execute the following query:
|
108
|
+
|
109
|
+
```sql
|
110
|
+
SELECT "posts".*
|
111
|
+
FROM "posts"
|
112
|
+
INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
|
113
|
+
INNER JOIN "authors" ON "authors"."id" = "comments"."author_id"
|
114
|
+
```
|
115
|
+
|
99
116
|
`#join_association` also allows you to customize the join conditions via a block:
|
100
117
|
|
101
118
|
```ruby
|
@@ -108,7 +108,7 @@ module ArelHelpers
|
|
108
108
|
constraints = join_dependency.join_constraints([], join_type)
|
109
109
|
|
110
110
|
binds = constraints.flat_map do |info|
|
111
|
-
prepared_binds =
|
111
|
+
prepared_binds = info.binds.map(&:value_for_database)
|
112
112
|
prepared_binds.map { |value| table.connection.quote(value) }
|
113
113
|
end
|
114
114
|
|
data/lib/arel-helpers/version.rb
CHANGED
data/spec/env/migrations.rb
CHANGED
@@ -1,6 +1,8 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
|
3
|
+
SuperClass = ActiveRecord::VERSION::MAJOR >= 5 && ActiveRecord::VERSION::MINOR >= 1 ? ActiveRecord::Migration[5.1] : ActiveRecord::Migration
|
4
|
+
|
5
|
+
class CreatePostsTable < SuperClass
|
4
6
|
def change
|
5
7
|
create_table :posts do |t|
|
6
8
|
t.column :title, :string
|
@@ -8,7 +10,7 @@ class CreatePostsTable < ActiveRecord::Migration
|
|
8
10
|
end
|
9
11
|
end
|
10
12
|
|
11
|
-
class CreateCommentsTable <
|
13
|
+
class CreateCommentsTable < SuperClass
|
12
14
|
def change
|
13
15
|
create_table :comments do |t|
|
14
16
|
t.references :post
|
@@ -16,7 +18,7 @@ class CreateCommentsTable < ActiveRecord::Migration
|
|
16
18
|
end
|
17
19
|
end
|
18
20
|
|
19
|
-
class CreateAuthorsTable <
|
21
|
+
class CreateAuthorsTable < SuperClass
|
20
22
|
def change
|
21
23
|
create_table :authors do |t|
|
22
24
|
t.references :comment
|
@@ -25,7 +27,7 @@ class CreateAuthorsTable < ActiveRecord::Migration
|
|
25
27
|
end
|
26
28
|
end
|
27
29
|
|
28
|
-
class CreateFavoritesTable <
|
30
|
+
class CreateFavoritesTable < SuperClass
|
29
31
|
def change
|
30
32
|
create_table :favorites do |t|
|
31
33
|
t.references :post
|
@@ -33,7 +35,7 @@ class CreateFavoritesTable < ActiveRecord::Migration
|
|
33
35
|
end
|
34
36
|
end
|
35
37
|
|
36
|
-
class CreateCollabPostsTable <
|
38
|
+
class CreateCollabPostsTable < SuperClass
|
37
39
|
def change
|
38
40
|
create_table :collab_posts do |t|
|
39
41
|
t.references :authors
|
@@ -41,13 +43,13 @@ class CreateCollabPostsTable < ActiveRecord::Migration
|
|
41
43
|
end
|
42
44
|
end
|
43
45
|
|
44
|
-
class CreateCardsTable <
|
46
|
+
class CreateCardsTable < SuperClass
|
45
47
|
def change
|
46
48
|
create_table :cards
|
47
49
|
end
|
48
50
|
end
|
49
51
|
|
50
|
-
class CreateCardLocationsTable <
|
52
|
+
class CreateCardLocationsTable < SuperClass
|
51
53
|
def change
|
52
54
|
create_table :card_locations do |t|
|
53
55
|
t.references :location
|
@@ -56,13 +58,13 @@ class CreateCardLocationsTable < ActiveRecord::Migration
|
|
56
58
|
end
|
57
59
|
end
|
58
60
|
|
59
|
-
class CreateLocationsTable <
|
61
|
+
class CreateLocationsTable < SuperClass
|
60
62
|
def change
|
61
63
|
create_table :locations
|
62
64
|
end
|
63
65
|
end
|
64
66
|
|
65
|
-
class CreateCommunityTicketsTable <
|
67
|
+
class CreateCommunityTicketsTable < SuperClass
|
66
68
|
def change
|
67
69
|
create_table :community_tickets
|
68
70
|
end
|
@@ -29,6 +29,20 @@ describe ArelHelpers do
|
|
29
29
|
'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" INNER JOIN "authors" ON "authors"."id" = "comments"."author_id"'
|
30
30
|
end
|
31
31
|
|
32
|
+
it "should work for a nested hash of association names" do
|
33
|
+
Post
|
34
|
+
.joins(ArelHelpers.join_association(Post, { comments: :author }))
|
35
|
+
.to_sql.should ==
|
36
|
+
'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" INNER JOIN "authors" ON "authors"."id" = "comments"."author_id"'
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should work with outer joins when given a nested hash of association names" do
|
40
|
+
Post
|
41
|
+
.joins(ArelHelpers.join_association(Post, { comments: :author }, Arel::Nodes::OuterJoin))
|
42
|
+
.to_sql.should ==
|
43
|
+
'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id" LEFT OUTER JOIN "authors" ON "authors"."id" = "comments"."author_id"'
|
44
|
+
end
|
45
|
+
|
32
46
|
it "should be able to handle multiple associations" do
|
33
47
|
Post.joins(ArelHelpers.join_association(Post, [:comments, :favorites])).to_sql.should ==
|
34
48
|
'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" INNER JOIN "favorites" ON "favorites"."post_id" = "posts"."id"'
|
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.
|
4
|
+
version: 2.4.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: 2017-05-16 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|