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
52
53
54
55
56
57
58
59
60
61
62
63
64
|
# File 'lib/active_flag.rb', line 11
def flag(column, keys)
unless respond_to?(:active_flags)
class_attribute :active_flags
self.active_flags = {}
end
raise "active_flags on :#{column} already defined!" if active_flags[column]
self.active_flags[column] = Definition.new(column, keys, self)
define_method column do
self.class.active_flags[column].to_value(self, read_attribute(column).to_i)
end
define_method "#{column}=" do |arg|
write_attribute column, self.class.active_flags[column].to_i(arg)
end
define_singleton_method column.to_s.pluralize do
active_flags[column]
end
define_singleton_method "where_#{column}" do |*args|
integer, column_name = send("_where_#{column}", *args)
where("#{column_name} & #{integer} > 0")
end
define_singleton_method "where_all_#{column}" do |*args|
integer, column_name = send("_where_#{column}", *args)
where("#{column_name} & #{integer} = #{integer}")
end
define_singleton_method "where_not_#{column}" do |*args|
integer, column_name = send("_where_#{column}", *args)
where("#{column_name} & #{integer} = 0")
end
define_singleton_method "where_not_all_#{column}" do |*args|
integer, column_name = send("_where_#{column}", *args)
where("#{column_name} & #{integer} < #{integer}")
end
define_singleton_method "_where_#{column}" do |*args|
return [
active_flags[column].to_i(args),
"#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(column)}"
]
end
end
|