From 1455c4c22feb24b0ff2cbb191afb0bd98ebf7b06 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Tue, 17 Feb 2015 13:35:23 -0700 Subject: `type_cast_for_database` -> `serialize` --- activerecord/lib/active_record/attribute.rb | 2 +- activerecord/lib/active_record/attributes.rb | 4 ++-- .../lib/active_record/connection_adapters/abstract/quoting.rb | 4 ++-- .../lib/active_record/connection_adapters/abstract_mysql_adapter.rb | 2 +- .../lib/active_record/connection_adapters/postgresql/oid/array.rb | 4 ++-- .../lib/active_record/connection_adapters/postgresql/oid/bit.rb | 2 +- .../lib/active_record/connection_adapters/postgresql/oid/cidr.rb | 2 +- .../lib/active_record/connection_adapters/postgresql/oid/hstore.rb | 2 +- .../lib/active_record/connection_adapters/postgresql/oid/json.rb | 2 +- .../lib/active_record/connection_adapters/postgresql/oid/jsonb.rb | 2 +- .../lib/active_record/connection_adapters/postgresql/oid/point.rb | 2 +- .../lib/active_record/connection_adapters/postgresql/oid/range.rb | 4 ++-- .../lib/active_record/connection_adapters/postgresql/oid/uuid.rb | 2 +- .../lib/active_record/connection_adapters/postgresql/oid/xml.rb | 2 +- activerecord/lib/active_record/enum.rb | 2 +- activerecord/lib/active_record/sanitization.rb | 2 +- activerecord/lib/active_record/type/binary.rb | 2 +- activerecord/lib/active_record/type/date_time.rb | 2 +- activerecord/lib/active_record/type/float.rb | 2 +- activerecord/lib/active_record/type/helpers/mutable.rb | 4 ++-- activerecord/lib/active_record/type/integer.rb | 2 +- activerecord/lib/active_record/type/serialized.rb | 4 ++-- activerecord/lib/active_record/type/string.rb | 2 +- activerecord/lib/active_record/type/value.rb | 4 ++-- activerecord/lib/active_record/type_caster/map.rb | 2 +- activerecord/lib/active_record/validations/uniqueness.rb | 2 +- 26 files changed, 33 insertions(+), 33 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb index 419513fee1..0be0fc79f5 100644 --- a/activerecord/lib/active_record/attribute.rb +++ b/activerecord/lib/active_record/attribute.rb @@ -43,7 +43,7 @@ module ActiveRecord end def value_for_database - type.type_cast_for_database(value) + type.serialize(value) end def changed_from?(old_value) diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb index f259bfe728..650712dd19 100644 --- a/activerecord/lib/active_record/attributes.rb +++ b/activerecord/lib/active_record/attributes.rb @@ -133,7 +133,7 @@ module ActiveRecord # # When ActiveRecord::QueryMethods#where is called, it will # use the type defined by the model class to convert the value to SQL, - # calling +type_cast_for_database+ on your type object. For example: + # calling +serialize+ on your type object. For example: # # class Money < Struct.new(:amount, :currency) # end @@ -146,7 +146,7 @@ module ActiveRecord # # value will be the result of +deserialize+ or # # +type_cast_from_user+. Assumed to be in instance of +Money+ in # # this case. - # def type_cast_for_database(value) + # def serialize(value) # value_in_bitcoins = @currency_converter.convert_to_bitcoins(value) # value_in_bitcoins.amount # end diff --git a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb index 1ac909da2e..d2840b9498 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract/quoting.rb @@ -52,7 +52,7 @@ module ActiveRecord def type_cast_from_column(column, value) # :nodoc: if column type = lookup_cast_type_from_column(column) - type.type_cast_for_database(value) + type.serialize(value) else value end @@ -103,7 +103,7 @@ module ActiveRecord end def quote_default_expression(value, column) #:nodoc: - value = lookup_cast_type(column.sql_type).type_cast_for_database(value) + value = lookup_cast_type(column.sql_type).serialize(value) quote(value) end diff --git a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb index c6cc98d002..0a1c66be1e 100644 --- a/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb +++ b/activerecord/lib/active_record/connection_adapters/abstract_mysql_adapter.rb @@ -926,7 +926,7 @@ module ActiveRecord end class MysqlString < Type::String # :nodoc: - def type_cast_for_database(value) + def serialize(value) case value when true then "1" when false then "0" diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb index deb2ea50a3..ca9ceff5e4 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb @@ -40,7 +40,7 @@ module ActiveRecord type_cast_array(value, :type_cast_from_user) end - def type_cast_for_database(value) + def serialize(value) if value.is_a?(::Array) cast_value_for_database(value) else @@ -69,7 +69,7 @@ module ActiveRecord casted_values = value.map { |item| cast_value_for_database(item) } "{#{casted_values.join(delimiter)}}" else - quote_and_escape(subtype.type_cast_for_database(value)) + quote_and_escape(subtype.serialize(value)) end end 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 1dbb40ca1d..7b3a8de860 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/bit.rb @@ -20,7 +20,7 @@ module ActiveRecord end end - def type_cast_for_database(value) + def serialize(value) Data.new(super) if value end diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb index 222f10fa8f..eeccb09bdf 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/cidr.rb @@ -18,7 +18,7 @@ module ActiveRecord end end - def type_cast_for_database(value) + def serialize(value) if IPAddr === value "#{value}/#{value.instance_variable_get(:@mask_addr).to_s(2).count('1')}" else 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 40bec8a3bf..9270fc9f21 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/hstore.rb @@ -21,7 +21,7 @@ module ActiveRecord end end - def type_cast_for_database(value) + def serialize(value) if value.is_a?(::Hash) value.map { |k, v| "#{escape_hstore(k)}=>#{escape_hstore(v)}" }.join(', ') else 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 27cc00b722..98a5968183 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/json.rb @@ -17,7 +17,7 @@ module ActiveRecord end end - def type_cast_for_database(value) + def serialize(value) if value.is_a?(::Array) || value.is_a?(::Hash) ::ActiveSupport::JSON.encode(value) else diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb index c761494a00..afc9383f91 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/jsonb.rb @@ -13,7 +13,7 @@ module ActiveRecord # the comparison here. Therefore, we need to parse and re-dump the # raw value here to ensure the insignificant whitespaces are # consistent with our encoder's output. - raw_old_value = type_cast_for_database(deserialize(raw_old_value)) + raw_old_value = serialize(deserialize(raw_old_value)) super(raw_old_value, new_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 4084725ed7..44b10c94bf 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/point.rb @@ -23,7 +23,7 @@ module ActiveRecord end end - def type_cast_for_database(value) + def serialize(value) if value.is_a?(::Array) "(#{number_for_point(value[0])},#{number_for_point(value[1])})" else diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb index c4d7aaaba5..fc201f8fb9 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/range.rb @@ -30,7 +30,7 @@ module ActiveRecord ::Range.new(from, to, extracted[:exclude_end]) end - def type_cast_for_database(value) + def serialize(value) if value.is_a?(::Range) from = type_cast_single_for_database(value.begin) to = type_cast_single_for_database(value.end) @@ -53,7 +53,7 @@ module ActiveRecord end def type_cast_single_for_database(value) - infinity?(value) ? '' : @subtype.type_cast_for_database(value) + infinity?(value) ? '' : @subtype.serialize(value) end def extract_bounds(value) 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 98ab5fc667..c44645acfc 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/uuid.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/uuid.rb @@ -5,7 +5,7 @@ module ActiveRecord class Uuid < Type::Value # :nodoc: ACCEPTABLE_UUID = %r{\A\{?([a-fA-F0-9]{4}-?){8}\}?\z}x - alias_method :type_cast_for_database, :deserialize + alias_method :serialize, :deserialize def type :uuid diff --git a/activerecord/lib/active_record/connection_adapters/postgresql/oid/xml.rb b/activerecord/lib/active_record/connection_adapters/postgresql/oid/xml.rb index 334af7c598..d40d837cee 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/xml.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/xml.rb @@ -7,7 +7,7 @@ module ActiveRecord :xml end - def type_cast_for_database(value) + def serialize(value) return unless value Data.new(super) end diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 22b52c7347..9a5db58e78 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -110,7 +110,7 @@ module ActiveRecord mapping.key(value.to_i) end - def type_cast_for_database(value) + def serialize(value) mapping.fetch(value, value) end diff --git a/activerecord/lib/active_record/sanitization.rb b/activerecord/lib/active_record/sanitization.rb index 313e767dcb..c7f55ebaa1 100644 --- a/activerecord/lib/active_record/sanitization.rb +++ b/activerecord/lib/active_record/sanitization.rb @@ -75,7 +75,7 @@ module ActiveRecord def sanitize_sql_hash_for_assignment(attrs, table) c = connection attrs.map do |attr, value| - value = type_for_attribute(attr.to_s).type_cast_for_database(value) + value = type_for_attribute(attr.to_s).serialize(value) "#{c.quote_table_name_for_assignment(table, attr)} = #{c.quote(value)}" end.join(', ') end diff --git a/activerecord/lib/active_record/type/binary.rb b/activerecord/lib/active_record/type/binary.rb index 412d387c76..7d23cb5584 100644 --- a/activerecord/lib/active_record/type/binary.rb +++ b/activerecord/lib/active_record/type/binary.rb @@ -17,7 +17,7 @@ module ActiveRecord end end - def type_cast_for_database(value) + def serialize(value) return if value.nil? Data.new(super) end diff --git a/activerecord/lib/active_record/type/date_time.rb b/activerecord/lib/active_record/type/date_time.rb index a25f2521bb..05d2af3808 100644 --- a/activerecord/lib/active_record/type/date_time.rb +++ b/activerecord/lib/active_record/type/date_time.rb @@ -10,7 +10,7 @@ module ActiveRecord :datetime end - def type_cast_for_database(value) + def serialize(value) if precision && value.respond_to?(:usec) number_of_insignificant_digits = 6 - precision round_power = 10 ** number_of_insignificant_digits diff --git a/activerecord/lib/active_record/type/float.rb b/activerecord/lib/active_record/type/float.rb index b3928242b7..86e3c12258 100644 --- a/activerecord/lib/active_record/type/float.rb +++ b/activerecord/lib/active_record/type/float.rb @@ -7,7 +7,7 @@ module ActiveRecord :float end - alias type_cast_for_database type_cast + alias serialize type_cast private diff --git a/activerecord/lib/active_record/type/helpers/mutable.rb b/activerecord/lib/active_record/type/helpers/mutable.rb index 9af39aa9fb..94f246f567 100644 --- a/activerecord/lib/active_record/type/helpers/mutable.rb +++ b/activerecord/lib/active_record/type/helpers/mutable.rb @@ -3,14 +3,14 @@ module ActiveRecord module Helpers module Mutable # :nodoc: def type_cast_from_user(value) - deserialize(type_cast_for_database(value)) + deserialize(serialize(value)) end # +raw_old_value+ will be the `_before_type_cast` version of the # value (likely a string). +new_value+ will be the current, type # cast value. def changed_in_place?(raw_old_value, new_value) - raw_old_value != type_cast_for_database(new_value) + raw_old_value != serialize(new_value) end end end diff --git a/activerecord/lib/active_record/type/integer.rb b/activerecord/lib/active_record/type/integer.rb index b8cbf6114d..a3e6cb215f 100644 --- a/activerecord/lib/active_record/type/integer.rb +++ b/activerecord/lib/active_record/type/integer.rb @@ -21,7 +21,7 @@ module ActiveRecord value.to_i end - def type_cast_for_database(value) + def serialize(value) result = type_cast(value) if result ensure_in_range(result) diff --git a/activerecord/lib/active_record/type/serialized.rb b/activerecord/lib/active_record/type/serialized.rb index 67aac58b1d..732029c723 100644 --- a/activerecord/lib/active_record/type/serialized.rb +++ b/activerecord/lib/active_record/type/serialized.rb @@ -19,7 +19,7 @@ module ActiveRecord end end - def type_cast_for_database(value) + def serialize(value) return if value.nil? unless default_value?(value) super coder.dump(value) @@ -28,7 +28,7 @@ module ActiveRecord def changed_in_place?(raw_old_value, value) return false if value.nil? - subtype.changed_in_place?(raw_old_value, type_cast_for_database(value)) + subtype.changed_in_place?(raw_old_value, serialize(value)) end def accessor diff --git a/activerecord/lib/active_record/type/string.rb b/activerecord/lib/active_record/type/string.rb index fbc0af2c5a..2662b7e874 100644 --- a/activerecord/lib/active_record/type/string.rb +++ b/activerecord/lib/active_record/type/string.rb @@ -11,7 +11,7 @@ module ActiveRecord end end - def type_cast_for_database(value) + def serialize(value) case value when ::Numeric, ActiveSupport::Duration then value.to_s when ::String then ::String.new(value) diff --git a/activerecord/lib/active_record/type/value.rb b/activerecord/lib/active_record/type/value.rb index d6b61ead63..021685c4c4 100644 --- a/activerecord/lib/active_record/type/value.rb +++ b/activerecord/lib/active_record/type/value.rb @@ -40,7 +40,7 @@ module ActiveRecord # to understand. The returned value from this method should be a # +String+, +Numeric+, +Date+, +Time+, +Symbol+, +true+, +false+, or # +nil+. - def type_cast_for_database(value) + def serialize(value) value end @@ -68,7 +68,7 @@ module ActiveRecord # which could be mutated, you should override this method. You will need # to either: # - # - pass +new_value+ to Value#type_cast_for_database and compare it to + # - pass +new_value+ to Value#serialize and compare it to # +raw_old_value+ # # or diff --git a/activerecord/lib/active_record/type_caster/map.rb b/activerecord/lib/active_record/type_caster/map.rb index 03c9e8ff83..4b1941351c 100644 --- a/activerecord/lib/active_record/type_caster/map.rb +++ b/activerecord/lib/active_record/type_caster/map.rb @@ -8,7 +8,7 @@ module ActiveRecord def type_cast_for_database(attr_name, value) return value if value.is_a?(Arel::Nodes::BindParam) type = types.type_for_attribute(attr_name.to_s) - type.type_cast_for_database(value) + type.serialize(value) end protected diff --git a/activerecord/lib/active_record/validations/uniqueness.rb b/activerecord/lib/active_record/validations/uniqueness.rb index a766d77e88..9be4b10a55 100644 --- a/activerecord/lib/active_record/validations/uniqueness.rb +++ b/activerecord/lib/active_record/validations/uniqueness.rb @@ -61,7 +61,7 @@ module ActiveRecord column = klass.columns_hash[attribute_name] cast_type = klass.type_for_attribute(attribute_name) - value = cast_type.type_cast_for_database(value) + value = cast_type.serialize(value) value = klass.connection.type_cast(value) if value.is_a?(String) && column.limit value = value.to_s[0, column.limit] -- cgit v1.2.3