88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
|
# File 'lib/fixation/fixtures.rb', line 88
def fixture_methods
fixture_ids = @fixture_ids
class_names = @class_names
methods = Module.new do
def setup_fixtures(config = ActiveRecord::Base)
if run_in_transaction?
@@fixated_fixtures_applied ||= false
unless @@fixated_fixtures_applied
puts "#{Time.now} applying fixtures" if Fixation.trace
Fixation.apply_fixtures
@@fixated_fixtures_applied = true
puts "#{Time.now} applied fixtures" if Fixation.trace
end
else
@@fixated_fixtures_applied = false
end
super
end
fixture_ids.each do |fixture_name, fixtures|
begin
klass = class_names[fixture_name].constantize
rescue NameError
next
end
define_method(fixture_name) do |*fixture_names|
force_reload = fixture_names.pop if fixture_names.last == true || fixture_names.last == :reload
@fixture_cache[fixture_name] ||= {}
instances = fixture_names.map do |name|
id = fixtures[name.to_s]
raise StandardError, "No fixture named '#{name}' found for fixture set '#{fixture_name}'" if id.nil?
@fixture_cache[fixture_name].delete(name) if force_reload
@fixture_cache[fixture_name][name] ||= klass.find(id)
end
instances.size == 1 ? instances.first : instances
end
private fixture_name
name_method = :"#{fixture_name.singularize}_fixture_name_for_id"
define_method(name_method) do |fixture_id|
fixture_id = fixture_id.to_param
fixtures.detect do |name, id|
break name.to_sym if id.to_param == fixture_id
end or raise ArgumentError, "No fixture with ID #{fixture_id.inspect} in table #{fixture_name}"
end
private name_method
end
end
end
|