Skip to content

Commit

Permalink
adding inherit attribute to if_content and unless_content
Browse files Browse the repository at this point in the history
  • Loading branch information
saturnflyer committed Jul 8, 2008
1 parent 4e3db10 commit 8999621
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 4 deletions.
25 changes: 21 additions & 4 deletions app/models/standard_tags.rb
Original file line number Diff line number Diff line change
Expand Up @@ -275,9 +275,10 @@ class TagError < StandardError; end
end

desc %{
Renders the containing elements only if all of the listed parts exists on a page.
Renders the containing elements only if all of the listed parts exist on a page.
By default the @part@ attribute is set to @body@, but you may list more than one
part by seprating them with a comma.
part by seprating them with a comma. Setting the optional @inherit@ to true will
search ancestors independently for each part. By default @inherit@ is set to @false@.
*Usage:*
<pre><code><r:if_content [part="part_name, other_part"]>...</r:if_content></code></pre>
Expand All @@ -286,10 +287,18 @@ class TagError < StandardError; end
page = tag.locals.page
part_name = tag_part_name(tag)
parts_arr = part_name.split(',')
inherit_attr = tag.attr['inherit'] || 'false'
inherit = inherit_attr.downcase == 'true' ? true : false
all_parts_present = true
part_page = page
parts_arr.each do |name|
name.strip!
all_parts_present = false if page.part(name).nil?
if inherit
while (part_page.part(name).nil? and (not part_page.parent.nil?)) do
part_page = part_page.parent
end
end
all_parts_present = false if part_page.part(name).nil?
end
tag.expand if all_parts_present
end
Expand All @@ -305,10 +314,18 @@ class TagError < StandardError; end
page = tag.locals.page
part_name = tag_part_name(tag)
parts_arr = part_name.split(',')
inherit_attr = tag.attr['inherit'] || 'false'
inherit = inherit_attr.downcase == 'true' ? true : false
all_parts_present = false
part_page = page
parts_arr.each do |name|
name.strip!
all_parts_present = true if !page.part(name).nil?
if inherit
while (part_page.part(name).nil? and (not part_page.parent.nil?)) do
part_page = part_page.parent
end
end
all_parts_present = true if !part_page.part(name).nil?
end
tag.expand unless all_parts_present
end
Expand Down
19 changes: 19 additions & 0 deletions spec/models/standard_tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,16 @@
end

describe "<r:if_content>" do
describe "with inherit attribute set to 'true'" do
it 'should render the contained block if the current or ancestor pages have the specified parts' do
page(:guests).should render('<r:if_content part="favors, extended" inherit="true">true</r:if_content>').as('true')
end

it 'should not render the contained block if the current or ancestor pages do not have all of the specified parts' do
page(:guests).should render('<r:if_content part="favors, madeup" inherit="true">true</r:if_content>').as('')
end
end

it "without 'part' attribute should render the contained block if the 'body' part exists" do
page.should render('<r:if_content>true</r:if_content>').as('true')
end
Expand All @@ -271,6 +281,15 @@
end

describe "<r:unless_content>" do
describe "with inherit attribute set to 'true'" do
it 'should not render the contained block if the current or ancestor pages have the specified parts' do
page(:guests).should render('<r:unless_content part="favors, extended" inherit="true">true</r:unless_content>').as('')
end

it 'should render the contained block if the current or ancestor pages do not have the specified parts' do
page(:guests).should render('<r:unless_content part="madeup, imaginary" inherit="true">true</r:unless_content>').as('true')
end
end
it "without 'part' attribute should not render the contained block if the 'body' part exists" do
page.should render('<r:unless_content>false</r:unless_content>').as('')
end
Expand Down
4 changes: 4 additions & 0 deletions spec/scenarios/pages_scenario.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,10 @@ def load
create_page_part "unless_dev", :content => "<r:unless_dev>not dev</r:unless_dev>"
end
create_page "Virtual", :class_name => "VirtualPage", :virtual => true
create_page "Party" do
create_page_part "favors"
create_page "Guests"
end
end

end

0 comments on commit 8999621

Please sign in to comment.