adding collection tag

Oleg committed Sep 24, 2011
commit 7e25c4bce10b42b5576b10d8da0c582f81bea4be
Showing 8 changed files with 212 additions and 60 deletions
comfortable_mexican_sofa/form_builder.rb b/lib/comfortable_mexican_sofa/form_builder.rb +17 -10
@@ @@ -78,16 +78,8 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder
:id => nil,
:class => field_css_class
)
-
- %(
- <div class='form_element #{css_class}'>
- <div class='label'>#{label}</div>
- <div class='value'>
- #{field}
- #{@template.hidden_field_tag('page[blocks_attributes][][label]', tag.label, :id => nil)}
- </div>
- </div>
- ).html_safe
+ content = "#{field} #{@template.hidden_field_tag('page[blocks_attributes][][label]', tag.label, :id => nil)}"
+ simple_field(label, content, :class => css_class)
end
def field_date_time(tag)
@@ @@ -126,4 +118,19 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder
default_tag_field(tag, :content_field_method => :text_area_tag)
end
+ def collection(tag)
+ options = [["---- Select #{tag.collection_class.titleize} ----", nil]] +
+ tag.collection_class.constantize.all.collect do |m|
+ [m.send(tag.collection_title), m.send(tag.collection_identifier)]
+ end
+
+ content = @template.select_tag(
+ 'page[blocks_attributes][][content]',
+ @template.options_for_select(options, :selected => tag.content),
+ :id => nil
+ )
+ content << @template.hidden_field_tag('page[blocks_attributes][][label]', tag.label, :id => nil)
+ simple_field(tag.collection_class.titleize, content, :class => tag.class.to_s.demodulize.underscore )
+ end
+
end
comfortable_mexican_sofa/tag.rb b/lib/comfortable_mexican_sofa/tag.rb +1 -1
@@ @@ -88,7 +88,7 @@ module ComfortableMexicanSofa::Tag
# Checks if this tag is using Cms::Block
def is_cms_block?
- %w(page field).member?(self.class.to_s.demodulize.underscore.split(/_/).first)
+ %w(page field collection).member?(self.class.to_s.demodulize.underscore.split(/_/).first)
end
# Used in displaying form elements for Cms::Block
comfortable_mexican_sofa/tags/collection.rb b/lib/comfortable_mexican_sofa/tags/collection.rb +54 -0
@@ @@ -0,0 +1,54 @@
+ class ComfortableMexicanSofa::Tag::Collection
+ include ComfortableMexicanSofa::Tag
+
+ # Simple example for Albums collection rendered out by albums/show partial
+ # making assumtion that we will use `label` and `id` as Album attributes
+ # {{ cms:collection:Album:albums/show }}
+ # If Album uses `title` and `slug` tag will look as follows:
+ # {{ cms:collection:Album:albums/show:title:slug }}
+ # If you need to send more paramers to the partial just attach them as such:
+ # {{ cms:collection:Album:albums/show:title:slug:param_a:param_b }}
+ def self.regex_tag_signature(label = nil)
+ label ||= /[\w\/\-]+/
+ /\{\{\s*cms:collection:(#{label}):([\w\/\-\:]+)\s*\}\}/
+ end
+
+ def collection_partial
+ self.params.first
+ end
+
+ def collection_class
+ label.classify
+ end
+
+ def collection_title
+ self.params[1] || 'label'
+ end
+
+ def collection_identifier
+ self.params[2] || 'id'
+ end
+
+ def collection_params
+ self.params[3..-1] || []
+ end
+
+ def content=(value)
+ block.content = value
+ end
+
+ def content
+ block.content
+ end
+
+ def render
+ if self.content.present?
+ ps = collection_params.collect_with_index{|p, i| ":param_#{i+1} => '#{p}'"}.join(', ')
+ ps = ps.present?? ", #{ps}" : ''
+ "<%= render :partial => '#{collection_partial}', :locals => {:model => '#{collection_class}', :identifier => '#{content}'#{ps}} %>"
+ else
+ ''
+ end
+ end
+
+ end
\ No newline at end of file
test/functional/cms_admin/pages_controller_test.rb +11 -0
@@ @@ -101,6 +101,17 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
assert_select "textarea[name='page[blocks_attributes][][content]'][class='code']"
assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='test_label']"
end
+
+ def test_get_new_with_collection
+ snippet = cms_snippets(:default)
+ cms_layouts(:default).update_attribute(:content, '{{cms:collection:cms/snippet:snippets/show}}')
+ get :new, :site_id => cms_sites(:default)
+ assert_select "select[name='page[blocks_attributes][][content]']" do
+ assert_select "option[value='']", :html => '---- Select Cms/Snippet ----'
+ assert_select "option[value='#{snippet.id}']", :html => snippet.label
+ end
+ assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='cms/snippet']"
+ end
def test_get_new_with_rich_page_text
cms_layouts(:default).update_attribute(:content, '{{cms:page:test_label:rich_text}}')
test/unit/tag_test.rb +10 -6
@@ @@ -226,7 +226,7 @@ class TagTest < ActiveSupport::TestCase
site = cms_sites(:default)
layout = site.layouts.create!(
:slug => 'no-irb-layout',
- :content => '<% 1 + 1 %> {{cms:page:content}} <%= 1 + 1 %>'
+ :content => '<% 1 + 1 %> {{cms:page:content}} {{cms:collection:cms/snippet:snippets/show}} <%= 1 + 1 %>'
)
snippet = site.snippets.create!(
:slug => 'no-irb-snippet',
@@ @@ -238,10 +238,12 @@ class TagTest < ActiveSupport::TestCase
:layout_id => layout.id,
:blocks_attributes => [
{ :label => 'content',
- :content => 'text {{ cms:snippet:no-irb-snippet }} {{ cms:partial:path/to }} {{ cms:helper:method }} text' }
+ :content => 'text {{ cms:snippet:no-irb-snippet }} {{ cms:partial:path/to }} {{ cms:helper:method }} text' },
+ { :label => 'cms/snippet',
+ :content => snippet.id }
]
)
- assert_equal "&lt;% 1 + 1 %&gt; text &lt;% 2 + 2 %&gt; snippet &lt;%= 2 + 2 %&gt; <%= render :partial => 'path/to' %> <%= method() %> text &lt;%= 1 + 1 %&gt;", page.content
+ assert_equal "&lt;% 1 + 1 %&gt; text &lt;% 2 + 2 %&gt; snippet &lt;%= 2 + 2 %&gt; <%= render :partial => 'path/to' %> <%= method() %> text <%= render :partial => 'snippets/show', :locals => {:model => 'Cms::Snippet', :identifier => '#{snippet.id}'} %> &lt;%= 1 + 1 %&gt;", page.content
end
def test_content_with_irb_enabled
@@ @@ -250,7 +252,7 @@ class TagTest < ActiveSupport::TestCase
site = cms_sites(:default)
layout = site.layouts.create!(
:slug => 'irb-layout',
- :content => '<% 1 + 1 %> {{cms:page:content}} <%= 1 + 1 %>'
+ :content => '<% 1 + 1 %> {{cms:page:content}} {{cms:collection:cms/snippet:snippets/show}} <%= 1 + 1 %>'
)
snippet = site.snippets.create!(
:slug => 'irb-snippet',
@@ @@ -262,10 +264,12 @@ class TagTest < ActiveSupport::TestCase
:layout_id => layout.id,
:blocks_attributes => [
{ :label => 'content',
- :content => 'text {{ cms:snippet:irb-snippet }} {{ cms:partial:path/to }} {{ cms:helper:method }} text' }
+ :content => 'text {{ cms:snippet:irb-snippet }} {{ cms:partial:path/to }} {{ cms:helper:method }} text' },
+ { :label => 'cms/snippet',
+ :content => snippet.id }
]
)
- assert_equal "<% 1 + 1 %> text <% 2 + 2 %> snippet <%= 2 + 2 %> <%= render :partial => 'path/to' %> <%= method() %> text <%= 1 + 1 %>", page.content
+ assert_equal "<% 1 + 1 %> text <% 2 + 2 %> snippet <%= 2 + 2 %> <%= render :partial => 'path/to' %> <%= method() %> text <%= render :partial => 'snippets/show', :locals => {:model => 'Cms::Snippet', :identifier => '#{snippet.id}'} %> <%= 1 + 1 %>", page.content
end
end
test/unit/tags/collection_test.rb +76 -0
@@ @@ -0,0 +1,76 @@
+ require File.expand_path('../../test_helper', File.dirname(__FILE__))
+
+ class CollectionTagTest < ActiveSupport::TestCase
+
+ def test_initialize_tag
+ assert tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag(
+ cms_pages(:default), '{{ cms:collection:cms/snippet:path/to/partial }}'
+ )
+ assert_equal 'cms/snippet', tag.label
+ assert_equal 'Cms::Snippet', tag.collection_class
+ assert_equal 'path/to/partial', tag.collection_partial
+ assert_equal 'label', tag.collection_title
+ assert_equal 'id', tag.collection_identifier
+ assert_equal [], tag.collection_params
+ end
+
+ def test_initialize_tag_detailed
+ assert tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag(
+ cms_pages(:default), '{{ cms:collection:cms/snippet:path/to/partial:title:slug:param_a:param_b }}'
+ )
+ assert_equal 'cms/snippet', tag.label
+ assert_equal 'Cms::Snippet', tag.collection_class
+ assert_equal 'path/to/partial', tag.collection_partial
+ assert_equal 'title', tag.collection_title
+ assert_equal 'slug', tag.collection_identifier
+ assert_equal ['param_a', 'param_b'], tag.collection_params
+ end
+
+ def test_initialize_tag_failure
+ [
+ '{{cms:collection}}',
+ '{{cms:collection:label}}',
+ '{{cms:not_collection:label:partial}}',
+ '{not_a_tag}'
+ ].each do |tag_signature|
+ assert_nil ComfortableMexicanSofa::Tag::Collection.initialize_tag(
+ cms_pages(:default), tag_signature
+ )
+ end
+ end
+
+ def test_content_and_render
+ tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag(
+ cms_pages(:default), '{{ cms:collection:cms/snippet:path/to/partial }}'
+ )
+ assert tag.content.blank?
+
+ snippet = cms_snippets(:default)
+ tag.content = snippet.id
+ assert_equal snippet.id, tag.block.content
+ assert_equal snippet.id, tag.content
+ assert_equal "<%= render :partial => 'path/to/partial', :locals => {:model => 'Cms::Snippet', :identifier => '#{snippet.id}'} %>", tag.render
+ end
+
+ def test_content_and_render_detailed
+ tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag(
+ cms_pages(:default), '{{ cms:collection:cms/snippet:path/to/partial:label:slug:param_a:param_b }}'
+ )
+ assert tag.content.blank?
+
+ snippet = cms_snippets(:default)
+ tag.content = snippet.slug
+ assert_equal snippet.slug, tag.block.content
+ assert_equal snippet.slug, tag.content
+ assert_equal "<%= render :partial => 'path/to/partial', :locals => {:model => 'Cms::Snippet', :identifier => '#{snippet.slug}', :param_1 => 'param_a', :param_2 => 'param_b'} %>", tag.render
+ end
+
+ def test_content_and_render_with_no_content
+ tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag(
+ cms_pages(:default), '{{ cms:collection:cms/snippet:path/to/partial }}'
+ )
+ assert tag.content.blank?
+ assert_equal '', tag.render
+ end
+
+ end
\ No newline at end of file
test/unit/tags/page_rich_text.rb +0 -43
@@ @@ -1,43 +0,0 @@
- require File.expand_path('../../test_helper', File.dirname(__FILE__))
-
- class PageRichTextTagTest < ActiveSupport::TestCase
-
- def test_initialize_tag
- assert tag = ComfortableMexicanSofa::Tag::PageRichText.initialize_tag(
- cms_pages(:default), '{{ cms:page:content:rich_text }}'
- )
- assert_equal 'content', tag.label
- assert tag = ComfortableMexicanSofa::Tag::PageRichText.initialize_tag(
- cms_pages(:default), '{{cms:page:content:rich_text}}'
- )
- assert_equal 'content', tag.label
- assert tag = ComfortableMexicanSofa::Tag::PageRichText.initialize_tag(
- cms_pages(:default), '{{cms:page:dash-content:rich_text}}'
- )
- assert_equal 'dash-content', tag.label
- end
-
- def test_initialize_tag_failure
- [
- '{{cms:page:content:not_rich_text}}',
- '{{cms:page:content}}',
- '{{cms:not_page:content}}',
- '{not_a_tag}'
- ].each do |tag_signature|
- assert_nil ComfortableMexicanSofa::Tag::PageRichText.initialize_tag(
- cms_pages(:default), tag_signature
- )
- end
- end
-
- def test_content_and_render
- tag = ComfortableMexicanSofa::Tag::PageRichText.initialize_tag(
- cms_pages(:default), '{{cms:page:content:rich_text}}'
- )
- assert tag.content.blank?
- tag.content = 'test_content'
- assert_equal 'test_content', tag.content
- assert_equal 'test_content', tag.render
- end
-
- end
\ No newline at end of file
test/unit/tags/page_rich_text_test.rb +43 -0
@@ @@ -0,0 +1,43 @@
+ require File.expand_path('../../test_helper', File.dirname(__FILE__))
+
+ class PageRichTextTagTest < ActiveSupport::TestCase
+
+ def test_initialize_tag
+ assert tag = ComfortableMexicanSofa::Tag::PageRichText.initialize_tag(
+ cms_pages(:default), '{{ cms:page:content:rich_text }}'
+ )
+ assert_equal 'content', tag.label
+ assert tag = ComfortableMexicanSofa::Tag::PageRichText.initialize_tag(
+ cms_pages(:default), '{{cms:page:content:rich_text}}'
+ )
+ assert_equal 'content', tag.label
+ assert tag = ComfortableMexicanSofa::Tag::PageRichText.initialize_tag(
+ cms_pages(:default), '{{cms:page:dash-content:rich_text}}'
+ )
+ assert_equal 'dash-content', tag.label
+ end
+
+ def test_initialize_tag_failure
+ [
+ '{{cms:page:content:not_rich_text}}',
+ '{{cms:page:content}}',
+ '{{cms:not_page:content}}',
+ '{not_a_tag}'
+ ].each do |tag_signature|
+ assert_nil ComfortableMexicanSofa::Tag::PageRichText.initialize_tag(
+ cms_pages(:default), tag_signature
+ )
+ end
+ end
+
+ def test_content_and_render
+ tag = ComfortableMexicanSofa::Tag::PageRichText.initialize_tag(
+ cms_pages(:default), '{{cms:page:content:rich_text}}'
+ )
+ assert tag.content.blank?
+ tag.content = 'test_content'
+ assert_equal 'test_content', tag.content
+ assert_equal 'test_content', tag.render
+ end
+
+ end
\ No newline at end of file