ruby-ext-js 0.3.3 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/ruby-ext-js.rb +57 -2
- data/ruby-ext-js.gemspec +3 -4
- data/spec/ruby-ext-js_spec.rb +46 -13
- metadata +7 -7
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.
|
1
|
+
0.4.0
|
data/lib/ruby-ext-js.rb
CHANGED
@@ -80,7 +80,7 @@ module ExtJs
|
|
80
80
|
|
81
81
|
# @return [Hash] `find()` conditions for `Mongo::Collection.find( conditions, opts )`
|
82
82
|
def conditions
|
83
|
-
self.class.
|
83
|
+
self.class.filter_param( @params )
|
84
84
|
end
|
85
85
|
|
86
86
|
# @return [Hash] `find()` options for `Mongo::Collection.find( conditions, opts )`
|
@@ -99,6 +99,16 @@ module ExtJs
|
|
99
99
|
[]
|
100
100
|
end
|
101
101
|
|
102
|
+
# Define options for specific filters. Eg:
|
103
|
+
# {
|
104
|
+
# "field_1" => { :all_caps => true },
|
105
|
+
# "field_2" => { :operator => "$all" },
|
106
|
+
# "field_3" => { :strip => "true" }
|
107
|
+
# }
|
108
|
+
def self.filter_opts
|
109
|
+
{}
|
110
|
+
end
|
111
|
+
|
102
112
|
protected
|
103
113
|
|
104
114
|
def self.skip_param( params )
|
@@ -120,7 +130,7 @@ module ExtJs
|
|
120
130
|
{ :sort => [sort, dir] }
|
121
131
|
end
|
122
132
|
|
123
|
-
def self.
|
133
|
+
def self.filter_param( params )
|
124
134
|
conds = {}
|
125
135
|
|
126
136
|
if params["filter"] && params["filter"].size > 0
|
@@ -167,9 +177,54 @@ module ExtJs
|
|
167
177
|
end
|
168
178
|
end
|
169
179
|
|
180
|
+
values = self.apply_filter_opts( field, values )
|
181
|
+
|
170
182
|
return field, values
|
171
183
|
end
|
172
184
|
|
185
|
+
def self.apply_filter_opts( field, values )
|
186
|
+
return values if values.size == 0
|
187
|
+
|
188
|
+
opts = {
|
189
|
+
:strip => true,
|
190
|
+
:all_caps => false,
|
191
|
+
:operator => nil
|
192
|
+
}.merge( filter_opts[field] || {} )
|
193
|
+
|
194
|
+
if values.is_a?( String ) || values.is_a?( Array )
|
195
|
+
opts[:operator] ||= "$in"
|
196
|
+
values = Array( values )
|
197
|
+
values.map!{ |value| value.strip } if opts[:strip]
|
198
|
+
values.map!{ |value| value.upcase } if opts[:all_caps]
|
199
|
+
values = values.size > 1 ? { opts[:operator] => values } : values.first
|
200
|
+
elsif values.is_a?( Hash )
|
201
|
+
# Values is already a { "$in" => [1, 2] } style hash
|
202
|
+
if opts[:operator]
|
203
|
+
values = { opts[:operator] => values.values.flatten.uniq }
|
204
|
+
end
|
205
|
+
if opts[:strip]
|
206
|
+
values.each do |k, v|
|
207
|
+
if v.is_a?( Array )
|
208
|
+
values[k] = v.map{ |value| value.strip }
|
209
|
+
elsif v.is_a?( String )
|
210
|
+
values[k] = v.strip
|
211
|
+
end
|
212
|
+
end
|
213
|
+
end
|
214
|
+
if opts[:all_caps]
|
215
|
+
values.each do |k, v|
|
216
|
+
if v.is_a?( Array )
|
217
|
+
values[k] = v.map{ |value| value.upcase }
|
218
|
+
elsif v.is_a?( String )
|
219
|
+
values[k] = v.upcase
|
220
|
+
end
|
221
|
+
end
|
222
|
+
end
|
223
|
+
end
|
224
|
+
|
225
|
+
values
|
226
|
+
end
|
227
|
+
|
173
228
|
def self.comparison_for( str )
|
174
229
|
case str
|
175
230
|
when "lt"; "$lte"
|
data/ruby-ext-js.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{ruby-ext-js}
|
8
|
-
s.version = "0.
|
8
|
+
s.version = "0.4.0"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Tyson Tate"]
|
12
|
-
s.date = %q{2011-
|
12
|
+
s.date = %q{2011-03-21}
|
13
13
|
s.description = %q{Ultra-basic classes for working with Ext.js requests and translating them to DataMapper / Mongood query opts.}
|
14
14
|
s.email = %q{tyson@doloreslabs.com}
|
15
15
|
s.extra_rdoc_files = [
|
@@ -31,7 +31,7 @@ Gem::Specification.new do |s|
|
|
31
31
|
s.homepage = %q{http://github.com/help/ruby-ext-js}
|
32
32
|
s.licenses = ["MIT"]
|
33
33
|
s.require_paths = ["lib"]
|
34
|
-
s.rubygems_version = %q{1.
|
34
|
+
s.rubygems_version = %q{1.4.2}
|
35
35
|
s.summary = %q{Ultra-basic classes for working with Ext.js requests and translating them to DataMapper / Mongood query opts.}
|
36
36
|
s.test_files = [
|
37
37
|
"spec/ruby-ext-js_spec.rb",
|
@@ -39,7 +39,6 @@ Gem::Specification.new do |s|
|
|
39
39
|
]
|
40
40
|
|
41
41
|
if s.respond_to? :specification_version then
|
42
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
43
42
|
s.specification_version = 3
|
44
43
|
|
45
44
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
data/spec/ruby-ext-js_spec.rb
CHANGED
@@ -2,19 +2,6 @@ require "date"
|
|
2
2
|
require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
|
3
3
|
|
4
4
|
describe "ExtJs" do
|
5
|
-
# describe "Postgres" do
|
6
|
-
# it "sorts on id when you ask it to sort on created_at" do
|
7
|
-
# ExtJs::Postgres.pagination_opts({
|
8
|
-
# :sort => "created_at",
|
9
|
-
# :order => "desc"
|
10
|
-
# })[:order].should == [:id.asc]
|
11
|
-
# end
|
12
|
-
#
|
13
|
-
# it "specs the rest of the class or it gets the hose" do
|
14
|
-
# pending "The current spec suite is currently rigidly tied to private models. Someday we'll write a generic spec suite here."
|
15
|
-
# end
|
16
|
-
# end
|
17
|
-
|
18
5
|
describe "Mongo" do
|
19
6
|
describe "conditions" do
|
20
7
|
class TestMongoNoFilters < ExtJs::Mongo; end
|
@@ -25,6 +12,20 @@ describe "ExtJs" do
|
|
25
12
|
end
|
26
13
|
end
|
27
14
|
|
15
|
+
class TestMongoWithFiltersAndFilterOpts < ExtJs::Mongo
|
16
|
+
def self.allowed_filters
|
17
|
+
["state", "score", "inserted_at"]
|
18
|
+
end
|
19
|
+
|
20
|
+
def self.filter_opts
|
21
|
+
{
|
22
|
+
"state" => { :all_caps => true },
|
23
|
+
"score" => { :operator => "$all" },
|
24
|
+
"inserted_at" => { :strip => true }
|
25
|
+
}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
28
29
|
it "handles empty params" do
|
29
30
|
mongo = TestMongoNoFilters.new( {} )
|
30
31
|
mongo.conditions.should == {}
|
@@ -152,6 +153,38 @@ describe "ExtJs" do
|
|
152
153
|
mongo = TestMongoWithFilters.new( params )
|
153
154
|
mongo.conditions.should == { "inserted_at" => { "$in" => start_time } }
|
154
155
|
end
|
156
|
+
|
157
|
+
it "allows specifying filter options per-field" do
|
158
|
+
params = {
|
159
|
+
"filter" => {
|
160
|
+
"0" => {
|
161
|
+
"field" => "state",
|
162
|
+
"data" => {
|
163
|
+
"value" => "open"
|
164
|
+
}
|
165
|
+
},
|
166
|
+
"1" => {
|
167
|
+
"field" => "score",
|
168
|
+
"data" => {
|
169
|
+
"value" => ["5", "4"]
|
170
|
+
}
|
171
|
+
},
|
172
|
+
"2" => {
|
173
|
+
"field" => "inserted_at",
|
174
|
+
"data" => {
|
175
|
+
"value" => [" Thursday "]
|
176
|
+
}
|
177
|
+
}
|
178
|
+
}
|
179
|
+
}
|
180
|
+
|
181
|
+
mongo = TestMongoWithFiltersAndFilterOpts.new( params )
|
182
|
+
mongo.conditions.should == {
|
183
|
+
"state" => "OPEN",
|
184
|
+
"score" => { "$all" => ["5", "4"] },
|
185
|
+
"inserted_at" => "Thursday"
|
186
|
+
}
|
187
|
+
end
|
155
188
|
end
|
156
189
|
|
157
190
|
describe "opts" do
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ruby-ext-js
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
5
|
-
prerelease:
|
4
|
+
hash: 15
|
5
|
+
prerelease:
|
6
6
|
segments:
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
-
|
10
|
-
version: 0.
|
8
|
+
- 4
|
9
|
+
- 0
|
10
|
+
version: 0.4.0
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Tyson Tate
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2011-
|
18
|
+
date: 2011-03-21 00:00:00 -07:00
|
19
19
|
default_executable:
|
20
20
|
dependencies:
|
21
21
|
- !ruby/object:Gem::Dependency
|
@@ -100,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
100
100
|
requirements: []
|
101
101
|
|
102
102
|
rubyforge_project:
|
103
|
-
rubygems_version: 1.
|
103
|
+
rubygems_version: 1.4.2
|
104
104
|
signing_key:
|
105
105
|
specification_version: 3
|
106
106
|
summary: Ultra-basic classes for working with Ext.js requests and translating them to DataMapper / Mongood query opts.
|