2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
# File 'lib/acts_as_status.rb', line 2
def acts_as_status( field, valid_values )
status_variable = '@' + field.to_s.upcase() + '_STATUS'
instance_variable_set status_variable, valid_values
self.validates field, :inclusion => { :in => instance_variable_get(status_variable) }
valid_values.each do |valid_value|
define_method "#{valid_value}?" do
self.send("#{field}") == valid_value
end
(class << self; self; end).send(:define_method, "only_#{valid_value}") do
where(["#{field} = ?", valid_value])
end
end
(class << self; self; end).send(:define_method, "available_#{field}") do
valid_values
end
end
|