diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-01 11:30:09 -0800 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2011-02-01 14:25:46 -0800 |
commit | ee34b4cf346975d0aef7f26ef47ee2e4f3e13c37 (patch) | |
tree | a1e6cd21828eb2c70ef7312500f3cb6c147408af /activerecord/lib | |
parent | a6cf6ec98b58dc2a7d0586ccb6ef970d93f0bafc (diff) | |
download | rails-ee34b4cf346975d0aef7f26ef47ee2e4f3e13c37.tar.gz rails-ee34b4cf346975d0aef7f26ef47ee2e4f3e13c37.tar.bz2 rails-ee34b4cf346975d0aef7f26ef47ee2e4f3e13c37.zip |
share column cache among subclasses, only look up columns per AR::Base subclass once
Diffstat (limited to 'activerecord/lib')
-rw-r--r-- | activerecord/lib/active_record/base.rb | 28 | ||||
-rw-r--r-- | activerecord/lib/active_record/session_store.rb | 4 |
2 files changed, 17 insertions, 15 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 42226f83ea..f66b84935c 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -662,20 +662,16 @@ module ActiveRecord #:nodoc: # Returns an array of column objects for the table associated with this class. def columns - unless defined?(@columns) && @columns - @columns = connection.columns(table_name, "#{name} Columns") - @columns.each { |column| column.primary = column.name == primary_key } - end - @columns + @@columns[table_name] ||= connection.columns( + table_name, "#{name} Columns" + ).tap { |columns| + columns.each { |column| column.primary = column.name == primary_key } + } end # Returns a hash of column objects for the table associated with this class. def columns_hash - @@columns_cache[table_name] ||= Hash[columns.map { |column| [column.name, column] }] - end - - def columns_hash=(value) - @@columns_cache[table_name] = value + @@columns_hash[table_name] ||= Hash[columns.map { |column| [column.name, column] }] end # Returns an array of column names as strings. @@ -732,11 +728,16 @@ module ActiveRecord #:nodoc: def reset_column_information connection.clear_cache! undefine_attribute_methods - self.columns_hash = nil - @column_names = @columns = @content_columns = @dynamic_methods_hash = @inheritance_column = nil + reset_column_cache + @column_names = @content_columns = @dynamic_methods_hash = @inheritance_column = nil @arel_engine = @relation = @arel_table = nil end + def reset_column_cache # :nodoc: + @@columns.delete table_name + @@columns_hash.delete table_name + end + def attribute_method?(attribute) super || (table_exists? && column_names.include?(attribute.to_s.sub(/=$/, ''))) end @@ -1381,7 +1382,8 @@ MSG quoted_value end end - @@columns_cache = {} + @@columns_hash = {} + @@columns = {} public # New objects can be instantiated as either empty (pass no construction parameter) or pre-set with diff --git a/activerecord/lib/active_record/session_store.rb b/activerecord/lib/active_record/session_store.rb index 68d9f89edd..e3342f046f 100644 --- a/activerecord/lib/active_record/session_store.rb +++ b/activerecord/lib/active_record/session_store.rb @@ -59,12 +59,12 @@ module ActiveRecord end def drop_table! - self.columns_hash = nil + reset_column_cache connection.drop_table table_name end def create_table! - self.columns_hash = nil + reset_column_cache connection.create_table(table_name) do |t| t.string session_id_column, :limit => 255 t.text data_column_name |