diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb index 7b3a8de860c4231799b1c05b2a0dacb19a77a98e..ea0fa2517fb1a10c761bb6419060f19b981a9591 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb @@ -7,7 +7,7 @@ def type :bit end - def type_cast(value) + def cast(value) if ::String === value case value when /^0x/i diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/enum.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/enum.rb index 77d5038efd29835a7333dad8f933a6a841152721..b3b610a5f614ab693cc9ab89bc268f93b05e1bbb 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/enum.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/enum.rb @@ -7,7 +7,7 @@ def type :enum end - def type_cast(value) + def cast(value) value.to_s end 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 98a5968183132a3f0cc92d232d9ecd6fd4ef6dfc..8e1256baad4f6a2037a16ccb3c2ed8245cb3f589 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb @@ -13,7 +13,7 @@ def deserialize(value) if value.is_a?(::String) ::ActiveSupport::JSON.decode(value) rescue nil else - super + value end end @@ -21,7 +21,7 @@ def serialize(value) if value.is_a?(::Array) || value.is_a?(::Hash) ::ActiveSupport::JSON.encode(value) else - super + value end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb index 44b10c94bfafb1971923618e3bc5b81e4c02ac8d..bf565bcf470206569d8e6bd372375f0f22ed7c1f 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb @@ -9,13 +9,13 @@ def type :point end - def type_cast(value) + def cast(value) case value when ::String if value[0] == '(' && value[-1] == ')' value = value[1...-1] end - type_cast(value.split(',')) + cast(value.split(',')) when ::Array value.map { |v| Float(v) } else diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/uuid.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/uuid.rb index c44645acfccf1cd43057dbcf1e84c6827ad72994..5e839228e9fa07cdc77f0462b39bd218ede49481 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/uuid.rb @@ -11,7 +11,7 @@ def type :uuid end - def type_cast(value) + def cast(value) value.to_s[ACCEPTABLE_UUID, 0] end end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/vector.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/vector.rb index de4187b0281916121d94a006a73a982c1678eec3..b26e876b543a63725fef767e84d57327f66de70c 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/vector.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/vector.rb @@ -16,7 +16,7 @@ def initialize(delim, subtype) # FIXME: this should probably split on +delim+ and use +subtype+ # to cast the values. Unfortunately, the current Rails behavior # is to just return the string. - def type_cast(value) + def cast(value) value end end diff --git a/activerecord/lib/active_record/type/binary.rb b/activerecord/lib/active_record/type/binary.rb index 7d23cb5584c9f29566be9a6cc299a0e3a9a97f44..0baf8c63ad17785b364c4d461df385870a7d3125 100644 --- a/activerecord/lib/active_record/type/binary.rb +++ b/activerecord/lib/active_record/type/binary.rb @@ -9,7 +9,7 @@ def binary? true end - def type_cast(value) + def cast(value) if value.is_a?(Data) value.to_s else diff --git a/activerecord/lib/active_record/type/float.rb b/activerecord/lib/active_record/type/float.rb index 86e3c12258326d61c1d87dc6eaab02846003e249..d88482b85d4134f6d9fc069682c9618a576150db 100644 --- a/activerecord/lib/active_record/type/float.rb +++ b/activerecord/lib/active_record/type/float.rb @@ -7,7 +7,7 @@ def type :float end - alias serialize type_cast + alias serialize cast private diff --git a/activerecord/lib/active_record/type/helpers/numeric.rb b/activerecord/lib/active_record/type/helpers/numeric.rb index b0d4f031170c38abdd68fdb31c9c5baca0d8a6e6..a755a02a59958340f03370d3d5a0593468e8eb4c 100644 --- a/activerecord/lib/active_record/type/helpers/numeric.rb +++ b/activerecord/lib/active_record/type/helpers/numeric.rb @@ -2,7 +2,7 @@ module ActiveRecord module Type module Helpers module Numeric # :nodoc: - def type_cast(value) + def cast(value) value = case value when true then 1 when false then 0 diff --git a/activerecord/lib/active_record/type/integer.rb b/activerecord/lib/active_record/type/integer.rb index a3e6cb215f3abe734b334ff30aa61250a111c5e4..2a1b04ac7f58d9acbbcb13246f79ccb764d8129e 100644 --- a/activerecord/lib/active_record/type/integer.rb +++ b/activerecord/lib/active_record/type/integer.rb @@ -22,7 +22,7 @@ def deserialize(value) end def serialize(value) - result = type_cast(value) + result = cast(value) if result ensure_in_range(result) end diff --git a/activerecord/lib/active_record/type/value.rb b/activerecord/lib/active_record/type/value.rb index cdac1f9fa9b9624c698277cc1d4966def48ae581..fc3ef5e83b798cfda31753faeeda3dc7022eb243 100644 --- a/activerecord/lib/active_record/type/value.rb +++ b/activerecord/lib/active_record/type/value.rb @@ -14,12 +14,12 @@ def type # :nodoc: # Convert a value from database input to the appropriate ruby type. The # return value of this method will be returned from - # ActiveRecord::AttributeMethods::Read#read_attribute. See also - # Value#type_cast and Value#cast_value. + # ActiveRecord::AttributeMethods::Read#read_attribute. The default + # implementation just calls Value#cast. # # +value+ The raw input, as provided from the database. def deserialize(value) - type_cast(value) + cast(value) end # Type casts a value from user input (e.g. from a setter). This value may @@ -29,11 +29,11 @@ def deserialize(value) # # The return value of this method will be returned from # ActiveRecord::AttributeMethods::Read#read_attribute. See also: - # Value#type_cast and Value#cast_value. + # Value#cast_value. # # +value+ The raw input, as provided to the attribute setter. def cast(value) - type_cast(value) + cast_value(value) unless value.nil? end # Cast a value from the ruby type to a type that the database knows how @@ -93,16 +93,8 @@ def ==(other) private - # Convenience method. If you don't need separate behavior for - # Value#deserialize and Value#cast, you can override - # this method instead. The default behavior of both methods is to call - # this one. See also Value#cast_value. - def type_cast(value) # :doc: - cast_value(value) unless value.nil? - end - # Convenience method for types which do not need separate type casting - # behavior for user and database inputs. Called by Value#type_cast for + # behavior for user and database inputs. Called by Value#cast for # values except +nil+. def cast_value(value) # :doc: value diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index ef1173a2ba9f3b6391e2e4b51f34c94ebc7ab3e7..76907c832f2299fa2a9690abb5623cc5d7412d1d 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1427,7 +1427,7 @@ def test_column_types_typecast attrs.delete 'id' typecast = Class.new(ActiveRecord::Type::Value) { - def type_cast value + def cast value "t.lo" end } diff --git a/activerecord/test/cases/dirty_test.rb b/activerecord/test/cases/dirty_test.rb index 6d8076f3fd0901d18f2384ab88133d4f69abb2c2..3a7cc572e65c033c09e05f2934cb10096d9fcf3c 100644 --- a/activerecord/test/cases/dirty_test.rb +++ b/activerecord/test/cases/dirty_test.rb @@ -625,7 +625,7 @@ def test_datetime_attribute_doesnt_change_if_zone_is_modified_in_string test "defaults with type that implements `serialize`" do type = Class.new(ActiveRecord::Type::Value) do - def type_cast(value) + def cast(value) value.to_i end