File tree Expand file tree Collapse file tree 4 files changed +66
-10
lines changed Expand file tree Collapse file tree 4 files changed +66
-10
lines changed Original file line number Diff line number Diff line change @@ -118,6 +118,24 @@ See the section on :ref:`Localize :present Field Option <present-fields>` for
118
118
more details on how these are used.
119
119
120
120
121
+ Support for Passing Raw Values into Queries
122
+ -------------------------------------------
123
+
124
+ When performing queries, it is now possible skip Mongoid's type coercion logic
125
+ using the ``Mongoid::RawValue`` wrapper class. This can be useful when legacy
126
+ data in the database is of a different type than the field definition.
127
+
128
+ .. code-block:: ruby
129
+
130
+ class Person
131
+ include Mongoid::Document
132
+ field :age, type: Integer
133
+ end
134
+
135
+ # Query for the string "42", not the integer 42
136
+ Person.where(age: Mongoid::RawValue("42"))
137
+
138
+
121
139
Added ``:to`` and ``:from`` options to ``attribute_changed?``
122
140
-------------------------------------------------------------
123
141
Original file line number Diff line number Diff line change @@ -150,16 +150,20 @@ def evolve_multi(specs)
150
150
#
151
151
# @return [ Object ] The serialized object.
152
152
def evolve ( serializer , value )
153
- case value
154
- when Hash
155
- evolve_hash ( serializer , value )
156
- when Array
157
- evolve_array ( serializer , value )
158
- when Range
159
- value . __evolve_range__ ( serializer : serializer )
160
- else
161
- ( serializer || value . class ) . evolve ( value )
162
- end
153
+ _value = case value
154
+ when Hash
155
+ evolve_hash ( serializer , value )
156
+ when Array
157
+ evolve_array ( serializer , value )
158
+ when Range
159
+ value . __evolve_range__ ( serializer : serializer )
160
+ when Mongoid ::RawValue
161
+ value
162
+ else
163
+ ( serializer || value . class ) . evolve ( value )
164
+ end
165
+ _value = _value . raw_value if _value . is_a? ( Mongoid ::RawValue )
166
+ _value
163
167
end
164
168
165
169
# Evolve a single key selection with array values.
Original file line number Diff line number Diff line change @@ -48,6 +48,7 @@ def transform_keys
48
48
require "mongoid/extensions/object"
49
49
require "mongoid/extensions/object_id"
50
50
require "mongoid/extensions/range"
51
+ require "mongoid/extensions/raw_value"
51
52
require "mongoid/extensions/regexp"
52
53
require "mongoid/extensions/set"
53
54
require "mongoid/extensions/string"
Original file line number Diff line number Diff line change
1
+ # frozen_string_literal: true
2
+
3
+ # Wrapper class used when a value cannot be casted in evolve method.
4
+ module Mongoid
5
+ def RawValue ( *args )
6
+ RawValue . new ( *args )
7
+ end
8
+
9
+ class RawValue
10
+
11
+ attr_reader :raw_value
12
+
13
+ def initialize ( raw_value )
14
+ @raw_value = raw_value
15
+ end
16
+
17
+ # Delegate all missing methods to the raw value.
18
+ #
19
+ # @param [ String, Symbol ] method_name The name of the method.
20
+ # @param [ Array ] args The arguments passed to the method.
21
+ ruby2_keywords def method_missing ( method_name , *args , &block )
22
+ raw_value . send ( method_name , *args , &block )
23
+ end
24
+
25
+ # Delegate all missing methods to the raw value.
26
+ #
27
+ # @param [ String, Symbol ] method_name The name of the method.
28
+ # @param [ true | false ] include_private Whether to check private methods.
29
+ def respond_to_missing? ( method_name , include_private = false )
30
+ raw_value . respond_to? ( method_name , include_private )
31
+ end
32
+ end
33
+ end
You can’t perform that action at this time.
0 commit comments