@@ -96,26 +96,25 @@ def dump_tables
9696 Date ::DATE_FORMATS [ :default ] = Date ::DATE_FORMATS [ :db ]
9797 begin
9898 fixtures = tables . inject ( [ ] ) do |files , table_name |
99- table_klass = table_name . classify . constantize rescue nil
100- if table_klass && table_klass < ActiveRecord ::Base
101- rows = table_klass . unscoped do
102- table_klass . all . collect do |obj |
103- attrs = obj . attributes
104- attrs . inject ( { } ) do |hash , ( attr_name , value ) |
105- hash [ attr_name ] = serialized_value_if_needed ( table_klass , attr_name , value )
106- hash
107- end
108- end
109- end
110- else
111- rows = ActiveRecord ::Base . connection . select_all ( select_sql % { table : ActiveRecord ::Base . connection . quote_table_name ( table_name ) } )
99+ # Always create our own Class (inheriting from ActiveRecord) so that:
100+ # 1) We can always use ActiveRecord, even if the app doesn't have an
101+ # ActiveRecord model defined (e.g. some join tables)
102+ # 2) We don't have to worry about default scopes and other things that
103+ # may be present on the application's class.
104+ table_class = Class . new ( ActiveRecord ::Base ) { self . table_name = table_name }
105+
106+ records = select_scope_proc . call ( table_class ) . to_a
107+
108+ rows = records . map do |record |
109+ hashize_record_proc . call ( record )
112110 end
111+
113112 next files if rows . empty?
114113
115114 row_index = '000'
116- fixture_data = rows . inject ( { } ) do |hash , record |
117- hash . merge ( record_name ( record , table_name , row_index ) => record )
118- end
115+ fixture_data = rows . map do |row |
116+ [ record_name ( row , table_name , row_index ) , row ]
117+ end . to_h
119118
120119 write_fixture_file fixture_data , table_name
121120
@@ -127,22 +126,6 @@ def dump_tables
127126 say "Built #{ fixtures . to_sentence } "
128127 end
129128
130- def serialized_value_if_needed ( table_klass , attr_name , value )
131- if table_klass . respond_to? ( :type_for_attribute )
132- if table_klass . type_for_attribute ( attr_name ) . respond_to? ( :serialize )
133- table_klass . type_for_attribute ( attr_name ) . serialize ( value )
134- else
135- table_klass . type_for_attribute ( attr_name ) . type_cast_for_database ( value )
136- end
137- else
138- if table_klass . serialized_attributes . has_key? attr_name
139- table_klass . serialized_attributes [ attr_name ] . dump ( value )
140- else
141- value
142- end
143- end
144- end
145-
146129 def write_fixture_file ( fixture_data , table_name )
147130 File . open ( fixture_file ( table_name ) , 'w' ) do |file |
148131 file . write fixture_data . to_yaml
0 commit comments