aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib/active_record/type
diff options
context:
space:
mode:
authorSean Griffin <sean@thoughtbot.com>2014-06-09 12:30:12 -0600
committerSean Griffin <sean@thoughtbot.com>2014-06-09 12:30:12 -0600
commitc93dbfef36c9b095121650beec2362de42d6b715 (patch)
tree04020266c12d13eedfc318fd15bd7c8d787851a5 /activerecord/lib/active_record/type
parenta5c12cbd3c0e9b392edb9e4eb13dcdd256327acd (diff)
downloadrails-c93dbfef36c9b095121650beec2362de42d6b715.tar.gz
rails-c93dbfef36c9b095121650beec2362de42d6b715.tar.bz2
rails-c93dbfef36c9b095121650beec2362de42d6b715.zip
Make `_before_type_cast` actually be before type cast
- The following is now true for all types, all the time - `model.attribute_before_type_cast == given_value` - `model.attribute == model.save_and_reload.attribute` - `model.attribute == model.dup.attribute` - `model.attribute == YAML.load(YAML.dump(model)).attribute` - Removes the remaining types implementing `type_cast_for_write` - Simplifies the implementation of time zone aware attributes - Brings tz aware attributes closer to being implemented as an attribute decorator - Adds additional point of control for custom types
Diffstat (limited to 'activerecord/lib/active_record/type')
-rw-r--r--activerecord/lib/active_record/type/binary.rb9
-rw-r--r--activerecord/lib/active_record/type/serialized.rb8
-rw-r--r--activerecord/lib/active_record/type/value.rb10
3 files changed, 19 insertions, 8 deletions
diff --git a/activerecord/lib/active_record/type/binary.rb b/activerecord/lib/active_record/type/binary.rb
index bc93f6e1bf..3bf29b5026 100644
--- a/activerecord/lib/active_record/type/binary.rb
+++ b/activerecord/lib/active_record/type/binary.rb
@@ -9,7 +9,16 @@ module ActiveRecord
true
end
+ def type_cast(value)
+ if value.is_a?(Data)
+ value.to_s
+ else
+ super
+ end
+ end
+
def type_cast_for_database(value)
+ return if value.nil?
Data.new(super)
end
diff --git a/activerecord/lib/active_record/type/serialized.rb b/activerecord/lib/active_record/type/serialized.rb
index 0866383de9..a1205fa819 100644
--- a/activerecord/lib/active_record/type/serialized.rb
+++ b/activerecord/lib/active_record/type/serialized.rb
@@ -17,15 +17,17 @@ module ActiveRecord
end
end
- def type_cast_for_write(value)
+ def type_cast_from_user(value)
+ type_cast(type_cast_for_database(value))
+ end
+
+ def type_cast_for_database(value)
return if value.nil?
unless is_default_value?(value)
super coder.dump(value)
end
end
- alias type_cast_for_database type_cast_for_write
-
def serialized?
true
end
diff --git a/activerecord/lib/active_record/type/value.rb b/activerecord/lib/active_record/type/value.rb
index 1c41b28646..deacc398ef 100644
--- a/activerecord/lib/active_record/type/value.rb
+++ b/activerecord/lib/active_record/type/value.rb
@@ -23,8 +23,12 @@ module ActiveRecord
cast_value(value) unless value.nil?
end
+ def type_cast_from_user(value)
+ type_cast(value)
+ end
+
def type_cast_for_database(value)
- type_cast_for_write(value)
+ value
end
def type_cast_for_schema(value)
@@ -50,10 +54,6 @@ module ActiveRecord
def klass # :nodoc:
end
- def type_cast_for_write(value) # :nodoc:
- value
- end
-
# +old_value+ will always be type-cast.
# +new_value+ will come straight from the database
# or from assignment, so it could be anything. Types