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
65
66
67
68
69
70
71
72
73
74
|
# File 'lib/flag_shih_tzu.rb', line 14
def has_flags(*args)
flag_hash, opts = parse_options(*args)
opts = {
:named_scopes => true,
:column => DEFAULT_COLUMN_NAME,
:flag_query_mode => :in_list
}.update(opts)
colmn = opts[:column]
return unless check_flag_column(colmn)
class_inheritable_hash :flag_options
write_inheritable_attribute(:flag_options, {}) if flag_options.nil?
flag_options[colmn] = opts
class_inheritable_hash :flag_mapping
write_inheritable_attribute(:flag_mapping, {}) if flag_mapping.nil?
flag_mapping[colmn] ||= {}
flag_hash.each do |flag_key, flag_name|
raise ArgumentError, "has_flags: flag keys should be positive integers, and #{flag_key} is not" unless is_valid_flag_key(flag_key)
raise ArgumentError, "has_flags: flag names should be symbols, and #{flag_name} is not" unless is_valid_flag_name(flag_name)
next if flag_mapping[colmn][flag_name] & (1 << (flag_key - 1)) raise ArgumentError, "has_flags: flag name #{flag_name} already defined, please choose different name" if method_defined?(flag_name)
flag_mapping[colmn][flag_name] = 1 << (flag_key - 1)
class_eval <<-EVAL
def #{flag_name}
flag_enabled?(:#{flag_name}, '#{colmn}')
end
def #{flag_name}?
flag_enabled?(:#{flag_name}, '#{colmn}')
end
def #{flag_name}=(value)
FlagShihTzu::TRUE_VALUES.include?(value) ? enable_flag(:#{flag_name}, '#{colmn}') : disable_flag(:#{flag_name}, '#{colmn}')
end
def self.#{flag_name}_condition(options = {})
sql_condition_for_flag(:#{flag_name}, '#{colmn}', true, options[:table_alias] || self.table_name)
end
def self.not_#{flag_name}_condition
sql_condition_for_flag(:#{flag_name}, '#{colmn}', false)
end
EVAL
if flag_options[colmn][:named_scopes] && respond_to?(named_scope_method)
class_eval <<-EVAL
#{named_scope_method} :#{flag_name}, lambda { { :conditions => #{flag_name}_condition } }
#{named_scope_method} :not_#{flag_name}, lambda { { :conditions => not_#{flag_name}_condition } }
EVAL
end
end
end
|