From c93dbfef36c9b095121650beec2362de42d6b715 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Mon, 9 Jun 2014 12:30:12 -0600 Subject: 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 --- activerecord/lib/active_record/connection_adapters/column.rb | 2 +- .../lib/active_record/connection_adapters/postgresql/oid/hstore.rb | 6 +++++- .../lib/active_record/connection_adapters/postgresql/oid/json.rb | 6 +++++- 3 files changed, 11 insertions(+), 3 deletions(-) (limited to 'activerecord/lib/active_record/connection_adapters') diff --git a/activerecord/lib/active_record/connection_adapters/column.rb b/activerecord/lib/active_record/connection_adapters/column.rb index 23434df1fe..22decbc2da 100644 --- a/activerecord/lib/active_record/connection_adapters/column.rb +++ b/activerecord/lib/active_record/connection_adapters/column.rb @@ -17,7 +17,7 @@ module ActiveRecord delegate :type, :precision, :scale, :limit, :klass, :accessor, :text?, :number?, :binary?, :serialized?, :changed?, - :type_cast, :type_cast_for_write, :type_cast_for_database, + :type_cast, :type_cast_from_user, :type_cast_for_database, :type_cast_for_schema, to: :cast_type diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb index a65ca83f77..0a48a14b06 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb @@ -7,7 +7,11 @@ module ActiveRecord :hstore 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) ConnectionAdapters::PostgreSQLColumn.hstore_to_string(value) end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb index c87422fe32..c64cf27797 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb @@ -7,7 +7,11 @@ module ActiveRecord :json 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) ConnectionAdapters::PostgreSQLColumn.json_to_string(value) end -- cgit v1.2.3