itrigga-param_fu 0.4.0 → 0.4.1
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.
- data/VERSION +1 -1
- data/lib/trigga/param_fu/param_fu.rb +5 -3
- data/spec/param_fu_spec.rb +73 -55
- metadata +4 -4
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.1
|
@@ -106,9 +106,11 @@ module Trigga
|
|
106
106
|
|
107
107
|
def parse_field_definition( opts, model_class, param_name, definition )
|
108
108
|
c = {:where=>[], :values=>[]}
|
109
|
-
if opts[param_name]
|
110
|
-
|
111
|
-
|
109
|
+
if opts[param_name]
|
110
|
+
unless opts[param_name].to_s.empty?
|
111
|
+
c[:where] << "( #{model_class.table_name}.#{definition[:field_name] || param_name.to_s} #{definition[:operator] || '='} (?) )"
|
112
|
+
c[:values] << ( definition[:operator] =~ /\blike\b/i ? opts[param_name].to_active_record_condition : opts[param_name] )
|
113
|
+
end
|
112
114
|
end
|
113
115
|
c
|
114
116
|
end
|
data/spec/param_fu_spec.rb
CHANGED
@@ -172,86 +172,104 @@ describe ::Trigga::ParamFu do
|
|
172
172
|
@param_name = :param_1
|
173
173
|
end
|
174
174
|
|
175
|
-
|
176
|
-
Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where].size.should == 1
|
177
|
-
end
|
175
|
+
context "and the param value is not empty" do
|
178
176
|
|
179
|
-
|
180
|
-
|
181
|
-
Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where][0].should =~ /^\s*\(.*\)\s*$/
|
177
|
+
it "should add an element to the :where key" do
|
178
|
+
Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where].size.should == 1
|
182
179
|
end
|
183
|
-
|
184
|
-
describe "
|
185
|
-
|
186
|
-
|
187
|
-
end
|
188
|
-
|
189
|
-
it "should start with the model_class's table name as a scope" do
|
190
|
-
@inside_the_brackets.should =~ /\s*model_class_table_name\..*/
|
180
|
+
|
181
|
+
describe "where clause" do
|
182
|
+
it "should be wrapped in brackets" do
|
183
|
+
Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where][0].should =~ /^\s*\(.*\)\s*$/
|
191
184
|
end
|
192
|
-
|
193
|
-
|
185
|
+
|
186
|
+
describe "inside the brackets" do
|
194
187
|
before(:each) do
|
195
|
-
@definition[:field_name] = 'field_name'
|
196
188
|
@inside_the_brackets = Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where][0].gsub(/^\s*\((.*)\)\s*$/, '\1')
|
197
189
|
end
|
198
|
-
|
199
|
-
it "should use the :fieldname" do
|
200
|
-
@inside_the_brackets.should =~ /\s*[^\.]+\.field_name.*/
|
201
|
-
end
|
202
|
-
end
|
203
190
|
|
204
|
-
|
205
|
-
|
206
|
-
@definition[:field_name] = nil
|
207
|
-
@inside_the_brackets = Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where][0].gsub(/^\s*\((.*)\)\s*$/, '\1')
|
191
|
+
it "should start with the model_class's table name as a scope" do
|
192
|
+
@inside_the_brackets.should =~ /\s*model_class_table_name\..*/
|
208
193
|
end
|
194
|
+
|
195
|
+
context "When the definition has a :fieldname" do
|
196
|
+
before(:each) do
|
197
|
+
@definition[:field_name] = 'field_name'
|
198
|
+
@inside_the_brackets = Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where][0].gsub(/^\s*\((.*)\)\s*$/, '\1')
|
199
|
+
end
|
209
200
|
|
210
|
-
|
211
|
-
|
201
|
+
it "should use the :fieldname" do
|
202
|
+
@inside_the_brackets.should =~ /\s*[^\.]+\.field_name.*/
|
203
|
+
end
|
212
204
|
end
|
213
|
-
end
|
214
205
|
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
|
206
|
+
context "When the definition has no :fieldname" do
|
207
|
+
before(:each) do
|
208
|
+
@definition[:field_name] = nil
|
209
|
+
@inside_the_brackets = Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where][0].gsub(/^\s*\((.*)\)\s*$/, '\1')
|
210
|
+
end
|
220
211
|
|
221
|
-
|
222
|
-
|
212
|
+
it "should use the given param_name converted to a string as the field name" do
|
213
|
+
@inside_the_brackets.should =~ /\s*[^\.]+\.param_1.*/
|
214
|
+
end
|
223
215
|
end
|
224
|
-
end
|
225
216
|
|
226
|
-
|
227
|
-
|
228
|
-
|
229
|
-
|
217
|
+
context "When the definition has a :operator" do
|
218
|
+
before(:each) do
|
219
|
+
@definition[:operator] = 'my_operator'
|
220
|
+
@inside_the_brackets = Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where][0].gsub(/^\s*\((.*)\)\s*$/, '\1')
|
221
|
+
end
|
222
|
+
|
223
|
+
it "should use the :operator" do
|
224
|
+
@inside_the_brackets.should =~ /.*\bmy_operator\b.*/
|
225
|
+
end
|
230
226
|
end
|
227
|
+
|
228
|
+
context "When the definition has no :operator" do
|
229
|
+
before(:each) do
|
230
|
+
@definition[:operator] = nil
|
231
|
+
@inside_the_brackets = Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where][0].gsub(/^\s*\((.*)\)\s*$/, '\1')
|
232
|
+
end
|
231
233
|
|
232
|
-
|
233
|
-
|
234
|
+
it "should use = as the :operator" do
|
235
|
+
@inside_the_brackets.should =~ /[^=]+\s*=\s*[^=]+/
|
236
|
+
end
|
234
237
|
end
|
235
|
-
end
|
236
238
|
|
237
|
-
|
238
|
-
|
239
|
+
it "should end with (?)" do
|
240
|
+
@inside_the_brackets.should =~ /\(\?\)\s*$/
|
241
|
+
end
|
239
242
|
end
|
240
243
|
end
|
241
|
-
end
|
242
244
|
|
243
|
-
|
244
|
-
|
245
|
-
|
246
|
-
|
245
|
+
context "when the given :operator matches 'like'" do
|
246
|
+
before(:each) do
|
247
|
+
@definition[:operator] = " LIKE "
|
248
|
+
end
|
247
249
|
|
248
|
-
|
249
|
-
|
250
|
+
it "should add the corresponding value, converted to a like condition, to the :values key" do
|
251
|
+
Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:values].should == ['%param_1%value%']
|
252
|
+
end
|
253
|
+
end
|
254
|
+
|
255
|
+
it "should add the corresponding value to the :values key" do
|
256
|
+
Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:values].should == ['param_1 value']
|
250
257
|
end
|
251
258
|
end
|
252
259
|
|
253
|
-
|
254
|
-
|
260
|
+
context "when the given value is empty" do
|
261
|
+
before(:each) do
|
262
|
+
@params = {:param_1=>'', :param_2=>'dave'}
|
263
|
+
@param_name = :param_1
|
264
|
+
end
|
265
|
+
|
266
|
+
it "should not add the key to the where clause" do
|
267
|
+
Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:where].should be_empty
|
268
|
+
end
|
269
|
+
|
270
|
+
it "should not add the value to the :values key" do
|
271
|
+
Tester.parse_field_definition( @params, @model_class, @param_name, @definition )[:values].should_not include('')
|
272
|
+
end
|
255
273
|
end
|
256
274
|
end
|
257
275
|
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: itrigga-param_fu
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 4
|
9
|
-
-
|
10
|
-
version: 0.4.
|
9
|
+
- 1
|
10
|
+
version: 0.4.1
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Al Davidson
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2011-11-
|
19
|
+
date: 2011-11-18 00:00:00 +00:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|