diff --git a/CHANGELOG b/CHANGELOG index ef046fec3..05b405ae9 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ === Edge +* Use 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] diff --git a/CONTRIBUTORS b/CONTRIBUTORS index 920228b4f..cc1017159 100644 --- a/CONTRIBUTORS +++ b/CONTRIBUTORS @@ -5,6 +5,7 @@ core: === Edge +* Dirk Kelly * Oriol Gual * Petrik de Heus * Gert Goet diff --git a/app/models/standard_tags.rb b/app/models/standard_tags.rb index 27e6cb805..944299da9 100644 --- a/app/models/standard_tags.rb +++ b/app/models/standard_tags.rb @@ -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:* -
+
+
} 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 %{ diff --git a/lib/plugins/object_extensions/lib/object_extensions.rb b/lib/plugins/object_extensions/lib/object_extensions.rb index b051da4aa..67931f414 100644 --- a/lib/plugins/object_extensions/lib/object_extensions.rb +++ b/lib/plugins/object_extensions/lib/object_extensions.rb @@ -2,4 +2,7 @@ class Object def self.descendants subclasses_of(self) end + def presence + return self if present? + end end \ No newline at end of file diff --git a/spec/models/standard_tags_spec.rb b/spec/models/standard_tags_spec.rb index 753300f6b..b813f761a 100644 --- a/spec/models/standard_tags_spec.rb +++ b/spec/models/standard_tags_spec.rb @@ -944,6 +944,7 @@ end describe "" do + subject { page } it "should render passed values in succession" do page.should render(' ').as('first second') end @@ -952,6 +953,10 @@ page.should render(' ').as('first second first') end + it "should start at a given start value" do + page.should render(' ').as('second third first') + end + it "should use a default cycle name of 'cycle'" do page.should render(' ').as('first second') end @@ -963,10 +968,12 @@ it "should reset the counter" do page.should render(' ').as('first first') end - - it "should require the values attribute" do - page.should render('').with_error("`cycle' tag must contain a `values' attribute.") - end + + it { should render(' ').as('0 1') } + it { should render(' ').as('3 4 5') } + it { should render(' ').as('3 0 4') } + it { should render(' ').as('3 23 4') } + it { should render(' ').as('3 23 0') } end describe "" do