Skip to content

Commit

Permalink
Added :bang_methods option to enable bang methods for each flag
Browse files Browse the repository at this point in the history
  • Loading branch information
miyagawa committed Jun 22, 2012
1 parent 1d4acea commit 7058407
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 0 deletions.
7 changes: 7 additions & 0 deletions README.rdoc
Expand Up @@ -163,6 +163,13 @@ on Spaceship:
Spaceship#electrolytes=
Spaceship#electrolytes_changed?

Opionally, you can set the <tt>:bang_methods</tt> option to true to enable the bang methods:

Spaceship#electrolytes!
Spaceship#not_electrolytes!

which respectively enables or disables the electrolytes flag.


===Generated named scopes

Expand Down
13 changes: 13 additions & 0 deletions lib/flag_shih_tzu.rb
Expand Up @@ -77,6 +77,19 @@ def self.not_#{flag_name}_condition
end
EVAL

# Define bancg methods when requested
if flag_options[colmn][:bang_methods]
class_eval <<-EVAL
def #{flag_name}!
enable_flag(:#{flag_name}, '#{colmn}')
end
def not_#{flag_name}!
disable_flag(:#{flag_name}, '#{colmn}')
end
EVAL
end

# Define the named scopes if the user wants them and AR supports it
if flag_options[colmn][:named_scopes] && respond_to?(named_scope_method)
class_eval <<-EVAL
Expand Down
15 changes: 15 additions & 0 deletions test/flag_shih_tzu_test.rb
Expand Up @@ -52,6 +52,13 @@ class SpaceshipWithBitOperatorQueryMode < ActiveRecord::Base
has_flags(1 => :warpdrive, 2 => :shields, :flag_query_mode => :bit_operator)
end

class SpaceshipWithBangMethods < ActiveRecord::Base
self.table_name = 'spaceships'
include FlagShihTzu

has_flags(1 => :warpdrive, 2 => :shields, :bang_methods => true)
end

class SpaceCarrier < Spaceship
end

Expand Down Expand Up @@ -538,4 +545,12 @@ def test_should_respect_true_values_like_active_record
assert !@spaceship.warpdrive
end
end

def test_should_define_bang_methods
spaceship = SpaceshipWithBangMethods.new
spaceship.warpdrive!
assert spaceship.warpdrive
spaceship.not_warpdrive!
assert !spaceship.warpdrive
end
end

0 comments on commit 7058407

Please sign in to comment.