Permalink
Browse files

Added customized methods for manipulating flags

  • Loading branch information...
1 parent 1d4acea commit 00bf26620da7fade5aa68bf98b1ff7adc923f047 @cinconnu cinconnu committed Jun 21, 2012
Showing with 231 additions and 0 deletions.
  1. +50 −0 lib/flag_shih_tzu.rb
  2. +181 −0 test/flag_shih_tzu_test.rb
View
@@ -77,6 +77,36 @@ def self.not_#{flag_name}_condition
end
EVAL
+ if colmn != DEFAULT_COLUMN_NAME
+ class_eval <<-EVAL
+
+ def all_#{colmn}
+ all_flags('#{colmn}')
+ end
+
+ def selected_#{colmn}
+ selected_flags('#{colmn}')
+ end
+
+ def select_all_#{colmn}
+ select_all_flags('#{colmn}')
+ end
+
+ def unselect_all_#{colmn}
+ unselect_all_flags('#{colmn}')
+ end
+
+ # useful for a form builder
+ def selected_#{colmn}=(selected_flags)
+ unselect_all_flags('#{colmn}')
+ selected_flags.each do |selected_flag|
+ enable_flag(selected_flag.to_sym, '#{colmn}') if selected_flag.present?
+ end
+ 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
@@ -207,6 +237,26 @@ def set_flags(value, colmn)
self[colmn] = value
end
+ def all_flags(column)
+ flag_mapping[column].keys
+ end
+
+ def selected_flags(column)
+ all_flags(column).map { |flag_name| self.send(flag_name) ? flag_name : nil }.compact
+ end
+
+ def select_all_flags(column)
+ all_flags(column).each do |flag|
+ enable_flag(flag, column)
+ end
+ end
+
+ def unselect_all_flags(column)
+ all_flags(column).each do |flag|
+ disable_flag(flag, column)
+ end
+ end
+
private
def get_bit_for(flag, colmn)
View
@@ -294,6 +294,7 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
def setup
@spaceship = Spaceship.new
@big_spaceship = SpaceshipWith2CustomFlagsColumn.new
+ @small_spaceship = SpaceshipWithCustomFlagsColumn.new
end
def test_should_enable_flag
@@ -376,6 +377,186 @@ def test_should_define_an_attribute_reader_method
assert_equal false, @spaceship.warpdrive
end
+ # --------------------------------------------------
+
+ def test_should_define_an_all_flags_reader_method
+ assert_equal [:electrolytes, :warpdrive, :shields], @spaceship.all_flags('flags')
+ end
+
+ def test_should_define_a_selected_flags_reader_method
+ assert_equal [], @spaceship.selected_flags('flags')
+
+ @spaceship.warpdrive = true
+ assert_equal [:warpdrive], @spaceship.selected_flags('flags')
+
+ @spaceship.electrolytes = true
+ assert_equal [:electrolytes, :warpdrive], @spaceship.selected_flags('flags')
+
+ @spaceship.warpdrive = false
+ @spaceship.electrolytes = false
+ assert_equal [], @spaceship.selected_flags('flags')
+ end
+
+ def test_should_define_a_select_all_flags_method
+ @spaceship.select_all_flags('flags')
+ assert @spaceship.warpdrive
+ assert @spaceship.shields
+ assert @spaceship.electrolytes
+ end
+
+ def test_should_define_an_unselect_all_flags_method
+ @spaceship.warpdrive = true
+ @spaceship.shields = true
+ @spaceship.electrolytes = true
+
+ @spaceship.unselect_all_flags('flags')
+
+ assert !@spaceship.warpdrive
+ assert !@spaceship.shields
+ assert !@spaceship.electrolytes
+ end
+
+ # --------------------------------------------------
+
+ def test_should_define_a_customized_all_flags_reader_method
+ assert_equal [:hyperspace, :warpdrive], @small_spaceship.all_bits
+ end
+
+ def test_should_define_a_customized_selected_flags_reader_method
+ assert_equal [], @small_spaceship.selected_bits
+
+ @small_spaceship.warpdrive = true
+ assert_equal [:warpdrive], @small_spaceship.selected_bits
+
+ @small_spaceship.hyperspace = true
+ assert_equal [:hyperspace, :warpdrive], @small_spaceship.selected_bits
+
+ @small_spaceship.warpdrive = false
+ @small_spaceship.hyperspace = false
+ assert_equal [], @small_spaceship.selected_bits
+ end
+
+ def test_should_define_a_customized_select_all_flags_method
+ @small_spaceship.select_all_bits
+ assert @small_spaceship.warpdrive
+ assert @small_spaceship.hyperspace
+ end
+
+ def test_should_define_a_customized_unselect_all_flags_method
+ @small_spaceship.warpdrive = true
+ @small_spaceship.hyperspace = true
+
+ @small_spaceship.unselect_all_bits
+
+ assert !@small_spaceship.warpdrive
+ assert !@small_spaceship.hyperspace
+ end
+
+ def test_should_define_a_customized_selected_flags_writer_method
+ @small_spaceship.selected_bits = [:warpdrive]
+ assert @small_spaceship.warpdrive
+ assert !@small_spaceship.hyperspace
+
+ @small_spaceship.selected_bits = [:hyperspace]
+ assert !@small_spaceship.warpdrive
+ assert @small_spaceship.hyperspace
+
+ @small_spaceship.selected_bits = [:hyperspace, :warpdrive]
+ assert @small_spaceship.warpdrive
+ assert @small_spaceship.hyperspace
+
+ @small_spaceship.selected_bits = []
+ assert !@small_spaceship.warpdrive
+ assert !@small_spaceship.hyperspace
+ end
+
+ # --------------------------------------------------
+
+ def test_should_define_a_customized_all_flags_reader_method_with_2_columns
+ assert_equal [:hyperspace, :warpdrive], @big_spaceship.all_bits
+ assert_equal [:dajanatroj, :jeanlucpicard], @big_spaceship.all_commanders
+ end
+
+ def test_should_define_a_customized_selected_flags_reader_method_with_2_columns
+ assert_equal [], @big_spaceship.selected_bits
+ assert_equal [], @big_spaceship.selected_commanders
+
+ @big_spaceship.warpdrive = true
+ @big_spaceship.jeanlucpicard = true
+ assert_equal [:warpdrive], @big_spaceship.selected_bits
+ assert_equal [:jeanlucpicard], @big_spaceship.selected_commanders
+
+ @big_spaceship.hyperspace = true
+ @big_spaceship.hyperspace = true
+ @big_spaceship.jeanlucpicard = true
+ @big_spaceship.dajanatroj = true
+ assert_equal [:hyperspace, :warpdrive], @big_spaceship.selected_bits
+ assert_equal [:dajanatroj, :jeanlucpicard], @big_spaceship.selected_commanders
+
+ @big_spaceship.warpdrive = false
+ @big_spaceship.hyperspace = false
+ @big_spaceship.jeanlucpicard = false
+ @big_spaceship.dajanatroj = false
+ assert_equal [], @big_spaceship.selected_bits
+ assert_equal [], @big_spaceship.selected_commanders
+ end
+
+ def test_should_define_a_customized_select_all_flags_method_with_2_columns
+ @big_spaceship.select_all_bits
+ @big_spaceship.select_all_commanders
+ assert @big_spaceship.warpdrive
+ assert @big_spaceship.hyperspace
+ assert @big_spaceship.jeanlucpicard
+ assert @big_spaceship.dajanatroj
+ end
+
+ def test_should_define_a_customized_unselect_all_flags_method_with_2_columns
+ @big_spaceship.warpdrive = true
+ @big_spaceship.hyperspace = true
+ @big_spaceship.jeanlucpicard = true
+ @big_spaceship.dajanatroj = true
+
+ @big_spaceship.unselect_all_bits
+ @big_spaceship.unselect_all_commanders
+
+ assert !@big_spaceship.warpdrive
+ assert !@big_spaceship.hyperspace
+ assert !@big_spaceship.jeanlucpicard
+ assert !@big_spaceship.dajanatroj
+ end
+
+ def test_should_define_a_customized_selected_flags_writer_method_with_2_columns
+ @big_spaceship.selected_bits = [:warpdrive]
+ @big_spaceship.selected_commanders = [:jeanlucpicard]
+ assert @big_spaceship.warpdrive
+ assert !@big_spaceship.hyperspace
+ assert @big_spaceship.jeanlucpicard
+ assert !@big_spaceship.dajanatroj
+
+ @big_spaceship.selected_bits = [:hyperspace]
+ @big_spaceship.selected_commanders = [:dajanatroj]
+ assert !@big_spaceship.warpdrive
+ assert @big_spaceship.hyperspace
+ assert !@big_spaceship.jeanlucpicard
+ assert @big_spaceship.dajanatroj
+
+ @big_spaceship.selected_bits = [:hyperspace, :warpdrive]
+ @big_spaceship.selected_commanders = [:dajanatroj, :jeanlucpicard]
+ assert @big_spaceship.warpdrive
+ assert @big_spaceship.hyperspace
+ assert @big_spaceship.jeanlucpicard
+ assert @big_spaceship.dajanatroj
+
+ @big_spaceship.selected_bits = []
+ @big_spaceship.selected_commanders = []
+ assert !@big_spaceship.warpdrive
+ assert !@big_spaceship.hyperspace
+ assert !@big_spaceship.jeanlucpicard
+ assert !@big_spaceship.dajanatroj
+ end
+
+ # --------------------------------------------------
+
def test_should_define_an_attribute_reader_predicate_method
assert_equal false, @spaceship.warpdrive?
end

0 comments on commit 00bf266

Please sign in to comment.