diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-05-28 10:11:57 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-05-28 11:34:20 -0700 |
commit | c59e01a108b37ad74c9ed2e8fb022e27a9f95213 (patch) | |
tree | 5a5918669692dd7c2413926a7937f78e314363ba | |
parent | abd6461e2881a09cb7b719ffb016c0c85d89ffe0 (diff) | |
download | rails-c59e01a108b37ad74c9ed2e8fb022e27a9f95213.tar.gz rails-c59e01a108b37ad74c9ed2e8fb022e27a9f95213.tar.bz2 rails-c59e01a108b37ad74c9ed2e8fb022e27a9f95213.zip |
Ensure custom properties work correctly with inheritance
-rw-r--r-- | activerecord/lib/active_record/properties.rb | 12 | ||||
-rw-r--r-- | activerecord/test/cases/custom_properties_test.rb | 19 |
2 files changed, 26 insertions, 5 deletions
diff --git a/activerecord/lib/active_record/properties.rb b/activerecord/lib/active_record/properties.rb index a25c1cec58..aee1116511 100644 --- a/activerecord/lib/active_record/properties.rb +++ b/activerecord/lib/active_record/properties.rb @@ -4,6 +4,11 @@ module ActiveRecord Type = ConnectionAdapters::Type + included do + class_attribute :user_provided_columns, instance_accessor: false # :internal + self.user_provided_columns = {} + end + module ClassMethods # Defines or overrides a property on this model. This allows customization of # Active Record's type casting behavior, as well as adding support for user defined @@ -59,7 +64,8 @@ module ActiveRecord # store_listing.price_in_cents # => 1000 def property(name, cast_type) name = name.to_s - user_provided_columns[name] = ConnectionAdapters::Column.new(name, nil, cast_type) + # Assign a new hash to ensure that subclasses do not share a hash + self.user_provided_columns = user_provided_columns.merge(name => ConnectionAdapters::Column.new(name, nil, cast_type)) end # Returns an array of column objects for the table associated with this class. @@ -81,10 +87,6 @@ module ActiveRecord private - def user_provided_columns - @user_provided_columns ||= {} - end - def add_user_provided_columns(schema_columns) schema_columns.reject { |column| user_provided_columns.key? column.name diff --git a/activerecord/test/cases/custom_properties_test.rb b/activerecord/test/cases/custom_properties_test.rb index 9598f0299c..9910e4042c 100644 --- a/activerecord/test/cases/custom_properties_test.rb +++ b/activerecord/test/cases/custom_properties_test.rb @@ -6,6 +6,13 @@ class OverloadedType < ActiveRecord::Base property :non_existent_decimal, Type::Decimal.new end +class ChildOfOverloadedType < OverloadedType +end + +class GrandchildOfOverloadedType < ChildOfOverloadedType + property :overloaded_float, Type::Float.new +end + class UnoverloadedType < ActiveRecord::Base self.table_name = 'overloaded_types' end @@ -60,5 +67,17 @@ module ActiveRecord assert_nil data.overloaded_float assert unoverloaded_data.overloaded_float end + + def test_children_inherit_custom_properties + data = ChildOfOverloadedType.new(overloaded_float: '4.4') + + assert_equal 4, data.overloaded_float + end + + def test_children_can_override_parents + data = GrandchildOfOverloadedType.new(overloaded_float: '4.4') + + assert_equal 4.4, data.overloaded_float + end end end |