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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 209526ee9962414d0eb5bab7820f6c97cfc51edf362d4ccdaf6b7309e7196fbe
4
- data.tar.gz: e3923216fd30b2ab762c581ef0056145a1b09758433159123bee1df3443bc924
3
+ metadata.gz: b15a810e483ddccf300cec2e2926a44b05be707e557fa7f89d432fa6542ddc72
4
+ data.tar.gz: 8acaece31ba468798d127deab33ac06c8569b71d51e0a813902f398a5f3efbcc
5
5
  SHA512:
6
- metadata.gz: 48814845832553b87d496b70982c46a905b0a2aecba5426d3f9e36f3fc2436cee0e71e68ab733fbc0b37cf90981515de695f309d5a4ea35d49becc83d4d1703f
7
- data.tar.gz: 638fa0c5ed41050e17bdd642fab0b4dfc94e0d3bb8084a1ca9c6cc89a9d340f11aadc0d0ba98d78628ecf4bbacd465385074cc1f0f82167f1b50298361672472
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
@@ -1,5 +1,5 @@
1
1
  # encoding: UTF-8
2
2
 
3
3
  module ArelHelpers
4
- VERSION = '2.10.0'
4
+ VERSION = '2.11.0'
5
5
  end
@@ -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.10.0
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-07-31 00:00:00.000000000 Z
11
+ date: 2019-12-21 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord