Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Josh French
committed
Jul 20, 2010
1 parent
73ad94a
commit ea02818
Showing
3 changed files
with
85 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,36 @@ | |||
module PageFieldsExtension::PageFieldTags | |||
include Radiant::Taggable | |||
|
|||
desc %( | |||
Renders the content of the field given in the @name@ attribute. | |||
*Usage:* | |||
<pre><code><r:field name="Keywords" /></code></pre> | |||
) | |||
tag 'field' do |tag| | |||
raise TagError.new("`field' tag must contain a `name' attribute.") unless tag.attr.has_key?('name') | |||
tag.locals.page.fields[tag.attr['name']].try :content | |||
end | |||
|
|||
desc %( | |||
Renders the contained elements if the field given in the @name@ attribute | |||
exists. The tag also takes an optional @equals@ or @matches@ attribute; | |||
these will expand the tag if the field's content equals or matches the | |||
given string or regex. | |||
*Usage:* | |||
<pre><code><r:if_field name="author" [equals|matches="John"]>...</r:if_field></code></pre> | |||
) | |||
tag 'if_field' do |tag| | |||
raise TagError.new("`field' tag must contain a `name' attribute.") unless tag.attr.has_key?('name') | |||
field = tag.locals.page.fields[tag.attr['name']] | |||
tag.expand if case | |||
when (tag.attr['equals'] and tag.attr['ignore_case'] == 'false') : field.content == tag.attr['equals'] | |||
when tag.attr['equals'] : field.content.downcase == tag.attr['equals'].downcase | |||
when tag.attr['matches'] : field.content =~ build_regexp_for(tag, 'matches') | |||
else field | |||
end | |||
end | |||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Original file line | Diff line number | Diff line change |
---|---|---|---|
@@ -0,0 +1,48 @@ | |||
require File.dirname(__FILE__) + '/../spec_helper' | |||
|
|||
describe PageFieldsExtension::PageFieldTags do | |||
before do | |||
@page = Page.new(:slug => "/", :parent_id => nil, :title => 'Home') | |||
@field = PageField.new(:name => 'Field', :content => "Sweet harmonious biscuits") | |||
@page.fields = [@field] | |||
end | |||
|
|||
describe '<r:field>' do | |||
it "should return field content" do | |||
@page.should render('<r:field name="field" />').as('Sweet harmonious biscuits') | |||
end | |||
|
|||
it "should do nothing for missing field" do | |||
@page.should render('<r:field name="bogus" />').as('') | |||
end | |||
end | |||
|
|||
describe "<r:if_field>" do | |||
describe "with `name` attr" do | |||
it "should should expand if field exists" do | |||
@page.should render('<r:if_field name="field">Ok</r:if_field>').as('Ok') | |||
end | |||
end | |||
|
|||
describe "with `equals` attr" do | |||
it "should expand if content equals attr" do | |||
@page.should render('<r:if_field name="field" equals="sweet harmonious biscuits">Ok</r:if_field>').as('Ok') | |||
end | |||
|
|||
it "should be case sensitive if ignore_case is false" do | |||
@page.should render('<r:if_field name="field" equals="sweet harmonious biscuits" ignore_case="false">Ok</r:if_field>').as('') | |||
end | |||
end | |||
|
|||
describe "with `matches` attr" do | |||
it "should expand if content matches attr" do | |||
@page.should render('<r:if_field name="field" matches="^sweet\s">Ok</r:if_field>').as('Ok') | |||
end | |||
|
|||
it "should be case sensitive if ignore_case is false" do | |||
@page.should render('<r:if_field name="field" matches="^sweet\s" ignore_case="false">Ok</r:if_field>').as('') | |||
end | |||
end | |||
end | |||
|
|||
end |