ruby-ext-js 0.2.1 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
data/README.markdown CHANGED
@@ -65,6 +65,10 @@ Examples:
65
65
 
66
66
  Version number conventions: Patch-level bumps for bug fixes, minor-level bumps for changes that break backwards-compatibility, major-level bumps for major new features.
67
67
 
68
+ ## 0.3.0
69
+
70
+ * `ExtJs::Mongo` now supports 'date' filter params with "gt" and "lt" params.
71
+
68
72
  ## 0.2.1
69
73
 
70
74
  * `ExtJs::Mongo.options` fixes.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.1
1
+ 0.3.0
data/lib/ruby-ext-js.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "date"
2
+
1
3
  module ExtJs
2
4
  class Postgres
3
5
  def self.db_opts(params, opts = {}); raise NotImplementedError; end
@@ -127,14 +129,7 @@ module ExtJs
127
129
 
128
130
  next unless params["filter"][i]
129
131
 
130
- field = (params["filter"][i]["field"] || "").gsub(/[^\.\w\d_-]/, "").strip
131
- values = Array( params["filter"][i]["data"] ? params["filter"][i]["data"]["value"] : nil )
132
-
133
- if values.size == 1
134
- values = values[0]
135
- elsif values.size > 1
136
- values = { "$in" => values }
137
- end
132
+ field, values = self.filter_for( params["filter"][i] )
138
133
 
139
134
  unless field.empty? || !allowed_filters.include?( field ) || values.empty?
140
135
  conds.merge! field => values
@@ -144,5 +139,36 @@ module ExtJs
144
139
 
145
140
  conds
146
141
  end
142
+
143
+ def self.filter_for( hash )
144
+ hash["data"] ||= {}
145
+ hash["field"] ||= ""
146
+
147
+ field = hash["field"].gsub(/[^\.\w\d_-]/, "").strip
148
+ values = Array( hash["data"]["value"] )
149
+ comparison = self.comparison_for( hash["data"]["comparison"] )
150
+
151
+ case hash["data"]["type"]
152
+ when "date"
153
+ values.map!{ |date| Date.parse( date ) }
154
+ values = { comparison => values[0] }
155
+ else
156
+ if values.size == 1
157
+ values = values[0]
158
+ elsif values.size > 1
159
+ values = { comparison => values }
160
+ end
161
+ end
162
+
163
+ return field, values
164
+ end
165
+
166
+ def self.comparison_for( str )
167
+ case str
168
+ when "lt"; "$lt"
169
+ when "gt"; "$gt"
170
+ else; "$in"
171
+ end
172
+ end
147
173
  end
148
174
  end
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.2.1"
8
+ s.version = "0.3.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-01-21}
12
+ s.date = %q{2011-01-24}
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 = [
@@ -1,3 +1,4 @@
1
+ require "date"
1
2
  require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
2
3
 
3
4
  describe "ExtJs" do
@@ -20,7 +21,7 @@ describe "ExtJs" do
20
21
 
21
22
  class TestMongoWithFilters < ExtJs::Mongo
22
23
  def self.allowed_filters
23
- ["state", "score"]
24
+ ["state", "score", "inserted_at"]
24
25
  end
25
26
  end
26
27
 
@@ -85,6 +86,56 @@ describe "ExtJs" do
85
86
  mongo = TestMongoWithFilters.new( params )
86
87
  mongo.conditions.should == { "state" => "open", "score" => { "$in" => ["5", "4"] } }
87
88
  end
89
+
90
+ it "handles date range params" do
91
+ date = Date.parse "11/26/2010"
92
+
93
+ params = { "filter" => {
94
+ "0" => {
95
+ "data" => {
96
+ "type" => "date",
97
+ "value" => "11/26/2010",
98
+ "comparison" => "lt"
99
+ },
100
+ "field" => "inserted_at"
101
+ }
102
+ }}
103
+
104
+ mongo = TestMongoWithFilters.new( params )
105
+ mongo.conditions.should == { "inserted_at" => { "$lt" => date } }
106
+
107
+ params = { "filter" => {
108
+ "0" => {
109
+ "data" => {
110
+ "type" => "date",
111
+ "value" => "11/26/2010",
112
+ "comparison" => "gt"
113
+ },
114
+ "field" => "inserted_at"
115
+ }
116
+ }}
117
+
118
+ mongo = TestMongoWithFilters.new( params )
119
+ mongo.conditions.should == { "inserted_at" => { "$gt" => date } }
120
+ end
121
+
122
+ it "handles invalid comparison operators by falling back to $in" do
123
+ date = Date.parse "11/26/2010"
124
+
125
+ params = { "filter" => {
126
+ "0" => {
127
+ "data" => {
128
+ "type" => "date",
129
+ "value" => "11/26/2010",
130
+ "comparison" => "BOO"
131
+ },
132
+ "field" => "inserted_at"
133
+ }
134
+ }}
135
+
136
+ mongo = TestMongoWithFilters.new( params )
137
+ mongo.conditions.should == { "inserted_at" => { "$in" => date } }
138
+ end
88
139
  end
89
140
 
90
141
  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: 21
4
+ hash: 19
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 2
9
- - 1
10
- version: 0.2.1
8
+ - 3
9
+ - 0
10
+ version: 0.3.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-01-21 00:00:00 -08:00
18
+ date: 2011-01-24 00:00:00 -08:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency