mongo_mapper_ign 0.7.4 → 0.7.6
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/mongo_mapper.rb +5 -8
- data/lib/mongo_mapper/document.rb +24 -53
- data/lib/mongo_mapper/embedded_document.rb +1 -0
- data/lib/mongo_mapper/plugins.rb +1 -0
- data/lib/mongo_mapper/plugins/associations.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/base.rb +5 -4
- data/lib/mongo_mapper/plugins/associations/belongs_to_polymorphic_proxy.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/belongs_to_proxy.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/embedded_collection.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/in_array_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_as_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_documents_proxy.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/many_embedded_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_embedded_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/many_polymorphic_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_embedded_proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/associations/one_proxy.rb +2 -1
- data/lib/mongo_mapper/plugins/associations/proxy.rb +1 -0
- data/lib/mongo_mapper/plugins/callbacks.rb +1 -0
- data/lib/mongo_mapper/plugins/clone.rb +1 -0
- data/lib/mongo_mapper/plugins/descendants.rb +1 -0
- data/lib/mongo_mapper/plugins/dirty.rb +1 -0
- data/lib/mongo_mapper/plugins/equality.rb +4 -3
- data/lib/mongo_mapper/plugins/identity_map.rb +9 -7
- data/lib/mongo_mapper/plugins/inspect.rb +1 -0
- data/lib/mongo_mapper/plugins/keys.rb +50 -73
- data/lib/mongo_mapper/plugins/keys/key.rb +54 -0
- data/lib/mongo_mapper/plugins/logger.rb +1 -0
- data/lib/mongo_mapper/plugins/modifiers.rb +10 -5
- data/lib/mongo_mapper/plugins/pagination.rb +1 -0
- data/lib/mongo_mapper/plugins/pagination/proxy.rb +4 -3
- data/lib/mongo_mapper/plugins/persistence.rb +1 -0
- data/lib/mongo_mapper/plugins/protected.rb +1 -0
- data/lib/mongo_mapper/plugins/rails.rb +1 -0
- data/lib/mongo_mapper/plugins/serialization.rb +4 -3
- data/lib/mongo_mapper/plugins/timestamps.rb +2 -1
- data/lib/mongo_mapper/plugins/userstamps.rb +1 -0
- data/lib/mongo_mapper/plugins/validations.rb +3 -2
- data/lib/mongo_mapper/query.rb +10 -129
- data/lib/mongo_mapper/support.rb +18 -39
- data/lib/mongo_mapper/support/descendant_appends.rb +1 -0
- data/lib/mongo_mapper/support/find.rb +1 -0
- data/lib/mongo_mapper/version.rb +2 -1
- metadata +32 -134
- data/.gitignore +0 -10
- data/Rakefile +0 -37
- data/mongo_mapper.gemspec +0 -214
- data/mongo_mapper_ign.gemspec +0 -217
- data/performance/read_write.rb +0 -52
- data/specs.watchr +0 -51
- data/test/NOTE_ON_TESTING +0 -1
- data/test/active_model_lint_test.rb +0 -13
- data/test/functional/associations/test_belongs_to_polymorphic_proxy.rb +0 -63
- data/test/functional/associations/test_belongs_to_proxy.rb +0 -101
- data/test/functional/associations/test_in_array_proxy.rb +0 -325
- data/test/functional/associations/test_many_documents_as_proxy.rb +0 -229
- data/test/functional/associations/test_many_documents_proxy.rb +0 -536
- data/test/functional/associations/test_many_embedded_polymorphic_proxy.rb +0 -176
- data/test/functional/associations/test_many_embedded_proxy.rb +0 -256
- data/test/functional/associations/test_many_polymorphic_proxy.rb +0 -302
- data/test/functional/associations/test_one_embedded_proxy.rb +0 -68
- data/test/functional/associations/test_one_proxy.rb +0 -196
- data/test/functional/test_associations.rb +0 -44
- data/test/functional/test_binary.rb +0 -27
- data/test/functional/test_callbacks.rb +0 -151
- data/test/functional/test_dirty.rb +0 -163
- data/test/functional/test_document.rb +0 -1219
- data/test/functional/test_embedded_document.rb +0 -210
- data/test/functional/test_identity_map.rb +0 -507
- data/test/functional/test_indexing.rb +0 -44
- data/test/functional/test_logger.rb +0 -20
- data/test/functional/test_modifiers.rb +0 -394
- data/test/functional/test_pagination.rb +0 -93
- data/test/functional/test_protected.rb +0 -163
- data/test/functional/test_string_id_compatibility.rb +0 -67
- data/test/functional/test_timestamps.rb +0 -64
- data/test/functional/test_userstamps.rb +0 -28
- data/test/functional/test_validations.rb +0 -342
- data/test/models.rb +0 -227
- data/test/support/custom_matchers.rb +0 -37
- data/test/support/timing.rb +0 -16
- data/test/test_helper.rb +0 -64
- data/test/unit/associations/test_base.rb +0 -212
- data/test/unit/associations/test_proxy.rb +0 -105
- data/test/unit/serializers/test_json_serializer.rb +0 -202
- data/test/unit/test_descendant_appends.rb +0 -71
- data/test/unit/test_document.rb +0 -225
- data/test/unit/test_dynamic_finder.rb +0 -123
- data/test/unit/test_embedded_document.rb +0 -657
- data/test/unit/test_keys.rb +0 -185
- data/test/unit/test_mongo_mapper.rb +0 -118
- data/test/unit/test_pagination.rb +0 -160
- data/test/unit/test_plugins.rb +0 -50
- data/test/unit/test_query.rb +0 -374
- data/test/unit/test_rails.rb +0 -181
- data/test/unit/test_rails_compatibility.rb +0 -52
- data/test/unit/test_serialization.rb +0 -51
- data/test/unit/test_support.rb +0 -382
- data/test/unit/test_time_zones.rb +0 -39
- data/test/unit/test_validations.rb +0 -544
@@ -1,6 +1,9 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
module MongoMapper
|
2
3
|
module Plugins
|
3
4
|
module Keys
|
5
|
+
autoload :Key, 'mongo_mapper/plugins/keys/key'
|
6
|
+
|
4
7
|
def self.configure(model)
|
5
8
|
model.key :_id, ObjectId
|
6
9
|
end
|
@@ -17,15 +20,13 @@ module MongoMapper
|
|
17
20
|
end
|
18
21
|
|
19
22
|
def key(*args)
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
key
|
23
|
+
Key.new(*args).tap do |key|
|
24
|
+
keys[key.name] = key
|
25
|
+
create_accessors_for(key)
|
26
|
+
create_key_in_descendants(*args)
|
27
|
+
create_indexes_for(key)
|
28
|
+
create_validations_for(key)
|
29
|
+
end
|
29
30
|
end
|
30
31
|
|
31
32
|
def key?(key)
|
@@ -36,9 +37,12 @@ module MongoMapper
|
|
36
37
|
object_id_key?(:_id)
|
37
38
|
end
|
38
39
|
|
40
|
+
def object_id_keys
|
41
|
+
keys.keys.select { |key| keys[key].type == ObjectId }.map(&:to_sym)
|
42
|
+
end
|
43
|
+
|
39
44
|
def object_id_key?(name)
|
40
|
-
|
41
|
-
key && key.type == ObjectId
|
45
|
+
object_id_keys.include?(name.to_sym)
|
42
46
|
end
|
43
47
|
|
44
48
|
def to_mongo(instance)
|
@@ -53,6 +57,7 @@ module MongoMapper
|
|
53
57
|
|
54
58
|
# load is overridden in identity map to ensure same objects are loaded
|
55
59
|
def load(attrs)
|
60
|
+
return nil if attrs.nil?
|
56
61
|
begin
|
57
62
|
klass = attrs['_type'].present? ? attrs['_type'].constantize : self
|
58
63
|
klass.new(attrs, true)
|
@@ -62,14 +67,16 @@ module MongoMapper
|
|
62
67
|
end
|
63
68
|
|
64
69
|
private
|
65
|
-
def
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
70
|
+
def key_accessors_module_defined?
|
71
|
+
if method(:const_defined?).arity == 1 # Ruby 1.9 compat check
|
72
|
+
const_defined?('MongoMapperKeys')
|
73
|
+
else
|
74
|
+
const_defined?('MongoMapperKeys', false)
|
75
|
+
end
|
76
|
+
end
|
71
77
|
|
72
|
-
|
78
|
+
def accessors_module
|
79
|
+
if key_accessors_module_defined?
|
73
80
|
const_get 'MongoMapperKeys'
|
74
81
|
else
|
75
82
|
const_set 'MongoMapperKeys', Module.new
|
@@ -154,7 +161,7 @@ module MongoMapper
|
|
154
161
|
|
155
162
|
if from_database
|
156
163
|
@new = false
|
157
|
-
|
164
|
+
load_from_database(attrs)
|
158
165
|
else
|
159
166
|
@new = true
|
160
167
|
assign(attrs)
|
@@ -170,13 +177,11 @@ module MongoMapper
|
|
170
177
|
def attributes=(attrs)
|
171
178
|
return if attrs.blank?
|
172
179
|
|
173
|
-
attrs.each_pair do |
|
174
|
-
|
175
|
-
|
176
|
-
if respond_to?(writer_method)
|
177
|
-
self.send(writer_method, value)
|
180
|
+
attrs.each_pair do |key, value|
|
181
|
+
if respond_to?(:"#{key}=")
|
182
|
+
self.send(:"#{key}=", value)
|
178
183
|
else
|
179
|
-
self[
|
184
|
+
self[key] = value
|
180
185
|
end
|
181
186
|
end
|
182
187
|
end
|
@@ -255,6 +260,17 @@ module MongoMapper
|
|
255
260
|
end
|
256
261
|
|
257
262
|
private
|
263
|
+
def load_from_database(attrs)
|
264
|
+
return if attrs.blank?
|
265
|
+
attrs.each do |key, value|
|
266
|
+
if respond_to?(:"#{key}=") && !self.class.key?(key)
|
267
|
+
self.send(:"#{key}=", value)
|
268
|
+
else
|
269
|
+
self[key] = value
|
270
|
+
end
|
271
|
+
end
|
272
|
+
end
|
273
|
+
|
258
274
|
def default_id_value(attrs)
|
259
275
|
unless attrs.nil?
|
260
276
|
provided_keys = attrs.keys.map { |k| k.to_s }
|
@@ -278,68 +294,29 @@ module MongoMapper
|
|
278
294
|
end
|
279
295
|
end
|
280
296
|
|
281
|
-
def read_key(
|
282
|
-
if key = keys[
|
283
|
-
|
284
|
-
value = key.get(instance_variable_get(var_name))
|
297
|
+
def read_key(key_name)
|
298
|
+
if key = keys[key_name]
|
299
|
+
value = key.get(instance_variable_get(:"@#{key_name}"))
|
285
300
|
set_parent_document(key, value)
|
286
|
-
instance_variable_set(
|
301
|
+
instance_variable_set(:"@#{key_name}", value)
|
287
302
|
else
|
288
|
-
raise KeyNotFound, "Could not find key: #{
|
303
|
+
raise KeyNotFound, "Could not find key: #{key_name.inspect}"
|
289
304
|
end
|
290
305
|
end
|
291
306
|
|
292
307
|
def read_key_before_typecast(name)
|
293
|
-
instance_variable_get("@#{name}_before_typecast")
|
308
|
+
instance_variable_get(:"@#{name}_before_typecast")
|
294
309
|
end
|
295
310
|
|
296
311
|
def write_key(name, value)
|
297
|
-
key = keys[name]
|
298
|
-
|
312
|
+
key = keys[name.to_s]
|
299
313
|
set_parent_document(key, value)
|
300
|
-
instance_variable_set "@#{name}_before_typecast", value
|
301
|
-
instance_variable_set "@#{name}", key.set(value)
|
314
|
+
instance_variable_set :"@#{name}_before_typecast", value
|
315
|
+
instance_variable_set :"@#{name}", key.set(value)
|
302
316
|
end
|
303
317
|
end
|
304
318
|
|
305
|
-
class Key
|
306
|
-
attr_accessor :name, :type, :options, :default_value
|
307
|
-
|
308
|
-
def initialize(*args)
|
309
|
-
options = args.extract_options!
|
310
|
-
@name, @type = args.shift.to_s, args.shift
|
311
|
-
self.options = (options || {}).symbolize_keys
|
312
|
-
self.default_value = self.options.delete(:default)
|
313
|
-
end
|
314
|
-
|
315
|
-
def ==(other)
|
316
|
-
@name == other.name && @type == other.type
|
317
|
-
end
|
318
|
-
|
319
|
-
def embeddable?
|
320
|
-
type.respond_to?(:embeddable?) && type.embeddable? ? true : false
|
321
|
-
end
|
322
|
-
|
323
|
-
def number?
|
324
|
-
[Integer, Float].include?(type)
|
325
|
-
end
|
326
319
|
|
327
|
-
def get(value)
|
328
|
-
if value.nil? && !default_value.nil?
|
329
|
-
if default_value.respond_to?(:call)
|
330
|
-
return default_value.call
|
331
|
-
else
|
332
|
-
return default_value
|
333
|
-
end
|
334
|
-
end
|
335
|
-
|
336
|
-
type.from_mongo(value)
|
337
|
-
end
|
338
|
-
|
339
|
-
def set(value)
|
340
|
-
type.to_mongo(value)
|
341
|
-
end
|
342
|
-
end
|
343
320
|
end
|
344
321
|
end
|
345
322
|
end
|
@@ -0,0 +1,54 @@
|
|
1
|
+
# encoding: UTF-8
|
2
|
+
module MongoMapper
|
3
|
+
module Plugins
|
4
|
+
module Keys
|
5
|
+
class Key
|
6
|
+
attr_accessor :name, :type, :options, :default_value
|
7
|
+
|
8
|
+
def initialize(*args)
|
9
|
+
options = args.extract_options!
|
10
|
+
@name, @type = args.shift.to_s, args.shift
|
11
|
+
self.options = (options || {}).symbolize_keys
|
12
|
+
self.default_value = self.options[:default]
|
13
|
+
end
|
14
|
+
|
15
|
+
def ==(other)
|
16
|
+
@name == other.name && @type == other.type
|
17
|
+
end
|
18
|
+
|
19
|
+
def embeddable?
|
20
|
+
type.respond_to?(:embeddable?) && type.embeddable? ? true : false
|
21
|
+
end
|
22
|
+
|
23
|
+
def number?
|
24
|
+
[Integer, Float].include?(type)
|
25
|
+
end
|
26
|
+
|
27
|
+
def get(value)
|
28
|
+
if value.nil? && !default_value.nil?
|
29
|
+
if default_value.respond_to?(:call)
|
30
|
+
return default_value.call
|
31
|
+
else
|
32
|
+
return default_value
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
type.from_mongo(value)
|
37
|
+
end
|
38
|
+
|
39
|
+
def set(value)
|
40
|
+
type.to_mongo(value).tap do |values|
|
41
|
+
if options[:typecast].present?
|
42
|
+
values.map! { |v| typecast_class.to_mongo(v) }
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
private
|
48
|
+
def typecast_class
|
49
|
+
options[:typecast].constantize
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
module MongoMapper
|
2
3
|
module Plugins
|
3
4
|
module Modifiers
|
@@ -14,7 +15,11 @@ module MongoMapper
|
|
14
15
|
end
|
15
16
|
|
16
17
|
def set(*args)
|
17
|
-
|
18
|
+
criteria, updates = criteria_and_keys_from_args(args)
|
19
|
+
updates.each do |key, value|
|
20
|
+
updates[key] = keys[key].set(value) if key?(key)
|
21
|
+
end
|
22
|
+
collection.update(criteria, {'$set' => updates}, :multi => true)
|
18
23
|
end
|
19
24
|
|
20
25
|
def unset(*args)
|
@@ -25,7 +30,7 @@ module MongoMapper
|
|
25
30
|
criteria = {:id => ids}
|
26
31
|
end
|
27
32
|
|
28
|
-
criteria =
|
33
|
+
criteria = query(criteria).criteria.to_hash
|
29
34
|
modifiers = keys.inject({}) { |hash, key| hash[key] = 1; hash }
|
30
35
|
collection.update(criteria, {'$unset' => modifiers}, :multi => true)
|
31
36
|
end
|
@@ -57,14 +62,14 @@ module MongoMapper
|
|
57
62
|
|
58
63
|
private
|
59
64
|
def modifier_update(modifier, args)
|
60
|
-
criteria,
|
61
|
-
collection.update(criteria, {modifier =>
|
65
|
+
criteria, updates = criteria_and_keys_from_args(args)
|
66
|
+
collection.update(criteria, {modifier => updates}, :multi => true)
|
62
67
|
end
|
63
68
|
|
64
69
|
def criteria_and_keys_from_args(args)
|
65
70
|
keys = args.pop
|
66
71
|
criteria = args[0].is_a?(Hash) ? args[0] : {:id => args}
|
67
|
-
[
|
72
|
+
[query(criteria).criteria.to_hash, keys]
|
68
73
|
end
|
69
74
|
end
|
70
75
|
|
@@ -1,9 +1,10 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
module MongoMapper
|
2
3
|
module Plugins
|
3
4
|
module Pagination
|
4
5
|
class Proxy
|
5
6
|
instance_methods.each { |m| undef_method m unless m =~ /(^__|^nil\?$|^send$|respond_to\?|proxy_|^object_id$)/ }
|
6
|
-
|
7
|
+
|
7
8
|
attr_accessor :subject
|
8
9
|
attr_reader :total_entries, :per_page, :current_page
|
9
10
|
alias limit per_page
|
@@ -50,11 +51,11 @@ module MongoMapper
|
|
50
51
|
def method_missing(name, *args, &block)
|
51
52
|
@subject.send(name, *args, &block)
|
52
53
|
end
|
53
|
-
|
54
|
+
|
54
55
|
def respond_to?(name, *args, &block)
|
55
56
|
super || @subject.respond_to?(name, *args, &block)
|
56
57
|
end
|
57
|
-
|
58
|
+
|
58
59
|
private
|
59
60
|
def per_page=(value)
|
60
61
|
value = 25 if value.blank?
|
@@ -1,3 +1,4 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
require 'active_support/json'
|
2
3
|
|
3
4
|
module MongoMapper
|
@@ -6,7 +7,7 @@ module MongoMapper
|
|
6
7
|
def self.configure(model)
|
7
8
|
model.class_eval { cattr_accessor :include_root_in_json, :instance_writer => true }
|
8
9
|
end
|
9
|
-
|
10
|
+
|
10
11
|
module InstanceMethods
|
11
12
|
def to_xml(options = {}, &block)
|
12
13
|
options[:dasherize] = false unless options.has_key?(:dasherize)
|
@@ -49,7 +50,7 @@ module MongoMapper
|
|
49
50
|
}
|
50
51
|
end
|
51
52
|
# End rip
|
52
|
-
|
53
|
+
|
53
54
|
options.delete(:only) if options[:only].nil? or options[:only].empty?
|
54
55
|
|
55
56
|
hash.each do |key, value|
|
@@ -63,7 +64,7 @@ module MongoMapper
|
|
63
64
|
hash[key] = value.as_json(options)
|
64
65
|
end
|
65
66
|
end
|
66
|
-
|
67
|
+
|
67
68
|
# Replicate Rails 3 naming - and also bin anytihng after : for use in our dynamic classes from unit tests
|
68
69
|
hash = { ActiveSupport::Inflector.underscore(ActiveSupport::Inflector.demodulize(self)).gsub(/:.*/,'') => hash } if include_root_in_json
|
69
70
|
hash
|
@@ -1,16 +1,17 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
module MongoMapper
|
2
3
|
module Plugins
|
3
4
|
module Validations
|
4
5
|
def self.configure(model)
|
5
6
|
model.class_eval { include Validatable }
|
6
7
|
end
|
7
|
-
|
8
|
+
|
8
9
|
module DocumentMacros
|
9
10
|
def validates_uniqueness_of(*args)
|
10
11
|
add_validations(args, MongoMapper::Plugins::Validations::ValidatesUniquenessOf)
|
11
12
|
end
|
12
13
|
end
|
13
|
-
|
14
|
+
|
14
15
|
class ValidatesUniquenessOf < Validatable::ValidationBase
|
15
16
|
option :scope, :case_sensitive
|
16
17
|
default :case_sensitive => true
|
data/lib/mongo_mapper/query.rb
CHANGED
@@ -1,143 +1,24 @@
|
|
1
|
+
# encoding: UTF-8
|
1
2
|
module MongoMapper
|
2
|
-
# IMPORTANT
|
3
|
-
# This class is private to MongoMapper and should not be considered part of MongoMapper's public API.
|
4
|
-
#
|
5
3
|
class Query
|
6
|
-
|
7
|
-
|
8
|
-
attr_reader :model
|
9
|
-
|
10
|
-
def initialize(model, options)
|
4
|
+
def initialize(model, options={})
|
11
5
|
raise ArgumentError, "Options must be a hash" unless options.is_a?(Hash)
|
12
|
-
@model, @options, @conditions
|
13
|
-
|
6
|
+
@model, @options, @conditions = model, {}, {}
|
7
|
+
query.update(options)
|
14
8
|
add_sci_condition
|
15
9
|
end
|
16
10
|
|
17
|
-
def criteria
|
18
|
-
to_criteria(@conditions)
|
19
|
-
end
|
20
|
-
|
21
|
-
def options
|
22
|
-
fields = @options[:fields] || @options[:select]
|
23
|
-
skip = @options[:skip] || @options[:offset] || 0
|
24
|
-
limit = @options[:limit] || 0
|
25
|
-
sort = @options[:sort] || normalized_sort(@options[:order])
|
26
|
-
|
27
|
-
{:fields => to_fields(fields), :skip => skip.to_i, :limit => limit.to_i, :sort => sort}
|
28
|
-
end
|
29
|
-
|
30
|
-
def to_a
|
31
|
-
[criteria, options]
|
32
|
-
end
|
33
|
-
|
34
11
|
private
|
35
|
-
def
|
36
|
-
|
37
|
-
key = key.respond_to?(:to_sym) ? key.to_sym : key
|
38
|
-
|
39
|
-
if OptionKeys.include?(key)
|
40
|
-
@options[key] = value
|
41
|
-
elsif key == :conditions
|
42
|
-
@conditions.update(value)
|
43
|
-
else
|
44
|
-
@conditions[key] = value
|
45
|
-
end
|
46
|
-
end
|
12
|
+
def method_missing(method, *args, &block)
|
13
|
+
query.send(method, *args, &block)
|
47
14
|
end
|
48
15
|
|
49
|
-
|
50
|
-
|
51
|
-
@conditions[:_type] = model.to_s if model.single_collection_inherited?
|
16
|
+
def query
|
17
|
+
@query ||= Plucky::Query.new(@model.collection).object_ids(@model.object_id_keys)
|
52
18
|
end
|
53
19
|
|
54
|
-
def
|
55
|
-
|
56
|
-
end
|
57
|
-
|
58
|
-
def symbol_operator?(object)
|
59
|
-
object.respond_to?(:field, :operator)
|
60
|
-
end
|
61
|
-
|
62
|
-
def to_criteria(conditions, parent_key=nil)
|
63
|
-
criteria = {}
|
64
|
-
|
65
|
-
conditions.each_pair do |key, value|
|
66
|
-
key = normalized_key(key)
|
67
|
-
|
68
|
-
if model.object_id_key?(key)
|
69
|
-
case value
|
70
|
-
when String
|
71
|
-
value = ObjectId.to_mongo(value)
|
72
|
-
when Array
|
73
|
-
value.map! { |id| ObjectId.to_mongo(id) }
|
74
|
-
end
|
75
|
-
end
|
76
|
-
|
77
|
-
if symbol_operator?(key)
|
78
|
-
key, value = normalized_key(key.field), {"$#{key.operator}" => value}
|
79
|
-
end
|
80
|
-
|
81
|
-
criteria[key] = normalized_value(criteria, key, value)
|
82
|
-
end
|
83
|
-
|
84
|
-
criteria
|
85
|
-
end
|
86
|
-
|
87
|
-
def to_fields(keys)
|
88
|
-
return keys if keys.is_a?(Hash)
|
89
|
-
return nil if keys.blank?
|
90
|
-
|
91
|
-
if keys.respond_to?(:flatten, :compact)
|
92
|
-
keys.flatten.compact
|
93
|
-
else
|
94
|
-
keys.split(',').map { |key| key.strip }
|
95
|
-
end
|
96
|
-
end
|
97
|
-
|
98
|
-
def to_order(key, direction=nil)
|
99
|
-
[normalized_key(key).to_s, normalized_direction(direction)]
|
100
|
-
end
|
101
|
-
|
102
|
-
def normalized_key(key)
|
103
|
-
key.to_s == 'id' ? :_id : key
|
104
|
-
end
|
105
|
-
|
106
|
-
# TODO: this is getting heavy enough to move to a class
|
107
|
-
def normalized_value(criteria, key, value)
|
108
|
-
case value
|
109
|
-
when Array, Set
|
110
|
-
modifier?(key) ? value.to_a : {'$in' => value.to_a}
|
111
|
-
when Hash
|
112
|
-
if criteria[key].kind_of?(Hash)
|
113
|
-
criteria[key].dup.merge(to_criteria(value, key))
|
114
|
-
else
|
115
|
-
to_criteria(value, key)
|
116
|
-
end
|
117
|
-
when Time
|
118
|
-
value.utc
|
119
|
-
else
|
120
|
-
value
|
121
|
-
end
|
122
|
-
end
|
123
|
-
|
124
|
-
def normalized_direction(direction)
|
125
|
-
direction ||= 'asc'
|
126
|
-
direction.downcase == 'asc' ? Mongo::ASCENDING : Mongo::DESCENDING
|
127
|
-
end
|
128
|
-
|
129
|
-
def normalized_sort(sort)
|
130
|
-
return if sort.blank?
|
131
|
-
|
132
|
-
if sort.respond_to?(:all?) && sort.all? { |s| symbol_operator?(s) }
|
133
|
-
sort.map { |s| to_order(s.field, s.operator) }
|
134
|
-
elsif symbol_operator?(sort)
|
135
|
-
[to_order(sort.field, sort.operator)]
|
136
|
-
else
|
137
|
-
sort.split(',').map do |str|
|
138
|
-
to_order(*str.strip.split(' '))
|
139
|
-
end
|
140
|
-
end
|
20
|
+
def add_sci_condition
|
21
|
+
query[:_type] = @model.to_s if @model.single_collection_inherited?
|
141
22
|
end
|
142
23
|
end
|
143
24
|
end
|