public
Description: Ruby on Rails
Homepage: http://rubyonrails.org
Clone URL: git://github.com/rails/rails.git
Added TextHelper#current_cycle to return the current cycle for better design 
options.

[#417 state:resolved]

Signed-off-by: Jeremy Kemper <jeremy@bitsweat.net>
metaskills (author)
Sat Jun 14 11:06:27 -0700 2008
jeremy (committer)
Wed Aug 27 23:06:20 -0700 2008
commit  f277e1d8fddfa417104c6fe095c15559f0c8713d
tree    39865611a3e8b3b8d3df441c1c646dad30c03edb
parent  e42a235dd18a39ccc83382365088de96f24fa236
...
1
2
 
 
3
4
5
...
1
2
3
4
5
6
7
0
@@ -1,5 +1,7 @@
0
 *Edge*
0
 
0
+* Introduce current_cycle helper method to return the current value without bumping the cycle.  #417 [Ken Collins]
0
+
0
 * Allow polymorphic_url helper to take url options. #880 [Tarmo Tänav]
0
 
0
 * Switched integration test runner to use Rack processor instead of CGI [Josh Peek]
...
448
449
450
451
452
 
 
 
 
453
454
455
...
496
497
498
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
499
500
501
...
532
533
534
 
 
 
 
535
536
537
 
538
539
 
 
 
 
 
 
 
 
 
 
 
 
 
 
540
541
542
...
448
449
450
 
 
451
452
453
454
455
456
457
...
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
...
551
552
553
554
555
556
557
558
559
 
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
0
@@ -448,8 +448,10 @@ module ActionView
0
       # array every time it is called. This can be used for example, to alternate
0
       # classes for table rows.  You can use named cycles to allow nesting in loops.
0
       # Passing a Hash as the last parameter with a <tt>:name</tt> key will create a
0
-      # named cycle.  You can manually reset a cycle by calling reset_cycle and passing the
0
-      # name of the cycle.
0
+      # named cycle. The default name for a cycle without a +:name+ key is
0
+      # <tt>"default"</tt>. You can manually reset a cycle by calling reset_cycle
0
+      # and passing the name of the cycle. The current cycle string can be obtained
0
+      # anytime using the current_cycle method.
0
       #
0
       # ==== Examples
0
       #   # Alternate CSS classes for even and odd numbers...
0
@@ -496,6 +498,23 @@ module ActionView
0
         return cycle.to_s
0
       end
0
 
0
+      # Returns the current cycle string after a cycle has been started. Useful
0
+      # for complex table highlighing or any other design need which requires
0
+      # the current cycle string in more than one place.
0
+      #
0
+      # ==== Example
0
+      #   # Alternate background colors
0
+      #   @items = [1,2,3,4]
0
+      #   <% @items.each do |item| %>
0
+      #     <div style="background-color:<%= cycle("red","white","blue") %>">
0
+      #       <span style="background-color:<%= current_cycle %><%= item %></span>
0
+      #     </div>
0
+      #   <% end %>
0
+      def current_cycle(name = "default")
0
+        cycle = get_cycle(name)
0
+        cycle.current_value unless cycle.nil?
0
+      end
0
+
0
       # Resets a cycle so that it starts from the first element the next time
0
       # it is called. Pass in +name+ to reset a named cycle.
0
       #
0
@@ -532,11 +551,29 @@ module ActionView
0
           @index = 0
0
         end
0
 
0
+        def current_value
0
+          @values[previous_index].to_s
0
+        end
0
+
0
         def to_s
0
           value = @values[@index].to_s
0
-          @index = (@index + 1) % @values.size
0
+          @index = next_index
0
           return value
0
         end
0
+
0
+        private
0
+
0
+        def next_index
0
+          step_index(1)
0
+        end
0
+
0
+        def previous_index
0
+          step_index(-1)
0
+        end
0
+
0
+        def step_index(n)
0
+          (@index + n) % @values.size
0
+        end
0
       end
0
 
0
       private
...
369
370
371
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
372
373
374
...
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
0
@@ -369,6 +369,40 @@ class TextHelperTest < ActionView::TestCase
0
     assert_equal("red", cycle("red", "blue", :name => "colors"))
0
   end
0
 
0
+  def test_current_cycle_with_default_name
0
+    cycle("even","odd")
0
+    assert_equal "even", current_cycle
0
+    cycle("even","odd")
0
+    assert_equal "odd", current_cycle
0
+    cycle("even","odd")
0
+    assert_equal "even", current_cycle
0
+  end
0
+
0
+  def test_current_cycle_with_named_cycles
0
+    cycle("red", "blue", :name => "colors")
0
+    assert_equal "red", current_cycle("colors")
0
+    cycle("red", "blue", :name => "colors")
0
+    assert_equal "blue", current_cycle("colors")
0
+    cycle("red", "blue", :name => "colors")
0
+    assert_equal "red", current_cycle("colors")
0
+  end
0
+
0
+  def test_current_cycle_safe_call
0
+    assert_nothing_raised { current_cycle }
0
+    assert_nothing_raised { current_cycle("colors") }
0
+  end
0
+
0
+  def test_current_cycle_with_more_than_two_names
0
+    cycle(1,2,3)
0
+    assert_equal "1", current_cycle
0
+    cycle(1,2,3)
0
+    assert_equal "2", current_cycle
0
+    cycle(1,2,3)
0
+    assert_equal "3", current_cycle
0
+    cycle(1,2,3)
0
+    assert_equal "1", current_cycle
0
+  end
0
+
0
   def test_default_named_cycle
0
     assert_equal("1", cycle(1, 2, 3))
0
     assert_equal("2", cycle(1, 2, 3, :name => "default"))

Comments