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
|
# 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|
options = args.
integer = active_flags[column].to_i(args)
column_name = connection.quote_table_name_for_assignment(table_name, column)
if options[:op] == :and
where("#{column_name} & #{integer} = #{integer}")
else
where("#{column_name} & #{integer} > 0")
end
end
end
|