5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
# File 'lib/active_record/fixtures_override.rb', line 5
def table_rows
now = config.default_timezone == :utc ? Time.now.utc : Time.now
fixtures.delete('DEFAULTS')
rows = Hash.new { |h,table| h[table] = [] }
rows[table_name] = fixtures.map do |label, fixture|
row = fixture.to_hash
if model_class
if model_class.record_timestamps
timestamp_column_names.each do |c_name|
row[c_name] = now unless row.key?(c_name)
end
end
row.each do |key, value|
row[key] = value.gsub("$LABEL", label.to_s) if value.is_a?(String)
end
if has_primary_key_column? && !row.include?(primary_key_name)
row[primary_key_name] = ActiveRecord::FixtureSet.identify(label, primary_key_type)
end
reflection_class =
if row.include?(inheritance_column_name)
row[inheritance_column_name].constantize rescue model_class
else
model_class
end
reflection_class._reflections.each_value do |association|
case association.macro
when :belongs_to
fk_name = (association.options[:foreign_key] || "#{association.name}_id").to_s
if association.name.to_s != fk_name && value = row.delete(association.name.to_s)
if association.polymorphic? && value.sub!(/\s*\(([^\)]*)\)\s*$/, "")
row[association.foreign_type] = $1
end
fk_type = reflection_class.type_for_attribute(fk_name).type
row[fk_name] = ActiveRecord::FixtureSet.identify(value, fk_type)
end
when :has_many
if association.options[:through]
add_join_records(rows, row, HasManyThroughProxy.new(association))
end
end
end
end
row
end
rows
end
|