10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
# File 'lib/rails_toggleable/toggleable_model_methods.rb', line 10
def toggleable_field(field_name, options = {})
field_sym = field_name.to_sym
field_str = field_name.to_s
default_value = options.fetch(:default, true)
scope field_sym, -> { where(field_sym => true) }
scope :"not_#{field_sym}", -> { where(field_sym => false) }
define_method "#{field_str}?" do
self.send(field_sym)
end
define_method "enable_#{field_str}!" do
update!(field_sym => true)
end
define_method "disable_#{field_str}!" do
update!(field_sym => false)
end
define_method "toggle_#{field_str}!" do
update!(field_sym => !self.send(field_sym))
end
end
|