Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

r:if_field tag

  • Loading branch information...
commit ea02818c038cf897033f8a3947b7d7bf21d56489 1 parent 73ad94a
@joshfrench authored
View
36 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
View
2  page_fields_extension.rb
@@ -6,7 +6,7 @@ class PageFieldsExtension < Radiant::Extension
url "http://github.com/joshfrench/radiant-page_fields-extension"
def activate
- Page.send :include, PageExtensions, DeprecatedMetaTags
+ Page.send :include, PageExtensions, PageFieldTags, DeprecatedMetaTags
Admin::PagesController.send :include, PagesControllerExtensions
admin.pages.edit.add :popups, 'add_field_popup'
admin.page.edit.add :extended_metadata, "page_fields"
View
48 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
Please sign in to comment.
Something went wrong with that request. Please try again.