Skip to content

Commit

Permalink
let <r:cycle /> act as a numeric counter
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnflyer committed Feb 25, 2011
1 parent 40ec427 commit a0cc0b2
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 14 deletions.
1 change: 1 addition & 0 deletions CHANGELOG
Expand Up @@ -2,6 +2,7 @@

=== Edge

* Use <r:cycle /> for a numeric counter [Jim Gay, Dirk Kelly]
* String format breadcrumb tag in archive pages [Oriol Gual]
* Fix bug with deletion of page parts [Josh French]
* Unroll PageMenu into core [Josh French]
Expand Down
1 change: 1 addition & 0 deletions CONTRIBUTORS
Expand Up @@ -5,6 +5,7 @@ core:

=== Edge

* Dirk Kelly
* Oriol Gual
* Petrik de Heus
* Gert Goet
Expand Down
41 changes: 31 additions & 10 deletions app/models/standard_tags.rb
Expand Up @@ -492,23 +492,44 @@ class RequiredAttributeError < StandardError; end
end

desc %{
Renders one of the passed values based on a global cycle counter. Use the @reset@
attribute to reset the cycle to the beginning. Use the @name@ attribute to track
multiple cycles; the default is @cycle@.
Renders a counter value or one of the values given based on a global cycle counter.
To get a numeric counter just use the tag, or specify a start value with @start@.
Use the @reset@ attribute to reset the cycle to the beginning. Using @reset@ on a
numbered cycle will begin at 0. Use the @name@ attribute to track multiple cycles;
the default is @cycle@.
*Usage:*
<pre><code><r:cycle values="first, second, third" [reset="true|false"] [name="cycle"] /></code></pre>
<pre><code><r:cycle [values="first, second, third"] [reset="true|false"] [name="cycle"] [start="second"] /></code></pre>
<pre><code><r:cycle start="3" /></code></pre>
}
tag 'cycle' do |tag|
required_attr(tag, 'values')
cycle = (tag.globals.cycle ||= {})
values = tag.attr['values'].split(",").collect(&:strip)
if tag.attr['values']
values = tag.attr['values'].split(",").collect(&:strip)
end
start = tag.attr['start']
cycle_name = tag.attr['name'] || 'cycle'
current_index = (cycle[cycle_name] ||= 0)
current_index = 0 if tag.attr['reset'] == 'true'
cycle[cycle_name] = (current_index + 1) % values.size
values[current_index]
if values
if start
current_index = (cycle[cycle_name] ||= values.index(start))
else
current_index = (cycle[cycle_name] ||= 0)
end
current_index = 0 if tag.attr['reset'] == 'true'
cycle[cycle_name] = (current_index + 1) % values.size
values[current_index]
else
cycle[cycle_name] ||= (start.presence || 0).to_i
output = cycle[cycle_name]
cycle[cycle_name] += 1
if tag.attr['reset'] == 'true'
cycle[cycle_name] = 0
output = cycle[cycle_name]
end
output
end
end

desc %{
Expand Down
3 changes: 3 additions & 0 deletions lib/plugins/object_extensions/lib/object_extensions.rb
Expand Up @@ -2,4 +2,7 @@ class Object
def self.descendants
subclasses_of(self)
end
def presence
return self if present?
end
end
15 changes: 11 additions & 4 deletions spec/models/standard_tags_spec.rb
Expand Up @@ -944,6 +944,7 @@
end

describe "<r:cycle>" do
subject { page }
it "should render passed values in succession" do
page.should render('<r:cycle values="first, second" /> <r:cycle values="first, second" />').as('first second')
end
Expand All @@ -952,6 +953,10 @@
page.should render('<r:cycle values="first, second" /> <r:cycle values="first, second" /> <r:cycle values="first, second" />').as('first second first')
end

it "should start at a given start value" do
page.should render('<r:cycle values="first, second, third" start="second" /> <r:cycle values="first, second, third" start="second" /> <r:cycle values="first, second, third" start="second" />').as('second third first')
end

it "should use a default cycle name of 'cycle'" do
page.should render('<r:cycle values="first, second" /> <r:cycle values="first, second" name="cycle" />').as('first second')
end
Expand All @@ -963,10 +968,12 @@
it "should reset the counter" do
page.should render('<r:cycle values="first, second" /> <r:cycle values="first, second" reset="true"/>').as('first first')
end

it "should require the values attribute" do
page.should render('<r:cycle />').with_error("`cycle' tag must contain a `values' attribute.")
end

it { should render('<r:cycle /> <r:cycle />').as('0 1') }
it { should render('<r:cycle start="3" /> <r:cycle start="3" /> <r:cycle start="3" />').as('3 4 5') }
it { should render('<r:cycle start="3" /> <r:cycle name="other" /> <r:cycle start="3" />').as('3 0 4') }
it { should render('<r:cycle start="3" /> <r:cycle name="other" start="23" /> <r:cycle />').as('3 23 4') }
it { should render('<r:cycle start="3" /> <r:cycle name="other" start="23" /> <r:cycle reset="true" />').as('3 23 0') }
end

describe "<r:if_dev>" do
Expand Down

0 comments on commit a0cc0b2

Please sign in to comment.