diff options
author | Sean Griffin <sean@thoughtbot.com> | 2015-02-17 13:39:42 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2015-02-17 13:39:42 -0700 |
commit | 9ca6948f72bef56445030a60e346376a821dbc72 (patch) | |
tree | 669b2e9b835a09067079c2afb3ff86df00e912e1 /activerecord | |
parent | 1455c4c22feb24b0ff2cbb191afb0bd98ebf7b06 (diff) | |
download | rails-9ca6948f72bef56445030a60e346376a821dbc72.tar.gz rails-9ca6948f72bef56445030a60e346376a821dbc72.tar.bz2 rails-9ca6948f72bef56445030a60e346376a821dbc72.zip |
`type_cast_from_user` -> `cast`
Diffstat (limited to 'activerecord')
22 files changed, 106 insertions, 106 deletions
diff --git a/activerecord/lib/active_record/associations/collection_association.rb b/activerecord/lib/active_record/associations/collection_association.rb index 33e4516bd1..eea847cfa3 100644 --- a/activerecord/lib/active_record/associations/collection_association.rb +++ b/activerecord/lib/active_record/associations/collection_association.rb @@ -63,7 +63,7 @@ module ActiveRecord def ids_writer(ids) pk_type = reflection.primary_key_type ids = Array(ids).reject(&:blank?) - ids.map! { |i| pk_type.type_cast_from_user(i) } + ids.map! { |i| pk_type.cast(i) } replace(klass.find(ids).index_by(&:id).values_at(*ids)) end diff --git a/activerecord/lib/active_record/attribute.rb b/activerecord/lib/active_record/attribute.rb index 0be0fc79f5..73dd3fa041 100644 --- a/activerecord/lib/active_record/attribute.rb +++ b/activerecord/lib/active_record/attribute.rb @@ -114,7 +114,7 @@ module ActiveRecord class FromUser < Attribute # :nodoc: def type_cast(value) - type.type_cast_from_user(value) + type.cast(value) end def came_from_user? diff --git a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb index f6a9fff343..f9beb43e4b 100644 --- a/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb +++ b/activerecord/lib/active_record/attribute_methods/time_zone_conversion.rb @@ -6,9 +6,9 @@ module ActiveRecord convert_time_to_time_zone(super) end - def type_cast_from_user(value) + def cast(value) if value.is_a?(Array) - value.map { |v| type_cast_from_user(v) } + value.map { |v| cast(v) } elsif value.is_a?(Hash) set_time_zone_without_conversion(super) elsif value.respond_to?(:in_time_zone) diff --git a/activerecord/lib/active_record/attributes.rb b/activerecord/lib/active_record/attributes.rb index 650712dd19..45a764bd5a 100644 --- a/activerecord/lib/active_record/attributes.rb +++ b/activerecord/lib/active_record/attributes.rb @@ -102,14 +102,14 @@ module ActiveRecord # # Users may also define their own custom types, as long as they respond # to the methods defined on the value type. The method - # +deserialize+ or +type_cast_from_user+ will be called on + # +deserialize+ or +cast+ will be called on # your type object, with raw input from the database or from your # controllers. See ActiveRecord::Type::Value for the expected API. It is # recommended that your type objects inherit from an existing type, or # from ActiveRecord::Type::Value # # class MoneyType < ActiveRecord::Type::Integer - # def type_cast_from_user(value) + # def cast(value) # if value.include?('$') # price_in_dollars = value.gsub(/\$/, '').to_f # super(price_in_dollars * 100) @@ -144,7 +144,7 @@ module ActiveRecord # end # # # value will be the result of +deserialize+ or - # # +type_cast_from_user+. Assumed to be in instance of +Money+ in + # # +cast+. Assumed to be in instance of +Money+ in # # this case. # def serialize(value) # value_in_bitcoins = @currency_converter.convert_to_bitcoins(value) @@ -195,7 +195,7 @@ module ActiveRecord # Otherwise, the default will be +nil+. # # +user_provided_default+ Whether the default value should be cast using - # +type_cast_from_user+ or +deserialize+. + # +cast+ or +deserialize+. def define_attribute( name, cast_type, 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 ca9ceff5e4..fb4e0de2a8 100644 --- a/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb +++ b/activerecord/lib/active_record/connection_adapters/postgresql/oid/array.rb @@ -33,11 +33,11 @@ module ActiveRecord end end - def type_cast_from_user(value) + def cast(value) if value.is_a?(::String) value = parse_pg_array(value) end - type_cast_array(value, :type_cast_from_user) + type_cast_array(value, :cast) end def serialize(value) diff --git a/activerecord/lib/active_record/enum.rb b/activerecord/lib/active_record/enum.rb index 9a5db58e78..ea88983917 100644 --- a/activerecord/lib/active_record/enum.rb +++ b/activerecord/lib/active_record/enum.rb @@ -93,7 +93,7 @@ module ActiveRecord @mapping = mapping end - def type_cast_from_user(value) + def cast(value) return if value.blank? if mapping.has_key?(value) diff --git a/activerecord/lib/active_record/nested_attributes.rb b/activerecord/lib/active_record/nested_attributes.rb index 117a128579..084ef397a8 100644 --- a/activerecord/lib/active_record/nested_attributes.rb +++ b/activerecord/lib/active_record/nested_attributes.rb @@ -521,7 +521,7 @@ module ActiveRecord # Determines if a hash contains a truthy _destroy key. def has_destroy_flag?(hash) - Type::Boolean.new.type_cast_from_user(hash['_destroy']) + Type::Boolean.new.cast(hash['_destroy']) end # Determines if a new record should be rejected by checking diff --git a/activerecord/lib/active_record/type/helpers/accepts_multiparameter_time.rb b/activerecord/lib/active_record/type/helpers/accepts_multiparameter_time.rb index 640943c5e9..be571fc1c7 100644 --- a/activerecord/lib/active_record/type/helpers/accepts_multiparameter_time.rb +++ b/activerecord/lib/active_record/type/helpers/accepts_multiparameter_time.rb @@ -3,7 +3,7 @@ module ActiveRecord module Helpers class AcceptsMultiparameterTime < Module # :nodoc: def initialize(defaults: {}) - define_method(:type_cast_from_user) do |value| + define_method(:cast) do |value| if value.is_a?(Hash) value_from_multiparameter_assignment(value) else diff --git a/activerecord/lib/active_record/type/helpers/mutable.rb b/activerecord/lib/active_record/type/helpers/mutable.rb index 94f246f567..88a9099277 100644 --- a/activerecord/lib/active_record/type/helpers/mutable.rb +++ b/activerecord/lib/active_record/type/helpers/mutable.rb @@ -2,7 +2,7 @@ module ActiveRecord module Type module Helpers module Mutable # :nodoc: - def type_cast_from_user(value) + def cast(value) deserialize(serialize(value)) end diff --git a/activerecord/lib/active_record/type/value.rb b/activerecord/lib/active_record/type/value.rb index 021685c4c4..cdac1f9fa9 100644 --- a/activerecord/lib/active_record/type/value.rb +++ b/activerecord/lib/active_record/type/value.rb @@ -32,7 +32,7 @@ module ActiveRecord # Value#type_cast and Value#cast_value. # # +value+ The raw input, as provided to the attribute setter. - def type_cast_from_user(value) + def cast(value) type_cast(value) end @@ -94,7 +94,7 @@ module ActiveRecord private # Convenience method. If you don't need separate behavior for - # Value#deserialize and Value#type_cast_from_user, you can override + # 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: diff --git a/activerecord/test/cases/adapters/postgresql/composite_test.rb b/activerecord/test/cases/adapters/postgresql/composite_test.rb index 14c81f8afb..2408ed4295 100644 --- a/activerecord/test/cases/adapters/postgresql/composite_test.rb +++ b/activerecord/test/cases/adapters/postgresql/composite_test.rb @@ -89,7 +89,7 @@ class PostgresqlCompositeWithCustomOIDTest < ActiveRecord::TestCase end end - def type_cast_from_user(value) + def cast(value) value end diff --git a/activerecord/test/cases/adapters/postgresql/money_test.rb b/activerecord/test/cases/adapters/postgresql/money_test.rb index ba9af4be6f..83aac1559f 100644 --- a/activerecord/test/cases/adapters/postgresql/money_test.rb +++ b/activerecord/test/cases/adapters/postgresql/money_test.rb @@ -47,10 +47,10 @@ class PostgresqlMoneyTest < ActiveRecord::TestCase def test_money_type_cast type = PostgresqlMoney.type_for_attribute('wealth') - assert_equal(12345678.12, type.type_cast_from_user("$12,345,678.12")) - assert_equal(12345678.12, type.type_cast_from_user("$12.345.678,12")) - assert_equal(-1.15, type.type_cast_from_user("-$1.15")) - assert_equal(-2.25, type.type_cast_from_user("($2.25)")) + assert_equal(12345678.12, type.cast("$12,345,678.12")) + assert_equal(12345678.12, type.cast("$12.345.678,12")) + assert_equal(-1.15, type.cast("-$1.15")) + assert_equal(-2.25, type.cast("($2.25)")) end def test_schema_dumping diff --git a/activerecord/test/cases/attribute_decorators_test.rb b/activerecord/test/cases/attribute_decorators_test.rb index 167db8147b..2aeb2601c2 100644 --- a/activerecord/test/cases/attribute_decorators_test.rb +++ b/activerecord/test/cases/attribute_decorators_test.rb @@ -12,11 +12,11 @@ module ActiveRecord super(delegate) end - def type_cast_from_user(value) + def cast(value) "#{super} #{@decoration}" end - alias deserialize type_cast_from_user + alias deserialize cast end setup do @@ -102,11 +102,11 @@ module ActiveRecord end class Multiplier < SimpleDelegator - def type_cast_from_user(value) + def cast(value) return if value.nil? value * 2 end - alias deserialize type_cast_from_user + alias deserialize cast end test "decorating with a proc" do diff --git a/activerecord/test/cases/attribute_set_test.rb b/activerecord/test/cases/attribute_set_test.rb index 948bcd9cd9..9d927481ec 100644 --- a/activerecord/test/cases/attribute_set_test.rb +++ b/activerecord/test/cases/attribute_set_test.rb @@ -151,7 +151,7 @@ module ActiveRecord end class MyType - def type_cast_from_user(value) + def cast(value) return if value.nil? value + " from user" end diff --git a/activerecord/test/cases/attribute_test.rb b/activerecord/test/cases/attribute_test.rb index 3b888950e2..aa419c7a67 100644 --- a/activerecord/test/cases/attribute_test.rb +++ b/activerecord/test/cases/attribute_test.rb @@ -22,7 +22,7 @@ module ActiveRecord end test "from_user + read type casts from user" do - @type.expect(:type_cast_from_user, 'type cast from user', ['a value']) + @type.expect(:cast, 'type cast from user', ['a value']) attribute = Attribute.from_user(nil, 'a value', @type) type_cast_value = attribute.value @@ -68,7 +68,7 @@ module ActiveRecord end test "from_user + read_for_database type casts from the user to the database" do - @type.expect(:type_cast_from_user, 'read from user', ['whatever']) + @type.expect(:cast, 'read from user', ['whatever']) @type.expect(:serialize, 'ready for database', ['read from user']) attribute = Attribute.from_user(nil, 'whatever', @type) @@ -102,7 +102,7 @@ module ActiveRecord end class MyType - def type_cast_from_user(value) + def cast(value) value + " from user" end diff --git a/activerecord/test/cases/attributes_test.rb b/activerecord/test/cases/attributes_test.rb index 3eb2cc4520..e7b76b1cf9 100644 --- a/activerecord/test/cases/attributes_test.rb +++ b/activerecord/test/cases/attributes_test.rb @@ -108,7 +108,7 @@ module ActiveRecord test "the given default value is cast from user" do custom_type = Class.new(Type::Value) do - def type_cast_from_user(*) + def cast(*) "from user" end diff --git a/activerecord/test/cases/connection_adapters/type_lookup_test.rb b/activerecord/test/cases/connection_adapters/type_lookup_test.rb index ac2e0053b8..05c57985a1 100644 --- a/activerecord/test/cases/connection_adapters/type_lookup_test.rb +++ b/activerecord/test/cases/connection_adapters/type_lookup_test.rb @@ -90,7 +90,7 @@ module ActiveRecord cast_type = @connection.type_map.lookup(type) assert_equal :decimal, cast_type.type - assert_equal 2, cast_type.type_cast_from_user(2.1) + assert_equal 2, cast_type.cast(2.1) end end diff --git a/activerecord/test/cases/reflection_test.rb b/activerecord/test/cases/reflection_test.rb index 206189350f..67e9bef808 100644 --- a/activerecord/test/cases/reflection_test.rb +++ b/activerecord/test/cases/reflection_test.rb @@ -93,7 +93,7 @@ class ReflectionTest < ActiveRecord::TestCase object = Object.new assert_equal object, type.deserialize(object) - assert_equal object, type.type_cast_from_user(object) + assert_equal object, type.cast(object) assert_equal object, type.serialize(object) end diff --git a/activerecord/test/cases/type/decimal_test.rb b/activerecord/test/cases/type/decimal_test.rb index 34ed1d7b19..fe49d0e79a 100644 --- a/activerecord/test/cases/type/decimal_test.rb +++ b/activerecord/test/cases/type/decimal_test.rb @@ -5,29 +5,29 @@ module ActiveRecord class DecimalTest < ActiveRecord::TestCase def test_type_cast_decimal type = Decimal.new - assert_equal BigDecimal.new("0"), type.type_cast_from_user(BigDecimal.new("0")) - assert_equal BigDecimal.new("123"), type.type_cast_from_user(123.0) - assert_equal BigDecimal.new("1"), type.type_cast_from_user(:"1") + assert_equal BigDecimal.new("0"), type.cast(BigDecimal.new("0")) + assert_equal BigDecimal.new("123"), type.cast(123.0) + assert_equal BigDecimal.new("1"), type.cast(:"1") end def test_type_cast_decimal_from_float_with_large_precision type = Decimal.new(precision: ::Float::DIG + 2) - assert_equal BigDecimal.new("123.0"), type.type_cast_from_user(123.0) + assert_equal BigDecimal.new("123.0"), type.cast(123.0) end def test_type_cast_from_float_with_unspecified_precision type = Decimal.new - assert_equal 22.68.to_d, type.type_cast_from_user(22.68) + assert_equal 22.68.to_d, type.cast(22.68) end def test_type_cast_decimal_from_rational_with_precision type = Decimal.new(precision: 2) - assert_equal BigDecimal("0.33"), type.type_cast_from_user(Rational(1, 3)) + assert_equal BigDecimal("0.33"), type.cast(Rational(1, 3)) end def test_type_cast_decimal_from_rational_without_precision_defaults_to_18_36 type = Decimal.new - assert_equal BigDecimal("0.333333333333333333E0"), type.type_cast_from_user(Rational(1, 3)) + assert_equal BigDecimal("0.333333333333333333E0"), type.cast(Rational(1, 3)) end def test_type_cast_decimal_from_object_responding_to_d @@ -36,7 +36,7 @@ module ActiveRecord BigDecimal.new("1") end type = Decimal.new - assert_equal BigDecimal("1"), type.type_cast_from_user(value) + assert_equal BigDecimal("1"), type.cast(value) end def test_changed? diff --git a/activerecord/test/cases/type/integer_test.rb b/activerecord/test/cases/type/integer_test.rb index a767c73cf2..84fb05dd8e 100644 --- a/activerecord/test/cases/type/integer_test.rb +++ b/activerecord/test/cases/type/integer_test.rb @@ -6,39 +6,39 @@ module ActiveRecord class IntegerTest < ActiveRecord::TestCase test "simple values" do type = Type::Integer.new - assert_equal 1, type.type_cast_from_user(1) - assert_equal 1, type.type_cast_from_user('1') - assert_equal 1, type.type_cast_from_user('1ignore') - assert_equal 0, type.type_cast_from_user('bad1') - assert_equal 0, type.type_cast_from_user('bad') - assert_equal 1, type.type_cast_from_user(1.7) - assert_equal 0, type.type_cast_from_user(false) - assert_equal 1, type.type_cast_from_user(true) - assert_nil type.type_cast_from_user(nil) + assert_equal 1, type.cast(1) + assert_equal 1, type.cast('1') + assert_equal 1, type.cast('1ignore') + assert_equal 0, type.cast('bad1') + assert_equal 0, type.cast('bad') + assert_equal 1, type.cast(1.7) + assert_equal 0, type.cast(false) + assert_equal 1, type.cast(true) + assert_nil type.cast(nil) end test "random objects cast to nil" do type = Type::Integer.new - assert_nil type.type_cast_from_user([1,2]) - assert_nil type.type_cast_from_user({1 => 2}) - assert_nil type.type_cast_from_user((1..2)) + assert_nil type.cast([1,2]) + assert_nil type.cast({1 => 2}) + assert_nil type.cast((1..2)) end test "casting ActiveRecord models" do type = Type::Integer.new firm = Firm.create(:name => 'Apple') - assert_nil type.type_cast_from_user(firm) + assert_nil type.cast(firm) end test "casting objects without to_i" do type = Type::Integer.new - assert_nil type.type_cast_from_user(::Object.new) + assert_nil type.cast(::Object.new) end test "casting nan and infinity" do type = Type::Integer.new - assert_nil type.type_cast_from_user(::Float::NAN) - assert_nil type.type_cast_from_user(1.0/0.0) + assert_nil type.cast(::Float::NAN) + assert_nil type.cast(1.0/0.0) end test "casting booleans for database" do diff --git a/activerecord/test/cases/type/string_test.rb b/activerecord/test/cases/type/string_test.rb index 880b6e8541..56e9bf434d 100644 --- a/activerecord/test/cases/type/string_test.rb +++ b/activerecord/test/cases/type/string_test.rb @@ -4,15 +4,15 @@ module ActiveRecord class StringTypeTest < ActiveRecord::TestCase test "type casting" do type = Type::String.new - assert_equal "t", type.type_cast_from_user(true) - assert_equal "f", type.type_cast_from_user(false) - assert_equal "123", type.type_cast_from_user(123) + assert_equal "t", type.cast(true) + assert_equal "f", type.cast(false) + assert_equal "123", type.cast(123) end test "values are duped coming out" do s = "foo" type = Type::String.new - assert_not_same s, type.type_cast_from_user(s) + assert_not_same s, type.cast(s) assert_not_same s, type.deserialize(s) end diff --git a/activerecord/test/cases/types_test.rb b/activerecord/test/cases/types_test.rb index 870d348928..9b1859c2ce 100644 --- a/activerecord/test/cases/types_test.rb +++ b/activerecord/test/cases/types_test.rb @@ -5,38 +5,38 @@ module ActiveRecord class TypesTest < ActiveRecord::TestCase def test_type_cast_boolean type = Type::Boolean.new - assert type.type_cast_from_user('').nil? - assert type.type_cast_from_user(nil).nil? - - assert type.type_cast_from_user(true) - assert type.type_cast_from_user(1) - assert type.type_cast_from_user('1') - assert type.type_cast_from_user('t') - assert type.type_cast_from_user('T') - assert type.type_cast_from_user('true') - assert type.type_cast_from_user('TRUE') - assert type.type_cast_from_user('on') - assert type.type_cast_from_user('ON') - assert type.type_cast_from_user(' ') - assert type.type_cast_from_user("\u3000\r\n") - assert type.type_cast_from_user("\u0000") - assert type.type_cast_from_user('SOMETHING RANDOM') + assert type.cast('').nil? + assert type.cast(nil).nil? + + assert type.cast(true) + assert type.cast(1) + assert type.cast('1') + assert type.cast('t') + assert type.cast('T') + assert type.cast('true') + assert type.cast('TRUE') + assert type.cast('on') + assert type.cast('ON') + assert type.cast(' ') + assert type.cast("\u3000\r\n") + assert type.cast("\u0000") + assert type.cast('SOMETHING RANDOM') # explicitly check for false vs nil - assert_equal false, type.type_cast_from_user(false) - assert_equal false, type.type_cast_from_user(0) - assert_equal false, type.type_cast_from_user('0') - assert_equal false, type.type_cast_from_user('f') - assert_equal false, type.type_cast_from_user('F') - assert_equal false, type.type_cast_from_user('false') - assert_equal false, type.type_cast_from_user('FALSE') - assert_equal false, type.type_cast_from_user('off') - assert_equal false, type.type_cast_from_user('OFF') + assert_equal false, type.cast(false) + assert_equal false, type.cast(0) + assert_equal false, type.cast('0') + assert_equal false, type.cast('f') + assert_equal false, type.cast('F') + assert_equal false, type.cast('false') + assert_equal false, type.cast('FALSE') + assert_equal false, type.cast('off') + assert_equal false, type.cast('OFF') end def test_type_cast_float type = Type::Float.new - assert_equal 1.0, type.type_cast_from_user("1") + assert_equal 1.0, type.cast("1") end def test_changing_float @@ -50,54 +50,54 @@ module ActiveRecord def test_type_cast_binary type = Type::Binary.new - assert_equal nil, type.type_cast_from_user(nil) - assert_equal "1", type.type_cast_from_user("1") - assert_equal 1, type.type_cast_from_user(1) + assert_equal nil, type.cast(nil) + assert_equal "1", type.cast("1") + assert_equal 1, type.cast(1) end def test_type_cast_time type = Type::Time.new - assert_equal nil, type.type_cast_from_user(nil) - assert_equal nil, type.type_cast_from_user('') - assert_equal nil, type.type_cast_from_user('ABC') + assert_equal nil, type.cast(nil) + assert_equal nil, type.cast('') + assert_equal nil, type.cast('ABC') time_string = Time.now.utc.strftime("%T") - assert_equal time_string, type.type_cast_from_user(time_string).strftime("%T") + assert_equal time_string, type.cast(time_string).strftime("%T") end def test_type_cast_datetime_and_timestamp type = Type::DateTime.new - assert_equal nil, type.type_cast_from_user(nil) - assert_equal nil, type.type_cast_from_user('') - assert_equal nil, type.type_cast_from_user(' ') - assert_equal nil, type.type_cast_from_user('ABC') + assert_equal nil, type.cast(nil) + assert_equal nil, type.cast('') + assert_equal nil, type.cast(' ') + assert_equal nil, type.cast('ABC') datetime_string = Time.now.utc.strftime("%FT%T") - assert_equal datetime_string, type.type_cast_from_user(datetime_string).strftime("%FT%T") + assert_equal datetime_string, type.cast(datetime_string).strftime("%FT%T") end def test_type_cast_date type = Type::Date.new - assert_equal nil, type.type_cast_from_user(nil) - assert_equal nil, type.type_cast_from_user('') - assert_equal nil, type.type_cast_from_user(' ') - assert_equal nil, type.type_cast_from_user('ABC') + assert_equal nil, type.cast(nil) + assert_equal nil, type.cast('') + assert_equal nil, type.cast(' ') + assert_equal nil, type.cast('ABC') date_string = Time.now.utc.strftime("%F") - assert_equal date_string, type.type_cast_from_user(date_string).strftime("%F") + assert_equal date_string, type.cast(date_string).strftime("%F") end def test_type_cast_duration_to_integer type = Type::Integer.new - assert_equal 1800, type.type_cast_from_user(30.minutes) - assert_equal 7200, type.type_cast_from_user(2.hours) + assert_equal 1800, type.cast(30.minutes) + assert_equal 7200, type.cast(2.hours) end def test_string_to_time_with_timezone [:utc, :local].each do |zone| with_timezone_config default: zone do type = Type::DateTime.new - assert_equal Time.utc(2013, 9, 4, 0, 0, 0), type.type_cast_from_user("Wed, 04 Sep 2013 03:00:00 EAT") + assert_equal Time.utc(2013, 9, 4, 0, 0, 0), type.cast("Wed, 04 Sep 2013 03:00:00 EAT") end end end |