Skip to content
This repository
Browse code

Added customized methods for manipulating flags

  • Loading branch information...
commit 00bf26620da7fade5aa68bf98b1ff7adc923f047 1 parent 1d4acea
cinconnu authored
50  lib/flag_shih_tzu.rb
@@ -77,6 +77,36 @@ def self.not_#{flag_name}_condition
77 77
           end
78 78
         EVAL
79 79
 
  80
+        if colmn != DEFAULT_COLUMN_NAME
  81
+          class_eval <<-EVAL
  82
+
  83
+            def all_#{colmn}
  84
+              all_flags('#{colmn}')
  85
+            end
  86
+
  87
+            def selected_#{colmn}
  88
+              selected_flags('#{colmn}')
  89
+            end
  90
+
  91
+            def select_all_#{colmn}
  92
+              select_all_flags('#{colmn}')
  93
+            end
  94
+
  95
+            def unselect_all_#{colmn}
  96
+              unselect_all_flags('#{colmn}')
  97
+            end
  98
+
  99
+            # useful for a form builder
  100
+            def selected_#{colmn}=(selected_flags)
  101
+              unselect_all_flags('#{colmn}')
  102
+              selected_flags.each do |selected_flag|
  103
+                enable_flag(selected_flag.to_sym, '#{colmn}') if selected_flag.present?
  104
+              end
  105
+            end
  106
+
  107
+          EVAL
  108
+        end
  109
+
80 110
         # Define the named scopes if the user wants them and AR supports it
81 111
         if flag_options[colmn][:named_scopes] && respond_to?(named_scope_method)
82 112
           class_eval <<-EVAL
@@ -207,6 +237,26 @@ def set_flags(value, colmn)
207 237
     self[colmn] = value
208 238
   end
209 239
 
  240
+  def all_flags(column)
  241
+    flag_mapping[column].keys
  242
+  end
  243
+
  244
+  def selected_flags(column)
  245
+    all_flags(column).map { |flag_name| self.send(flag_name) ? flag_name : nil }.compact
  246
+  end
  247
+
  248
+  def select_all_flags(column)
  249
+    all_flags(column).each do |flag|
  250
+      enable_flag(flag, column)
  251
+    end
  252
+  end
  253
+
  254
+  def unselect_all_flags(column)
  255
+    all_flags(column).each do |flag|
  256
+      disable_flag(flag, column)
  257
+    end
  258
+  end
  259
+
210 260
   private
211 261
 
212 262
     def get_bit_for(flag, colmn)
181  test/flag_shih_tzu_test.rb
@@ -294,6 +294,7 @@ class FlagShihTzuInstanceMethodsTest < Test::Unit::TestCase
294 294
   def setup
295 295
     @spaceship = Spaceship.new
296 296
     @big_spaceship = SpaceshipWith2CustomFlagsColumn.new
  297
+    @small_spaceship = SpaceshipWithCustomFlagsColumn.new
297 298
   end
298 299
 
299 300
   def test_should_enable_flag
@@ -376,6 +377,186 @@ def test_should_define_an_attribute_reader_method
376 377
     assert_equal false, @spaceship.warpdrive
377 378
   end
378 379
 
  380
+  # --------------------------------------------------
  381
+
  382
+  def test_should_define_an_all_flags_reader_method
  383
+    assert_equal [:electrolytes, :warpdrive, :shields], @spaceship.all_flags('flags')
  384
+  end
  385
+
  386
+  def test_should_define_a_selected_flags_reader_method
  387
+    assert_equal [], @spaceship.selected_flags('flags')
  388
+
  389
+    @spaceship.warpdrive = true
  390
+    assert_equal [:warpdrive], @spaceship.selected_flags('flags')
  391
+
  392
+    @spaceship.electrolytes = true
  393
+    assert_equal [:electrolytes, :warpdrive], @spaceship.selected_flags('flags')
  394
+
  395
+    @spaceship.warpdrive = false
  396
+    @spaceship.electrolytes = false
  397
+    assert_equal [], @spaceship.selected_flags('flags')
  398
+  end
  399
+
  400
+  def test_should_define_a_select_all_flags_method
  401
+    @spaceship.select_all_flags('flags')
  402
+    assert @spaceship.warpdrive
  403
+    assert @spaceship.shields
  404
+    assert @spaceship.electrolytes
  405
+  end
  406
+
  407
+  def test_should_define_an_unselect_all_flags_method
  408
+    @spaceship.warpdrive = true
  409
+    @spaceship.shields = true
  410
+    @spaceship.electrolytes = true
  411
+
  412
+    @spaceship.unselect_all_flags('flags')
  413
+
  414
+    assert !@spaceship.warpdrive
  415
+    assert !@spaceship.shields
  416
+    assert !@spaceship.electrolytes
  417
+  end
  418
+
  419
+  # --------------------------------------------------
  420
+
  421
+  def test_should_define_a_customized_all_flags_reader_method
  422
+    assert_equal [:hyperspace, :warpdrive], @small_spaceship.all_bits
  423
+  end
  424
+
  425
+  def test_should_define_a_customized_selected_flags_reader_method
  426
+    assert_equal [], @small_spaceship.selected_bits
  427
+
  428
+    @small_spaceship.warpdrive = true
  429
+    assert_equal [:warpdrive], @small_spaceship.selected_bits
  430
+
  431
+    @small_spaceship.hyperspace = true
  432
+    assert_equal [:hyperspace, :warpdrive], @small_spaceship.selected_bits
  433
+
  434
+    @small_spaceship.warpdrive = false
  435
+    @small_spaceship.hyperspace = false
  436
+    assert_equal [], @small_spaceship.selected_bits
  437
+  end
  438
+
  439
+  def test_should_define_a_customized_select_all_flags_method
  440
+    @small_spaceship.select_all_bits
  441
+    assert @small_spaceship.warpdrive
  442
+    assert @small_spaceship.hyperspace
  443
+  end
  444
+
  445
+  def test_should_define_a_customized_unselect_all_flags_method
  446
+    @small_spaceship.warpdrive = true
  447
+    @small_spaceship.hyperspace = true
  448
+
  449
+    @small_spaceship.unselect_all_bits
  450
+
  451
+    assert !@small_spaceship.warpdrive
  452
+    assert !@small_spaceship.hyperspace
  453
+  end
  454
+
  455
+  def test_should_define_a_customized_selected_flags_writer_method
  456
+    @small_spaceship.selected_bits = [:warpdrive]
  457
+    assert @small_spaceship.warpdrive
  458
+    assert !@small_spaceship.hyperspace
  459
+
  460
+    @small_spaceship.selected_bits = [:hyperspace]
  461
+    assert !@small_spaceship.warpdrive
  462
+    assert @small_spaceship.hyperspace
  463
+
  464
+    @small_spaceship.selected_bits = [:hyperspace, :warpdrive]
  465
+    assert @small_spaceship.warpdrive
  466
+    assert @small_spaceship.hyperspace
  467
+
  468
+    @small_spaceship.selected_bits = []
  469
+    assert !@small_spaceship.warpdrive
  470
+    assert !@small_spaceship.hyperspace
  471
+  end
  472
+
  473
+  # --------------------------------------------------
  474
+
  475
+  def test_should_define_a_customized_all_flags_reader_method_with_2_columns
  476
+    assert_equal [:hyperspace, :warpdrive], @big_spaceship.all_bits
  477
+    assert_equal [:dajanatroj, :jeanlucpicard], @big_spaceship.all_commanders
  478
+  end
  479
+
  480
+  def test_should_define_a_customized_selected_flags_reader_method_with_2_columns
  481
+    assert_equal [], @big_spaceship.selected_bits
  482
+    assert_equal [], @big_spaceship.selected_commanders
  483
+
  484
+    @big_spaceship.warpdrive = true
  485
+    @big_spaceship.jeanlucpicard = true
  486
+    assert_equal [:warpdrive], @big_spaceship.selected_bits
  487
+    assert_equal [:jeanlucpicard], @big_spaceship.selected_commanders
  488
+
  489
+    @big_spaceship.hyperspace = true
  490
+    @big_spaceship.hyperspace = true
  491
+    @big_spaceship.jeanlucpicard = true
  492
+    @big_spaceship.dajanatroj = true
  493
+    assert_equal [:hyperspace, :warpdrive], @big_spaceship.selected_bits
  494
+    assert_equal [:dajanatroj, :jeanlucpicard], @big_spaceship.selected_commanders
  495
+
  496
+    @big_spaceship.warpdrive = false
  497
+    @big_spaceship.hyperspace = false
  498
+    @big_spaceship.jeanlucpicard = false
  499
+    @big_spaceship.dajanatroj = false
  500
+    assert_equal [], @big_spaceship.selected_bits
  501
+    assert_equal [], @big_spaceship.selected_commanders
  502
+  end
  503
+
  504
+  def test_should_define_a_customized_select_all_flags_method_with_2_columns
  505
+    @big_spaceship.select_all_bits
  506
+    @big_spaceship.select_all_commanders
  507
+    assert @big_spaceship.warpdrive
  508
+    assert @big_spaceship.hyperspace
  509
+    assert @big_spaceship.jeanlucpicard
  510
+    assert @big_spaceship.dajanatroj
  511
+  end
  512
+
  513
+  def test_should_define_a_customized_unselect_all_flags_method_with_2_columns
  514
+    @big_spaceship.warpdrive = true
  515
+    @big_spaceship.hyperspace = true
  516
+    @big_spaceship.jeanlucpicard = true
  517
+    @big_spaceship.dajanatroj = true
  518
+
  519
+    @big_spaceship.unselect_all_bits
  520
+    @big_spaceship.unselect_all_commanders
  521
+
  522
+    assert !@big_spaceship.warpdrive
  523
+    assert !@big_spaceship.hyperspace
  524
+    assert !@big_spaceship.jeanlucpicard
  525
+    assert !@big_spaceship.dajanatroj
  526
+  end
  527
+
  528
+  def test_should_define_a_customized_selected_flags_writer_method_with_2_columns
  529
+    @big_spaceship.selected_bits = [:warpdrive]
  530
+    @big_spaceship.selected_commanders = [:jeanlucpicard]
  531
+    assert @big_spaceship.warpdrive
  532
+    assert !@big_spaceship.hyperspace
  533
+    assert @big_spaceship.jeanlucpicard
  534
+    assert !@big_spaceship.dajanatroj
  535
+
  536
+    @big_spaceship.selected_bits = [:hyperspace]
  537
+    @big_spaceship.selected_commanders = [:dajanatroj]
  538
+    assert !@big_spaceship.warpdrive
  539
+    assert @big_spaceship.hyperspace
  540
+    assert !@big_spaceship.jeanlucpicard
  541
+    assert @big_spaceship.dajanatroj
  542
+
  543
+    @big_spaceship.selected_bits = [:hyperspace, :warpdrive]
  544
+    @big_spaceship.selected_commanders = [:dajanatroj, :jeanlucpicard]
  545
+    assert @big_spaceship.warpdrive
  546
+    assert @big_spaceship.hyperspace
  547
+    assert @big_spaceship.jeanlucpicard
  548
+    assert @big_spaceship.dajanatroj
  549
+
  550
+    @big_spaceship.selected_bits = []
  551
+    @big_spaceship.selected_commanders = []
  552
+    assert !@big_spaceship.warpdrive
  553
+    assert !@big_spaceship.hyperspace
  554
+    assert !@big_spaceship.jeanlucpicard
  555
+    assert !@big_spaceship.dajanatroj
  556
+  end
  557
+
  558
+  # --------------------------------------------------
  559
+
379 560
   def test_should_define_an_attribute_reader_predicate_method
380 561
     assert_equal false, @spaceship.warpdrive?
381 562
   end

0 notes on commit 00bf266

Please sign in to comment.
Something went wrong with that request. Please try again.