diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 0e0b47f584..16d70e5bf7 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Added TextHelper#cycle to cycle over an array of values on each hit (useful for alternating row colors etc) #2154 [dave-ml@dribin.org] + * Ensure that request.path never returns nil. Closes #1675 [Nicholas Seckar] * Add ability to specify Route Regexps for controllers. Closes #1917. [Sebastian Kanthak] diff --git a/actionpack/lib/action_view/helpers/text_helper.rb b/actionpack/lib/action_view/helpers/text_helper.rb index 217cf6c9f8..413f32532d 100644 --- a/actionpack/lib/action_view/helpers/text_helper.rb +++ b/actionpack/lib/action_view/helpers/text_helper.rb @@ -189,9 +189,91 @@ module ActionView html end + + # Returns a Cycle object whose to_s value cycles through items of an + # array every time it is called. This can be used to alternate classes + # for table rows: + # + # <%- for item in @items do -%> + #
crazy\n
cross\n
platform linebreaks
A paragraph
\n\nand another one!
", simple_format("A paragraph\n\nand another one!") @@ -137,4 +143,87 @@ class TextHelperTest < Test::Unit::TestCase assert_equal %{href="javascript:bang" foo, bar}, result end + def test_cycle_class + value = Cycle.new("one", 2, "3") + assert_equal("one", value.to_s) + assert_equal("2", value.to_s) + assert_equal("3", value.to_s) + assert_equal("one", value.to_s) + value.reset + assert_equal("one", value.to_s) + assert_equal("2", value.to_s) + assert_equal("3", value.to_s) + end + + def test_cycle_class_with_no_arguments + assert_raise(ArgumentError) { value = Cycle.new() } + end + + def test_cycle + assert_equal("one", cycle("one", 2, "3")) + assert_equal("2", cycle("one", 2, "3")) + assert_equal("3", cycle("one", 2, "3")) + assert_equal("one", cycle("one", 2, "3")) + assert_equal("2", cycle("one", 2, "3")) + assert_equal("3", cycle("one", 2, "3")) + end + + def test_cycle_with_no_arguments + assert_raise(ArgumentError) { value = cycle() } + end + + def test_cycle_resets_with_new_values + assert_equal("even", cycle("even", "odd")) + assert_equal("odd", cycle("even", "odd")) + assert_equal("even", cycle("even", "odd")) + assert_equal("1", cycle(1, 2, 3)) + assert_equal("2", cycle(1, 2, 3)) + assert_equal("3", cycle(1, 2, 3)) + assert_equal("1", cycle(1, 2, 3)) + end + + def test_named_cycles + assert_equal("1", cycle(1, 2, 3, :name => "numbers")) + assert_equal("red", cycle("red", "blue", :name => "colors")) + assert_equal("2", cycle(1, 2, 3, :name => "numbers")) + assert_equal("blue", cycle("red", "blue", :name => "colors")) + assert_equal("3", cycle(1, 2, 3, :name => "numbers")) + assert_equal("red", cycle("red", "blue", :name => "colors")) + end + + def test_default_named_cycle + assert_equal("1", cycle(1, 2, 3)) + assert_equal("2", cycle(1, 2, 3, :name => "default")) + assert_equal("3", cycle(1, 2, 3)) + end + + def test_reset_cycle + assert_equal("1", cycle(1, 2, 3)) + assert_equal("2", cycle(1, 2, 3)) + reset_cycle + assert_equal("1", cycle(1, 2, 3)) + end + + def test_reset_unknown_cycle + reset_cycle("colors") + end + + def test_recet_named_cycle + assert_equal("1", cycle(1, 2, 3, :name => "numbers")) + assert_equal("red", cycle("red", "blue", :name => "colors")) + reset_cycle("numbers") + assert_equal("1", cycle(1, 2, 3, :name => "numbers")) + assert_equal("blue", cycle("red", "blue", :name => "colors")) + assert_equal("2", cycle(1, 2, 3, :name => "numbers")) + assert_equal("red", cycle("red", "blue", :name => "colors")) + end + + def test_cycle_no_instance_variable_clashes + @cycles = %w{Specialized Fuji Giant} + assert_equal("red", cycle("red", "blue")) + assert_equal("blue", cycle("red", "blue")) + assert_equal("red", cycle("red", "blue")) + assert_equal(%w{Specialized Fuji Giant}, @cycles) + end + end