diff options
author | Sean Griffin <sean@thoughtbot.com> | 2014-05-30 13:51:27 -0700 |
---|---|---|
committer | Sean Griffin <sean@thoughtbot.com> | 2014-05-31 05:43:21 -0700 |
commit | 6c5763b769a977c96f68008d5b50542f7d472526 (patch) | |
tree | 46fe0816db9e9bf8f96f25bfdd7e625f6bb001df | |
parent | 2c51c405e0eb75a1c3acc7f1ec0da4d4ee90cb30 (diff) | |
download | rails-6c5763b769a977c96f68008d5b50542f7d472526.tar.gz rails-6c5763b769a977c96f68008d5b50542f7d472526.tar.bz2 rails-6c5763b769a977c96f68008d5b50542f7d472526.zip |
Clear all caches calculated based on `@columns` when `@columns` changes
-rw-r--r-- | activerecord/lib/active_record/properties.rb | 10 | ||||
-rw-r--r-- | activerecord/test/cases/custom_properties_test.rb | 20 |
2 files changed, 27 insertions, 3 deletions
diff --git a/activerecord/lib/active_record/properties.rb b/activerecord/lib/active_record/properties.rb index 21ff906fec..e70f84808d 100644 --- a/activerecord/lib/active_record/properties.rb +++ b/activerecord/lib/active_record/properties.rb @@ -75,7 +75,7 @@ module ActiveRecord # store_listing.price_in_cents # => 1000 def property(name, cast_type, options = {}) name = name.to_s - clear_properties_cache + clear_caches_calculated_from_columns # Assign a new hash to ensure that subclasses do not share a hash self.user_provided_columns = user_provided_columns.merge(name => connection.new_column(name, options[:default], cast_type)) end @@ -92,7 +92,7 @@ module ActiveRecord def reset_column_information # :nodoc: super - clear_properties_cache + clear_caches_calculated_from_columns end private @@ -108,9 +108,13 @@ module ActiveRecord existing_columns + new_columns end - def clear_properties_cache + def clear_caches_calculated_from_columns @columns = nil @columns_hash = nil + @column_types = nil + @column_defaults = nil + @column_names = nil + @content_columns = nil end end end diff --git a/activerecord/test/cases/custom_properties_test.rb b/activerecord/test/cases/custom_properties_test.rb index a406704114..9ba1e83df6 100644 --- a/activerecord/test/cases/custom_properties_test.rb +++ b/activerecord/test/cases/custom_properties_test.rb @@ -87,5 +87,25 @@ module ActiveRecord column_names = OverloadedType.column_names assert_equal %w(id overloaded_float unoverloaded_float overloaded_string_with_limit string_with_default non_existent_decimal), column_names end + + def test_caches_are_cleared + klass = Class.new(OverloadedType) + + assert_equal 6, klass.columns.length + assert_not klass.columns_hash.key?('wibble') + assert_equal 6, klass.column_types.length + assert_equal 6, klass.column_defaults.length + assert_not klass.column_names.include?('wibble') + assert_equal 5, klass.content_columns.length + + klass.property :wibble, Type::Value.new + + assert_equal 7, klass.columns.length + assert klass.columns_hash.key?('wibble') + assert_equal 7, klass.column_types.length + assert_equal 7, klass.column_defaults.length + assert klass.column_names.include?('wibble') + assert_equal 6, klass.content_columns.length + end end end |