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
|
# File 'lib/backframe/activerecord/acts_as_status.rb', line 11
def acts_as_status(*args)
field = args[0]
arguments = args[1]
if arguments.key?(:required)
validates_presence_of field
end
if arguments.key?(:default)
after_initialize "init_#{field}_status".to_sym, :if => Proc.new { |d| d.new_record? }
class_eval <<-EOV
def init_#{field}_status
self.#{field} ||= '#{arguments[:default]}'
end
EOV
end
if arguments.key?(:in)
validates_inclusion_of field, :in => arguments[:in]
arguments[:in].each do |status|
class_eval <<-EOV
scope :#{status}, -> { where(:status => '#{status}') }
def #{status}?
self.#{field} == '#{status}'
end
EOV
end
end
end
|