forest_liana 7.0.0 → 7.0.1

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: 22a57de11c5ddee0b53cba1df6f74bebe5cb049534b7eabe58e8c77b5f7e8454
4
- data.tar.gz: 3c26aeac71c9b1aab9e5fcd76434136e8a0779e88433f2522c1e39a18b44442c
3
+ metadata.gz: 66ccf63de4bb8113b724cd22c63957431d50f53b5b5db53d1f48c8d9aa2e6b06
4
+ data.tar.gz: 134e434c2ad58ba2fa80253625e770b1b0c541f33784c9f184d666c14a75c77a
5
5
  SHA512:
6
- metadata.gz: ab71e9560ee8823ad87a3db7cb47ea142f020385dbe33b1679223b09f2190b98e30166864ac3765b58bbca1b8181036d5bd59dcfb616ef5b764a66d89f5d1138
7
- data.tar.gz: 3d9c7dd145ec8616b85df2d7f11c27e65dc5311b5e4e3b03f427143f4a0a906f14cd1b797d840c4830147ace5866d2c5840afc377c36ebfd58412286ecc7e8dd
6
+ metadata.gz: e45967d23524fa78600a52fbe2407d7c076cb3fea24d89c4705ca22ba9836cad534eeafc0a506e061279b57fb3529069f2ffe64c1d3c1a63ca44296e41f63db1
7
+ data.tar.gz: afc9d99afc6d83b3fed291fd9ccf508104bb30c0631506f174a367e1a762868fc2568fb42369b141b28c47b72c9b774a2068aad38ed5d3523497f1babb244501
@@ -109,16 +109,7 @@ module ForestLiana
109
109
  parsed_value = parse_value(operator, value)
110
110
  field_and_operator = "#{parsed_field} #{parsed_operator}"
111
111
 
112
- # parenthesis around the parsed_value are required to make the `IN` operator work
113
- # and have no side effects on other requests
114
- if Rails::VERSION::MAJOR < 5
115
- "#{field_and_operator} (#{ActiveRecord::Base.sanitize(parsed_value)})"
116
- # NOTICE: sanitize method as been removed in Rails 5.1 and sanitize_sql introduced in Rails 5.2.
117
- elsif Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 1
118
- "#{field_and_operator} (#{ActiveRecord::Base.connection.quote(parsed_value)})"
119
- else
120
- ActiveRecord::Base.sanitize_sql(["#{field_and_operator} (?)", parsed_value])
121
- end
112
+ sanitize_condition(field_and_operator, operator, parsed_value)
122
113
  end
123
114
 
124
115
  def parse_aggregation_operator(aggregator_operator)
@@ -296,5 +287,26 @@ module ForestLiana
296
287
  raise ForestLiana::Errors::HTTP422Error.new('Invalid condition format')
297
288
  end
298
289
  end
290
+
291
+ private
292
+
293
+ def prepare_value_for_operator(operator, value)
294
+ # parenthesis around the parsed_value are required to make the `IN` operator work
295
+ operator == 'in' ? "(#{value})" : value
296
+ end
297
+
298
+ def sanitize_condition(field_and_operator, operator, parsed_value)
299
+ if Rails::VERSION::MAJOR < 5
300
+ condition_value = prepare_value_for_operator(operator, ActiveRecord::Base.sanitize(parsed_value))
301
+ "#{field_and_operator} #{condition_value}"
302
+ # NOTICE: sanitize method as been removed in Rails 5.1 and sanitize_sql introduced in Rails 5.2.
303
+ elsif Rails::VERSION::MAJOR == 5 && Rails::VERSION::MINOR == 1
304
+ condition_value = prepare_value_for_operator(operator, ActiveRecord::Base.connection.quote(parsed_value))
305
+ "#{field_and_operator} #{condition_value}"
306
+ else
307
+ condition_value = prepare_value_for_operator(operator, '?')
308
+ ActiveRecord::Base.sanitize_sql(["#{field_and_operator} #{condition_value}", parsed_value])
309
+ end
310
+ end
299
311
  end
300
312
  end
@@ -1,3 +1,3 @@
1
1
  module ForestLiana
2
- VERSION = "7.0.0"
2
+ VERSION = "7.0.1"
3
3
  end
@@ -11,6 +11,7 @@ module ForestLiana
11
11
  let(:date_condition_1) { { 'field' => 'created_at', 'operator' => 'before', 'value' => 2.hours.ago } }
12
12
  let(:date_condition_2) { { 'field' => 'created_at', 'operator' => 'today' } }
13
13
  let(:date_condition_3) { { 'field' => 'created_at', 'operator' => 'previous_x_days', 'value' => 2 } }
14
+ let(:presence_condition) { { 'field' => 'name', 'operator' => 'present' } }
14
15
 
15
16
  before {
16
17
  island = Island.create!(name: "L'île de la muerta")
@@ -274,6 +275,11 @@ module ForestLiana
274
275
  let(:filters) { { 'aggregator' => 'or', 'conditions' => [simple_condition_2, simple_condition_3] } }
275
276
  it { expect(resource.where(query).count).to eq 2 }
276
277
  end
278
+
279
+ context "'name ends_with \"3\"' 'or' 'name is not null'" do
280
+ let(:filters) { { 'aggregator' => 'or', 'conditions' => [simple_condition_2, presence_condition] } }
281
+ it { expect(resource.where(query).count).to eq 3 }
282
+ end
277
283
  end
278
284
 
279
285
  describe 'parse_condition' do
@@ -281,7 +287,27 @@ module ForestLiana
281
287
  let(:result) { filter_parser.parse_condition(condition) }
282
288
 
283
289
  context 'on valid condition' do
284
- it { expect(result).to eq "\"trees\".\"name\" LIKE ('%3')" }
290
+ context 'when the condition uses the contains operator' do
291
+ it { expect(result).to eq "\"trees\".\"name\" LIKE '%3'" }
292
+ end
293
+
294
+ context 'when the condition uses the blank operator' do
295
+ let(:condition) { { 'field' => 'name', 'operator' => 'blank' } }
296
+
297
+ it { expect(result).to eq "\"trees\".\"name\" IS NULL" }
298
+ end
299
+
300
+ context 'when the condition uses the presence operator' do
301
+ let(:condition) { presence_condition }
302
+
303
+ it { expect(result).to eq "\"trees\".\"name\" IS NOT NULL" }
304
+ end
305
+
306
+ context 'when the condition uses the in operator' do
307
+ let(:condition) { { 'field' => 'name', 'operator' => 'in', 'value' => ['Tree n1', 'Tree n3'] } }
308
+
309
+ it { expect(result).to eq "\"trees\".\"name\" IN ('Tree n1','Tree n3')" }
310
+ end
285
311
  end
286
312
 
287
313
  context 'on belongs_to condition' do
@@ -84,7 +84,7 @@ module ForestLiana
84
84
  subject.perform
85
85
 
86
86
  expect(subject.record).to be nil
87
- expect(subject.errors[0][:detail]).to eq 'Couldn\'t find User with \'id\'=1 [WHERE (("users"."id" > (2)))]'
87
+ expect(subject.errors[0][:detail]).to eq 'Couldn\'t find User with \'id\'=1 [WHERE (("users"."id" > 2))]'
88
88
  end
89
89
  end
90
90
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: forest_liana
3
3
  version: !ruby/object:Gem::Version
4
- version: 7.0.0
4
+ version: 7.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sandro Munda
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-20 00:00:00.000000000 Z
11
+ date: 2021-07-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails