Skip to content

Commit 41ac6d1

Browse files
committed
Add wrap_uncastable_values_from_database flag
1 parent 897b1bd commit 41ac6d1

File tree

2 files changed

+18
-2
lines changed

2 files changed

+18
-2
lines changed

lib/mongoid/config.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,10 @@ module Config
7272
# to assign an incompatible type to a field.
7373
option :strict_type_assignment, default: false
7474

75+
# Indicates whether uncastable values from the database should
76+
# be returned wrapped by Mongoid::RawValue class.
77+
option :wrap_uncastable_values_from_database, default: false
78+
7579
# Use ActiveSupport's time zone in time operations instead of the
7680
# Ruby default time zone.
7781
option :use_activesupport_time_zone, default: true

lib/mongoid/fields/standard.rb

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,11 @@ class Standard
99
# Set readers for the instance variables.
1010
attr_accessor :default_val, :label, :name, :options
1111

12+
# If type.mongoize returns Mongoid::RawValue,
13+
# handle according to field or global strict setting
1214
def mongoize(object)
1315
value = type.mongoize(object)
14-
unless value.is_a?(Mongoid::RawValue)
16+
if value.is_a?(Mongoid::RawValue)
1517
case strict
1618
when :error then value.raise_error!
1719
when :warn then value.warn
@@ -20,7 +22,17 @@ def mongoize(object)
2022
value
2123
end
2224

23-
def_delegators :type, :demongoize, :evolve
25+
# If type.demongoize returns Mongoid::RawValue,
26+
# return the inner value according to Mongoid.wrap_uncastable_values_from_database
27+
def demongoize(object)
28+
value = type.demongoize(object)
29+
if value.is_a?(Mongoid::RawValue) && !Mongoid.wrap_uncastable_values_from_database
30+
return value.raw_value
31+
end
32+
value
33+
end
34+
35+
def_delegators :type, :evolve
2436

2537
# Adds the atomic changes for this type of resizable field.
2638
#

0 commit comments

Comments
 (0)