From 6c5763b769a977c96f68008d5b50542f7d472526 Mon Sep 17 00:00:00 2001 From: Sean Griffin Date: Fri, 30 May 2014 13:51:27 -0700 Subject: [PATCH] Clear all caches calculated based on `@columns` when `@columns` changes --- activerecord/lib/active_record/properties.rb | 10 +++++++--- .../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 ClassMethods # 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 @@ def columns_hash def reset_column_information # :nodoc: super - clear_properties_cache + clear_caches_calculated_from_columns end private @@ -108,9 +108,13 @@ def add_user_provided_columns(schema_columns) 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 @@ def test_overloading_properties_does_not_change_column_order 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 -- GitLab