arel-helpers 2.10.0 → 2.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -0
- data/lib/arel-helpers/query_builder.rb +15 -0
- data/lib/arel-helpers/version.rb +1 -1
- data/spec/query_builder_spec.rb +14 -0
- metadata +2 -2
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/README.md
CHANGED
@@ -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.
|
@@ -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/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
|
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: 2019-
|
11
|
+
date: 2019-12-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|