3
4
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
|
# File 'lib/status_accessor.rb', line 3
def status_accessor(*args)
options = args.last.is_a?(Hash) ? args.pop : {}
if args[1].is_a?(Array)
field_name, statuses = args
else
field_name = :status
statuses = args
end
statuses.each do |s|
state = options[:transform] ? s.to_s.send(options[:transform]) : s.to_s.upcase
define_method("#{s.to_s.downcase}!") do
self.send("#{field_name}=", state)
end
define_method("#{s.to_s.downcase}?") do
self.send(field_name) == state
end
define_method("update_to_#{s.to_s.downcase}!") do
self.update_attribute(field_name, state)
end
if self.respond_to?(:named_scope) named_scope s.to_s.downcase.to_sym, :conditions => {:"#{field_name}" => state}
elsif self.respond_to?(:scope) scope s.to_s.downcase.to_sym, :conditions => {:"#{field_name}" => state}
end
end
class_variable_set "@@#{field_name}_strings", statuses.collect {|s| s.to_s.upcase}
(class << self; self; end).send(:define_method, "#{field_name}_strings") do
class_variable_get "@@#{field_name}_strings"
end
end
|