arel-helpers 2.11.0 → 2.12.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile +3 -2
- data/arel-helpers.gemspec +12 -9
- data/lib/arel-helpers/join_association.rb +61 -34
- data/lib/arel-helpers/version.rb +1 -1
- data/spec/aliases_spec.rb +5 -7
- data/spec/arel_table_spec.rb +11 -13
- data/spec/env/models.rb +2 -6
- data/spec/internal/config/database.yml +3 -0
- data/spec/internal/db/combustion_test.sqlite +0 -0
- data/spec/internal/db/schema.rb +34 -0
- data/spec/internal/log/test.log +2979 -0
- data/spec/join_association_spec.rb +101 -57
- data/spec/query_builder_spec.rb +24 -25
- data/spec/spec_helper.rb +10 -19
- metadata +57 -13
- data/spec/env.rb +0 -44
- data/spec/env/migrations.rb +0 -75
@@ -1,94 +1,137 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
1
|
require 'spec_helper'
|
4
2
|
|
5
3
|
describe ArelHelpers do
|
6
|
-
describe
|
7
|
-
it
|
8
|
-
Post.joins(ArelHelpers.join_association(Post, :comments)).to_sql.
|
9
|
-
'SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"'
|
4
|
+
describe '#join_association' do
|
5
|
+
it 'should work for a directly associated model' do
|
6
|
+
expect(Post.joins(ArelHelpers.join_association(Post, :comments)).to_sql).to(
|
7
|
+
eq('SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"')
|
8
|
+
)
|
10
9
|
end
|
11
10
|
|
12
|
-
it
|
13
|
-
Post.joins(ArelHelpers.join_association(Post, :comments, Arel::Nodes::OuterJoin)).to_sql.
|
14
|
-
'SELECT "posts".* FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id"'
|
11
|
+
it 'should work with an outer join' do
|
12
|
+
expect(Post.joins(ArelHelpers.join_association(Post, :comments, Arel::Nodes::OuterJoin)).to_sql).to(
|
13
|
+
eq('SELECT "posts".* FROM "posts" LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id"')
|
14
|
+
)
|
15
15
|
end
|
16
16
|
|
17
|
-
it
|
18
|
-
Post.joins(ArelHelpers.join_association(Post, :comments) do |
|
17
|
+
it 'should allow adding additional join conditions' do
|
18
|
+
sql = Post.joins(ArelHelpers.join_association(Post, :comments) do |_assoc_name, join_conditions|
|
19
19
|
join_conditions.and(Comment[:id].eq(10))
|
20
|
-
end).to_sql
|
21
|
-
|
20
|
+
end).to_sql
|
21
|
+
|
22
|
+
expect(sql).to eq <<-SQL.squish
|
23
|
+
SELECT "posts".* FROM "posts"
|
24
|
+
INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" AND "comments"."id" = 10
|
25
|
+
SQL
|
22
26
|
end
|
23
27
|
|
24
|
-
it
|
25
|
-
Post
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
28
|
+
it 'should work for two models, one directly associated and the other indirectly' do
|
29
|
+
sql = Post
|
30
|
+
.joins(ArelHelpers.join_association(Post, :comments))
|
31
|
+
.joins(ArelHelpers.join_association(Comment, :author))
|
32
|
+
.to_sql
|
33
|
+
|
34
|
+
expect(sql).to eq <<-SQL.squish
|
35
|
+
SELECT "posts".* FROM "posts"
|
36
|
+
INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
|
37
|
+
INNER JOIN "authors" ON "authors"."id" = "comments"."author_id"
|
38
|
+
SQL
|
30
39
|
end
|
31
40
|
|
32
|
-
it
|
33
|
-
Post
|
34
|
-
|
35
|
-
|
36
|
-
|
41
|
+
it 'should work for a nested hash of association names' do
|
42
|
+
sql = Post
|
43
|
+
.joins(ArelHelpers.join_association(Post, { comments: :author }))
|
44
|
+
.to_sql
|
45
|
+
|
46
|
+
expect(sql).to eq <<-SQL.squish
|
47
|
+
SELECT "posts".* FROM "posts"
|
48
|
+
INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
|
49
|
+
INNER JOIN "authors" ON "authors"."id" = "comments"."author_id"
|
50
|
+
SQL
|
37
51
|
end
|
38
52
|
|
39
|
-
it
|
40
|
-
Post
|
41
|
-
|
42
|
-
|
43
|
-
|
53
|
+
it 'should work with outer joins when given a nested hash of association names' do
|
54
|
+
sql = Post.joins(ArelHelpers.join_association(Post, { comments: :author }, Arel::Nodes::OuterJoin)).to_sql
|
55
|
+
|
56
|
+
expect(sql).to eq <<-SQL.squish
|
57
|
+
SELECT "posts".* FROM "posts"
|
58
|
+
LEFT OUTER JOIN "comments" ON "comments"."post_id" = "posts"."id"
|
59
|
+
LEFT OUTER JOIN "authors" ON "authors"."id" = "comments"."author_id"
|
60
|
+
SQL
|
44
61
|
end
|
45
62
|
|
46
|
-
it
|
47
|
-
Post.joins(ArelHelpers.join_association(Post, [
|
48
|
-
|
63
|
+
it 'should be able to handle multiple associations' do
|
64
|
+
expect(Post.joins(ArelHelpers.join_association(Post, %i[comments favorites])).to_sql).to(
|
65
|
+
eq <<-SQL.squish
|
66
|
+
SELECT "posts".* FROM "posts"
|
67
|
+
INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
|
68
|
+
INNER JOIN "favorites" ON "favorites"."post_id" = "posts"."id"
|
69
|
+
SQL
|
70
|
+
)
|
49
71
|
end
|
50
72
|
|
51
|
-
it
|
52
|
-
Post.joins(ArelHelpers.join_association(Post, [
|
73
|
+
it 'should yield once for each association' do
|
74
|
+
sql = Post.joins(ArelHelpers.join_association(Post, %i[comments favorites]) do |assoc_name, join_conditions|
|
53
75
|
case assoc_name
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
76
|
+
when :favorites
|
77
|
+
join_conditions.or(Favorite[:amount].eq('lots'))
|
78
|
+
when :comments
|
79
|
+
join_conditions.and(Comment[:text].eq('Awesome post!'))
|
58
80
|
end
|
59
|
-
end).to_sql
|
60
|
-
|
81
|
+
end).to_sql
|
82
|
+
|
83
|
+
expect(sql).to eq <<-SQL.squish
|
84
|
+
SELECT "posts".* FROM "posts"
|
85
|
+
INNER JOIN "comments" ON "comments"."post_id" = "posts"."id" AND "comments"."text" = 'Awesome post!'
|
86
|
+
INNER JOIN "favorites" (ON "favorites"."post_id" = "posts"."id" OR "favorites"."amount" = 'lots')
|
87
|
+
SQL
|
61
88
|
end
|
62
89
|
|
63
90
|
it 'should be able to handle has_and_belongs_to_many associations' do
|
64
|
-
CollabPost.joins(ArelHelpers.join_association(CollabPost, :authors)).to_sql
|
65
|
-
|
91
|
+
sql = CollabPost.joins(ArelHelpers.join_association(CollabPost, :authors)).to_sql
|
92
|
+
|
93
|
+
expect(sql).to eq <<-SQL.squish
|
94
|
+
SELECT "collab_posts".* FROM "collab_posts"
|
95
|
+
INNER JOIN "authors_collab_posts" ON "authors_collab_posts"."collab_post_id" = "collab_posts"."id"
|
96
|
+
INNER JOIN "authors" ON "authors"."id" = "authors_collab_posts"."author_id"
|
97
|
+
SQL
|
66
98
|
end
|
67
99
|
|
68
|
-
it
|
100
|
+
it 'allows adding a custom alias to the joined table' do
|
69
101
|
Comment.aliased_as(:foo) do |foo|
|
70
|
-
Post.joins(
|
71
|
-
|
102
|
+
sql = Post.joins(
|
103
|
+
ArelHelpers.join_association(Post, :comments, Arel::Nodes::InnerJoin, aliases: [foo])
|
104
|
+
).to_sql
|
105
|
+
|
106
|
+
expect(sql).to eq 'SELECT "posts".* FROM "posts" INNER JOIN "comments" "foo" ON "foo"."post_id" = "posts"."id"'
|
72
107
|
end
|
73
108
|
end
|
74
109
|
|
75
|
-
it
|
110
|
+
it 'allows aliasing multiple associations' do
|
76
111
|
Comment.aliased_as(:foo) do |foo|
|
77
112
|
Favorite.aliased_as(:bar) do |bar|
|
78
|
-
Post.joins(
|
79
|
-
|
113
|
+
sql = Post.joins(
|
114
|
+
ArelHelpers.join_association(Post, %i[comments favorites], Arel::Nodes::InnerJoin, aliases: [foo, bar])
|
115
|
+
).to_sql
|
116
|
+
|
117
|
+
expect(sql).to eq <<-SQL.squish
|
118
|
+
SELECT "posts".* FROM "posts"
|
119
|
+
INNER JOIN "comments" "foo" ON "foo"."post_id" = "posts"."id"
|
120
|
+
INNER JOIN "favorites" "bar" ON "bar"."post_id" = "posts"."id"
|
121
|
+
SQL
|
80
122
|
end
|
81
123
|
end
|
82
124
|
end
|
83
125
|
|
84
126
|
it 'handles polymorphic through associations' do
|
85
|
-
|
86
|
-
|
87
|
-
|
127
|
+
location = Location.create!
|
128
|
+
ticket = CommunityTicket.create!
|
129
|
+
CardLocation.create! card: ticket, location: location
|
130
|
+
|
131
|
+
relation = Location.joins(ArelHelpers.join_association(Location, :community_tickets))
|
88
132
|
|
89
|
-
relation.
|
90
|
-
|
91
|
-
'INNER JOIN "community_tickets" ON "community_tickets"."id" = "card_locations"."card_id"'
|
133
|
+
expect(relation.count).to eq 1
|
134
|
+
expect(relation.to_a).to include location
|
92
135
|
end
|
93
136
|
end
|
94
137
|
end
|
@@ -98,8 +141,9 @@ describe ArelHelpers::JoinAssociation do
|
|
98
141
|
include ArelHelpers::JoinAssociation
|
99
142
|
end
|
100
143
|
|
101
|
-
it
|
102
|
-
AssocPost.joins(AssocPost.join_association(:comments)).to_sql.
|
103
|
-
|
144
|
+
it 'should provide the join_association method and use the parent class as the model to join on' do
|
145
|
+
expect(AssocPost.joins(AssocPost.join_association(:comments)).to_sql).to eq <<-SQL.squish
|
146
|
+
SELECT "posts".* FROM "posts" INNER JOIN "comments" ON "comments"."post_id" = "posts"."id"
|
147
|
+
SQL
|
104
148
|
end
|
105
149
|
end
|
data/spec/query_builder_spec.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
# encoding: UTF-8
|
2
|
-
|
3
1
|
require 'spec_helper'
|
4
2
|
|
5
3
|
class TestQueryBuilder < ArelHelpers::QueryBuilder
|
@@ -15,60 +13,61 @@ class TestQueryBuilder < ArelHelpers::QueryBuilder
|
|
15
13
|
end
|
16
14
|
|
17
15
|
not_nil def optional(skip:)
|
18
|
-
reflect(query.where(title:
|
16
|
+
reflect(query.where(title: 'BarBar')) unless skip
|
19
17
|
end
|
20
18
|
end
|
21
19
|
|
22
20
|
describe ArelHelpers::QueryBuilder do
|
23
21
|
let(:builder) { TestQueryBuilder.new }
|
24
22
|
|
25
|
-
it
|
23
|
+
it 'returns (i.e. reflects) new instances of QueryBuilder' do
|
26
24
|
builder.tap do |original_builder|
|
27
25
|
original_builder.params = true
|
28
26
|
new_builder = original_builder.noop
|
29
|
-
new_builder.object_id.
|
30
|
-
new_builder.params
|
27
|
+
expect(new_builder.object_id).not_to eq original_builder.object_id
|
28
|
+
expect(new_builder.params?).to be true
|
31
29
|
end
|
32
30
|
end
|
33
31
|
|
34
|
-
it
|
35
|
-
Post.create(title:
|
32
|
+
it 'forwards #to_a' do
|
33
|
+
Post.create(title: 'Foobar')
|
36
34
|
builder.to_a.tap do |posts|
|
37
|
-
posts.size.
|
38
|
-
posts.first.title.
|
35
|
+
expect(posts.size).to eq 1
|
36
|
+
expect(posts.first.title).to eq 'Foobar'
|
39
37
|
end
|
40
38
|
end
|
41
39
|
|
42
|
-
it
|
43
|
-
builder.to_sql.strip.
|
40
|
+
it 'forwards #to_sql' do
|
41
|
+
expect(builder.to_sql.strip).to eq 'SELECT "posts".* FROM "posts"'
|
44
42
|
end
|
45
43
|
|
46
|
-
it
|
47
|
-
created_post = Post.create(title:
|
44
|
+
it 'forwards #each' do
|
45
|
+
created_post = Post.create(title: 'Foobar')
|
48
46
|
builder.each do |post|
|
49
|
-
post.
|
47
|
+
expect(post).to eq created_post
|
50
48
|
end
|
51
49
|
end
|
52
50
|
|
53
|
-
it
|
54
|
-
Post.create(title:
|
55
|
-
builder.map(&:title).
|
51
|
+
it 'forwards other enumerable methods via #each' do
|
52
|
+
Post.create(title: 'Foobar')
|
53
|
+
expect(builder.map(&:title)).to eq ['Foobar']
|
56
54
|
end
|
57
55
|
|
58
56
|
ArelHelpers::QueryBuilder::TERMINAL_METHODS.each do |method|
|
59
57
|
it "does not enumerate records for #{method}" do
|
60
|
-
|
58
|
+
allow(builder).to receive :each
|
61
59
|
builder.public_send(method)
|
60
|
+
expect(builder).not_to have_received :each
|
62
61
|
end
|
63
62
|
end
|
64
63
|
|
65
|
-
it
|
66
|
-
builder.optional(skip: true).to_sql.strip.
|
64
|
+
it 'returns reflect on existing query if method returns a falsy value' do
|
65
|
+
expect(builder.optional(skip: true).to_sql.strip).to eq 'SELECT "posts".* FROM "posts"'
|
67
66
|
end
|
68
67
|
|
69
|
-
it
|
70
|
-
builder.optional(skip: false).to_sql.strip.gsub(/\s+/,
|
71
|
-
SELECT
|
72
|
-
|
68
|
+
it 'returns reflect on new query for default chainable method if value is truthy' do
|
69
|
+
expect(builder.optional(skip: false).to_sql.strip.gsub(/\s+/, ' ')).to eq <<-SQL.squish
|
70
|
+
SELECT "posts".* FROM "posts" WHERE "posts"."title" = 'BarBar'
|
71
|
+
SQL
|
73
72
|
end
|
74
73
|
end
|
data/spec/spec_helper.rb
CHANGED
@@ -1,29 +1,20 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
$:.push(File.dirname(__FILE__))
|
1
|
+
$LOAD_PATH.push(File.dirname(__FILE__))
|
4
2
|
|
5
3
|
require 'rspec'
|
6
4
|
require 'arel-helpers'
|
7
5
|
require 'fileutils'
|
8
6
|
|
9
|
-
require '
|
7
|
+
require 'combustion'
|
8
|
+
Combustion.initialize! :active_record
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
$stdout = StringIO.new
|
14
|
-
begin
|
15
|
-
yield
|
16
|
-
ensure
|
17
|
-
$stdout = original_stdout
|
18
|
-
end
|
19
|
-
end
|
10
|
+
require 'database_cleaner'
|
11
|
+
require 'env/models'
|
20
12
|
|
21
13
|
RSpec.configure do |config|
|
22
|
-
|
14
|
+
DatabaseCleaner.strategy = :transaction
|
15
|
+
|
16
|
+
config.before(:suite) { DatabaseCleaner.clean_with :truncation }
|
23
17
|
|
24
|
-
config.before
|
25
|
-
|
26
|
-
ArelHelpers::Env.reset
|
27
|
-
silence { ArelHelpers::Env.migrate }
|
28
|
-
end
|
18
|
+
config.before { DatabaseCleaner.start }
|
19
|
+
config.after { DatabaseCleaner.clean }
|
29
20
|
end
|
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.12.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Cameron Dutro
|
8
|
-
autorequire:
|
8
|
+
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2020-12-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -30,6 +30,48 @@ dependencies:
|
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
32
|
version: '7'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: appraisal
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - ">="
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: combustion
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '1.3'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '1.3'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: database_cleaner
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.8'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '1.8'
|
33
75
|
- !ruby/object:Gem::Dependency
|
34
76
|
name: rake
|
35
77
|
requirement: !ruby/object:Gem::Requirement
|
@@ -50,28 +92,28 @@ dependencies:
|
|
50
92
|
requirements:
|
51
93
|
- - "~>"
|
52
94
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
95
|
+
version: '3'
|
54
96
|
type: :development
|
55
97
|
prerelease: false
|
56
98
|
version_requirements: !ruby/object:Gem::Requirement
|
57
99
|
requirements:
|
58
100
|
- - "~>"
|
59
101
|
- !ruby/object:Gem::Version
|
60
|
-
version: '
|
102
|
+
version: '3'
|
61
103
|
- !ruby/object:Gem::Dependency
|
62
|
-
name:
|
104
|
+
name: sqlite3
|
63
105
|
requirement: !ruby/object:Gem::Requirement
|
64
106
|
requirements:
|
65
107
|
- - "~>"
|
66
108
|
- !ruby/object:Gem::Version
|
67
|
-
version:
|
109
|
+
version: 1.4.0
|
68
110
|
type: :development
|
69
111
|
prerelease: false
|
70
112
|
version_requirements: !ruby/object:Gem::Requirement
|
71
113
|
requirements:
|
72
114
|
- - "~>"
|
73
115
|
- !ruby/object:Gem::Version
|
74
|
-
version:
|
116
|
+
version: 1.4.0
|
75
117
|
description: Useful tools to help construct database queries with ActiveRecord and
|
76
118
|
Arel.
|
77
119
|
email:
|
@@ -93,9 +135,11 @@ files:
|
|
93
135
|
- lib/arel-helpers/version.rb
|
94
136
|
- spec/aliases_spec.rb
|
95
137
|
- spec/arel_table_spec.rb
|
96
|
-
- spec/env.rb
|
97
|
-
- spec/env/migrations.rb
|
98
138
|
- spec/env/models.rb
|
139
|
+
- spec/internal/config/database.yml
|
140
|
+
- spec/internal/db/combustion_test.sqlite
|
141
|
+
- spec/internal/db/schema.rb
|
142
|
+
- spec/internal/log/test.log
|
99
143
|
- spec/join_association_spec.rb
|
100
144
|
- spec/query_builder_spec.rb
|
101
145
|
- spec/spec_helper.rb
|
@@ -103,7 +147,7 @@ homepage: https://github.com/camertron/arel-helpers
|
|
103
147
|
licenses:
|
104
148
|
- MIT
|
105
149
|
metadata: {}
|
106
|
-
post_install_message:
|
150
|
+
post_install_message:
|
107
151
|
rdoc_options: []
|
108
152
|
require_paths:
|
109
153
|
- lib
|
@@ -118,8 +162,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
118
162
|
- !ruby/object:Gem::Version
|
119
163
|
version: '0'
|
120
164
|
requirements: []
|
121
|
-
rubygems_version: 3.
|
122
|
-
signing_key:
|
165
|
+
rubygems_version: 3.1.2
|
166
|
+
signing_key:
|
123
167
|
specification_version: 4
|
124
168
|
summary: Useful tools to help construct database queries with ActiveRecord and Arel.
|
125
169
|
test_files: []
|