Skip to content

Commit

Permalink
r:if_field tag
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh French committed Jul 20, 2010
1 parent 73ad94a commit ea02818
Show file tree
Hide file tree
Showing 3 changed files with 85 additions and 1 deletion.
36 changes: 36 additions & 0 deletions lib/page_fields_extension/page_field_tags.rb
@@ -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
2 changes: 1 addition & 1 deletion page_fields_extension.rb
Expand Up @@ -6,7 +6,7 @@ class PageFieldsExtension < Radiant::Extension
url "http://github.com/joshfrench/radiant-page_fields-extension" url "http://github.com/joshfrench/radiant-page_fields-extension"


def activate def activate
Page.send :include, PageExtensions, DeprecatedMetaTags Page.send :include, PageExtensions, PageFieldTags, DeprecatedMetaTags
Admin::PagesController.send :include, PagesControllerExtensions Admin::PagesController.send :include, PagesControllerExtensions
admin.pages.edit.add :popups, 'add_field_popup' admin.pages.edit.add :popups, 'add_field_popup'
admin.page.edit.add :extended_metadata, "page_fields" admin.page.edit.add :extended_metadata, "page_fields"
Expand Down
48 changes: 48 additions & 0 deletions spec/lib/page_field_tags_spec.rb
@@ -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

0 comments on commit ea02818

Please sign in to comment.