scoped_search 4.1.10 → 4.1.12

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: f0f6f98982cba23ec69f2b1adaf0c44832abb19adad7a347d4d78c4b0fbb63a8
4
- data.tar.gz: 114ea022f87208dc89b92247687d64c2a0146119afa33f2106b69507271ba210
3
+ metadata.gz: 0fe5811549cf2e222ac16ea429293269abdd0734793b9e83611e76527acd905b
4
+ data.tar.gz: 22463f4c28a03919dc7e42cd4942dcc3d66c0291bcb087fa5ad1688492726efd
5
5
  SHA512:
6
- metadata.gz: 6a87832ba5990fd45b7116d374a9c2505c7f9fcf62b4364859986ae497b2aa6d3a0890b222b1ad6c07ea999d48b25fbc42303a63cdc8ce3f4ea3a23e1f049eeb
7
- data.tar.gz: a5cd950d77712004cb649261f08a8e8e04169622b346de7c3b15b422dbae7343bf8211febbdb988cf390a43c5f4794c93e820b9bc440d4cc9130d7f3146464f9
6
+ metadata.gz: 3e36707917762b0f2759da997bceb96b2f91b91bcbdb920d03a1738e408bc72a545e380f55bd5c35b9448d2916ac7ddb5dcc0ec623b6a36726d464ac818e15cb
7
+ data.tar.gz: 1b0e4c026cffd08a3851a5929dd8ee7244c041c27d282789bac19fb7af77e2b0641e8d80cf52fcf8f8318190f15c647bcdf76ac98b3576c537a65a5250ec7784
@@ -281,13 +281,13 @@ module ScopedSearch
281
281
  # In case Time responds to #zone, we know this is Rails environment and we can use Time.zone.parse. The benefit is that the
282
282
  # current timezone is respected and does not have to be specified explicitly. That way even relative dates work as expected.
283
283
  def parse_temporal(value)
284
- return Date.current if value =~ /\btoday\b/i
285
- return 1.day.ago.to_date if value =~ /\byesterday\b/i
286
- return 1.day.from_now.to_date if value =~ /\btomorrow\b/i
284
+ return date_with_timezone(Date.current) if value =~ /\btoday\b/i
285
+ return date_with_timezone(1.day.ago) if value =~ /\byesterday\b/i
286
+ return date_with_timezone(1.day.from_now) if value =~ /\btomorrow\b/i
287
287
  return (eval($1.strip.gsub(/\s+/,'.').downcase)).to_datetime if value =~ /\A\s*(\d+\s+\b(?:hours?|minutes?)\b\s+\bago)\b\s*\z/i
288
- return (eval($1.strip.gsub(/\s+/,'.').downcase)).to_date if value =~ /\A\s*(\d+\s+\b(?:days?|weeks?|months?|years?)\b\s+\bago)\b\s*\z/i
288
+ return date_with_timezone(eval($1.strip.gsub(/\s+/,'.').downcase)) if value =~ /\A\s*(\d+\s+\b(?:days?|weeks?|months?|years?)\b\s+\bago)\b\s*\z/i
289
289
  return (eval($1.strip.gsub(/from\s+now/i,'from_now').gsub(/\s+/,'.').downcase)).to_datetime if value =~ /\A\s*(\d+\s+\b(?:hours?|minutes?)\b\s+\bfrom\s+now)\b\s*\z/i
290
- return (eval($1.strip.gsub(/from\s+now/i,'from_now').gsub(/\s+/,'.').downcase)).to_date if value =~ /\A\s*(\d+\s+\b(?:days?|weeks?|months?|years?)\b\s+\bfrom\s+now)\b\s*\z/i
290
+ return date_with_timezone(eval($1.strip.gsub(/from\s+now/i,'from_now').gsub(/\s+/,'.').downcase)) if value =~ /\A\s*(\d+\s+\b(?:days?|weeks?|months?|years?)\b\s+\bfrom\s+now)\b\s*\z/i
291
291
  if Time.respond_to?(:zone) && !Time.zone.nil?
292
292
  parsed = Time.zone.parse(value) rescue nil
293
293
  parsed && parsed.to_datetime
@@ -334,6 +334,14 @@ module ScopedSearch
334
334
 
335
335
  search_scope
336
336
  end
337
+
338
+ # This should get called only when the relative specifiers (1 day ago and
339
+ # so on), this code path is only viable when Rails are available
340
+ def date_with_timezone(datetime)
341
+ date = datetime.to_date
342
+ return datetime unless date.respond_to?(:in_time_zone)
343
+ date.in_time_zone.to_datetime
344
+ end
337
345
  end
338
346
 
339
347
  # Registers the complete_for method within the class that is used for searching.
@@ -450,7 +450,9 @@ module ScopedSearch
450
450
  field = definition.field_by_name(value)
451
451
  if field && field.set? && field.complete_value.values.include?(true)
452
452
  key = field.complete_value.map{|k,v| k if v == true}.compact.first
453
- return builder.set_test(field, :eq, key, &block)
453
+ sql, *params = builder.set_test(field, :eq, key, &block)
454
+ params.each { |p| yield(:parameter, p) }
455
+ return sql
454
456
  end
455
457
  # Search keywords found without context, just search on all the default fields
456
458
  fragments = definition.default_fields_for(value).map do |field|
@@ -507,7 +509,7 @@ module ScopedSearch
507
509
  raise ScopedSearch::QueryNotSupported, "Field '#{lhs.value}' not recognized for searching!" unless field
508
510
 
509
511
  # see if the value passes user defined validation
510
- if operator == :in
512
+ if [:in, :notin].include?(operator)
511
513
  rhs.value.split(',').each { |v| validate_value(field, v) }
512
514
  else
513
515
  validate_value(field, rhs.value)
@@ -1,3 +1,3 @@
1
1
  module ScopedSearch
2
- VERSION = "4.1.10"
2
+ VERSION = "4.1.12"
3
3
  end
@@ -210,6 +210,12 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
210
210
  @class.search_for('timestamp > "3 hours ago"').length.should == 4
211
211
  end
212
212
 
213
+ it "should take timezone into consideration" do
214
+ now = Time.now
215
+ expected = DateTime.new(now.year, now.month, now.day, 0, 0, 0, now.zone).utc.strftime('%Y-%m-%d %H:%M:%S')
216
+ @class.search_for('timestamp > yesterday').to_sql.should include(expected)
217
+ end
218
+
213
219
  it "should accept 1 week from now as date format" do
214
220
  @class.search_for('date < "1 week from now"').length.should == 6
215
221
  end
@@ -9,54 +9,54 @@ describe ScopedSearch::RailsHelper do
9
9
  let(:params) { HashWithIndifferentAccess.new(:controller => "resources", :action => "search") }
10
10
 
11
11
  it "should generate a link with the order param set" do
12
- should_receive(:url_for).with(
12
+ should_receive(:url_for).with({
13
13
  "controller" => "resources",
14
14
  "action" => "search",
15
15
  "order" => "field ASC"
16
- ).and_return("/example")
16
+ }).and_return("/example")
17
17
 
18
18
  sort("field")
19
19
  end
20
20
 
21
21
  it "should generate a link with order param set to alternative default sorting order" do
22
- should_receive(:url_for).with(
22
+ should_receive(:url_for).with({
23
23
  "controller" => "resources",
24
24
  "action" => "search",
25
25
  "order" => "field DESC"
26
- ).and_return("/example")
26
+ }).and_return("/example")
27
27
 
28
28
  sort("field", :default => "DESC")
29
29
  end
30
30
 
31
31
  it "should generate a link with the order param inverted" do
32
- should_receive(:url_for).with(
32
+ should_receive(:url_for).with({
33
33
  "controller" => "resources",
34
34
  "action" => "search",
35
35
  "order" => "field DESC"
36
- ).and_return("/example")
36
+ }).and_return("/example")
37
37
 
38
38
  params[:order] = "field ASC"
39
39
  sort("field")
40
40
  end
41
41
 
42
42
  it "should generate a link with other parameters retained" do
43
- should_receive(:url_for).with(
43
+ should_receive(:url_for).with({
44
44
  "controller" => "resources",
45
45
  "action" => "search",
46
46
  "walrus" => "unicorns",
47
47
  "order" => "field ASC"
48
- ).and_return("/example")
48
+ }).and_return("/example")
49
49
 
50
50
  params[:walrus] = "unicorns"
51
51
  sort("field")
52
52
  end
53
53
 
54
54
  it "should replace the current sorting order" do
55
- should_receive(:url_for).with(
55
+ should_receive(:url_for).with({
56
56
  "controller" => "resources",
57
57
  "action" => "search",
58
58
  "order" => "other ASC"
59
- ).and_return("/example")
59
+ }).and_return("/example")
60
60
 
61
61
  params[:order] = "field ASC"
62
62
  sort("other")
@@ -83,12 +83,12 @@ describe ScopedSearch::RailsHelper do
83
83
  let(:ac_params) { double('ActionController::Parameters') }
84
84
 
85
85
  it "should call to_h on passed params object" do
86
- should_receive(:url_for).with(
86
+ should_receive(:url_for).with({
87
87
  "controller" => "resources",
88
88
  "action" => "search",
89
89
  "walrus" => "unicorns",
90
90
  "order" => "field ASC"
91
- ).and_return("/example")
91
+ }).and_return("/example")
92
92
 
93
93
  params[:walrus] = "unicorns"
94
94
 
@@ -12,6 +12,7 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
12
12
  @class = ScopedSearch::RSpec::Database.create_model(:bool => :boolean, :status => :integer) do |klass|
13
13
  klass.scoped_search :on => :bool, :complete_value => {:yes => true, :no => false}
14
14
  klass.scoped_search :on => :status, :complete_value => {:unknown => 0, :up => 1, :down => 2}
15
+ klass.scoped_search :on => :bool, :rename => :bool2, :complete_value => {:true => true, :false => false}
15
16
  end
16
17
  end
17
18
 
@@ -71,6 +72,11 @@ ScopedSearch::RSpec::Database.test_databases.each do |db|
71
72
  it "should find two record with bool = false" do
72
73
  @class.search_for('bool != yes').length.should == 2
73
74
  end
75
+
76
+ it "should be able to search without value" do
77
+ @class.search_for('bool').length.should == 1
78
+ @class.search_for('bool2').length.should == 1
79
+ end
74
80
  end
75
81
  end
76
82
  end
@@ -68,6 +68,8 @@ describe ScopedSearch::QueryBuilder do
68
68
 
69
69
  lambda { ScopedSearch::QueryBuilder.build_query(@definition, 'test_field ^ (1,2)') }.should_not raise_error
70
70
  lambda { ScopedSearch::QueryBuilder.build_query(@definition, 'test_field ^ (1,a)') }.should raise_error(ScopedSearch::QueryNotSupported)
71
+ lambda { ScopedSearch::QueryBuilder.build_query(@definition, 'test_field !^ (1,2)') }.should_not raise_error
72
+ lambda { ScopedSearch::QueryBuilder.build_query(@definition, 'test_field !^ (1,a)') }.should raise_error(ScopedSearch::QueryNotSupported)
71
73
  end
72
74
 
73
75
  it "should display custom error from validator" do
metadata CHANGED
@@ -1,16 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: scoped_search
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.10
4
+ version: 4.1.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Amos Benari
8
8
  - Willem van Bergen
9
9
  - Wes Hays
10
- autorequire:
10
+ autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2021-11-29 00:00:00.000000000 Z
13
+ date: 2023-10-26 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activerecord
@@ -144,7 +144,7 @@ homepage: https://github.com/wvanbergen/scoped_search/wiki
144
144
  licenses:
145
145
  - MIT
146
146
  metadata: {}
147
- post_install_message:
147
+ post_install_message:
148
148
  rdoc_options:
149
149
  - "--title"
150
150
  - scoped_search
@@ -165,8 +165,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
165
165
  - !ruby/object:Gem::Version
166
166
  version: '0'
167
167
  requirements: []
168
- rubygems_version: 3.1.2
169
- signing_key:
168
+ rubygems_version: 3.1.6
169
+ signing_key:
170
170
  specification_version: 4
171
171
  summary: Easily search you ActiveRecord models with a simple query language using
172
172
  a named scope