Skip to content
This repository
Browse code

Defaults hash can go on the model

  • Loading branch information...
commit dee94d8ab5b7d27653052441a3b408db380dd784 1 parent 0b08ff7
Jon Leighton jonleighton authored
15 activerecord/lib/active_record/connection_adapters/schema_cache.rb
@@ -2,7 +2,6 @@ module ActiveRecord
2 2 module ConnectionAdapters
3 3 class SchemaCache
4 4 attr_reader :columns, :columns_hash, :primary_keys, :tables
5   - attr_reader :column_defaults
6 5 attr_reader :connection
7 6
8 7 def initialize(conn)
@@ -19,12 +18,6 @@ def initialize(conn)
19 18 }]
20 19 end
21 20
22   - @column_defaults = Hash.new do |h, table_name|
23   - h[table_name] = Hash[columns[table_name].map { |col|
24   - [col.name, col.default]
25   - }]
26   - end
27   -
28 21 @primary_keys = Hash.new do |h, table_name|
29 22 h[table_name] = table_exists?(table_name) ?
30 23 conn.primary_key(table_name) : 'id'
@@ -38,15 +31,10 @@ def table_exists?(name)
38 31 @tables[name] = connection.table_exists?(name)
39 32 end
40 33
41   - # Clears out internal caches:
42   - #
43   - # * columns
44   - # * columns_hash
45   - # * tables
  34 + # Clears out internal caches
46 35 def clear!
47 36 @columns.clear
48 37 @columns_hash.clear
49   - @column_defaults.clear
50 38 @tables.clear
51 39 end
52 40
@@ -54,7 +42,6 @@ def clear!
54 42 def clear_table_cache!(table_name)
55 43 @columns.delete table_name
56 44 @columns_hash.delete table_name
57   - @column_defaults.delete table_name
58 45 @primary_keys.delete table_name
59 46 @tables.delete table_name
60 47 end
2  activerecord/lib/active_record/model_schema.rb
@@ -236,7 +236,7 @@ def columns_hash
236 236 # Returns a hash where the keys are column names and the values are
237 237 # default values when instantiating the AR object for this table.
238 238 def column_defaults
239   - @column_defaults ||= connection.schema_cache.column_defaults[table_name]
  239 + @column_defaults ||= Hash[columns.map { |c| [c.name, c.default] }]
240 240 end
241 241
242 242 # Returns an array of column names as strings.

0 comments on commit dee94d8

Please sign in to comment.
Something went wrong with that request. Please try again.