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
|
# File 'lib/backframe/activerecord/acts_as_enum.rb', line 13
def acts_as_enum(*args)
field = args[0]
arguments = args[1]
if arguments.key?(:required)
validates_presence_of field
end
if arguments.key?(:default)
after_initialize "init_#{field}".to_sym, :if => Proc.new { |d| d.new_record? }
class_eval " def init_\#{field}\n self.\#{field} ||= '\#{arguments[:default]}'\n end\n EOV\n\n end\n\n if arguments.key?(:in)\n\n validates_inclusion_of field, :in => arguments[:in]\n\n class_eval <<-EOV\n scope :\#{field}_is, -> (*options) { where('\"\#{self.table_name}\".\"\#{field}\" IN (?)', (options.is_a?(Array) ? options : [options])) }\n\n def \#{field}_is?(*options)\n (options.is_a?(Array) ? options : [options]).map(&:to_s).include?(self.\#{field})\n end\n EOV\n\n class_eval <<-EOV\n scope :\#{field}_not, -> (*options) { where('\"\#{self.table_name}\".\"\#{field}\" NOT IN (?)', (options.is_a?(Array) ? options : [options])) }\n\n def \#{field}_not?(*options)\n !(options.is_a?(Array) ? options : [options]).map(&:to_s).include?(self.\#{field})\n end\n EOV\n\n end\n\nend\n"
|