arel-helpers 2.8.0 → 2.11.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/Gemfile +2 -16
- data/README.md +27 -1
- data/arel-helpers.gemspec +5 -6
- data/lib/arel-helpers/join_association.rb +32 -1
- data/lib/arel-helpers/query_builder.rb +15 -0
- data/lib/arel-helpers/version.rb +1 -1
- data/spec/env/migrations.rb +5 -1
- data/spec/query_builder_spec.rb +14 -0
- data/spec/spec_helper.rb +0 -1
- metadata +47 -6
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b15a810e483ddccf300cec2e2926a44b05be707e557fa7f89d432fa6542ddc72
|
4
|
+
data.tar.gz: 8acaece31ba468798d127deab33ac06c8569b71d51e0a813902f398a5f3efbcc
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 265f9cd3e9cbc2d8e3b4c4aa6cb4d2b3cbab4ab8cba267a7088ec56da4cc038271da0ca51d3e2d2d3923cf04d04fc4c8ee79fda0cd571f2b59561677b8dba6d2
|
7
|
+
data.tar.gz: 60de9c7c46e7d1ed33f8d5ac9466613075e8ab1fd0b9a3335a1f88c6ccc19b64a388297c151578fb23d090c58847151a01c4af5a3365702941fb607c9f0512c5
|
data/Gemfile
CHANGED
@@ -1,16 +1,2 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
gemspec
|
4
|
-
|
5
|
-
group :development, :test do
|
6
|
-
gem 'pry-byebug'
|
7
|
-
|
8
|
-
# lock to 10.0 until rspec is upgraded
|
9
|
-
gem 'rake', '~> 10.0'
|
10
|
-
end
|
11
|
-
|
12
|
-
group :test do
|
13
|
-
gem 'rspec', '~> 2.11.0'
|
14
|
-
gem 'rr', '~> 1.0.4'
|
15
|
-
gem 'sqlite3'
|
16
|
-
end
|
1
|
+
ENV['BUNDLE_GEMFILE'] = File.expand_path('../Gemfile-rails-6.0.x', __FILE__)
|
2
|
+
Bundler.load
|
data/README.md
CHANGED
@@ -155,7 +155,7 @@ class PostQueryBuilder < ArelHelpers::QueryBuilder
|
|
155
155
|
def with_comments_by(usernames)
|
156
156
|
reflect(
|
157
157
|
query
|
158
|
-
.joins(:
|
158
|
+
.joins(comments: :author)
|
159
159
|
.where(author[:username].in(usernames))
|
160
160
|
)
|
161
161
|
end
|
@@ -187,6 +187,32 @@ PostQueryBuilder.new
|
|
187
187
|
.since_yesterday
|
188
188
|
```
|
189
189
|
|
190
|
+
#### Conditional reflections
|
191
|
+
|
192
|
+
If you have parts of a query that should only be added under certain conditions you can return `reflect(query)` from your method. E.g:
|
193
|
+
|
194
|
+
```ruby
|
195
|
+
def with_comments_by(usernames)
|
196
|
+
if usernames
|
197
|
+
reflect(
|
198
|
+
query.where(post[:title].matches("%#{title}%"))
|
199
|
+
)
|
200
|
+
else
|
201
|
+
reflect(query)
|
202
|
+
end
|
203
|
+
end
|
204
|
+
```
|
205
|
+
|
206
|
+
This can become repetitive, and as an alternative you can choose to prepend `not_nil` to your method definition:
|
207
|
+
|
208
|
+
```ruby
|
209
|
+
class PostQueryBuilder < ArelHelpers::QueryBuilder
|
210
|
+
not_nil def with_comments_by(usernames)
|
211
|
+
reflect(query.where(post[:title].matches("%#{title}%"))) if usernames
|
212
|
+
end
|
213
|
+
end
|
214
|
+
```
|
215
|
+
|
190
216
|
## Requirements
|
191
217
|
|
192
218
|
Requires ActiveRecord >= 3.1.0, < 6, tested against Ruby 2.2.4. Depends on SQLite for testing purposes.
|
data/arel-helpers.gemspec
CHANGED
@@ -11,13 +11,12 @@ Gem::Specification.new do |s|
|
|
11
11
|
s.description = s.summary = "Useful tools to help construct database queries with ActiveRecord and Arel."
|
12
12
|
|
13
13
|
s.platform = Gem::Platform::RUBY
|
14
|
-
s.has_rdoc = true
|
15
14
|
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
15
|
+
s.add_dependency 'activerecord', '>= 3.1.0', '< 7'
|
16
|
+
|
17
|
+
s.add_development_dependency 'rake', '~> 10.0'
|
18
|
+
s.add_development_dependency 'rspec', '~> 2.11'
|
19
|
+
s.add_development_dependency 'rr', '~> 1.0'
|
21
20
|
|
22
21
|
s.require_path = 'lib'
|
23
22
|
s.files = Dir["{lib,spec}/**/*", "Gemfile", "History.txt", "README.md", "Rakefile", "arel-helpers.gemspec"]
|
@@ -18,7 +18,9 @@ module ArelHelpers
|
|
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
20
|
def join_association(table, association, join_type = Arel::Nodes::InnerJoin, options = {}, &block)
|
21
|
-
if version >= '
|
21
|
+
if version >= '6.0.0'
|
22
|
+
join_association_6_0_0(table, association, join_type, options, &block)
|
23
|
+
elsif version >= '5.2.1'
|
22
24
|
join_association_5_2_1(table, association, join_type, options, &block)
|
23
25
|
elsif version >= '5.2.0'
|
24
26
|
join_association_5_2(table, association, join_type, options, &block)
|
@@ -217,6 +219,35 @@ module ArelHelpers
|
|
217
219
|
end
|
218
220
|
end
|
219
221
|
|
222
|
+
def join_association_6_0_0(table, association, join_type, options = {})
|
223
|
+
aliases = options.fetch(:aliases, [])
|
224
|
+
associations = association.is_a?(Array) ? association : [association]
|
225
|
+
|
226
|
+
alias_tracker = ActiveRecord::Associations::AliasTracker.create(
|
227
|
+
table.connection, table.name, {}
|
228
|
+
)
|
229
|
+
|
230
|
+
join_dependency = ActiveRecord::Associations::JoinDependency.new(
|
231
|
+
table, table.arel_table, associations, join_type
|
232
|
+
)
|
233
|
+
|
234
|
+
constraints = join_dependency.join_constraints([], alias_tracker)
|
235
|
+
|
236
|
+
constraints.map do |join|
|
237
|
+
right = if block_given?
|
238
|
+
yield join.left.name.to_sym, join.right
|
239
|
+
else
|
240
|
+
join.right
|
241
|
+
end
|
242
|
+
|
243
|
+
if found_alias = find_alias(join.left.name, aliases)
|
244
|
+
join.left.table_alias = found_alias.name
|
245
|
+
end
|
246
|
+
|
247
|
+
join_type.new(join.left, right)
|
248
|
+
end
|
249
|
+
end
|
250
|
+
|
220
251
|
private
|
221
252
|
|
222
253
|
def to_sql(node, table, binds)
|
@@ -16,6 +16,21 @@ module ArelHelpers
|
|
16
16
|
|
17
17
|
def_delegators :@query, *TERMINAL_METHODS
|
18
18
|
|
19
|
+
def self.not_nil(name)
|
20
|
+
mod = Module.new do
|
21
|
+
define_method(name) do |*args|
|
22
|
+
if (value = super(*args))
|
23
|
+
value
|
24
|
+
else
|
25
|
+
reflect(query)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
prepend mod
|
31
|
+
name
|
32
|
+
end
|
33
|
+
|
19
34
|
def initialize(query)
|
20
35
|
@query = query
|
21
36
|
end
|
data/lib/arel-helpers/version.rb
CHANGED
data/spec/env/migrations.rb
CHANGED
@@ -1,6 +1,10 @@
|
|
1
1
|
# encoding: UTF-8
|
2
2
|
|
3
|
-
SuperClass = ActiveRecord::VERSION::MAJOR >= 5
|
3
|
+
SuperClass = if ActiveRecord::VERSION::MAJOR >= 5
|
4
|
+
ActiveRecord::Migration["#{ActiveRecord::VERSION::MAJOR}.#{ActiveRecord::VERSION::MINOR}".to_f]
|
5
|
+
else
|
6
|
+
ActiveRecord::Migration
|
7
|
+
end
|
4
8
|
|
5
9
|
class CreatePostsTable < SuperClass
|
6
10
|
def change
|
data/spec/query_builder_spec.rb
CHANGED
@@ -13,6 +13,10 @@ class TestQueryBuilder < ArelHelpers::QueryBuilder
|
|
13
13
|
def noop
|
14
14
|
reflect(query)
|
15
15
|
end
|
16
|
+
|
17
|
+
not_nil def optional(skip:)
|
18
|
+
reflect(query.where(title: "BarBar")) unless skip
|
19
|
+
end
|
16
20
|
end
|
17
21
|
|
18
22
|
describe ArelHelpers::QueryBuilder do
|
@@ -57,4 +61,14 @@ describe ArelHelpers::QueryBuilder do
|
|
57
61
|
builder.public_send(method)
|
58
62
|
end
|
59
63
|
end
|
64
|
+
|
65
|
+
it "returns reflect on existing query if method returns a falsy value" do
|
66
|
+
builder.optional(skip: true).to_sql.strip.should == 'SELECT "posts".* FROM "posts"'
|
67
|
+
end
|
68
|
+
|
69
|
+
it "returns reflect on new query for default chainable method if value is truthy" do
|
70
|
+
builder.optional(skip: false).to_sql.strip.gsub(/\s+/, " ").should == %Q{
|
71
|
+
SELECT \"posts\".* FROM \"posts\" WHERE \"posts\".\"title\" = 'BarBar'
|
72
|
+
}.strip
|
73
|
+
end
|
60
74
|
end
|
data/spec/spec_helper.rb
CHANGED
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.11.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: 2019-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|
@@ -19,7 +19,7 @@ dependencies:
|
|
19
19
|
version: 3.1.0
|
20
20
|
- - "<"
|
21
21
|
- !ruby/object:Gem::Version
|
22
|
-
version: '
|
22
|
+
version: '7'
|
23
23
|
type: :runtime
|
24
24
|
prerelease: false
|
25
25
|
version_requirements: !ruby/object:Gem::Requirement
|
@@ -29,7 +29,49 @@ dependencies:
|
|
29
29
|
version: 3.1.0
|
30
30
|
- - "<"
|
31
31
|
- !ruby/object:Gem::Version
|
32
|
-
version: '
|
32
|
+
version: '7'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '10.0'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '10.0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rspec
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '2.11'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '2.11'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rr
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '1.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '1.0'
|
33
75
|
description: Useful tools to help construct database queries with ActiveRecord and
|
34
76
|
Arel.
|
35
77
|
email:
|
@@ -76,8 +118,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
76
118
|
- !ruby/object:Gem::Version
|
77
119
|
version: '0'
|
78
120
|
requirements: []
|
79
|
-
|
80
|
-
rubygems_version: 2.7.6
|
121
|
+
rubygems_version: 3.0.4
|
81
122
|
signing_key:
|
82
123
|
specification_version: 4
|
83
124
|
summary: Useful tools to help construct database queries with ActiveRecord and Arel.
|