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
|
# File 'lib/active_flag.rb', line 11
def flag(column, keys)
class << self
attr_reader :active_flags
end
@active_flags ||= {}
@active_flags[column] = Definition.new(column, keys, self)
define_method column do
self.class.active_flags[column].to_value(self, read_attribute(column))
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
if integer & (integer - 1) == 0 where("#{column_name} = #{integer}")
else
where("#{column_name} & #{integer} > 0")
end
end
end
end
|