scoped_search 4.1.4 → 4.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.travis.yml +49 -1
- data/CHANGELOG.rdoc +21 -1
- data/Gemfile.activerecord52 +17 -0
- data/Gemfile.activerecord52_with_activesupport52 +18 -0
- data/Gemfile.activerecord60 +17 -0
- data/Gemfile.activerecord60_with_activesupport60 +18 -0
- data/lib/scoped_search/auto_complete_builder.rb +11 -4
- data/lib/scoped_search/definition.rb +45 -17
- data/lib/scoped_search/query_builder.rb +90 -78
- data/lib/scoped_search/version.rb +1 -1
- data/spec/integration/auto_complete_spec.rb +41 -4
- data/spec/integration/key_value_querying_spec.rb +15 -0
- data/spec/integration/nested_has_many_through_querying_spec.rb +100 -0
- data/spec/integration/relation_querying_spec.rb +63 -1
- data/spec/integration/sti_querying_spec.rb +12 -2
- data/spec/integration/uuid_query_spec.rb +57 -0
- data/spec/lib/database.rb +1 -1
- data/spec/spec_helper.rb +3 -0
- data/spec/unit/auto_complete_builder_spec.rb +32 -1
- data/spec/unit/query_builder_spec.rb +46 -2
- metadata +11 -4
data/spec/spec_helper.rb
CHANGED
@@ -10,6 +10,9 @@ require "#{File.dirname(__FILE__)}/lib/matchers"
|
|
10
10
|
require "#{File.dirname(__FILE__)}/lib/database"
|
11
11
|
require "#{File.dirname(__FILE__)}/lib/mocks"
|
12
12
|
|
13
|
+
def on_postgresql?
|
14
|
+
ActiveRecord::Base.connection.adapter_name == 'PostgreSQL'
|
15
|
+
end
|
13
16
|
|
14
17
|
RSpec.configure do |config|
|
15
18
|
config.include ScopedSearch::RSpec::Mocks
|
@@ -2,11 +2,15 @@ require "spec_helper"
|
|
2
2
|
|
3
3
|
describe ScopedSearch::AutoCompleteBuilder do
|
4
4
|
|
5
|
+
let(:klass) { Class.new(ActiveRecord::Base) }
|
6
|
+
|
5
7
|
before(:each) do
|
6
8
|
@definition = double('ScopedSearch::Definition')
|
7
|
-
@definition.stub(:klass).and_return(
|
9
|
+
@definition.stub(:klass).and_return(klass)
|
8
10
|
@definition.stub(:profile).and_return(:default)
|
9
11
|
@definition.stub(:profile=).and_return(true)
|
12
|
+
@definition.klass.stub(:connection).and_return(double())
|
13
|
+
@definition.stub(:default_order).and_return(nil)
|
10
14
|
end
|
11
15
|
|
12
16
|
it "should return empty suggestions if the search query is nil" do
|
@@ -17,4 +21,31 @@ describe ScopedSearch::AutoCompleteBuilder do
|
|
17
21
|
ScopedSearch::AutoCompleteBuilder.auto_complete(@definition, "").should == []
|
18
22
|
end
|
19
23
|
|
24
|
+
it 'should suggest special values' do
|
25
|
+
field = double('field')
|
26
|
+
[:temporal?, :set?, :key_field, :validator, :virtual?, :relation, :offset, :value_translation, :to_sql].each { |key| field.stub(key) }
|
27
|
+
field.stub(:special_values).and_return %w(foo bar baz)
|
28
|
+
field.stub(:complete_value).and_return(true)
|
29
|
+
@definition.stub(:default_fields_for).and_return([])
|
30
|
+
@definition.stub(:field_by_name).and_return(field)
|
31
|
+
@definition.stub(:fields).and_return [field]
|
32
|
+
ScopedSearch::AutoCompleteBuilder.any_instance.stub(:complete_value_from_db).and_return([])
|
33
|
+
ScopedSearch::AutoCompleteBuilder.auto_complete(@definition, "custom_field =").should eq(['custom_field = foo', 'custom_field = bar', 'custom_field = baz'])
|
34
|
+
ScopedSearch::AutoCompleteBuilder.auto_complete(@definition, "custom_field = f").should eq(['custom_field = foo'])
|
35
|
+
end
|
36
|
+
|
37
|
+
context "with ext_method" do
|
38
|
+
before do
|
39
|
+
@definition = ScopedSearch::Definition.new(klass)
|
40
|
+
@definition.define(:test_field, ext_method: :ext_test)
|
41
|
+
@definition.klass.stub(:connection).and_return(double())
|
42
|
+
@definition.klass.stub(:columns_hash).and_return({})
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should support operator auto-completion on a virtual field" do
|
46
|
+
klass.should_receive(:ext_test).with('', '=', 'test_field').and_return(conditions: '')
|
47
|
+
ScopedSearch::AutoCompleteBuilder.auto_complete(@definition, 'test_field ').should eq(["test_field = ", "test_field != ", "test_field > ", "test_field < ", "test_field <= ", "test_field >= ", "test_field ~ ", "test_field !~ ", "test_field ^ ", "test_field !^ "])
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
20
51
|
end
|
@@ -38,9 +38,11 @@ describe ScopedSearch::QueryBuilder do
|
|
38
38
|
|
39
39
|
it "should validate value if validator selected" do
|
40
40
|
field = double('field')
|
41
|
+
field.stub(:virtual?).and_return(false)
|
41
42
|
field.stub(:only_explicit).and_return(true)
|
42
43
|
field.stub(:field).and_return(:test_field)
|
43
44
|
field.stub(:validator).and_return(->(_value) { false })
|
45
|
+
field.stub(:special_values).and_return([])
|
44
46
|
|
45
47
|
@definition.stub(:field_by_name).and_return(field)
|
46
48
|
|
@@ -49,6 +51,9 @@ describe ScopedSearch::QueryBuilder do
|
|
49
51
|
|
50
52
|
it "should validate value if validator selected" do
|
51
53
|
field = double('field')
|
54
|
+
field.stub(:virtual?).and_return(false)
|
55
|
+
field.stub(:temporal?).and_return(false)
|
56
|
+
field.stub(:relation).and_return(nil)
|
52
57
|
field.stub(:only_explicit).and_return(true)
|
53
58
|
field.stub(:field).and_return(:test_field)
|
54
59
|
field.stub(:ext_method).and_return(nil)
|
@@ -56,6 +61,8 @@ describe ScopedSearch::QueryBuilder do
|
|
56
61
|
field.stub(:set?).and_return(false)
|
57
62
|
field.stub(:to_sql).and_return('')
|
58
63
|
field.stub(:validator).and_return(->(value) { value =~ /^\d+$/ })
|
64
|
+
field.stub(:value_translation).and_return(nil)
|
65
|
+
field.stub(:special_values).and_return([])
|
59
66
|
|
60
67
|
@definition.stub(:field_by_name).and_return(field)
|
61
68
|
|
@@ -65,15 +72,52 @@ describe ScopedSearch::QueryBuilder do
|
|
65
72
|
|
66
73
|
it "should display custom error from validator" do
|
67
74
|
field = double('field')
|
75
|
+
field.stub(:virtual?).and_return(false)
|
68
76
|
field.stub(:only_explicit).and_return(true)
|
69
77
|
field.stub(:field).and_return(:test_field)
|
70
78
|
field.stub(:validator).and_return(->(_value) { raise ScopedSearch::QueryNotSupported, 'my custom message' })
|
79
|
+
field.stub(:special_values).and_return([])
|
71
80
|
|
72
81
|
@definition.stub(:field_by_name).and_return(field)
|
73
82
|
|
74
83
|
lambda { ScopedSearch::QueryBuilder.build_query(@definition, 'test_field = test_val') }.should raise_error('my custom message')
|
75
84
|
end
|
76
85
|
|
86
|
+
context 'with value_translation' do
|
87
|
+
let(:translator) do
|
88
|
+
->(value) do
|
89
|
+
if %w(a b c).include?(value)
|
90
|
+
'good'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
let(:special_values) { %w(a b c) }
|
95
|
+
before do
|
96
|
+
field = double('field')
|
97
|
+
field.stub(:field).and_return(:test_field)
|
98
|
+
field.stub(:key_field).and_return(nil)
|
99
|
+
field.stub(:to_sql).and_return('test_field')
|
100
|
+
[:virtual?, :set?, :temporal?, :relation, :offset].each { |key| field.stub(key).and_return(false) }
|
101
|
+
field.stub(:validator).and_return(->(value) { value == 'x' }) # Nothing except for special_values and x is valid
|
102
|
+
field.stub(:special_values).and_return(special_values)
|
103
|
+
field.stub(:value_translation).and_return(translator)
|
104
|
+
@definition.stub(:field_by_name).and_return(field)
|
105
|
+
end
|
106
|
+
|
107
|
+
it 'should translate the value' do
|
108
|
+
ScopedSearch::QueryBuilder.build_query(@definition, 'test_field = a').should eq(conditions: ['(test_field = ?)', 'good'])
|
109
|
+
ScopedSearch::QueryBuilder.build_query(@definition, 'test_field ^ (a, b, c)').should eq(conditions: ['(test_field IN (?,?,?))', 'good', 'good', 'good'])
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'should validate before translation' do
|
113
|
+
proc { ScopedSearch::QueryBuilder.build_query(@definition, 'test_field = d') }.should raise_error(ScopedSearch::QueryNotSupported, /Value 'd' is not valid for field/)
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'should raise an error if translated value is nil' do
|
117
|
+
proc { ScopedSearch::QueryBuilder.build_query(@definition, 'test_field = x') }.should raise_error(ScopedSearch::QueryNotSupported, /Translation from any value to nil is not allowed/)
|
118
|
+
end
|
119
|
+
end
|
120
|
+
|
77
121
|
context "with ext_method" do
|
78
122
|
before do
|
79
123
|
@definition = ScopedSearch::Definition.new(klass)
|
@@ -90,7 +134,7 @@ describe ScopedSearch::QueryBuilder do
|
|
90
134
|
ScopedSearch::QueryBuilder.build_query(@definition, 'test_field = test_val').should eq(include: ['test1'], joins: ['test2'])
|
91
135
|
end
|
92
136
|
|
93
|
-
it "should support LIKE query
|
137
|
+
it "should support LIKE query on a virtual field" do
|
94
138
|
klass.should_receive(:ext_test).with('test_field', 'LIKE', 'test_val').and_return(conditions: 'field LIKE ?', parameter: ['%test_val%'])
|
95
139
|
ScopedSearch::QueryBuilder.build_query(@definition, 'test_field ~ test_val').should eq(conditions: ['(field LIKE ?)', '%test_val%'])
|
96
140
|
end
|
@@ -100,7 +144,7 @@ describe ScopedSearch::QueryBuilder do
|
|
100
144
|
lambda { ScopedSearch::QueryBuilder.build_query(@definition, 'test_field = test_val') }.should raise_error(ScopedSearch::QueryNotSupported, /should return hash/)
|
101
145
|
end
|
102
146
|
|
103
|
-
it "should raise error when
|
147
|
+
it "should raise error when ext_method doesn't exist" do
|
104
148
|
lambda { ScopedSearch::QueryBuilder.build_query(@definition, 'test_field = test_val') }.should raise_error(ScopedSearch::QueryNotSupported, /doesn't respond to 'ext_test'/)
|
105
149
|
end
|
106
150
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: scoped_search
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 4.1.
|
4
|
+
version: 4.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Amos Benari
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2020-08-24 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: activerecord
|
@@ -85,6 +85,10 @@ files:
|
|
85
85
|
- Gemfile.activerecord42
|
86
86
|
- Gemfile.activerecord50
|
87
87
|
- Gemfile.activerecord51
|
88
|
+
- Gemfile.activerecord52
|
89
|
+
- Gemfile.activerecord52_with_activesupport52
|
90
|
+
- Gemfile.activerecord60
|
91
|
+
- Gemfile.activerecord60_with_activesupport60
|
88
92
|
- LICENSE
|
89
93
|
- README.rdoc
|
90
94
|
- Rakefile
|
@@ -112,6 +116,7 @@ files:
|
|
112
116
|
- spec/integration/auto_complete_spec.rb
|
113
117
|
- spec/integration/ext_method_spec.rb
|
114
118
|
- spec/integration/key_value_querying_spec.rb
|
119
|
+
- spec/integration/nested_has_many_through_querying_spec.rb
|
115
120
|
- spec/integration/ordinal_querying_spec.rb
|
116
121
|
- spec/integration/profile_querying_spec.rb
|
117
122
|
- spec/integration/rails_helper_spec.rb
|
@@ -120,6 +125,7 @@ files:
|
|
120
125
|
- spec/integration/set_query_spec.rb
|
121
126
|
- spec/integration/sti_querying_spec.rb
|
122
127
|
- spec/integration/string_querying_spec.rb
|
128
|
+
- spec/integration/uuid_query_spec.rb
|
123
129
|
- spec/lib/database.rb
|
124
130
|
- spec/lib/matchers.rb
|
125
131
|
- spec/lib/mocks.rb
|
@@ -156,8 +162,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
156
162
|
- !ruby/object:Gem::Version
|
157
163
|
version: '0'
|
158
164
|
requirements: []
|
159
|
-
|
160
|
-
rubygems_version: 2.6.8
|
165
|
+
rubygems_version: 3.0.3
|
161
166
|
signing_key:
|
162
167
|
specification_version: 4
|
163
168
|
summary: Easily search you ActiveRecord models with a simple query language using
|
@@ -169,6 +174,7 @@ test_files:
|
|
169
174
|
- spec/integration/auto_complete_spec.rb
|
170
175
|
- spec/integration/ext_method_spec.rb
|
171
176
|
- spec/integration/key_value_querying_spec.rb
|
177
|
+
- spec/integration/nested_has_many_through_querying_spec.rb
|
172
178
|
- spec/integration/ordinal_querying_spec.rb
|
173
179
|
- spec/integration/profile_querying_spec.rb
|
174
180
|
- spec/integration/rails_helper_spec.rb
|
@@ -177,6 +183,7 @@ test_files:
|
|
177
183
|
- spec/integration/set_query_spec.rb
|
178
184
|
- spec/integration/sti_querying_spec.rb
|
179
185
|
- spec/integration/string_querying_spec.rb
|
186
|
+
- spec/integration/uuid_query_spec.rb
|
180
187
|
- spec/lib/database.rb
|
181
188
|
- spec/lib/matchers.rb
|
182
189
|
- spec/lib/mocks.rb
|