mongo_mapper-unstable 2009.12.29 → 2009.12.30
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/mongo_mapper/finder_options.rb +33 -13
- data/lib/mongo_mapper/support.rb +3 -0
- data/lib/mongo_mapper/validations.rb +1 -3
- data/test/functional/associations/test_many_polymorphic_proxy.rb +2 -2
- data/test/support/timing.rb +1 -1
- data/test/unit/test_finder_options.rb +16 -0
- metadata +2 -2
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
2009.12.
|
1
|
+
2009.12.30
|
@@ -8,10 +8,18 @@ module MongoMapper
|
|
8
8
|
# useful for understanding how MongoMapper handles the parsing of finder
|
9
9
|
# conditions and options.
|
10
10
|
#
|
11
|
-
# @private
|
12
11
|
class FinderOptions
|
13
12
|
OptionKeys = [:fields, :select, :skip, :offset, :limit, :sort, :order]
|
14
|
-
|
13
|
+
|
14
|
+
def self.normalized_field(field)
|
15
|
+
field.to_s == 'id' ? :_id : field
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.normalized_order_direction(direction)
|
19
|
+
direction ||= 'ASC'
|
20
|
+
direction.upcase == 'ASC' ? 1 : -1
|
21
|
+
end
|
22
|
+
|
15
23
|
def initialize(model, options)
|
16
24
|
raise ArgumentError, "Options must be a hash" unless options.is_a?(Hash)
|
17
25
|
options = options.symbolize_keys
|
@@ -58,14 +66,14 @@ module MongoMapper
|
|
58
66
|
criteria = {}
|
59
67
|
|
60
68
|
conditions.each_pair do |field, value|
|
61
|
-
field = normalized_field(field)
|
69
|
+
field = self.class.normalized_field(field)
|
62
70
|
|
63
71
|
if @model.object_id_key?(field) && value.is_a?(String)
|
64
72
|
value = Mongo::ObjectID.from_string(value)
|
65
73
|
end
|
66
74
|
|
67
75
|
if field.is_a?(FinderOperator)
|
68
|
-
criteria.
|
76
|
+
criteria.update(field.to_criteria(value))
|
69
77
|
next
|
70
78
|
end
|
71
79
|
|
@@ -86,10 +94,6 @@ module MongoMapper
|
|
86
94
|
field.to_s =~ /^\$/
|
87
95
|
end
|
88
96
|
|
89
|
-
def normalized_field(field)
|
90
|
-
field.to_s == 'id' ? :_id : field
|
91
|
-
end
|
92
|
-
|
93
97
|
# adds _type single collection inheritance scope for models that need it
|
94
98
|
def add_sci_scope
|
95
99
|
if @model.single_collection_inherited?
|
@@ -109,14 +113,20 @@ module MongoMapper
|
|
109
113
|
|
110
114
|
def convert_order_to_sort(sort)
|
111
115
|
return if sort.blank?
|
112
|
-
|
113
|
-
|
116
|
+
|
117
|
+
if sort.respond_to?(:all?) && sort.all? { |s| s.respond_to?(:to_order) }
|
118
|
+
sort.map { |s| s.to_order }
|
119
|
+
elsif sort.respond_to?(:to_order)
|
120
|
+
[sort.to_order]
|
121
|
+
else
|
122
|
+
pieces = sort.split(',')
|
123
|
+
pieces.map { |s| to_mongo_sort_piece(s) }
|
124
|
+
end
|
114
125
|
end
|
115
126
|
|
116
127
|
def to_mongo_sort_piece(str)
|
117
128
|
field, direction = str.strip.split(' ')
|
118
|
-
direction
|
119
|
-
direction = direction.upcase == 'ASC' ? 1 : -1
|
129
|
+
direction = FinderOptions.normalized_order_direction(direction)
|
120
130
|
[field, direction]
|
121
131
|
end
|
122
132
|
end
|
@@ -127,7 +137,17 @@ module MongoMapper
|
|
127
137
|
end
|
128
138
|
|
129
139
|
def to_criteria(value)
|
130
|
-
{@field => {@operator => value}}
|
140
|
+
{FinderOptions.normalized_field(@field) => {@operator => value}}
|
141
|
+
end
|
142
|
+
end
|
143
|
+
|
144
|
+
class OrderOperator
|
145
|
+
def initialize(field, direction)
|
146
|
+
@field, @direction = field, direction
|
147
|
+
end
|
148
|
+
|
149
|
+
def to_order
|
150
|
+
[@field.to_s, FinderOptions.normalized_order_direction(@direction)]
|
131
151
|
end
|
132
152
|
end
|
133
153
|
end
|
data/lib/mongo_mapper/support.rb
CHANGED
@@ -31,9 +31,7 @@ module MongoMapper
|
|
31
31
|
|
32
32
|
def where_conditions(instance)
|
33
33
|
conditions = {}
|
34
|
-
unless case_sensitive
|
35
|
-
conditions.merge!({'$where' => "this.#{attribute}.toLowerCase() == '#{instance[attribute].to_s.downcase}'"})
|
36
|
-
end
|
34
|
+
conditions[attribute] = /#{instance[attribute].to_s}/i unless case_sensitive
|
37
35
|
conditions
|
38
36
|
end
|
39
37
|
end
|
@@ -134,8 +134,8 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
134
134
|
|
135
135
|
@hall = Room.create(:name => 'Hall')
|
136
136
|
@hm1 = Message.create(:body => 'Do not fall in the hall', :position => 1)
|
137
|
-
@hm2 = Message.create(:body => 'Hall the king!', :position => 2)
|
138
137
|
@hm3 = Message.create(:body => 'Loungin!', :position => 3)
|
138
|
+
@hm2 = Message.create(:body => 'Hall the king!', :position => 2)
|
139
139
|
@hall.messages = [@hm1, @hm2, @hm3]
|
140
140
|
@hall.save
|
141
141
|
end
|
@@ -270,7 +270,7 @@ class ManyPolymorphicProxyTest < Test::Unit::TestCase
|
|
270
270
|
end
|
271
271
|
|
272
272
|
should "allow overriding the order provided to the association" do
|
273
|
-
@lounge.messages.all(:order => 'position
|
273
|
+
@lounge.messages.all(:order => 'position').should == [@lm1, @lm2]
|
274
274
|
end
|
275
275
|
|
276
276
|
should "allow using conditions on association" do
|
data/test/support/timing.rb
CHANGED
@@ -61,6 +61,13 @@ class FinderOptionsTest < Test::Unit::TestCase
|
|
61
61
|
FinderOptions.new(Room, :id => id).criteria.should == {:_id => id}
|
62
62
|
end
|
63
63
|
|
64
|
+
should "convert id with symbol operator to _id with modifier" do
|
65
|
+
id = Mongo::ObjectID.new
|
66
|
+
FinderOptions.new(Room, :id.ne => id).criteria.should == {
|
67
|
+
:_id => {'$ne' => id}
|
68
|
+
}
|
69
|
+
end
|
70
|
+
|
64
71
|
should "make sure that _id's are object ids" do
|
65
72
|
id = Mongo::ObjectID.new
|
66
73
|
FinderOptions.new(Room, :_id => id.to_s).criteria.should == {:_id => id}
|
@@ -121,6 +128,15 @@ class FinderOptionsTest < Test::Unit::TestCase
|
|
121
128
|
FinderOptions.new(Room, :order => 'foo DESC').options[:sort].should == sort
|
122
129
|
end
|
123
130
|
|
131
|
+
should "convert order operators to mongo sort" do
|
132
|
+
FinderOptions.new(Room, :order => :foo.asc).options[:sort].should == [['foo', 1]]
|
133
|
+
FinderOptions.new(Room, :order => :foo.desc).options[:sort].should == [['foo', -1]]
|
134
|
+
end
|
135
|
+
|
136
|
+
should "convert array of order operators to mongo sort" do
|
137
|
+
FinderOptions.new(Room, :order => [:foo.asc, :bar.desc]).options[:sort].should == [['foo', 1], ['bar', -1]]
|
138
|
+
end
|
139
|
+
|
124
140
|
should "convert field without direction to ascending" do
|
125
141
|
sort = [['foo', 1]]
|
126
142
|
FinderOptions.new(Room, :order => 'foo').options[:sort].should == sort
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mongo_mapper-unstable
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 2009.12.
|
4
|
+
version: 2009.12.30
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- John Nunemaker
|
@@ -9,7 +9,7 @@ autorequire:
|
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
11
|
|
12
|
-
date: 2009-12-
|
12
|
+
date: 2009-12-30 00:00:00 -05:00
|
13
13
|
default_executable: mmconsole
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|