bharat_filter_engine 0.1.2 → 0.2.0pre
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/Gemfile +10 -9
- data/README.md +1 -1
- data/Rakefile +5 -3
- data/lib/bharat_filter_engine/association_resolver.rb +4 -4
- data/lib/bharat_filter_engine/dynamic_filter_values.rb +28 -12
- data/lib/bharat_filter_engine/engine.rb +211 -85
- data/lib/bharat_filter_engine/errors.rb +1 -1
- data/lib/bharat_filter_engine/rule_normalizer.rb +88 -30
- data/lib/bharat_filter_engine/search_builder.rb +34 -16
- data/lib/bharat_filter_engine/version.rb +1 -1
- data/lib/bharat_filter_engine.rb +12 -12
- data/spec/association_resolver_spec.rb +60 -41
- data/spec/dynamic_filter_values_spec.rb +121 -29
- data/spec/engine_spec.rb +1074 -540
- data/spec/rule_normalizer_spec.rb +181 -32
- data/spec/search_builder_spec.rb +178 -43
- data/spec/spec_helper.rb +7 -12
- metadata +8 -5
|
@@ -1,51 +1,51 @@
|
|
|
1
|
-
|
|
1
|
+
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
require 'spec_helper'
|
|
4
4
|
|
|
5
|
+
RSpec.describe BharatFilterEngine::DynamicFilterValues do
|
|
5
6
|
let!(:client_one) do
|
|
6
7
|
Client.create!(
|
|
7
|
-
name:
|
|
8
|
+
name: 'John'
|
|
8
9
|
)
|
|
9
10
|
end
|
|
10
11
|
|
|
11
12
|
let!(:client_two) do
|
|
12
13
|
Client.create!(
|
|
13
|
-
name:
|
|
14
|
+
name: 'Jane'
|
|
14
15
|
)
|
|
15
16
|
end
|
|
16
17
|
|
|
17
18
|
let!(:lead_one) do
|
|
18
19
|
Lead.create!(
|
|
19
|
-
source:
|
|
20
|
+
source: 'google',
|
|
20
21
|
client: client_one
|
|
21
22
|
)
|
|
22
23
|
end
|
|
23
24
|
|
|
24
25
|
let!(:lead_two) do
|
|
25
26
|
Lead.create!(
|
|
26
|
-
source:
|
|
27
|
+
source: 'referral',
|
|
27
28
|
client: client_two
|
|
28
29
|
)
|
|
29
30
|
end
|
|
30
31
|
|
|
31
32
|
before do
|
|
32
33
|
Sale.create!(
|
|
33
|
-
stage:
|
|
34
|
+
stage: 'qualified',
|
|
34
35
|
lead: lead_one
|
|
35
36
|
)
|
|
36
37
|
|
|
37
38
|
Sale.create!(
|
|
38
|
-
stage:
|
|
39
|
+
stage: 'new',
|
|
39
40
|
lead: lead_two
|
|
40
41
|
)
|
|
41
42
|
end
|
|
42
43
|
|
|
43
|
-
it
|
|
44
|
-
|
|
44
|
+
it 'returns direct values' do
|
|
45
45
|
result =
|
|
46
46
|
described_class.new(
|
|
47
47
|
scope: Sale.all,
|
|
48
|
-
field:
|
|
48
|
+
field: 'stage'
|
|
49
49
|
).call
|
|
50
50
|
|
|
51
51
|
expect(result).to eq(
|
|
@@ -53,12 +53,11 @@ RSpec.describe BharatFilterEngine::DynamicFilterValues do
|
|
|
53
53
|
)
|
|
54
54
|
end
|
|
55
55
|
|
|
56
|
-
it
|
|
57
|
-
|
|
56
|
+
it 'returns nested values' do
|
|
58
57
|
result =
|
|
59
58
|
described_class.new(
|
|
60
59
|
scope: Sale.all,
|
|
61
|
-
field:
|
|
60
|
+
field: 'lead__source'
|
|
62
61
|
).call
|
|
63
62
|
|
|
64
63
|
expect(result).to eq(
|
|
@@ -66,12 +65,11 @@ RSpec.describe BharatFilterEngine::DynamicFilterValues do
|
|
|
66
65
|
)
|
|
67
66
|
end
|
|
68
67
|
|
|
69
|
-
it
|
|
70
|
-
|
|
68
|
+
it 'returns multi-level nested values' do
|
|
71
69
|
result =
|
|
72
70
|
described_class.new(
|
|
73
71
|
scope: Sale.all,
|
|
74
|
-
field:
|
|
72
|
+
field: 'lead__client__name'
|
|
75
73
|
).call
|
|
76
74
|
|
|
77
75
|
expect(result).to eq(
|
|
@@ -79,47 +77,54 @@ RSpec.describe BharatFilterEngine::DynamicFilterValues do
|
|
|
79
77
|
)
|
|
80
78
|
end
|
|
81
79
|
|
|
82
|
-
it
|
|
83
|
-
|
|
80
|
+
it 'returns nil for invalid field' do
|
|
84
81
|
result =
|
|
85
82
|
described_class.new(
|
|
86
83
|
scope: Sale.all,
|
|
87
|
-
field:
|
|
84
|
+
field: 'invalid_field'
|
|
88
85
|
).call
|
|
89
86
|
|
|
90
87
|
expect(result).to be_nil
|
|
91
88
|
end
|
|
92
89
|
|
|
93
|
-
it
|
|
94
|
-
|
|
90
|
+
it 'returns nil for a valid association with an invalid column' do
|
|
95
91
|
result =
|
|
96
92
|
described_class.new(
|
|
97
93
|
scope: Sale.all,
|
|
98
|
-
field:
|
|
94
|
+
field: 'lead__something_wrong'
|
|
99
95
|
).call
|
|
100
96
|
|
|
101
97
|
expect(result).to be_nil
|
|
102
98
|
end
|
|
103
99
|
|
|
104
|
-
it
|
|
100
|
+
it 'returns nil for an invalid association' do
|
|
101
|
+
result =
|
|
102
|
+
described_class.new(
|
|
103
|
+
scope: Sale.all,
|
|
104
|
+
field: 'something__name'
|
|
105
|
+
).call
|
|
106
|
+
|
|
107
|
+
expect(result).to be_nil
|
|
108
|
+
end
|
|
105
109
|
|
|
110
|
+
it 'returns nil for an invalid association at a deeper level' do
|
|
106
111
|
result =
|
|
107
112
|
described_class.new(
|
|
108
113
|
scope: Sale.all,
|
|
109
|
-
field:
|
|
114
|
+
field: 'lead__something__name'
|
|
110
115
|
).call
|
|
111
116
|
|
|
112
117
|
expect(result).to be_nil
|
|
113
118
|
end
|
|
114
119
|
|
|
115
|
-
it
|
|
116
|
-
Sale.create!(stage:
|
|
120
|
+
it 'does not return blank values' do
|
|
121
|
+
Sale.create!(stage: '')
|
|
117
122
|
Sale.create!(stage: nil)
|
|
118
123
|
|
|
119
124
|
result =
|
|
120
125
|
described_class.new(
|
|
121
126
|
scope: Sale.all,
|
|
122
|
-
field:
|
|
127
|
+
field: 'stage'
|
|
123
128
|
).call
|
|
124
129
|
|
|
125
130
|
expect(result).to eq(
|
|
@@ -127,4 +132,91 @@ RSpec.describe BharatFilterEngine::DynamicFilterValues do
|
|
|
127
132
|
)
|
|
128
133
|
end
|
|
129
134
|
|
|
130
|
-
|
|
135
|
+
it 'filters values by a partial term' do
|
|
136
|
+
Sale.create!(stage: 'closed')
|
|
137
|
+
|
|
138
|
+
result =
|
|
139
|
+
described_class.new(
|
|
140
|
+
scope: Sale.all,
|
|
141
|
+
field: 'stage',
|
|
142
|
+
term: 'qual'
|
|
143
|
+
).call
|
|
144
|
+
|
|
145
|
+
expect(result).to eq(['qualified'])
|
|
146
|
+
end
|
|
147
|
+
|
|
148
|
+
it 'is case-insensitive when filtering by term' do
|
|
149
|
+
result =
|
|
150
|
+
described_class.new(
|
|
151
|
+
scope: Sale.all,
|
|
152
|
+
field: 'stage',
|
|
153
|
+
term: 'QUAL'
|
|
154
|
+
).call
|
|
155
|
+
|
|
156
|
+
expect(result).to eq(['qualified'])
|
|
157
|
+
end
|
|
158
|
+
|
|
159
|
+
it 'escapes LIKE wildcards in the term' do
|
|
160
|
+
Sale.create!(stage: 'sale_2026')
|
|
161
|
+
Sale.create!(stage: 'saleX2026')
|
|
162
|
+
|
|
163
|
+
result =
|
|
164
|
+
described_class.new(
|
|
165
|
+
scope: Sale.all,
|
|
166
|
+
field: 'stage',
|
|
167
|
+
term: 'sale_2026'
|
|
168
|
+
).call
|
|
169
|
+
|
|
170
|
+
expect(result).to eq(['sale_2026'])
|
|
171
|
+
end
|
|
172
|
+
|
|
173
|
+
it 'limits the number of returned values' do
|
|
174
|
+
Sale.create!(stage: 'closed')
|
|
175
|
+
|
|
176
|
+
result =
|
|
177
|
+
described_class.new(
|
|
178
|
+
scope: Sale.all,
|
|
179
|
+
field: 'stage',
|
|
180
|
+
limit: 2
|
|
181
|
+
).call
|
|
182
|
+
|
|
183
|
+
expect(result).to eq(%w[closed new])
|
|
184
|
+
end
|
|
185
|
+
|
|
186
|
+
it 'ignores a limit of zero' do
|
|
187
|
+
result =
|
|
188
|
+
described_class.new(
|
|
189
|
+
scope: Sale.all,
|
|
190
|
+
field: 'stage',
|
|
191
|
+
limit: 0
|
|
192
|
+
).call
|
|
193
|
+
|
|
194
|
+
expect(result).to eq(%w[new qualified])
|
|
195
|
+
end
|
|
196
|
+
|
|
197
|
+
it 'combines term and limit together' do
|
|
198
|
+
Sale.create!(stage: 'quarantined')
|
|
199
|
+
Sale.create!(stage: 'qualified_lead')
|
|
200
|
+
|
|
201
|
+
result =
|
|
202
|
+
described_class.new(
|
|
203
|
+
scope: Sale.all,
|
|
204
|
+
field: 'stage',
|
|
205
|
+
term: 'qua',
|
|
206
|
+
limit: 1
|
|
207
|
+
).call
|
|
208
|
+
|
|
209
|
+
expect(result).to eq(['qualified'])
|
|
210
|
+
end
|
|
211
|
+
|
|
212
|
+
it 'works with a nested field and a term together' do
|
|
213
|
+
result =
|
|
214
|
+
described_class.new(
|
|
215
|
+
scope: Sale.all,
|
|
216
|
+
field: 'lead__client__name',
|
|
217
|
+
term: 'Jo'
|
|
218
|
+
).call
|
|
219
|
+
|
|
220
|
+
expect(result).to eq(['John'])
|
|
221
|
+
end
|
|
222
|
+
end
|