adjusting collection tag
Oleg
committed Sep 26, 2011
commit 40ab9cd9f189d03788482ad0100442a4e6122a3c
Showing 4
changed files with
39 additions
and 35 deletions
comfortable_mexican_sofa/tags/collection.rb b/lib/comfortable_mexican_sofa/tags/collection.rb
+19
-15
| @@ | @@ -1,36 +1,40 @@ |
| 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 }} |
| + | # Here's a full tag signature: |
| + | # {{ cms:collection:label:collection_class:collection_partial:collection_title:collection_identifier:collection_params }} |
| + | # A working example of the above: |
| + | # {{ cms:collection:album:foo/my_album:albums/show:title:slug:param_a:param_b }} |
| def self.regex_tag_signature(label = nil) | |
| label ||= /[\w\/\-]+/ | |
| - | /\{\{\s*cms:collection:(#{label}):([\w\/\-\:]+)\s*\}\}/ |
| + | /\{\{\s*cms:collection:(#{label}):(.*?)\s*\}\}/ |
| end | |
| - | def collection_partial |
| - | self.params.first |
| + | # Class definitition. It's basically `Herp::DerpityDerp.undescore` so an example |
| + | # of valid definition is: `herp/derpity_derp` |
| + | def collection_class |
| + | self.params[0].classify |
| end | |
| - | def collection_class |
| - | label.classify |
| + | # Path to the partial. Example: `path/to/partial` |
| + | def collection_partial |
| + | self.params[1] || self.collection_class.underscore.pluralize |
| end | |
| + | # Title method for the Collection objects. Default is `label` |
| def collection_title | |
| - | self.params[1] || 'label' |
| + | self.params[2] || 'label' |
| end | |
| + | # Identifier that will be used to find selected collection object. Defaults to `id` |
| def collection_identifier | |
| - | self.params[2] || 'id' |
| + | self.params[3] || 'id' |
| end | |
| + | # Extra params that will be passed to the partial AND ALSO will be passed as parameters |
| + | # for the `cms_collection` scope you can define for your Collection object |
| def collection_params | |
| - | self.params[3..-1] || [] |
| + | self.params[4..-1] || [] |
| end | |
| def content=(value) | |
test/functional/cms_admin/pages_controller_test.rb
+2
-2
| @@ | @@ -104,13 +104,13 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase |
| def test_get_new_with_collection | |
| snippet = cms_snippets(:default) | |
| - | cms_layouts(:default).update_attribute(:content, '{{cms:collection:cms/snippet:snippets/show}}') |
| + | cms_layouts(:default).update_attribute(:content, '{{cms:collection:snippet:cms/snippet}}') |
| 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']" |
| + | assert_select "input[type='hidden'][name='page[blocks_attributes][][label]'][value='snippet']" |
| end | |
| def test_get_new_with_rich_page_text | |
test/unit/tag_test.rb
+6
-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}} {{cms:collection:cms/snippet:snippets/show}} <%= 1 + 1 %>' |
| + | :content => '<% 1 + 1 %> {{cms:page:content}} {{cms:collection:snippet:cms/snippet}} <%= 1 + 1 %>' |
| ) | |
| snippet = site.snippets.create!( | |
| :slug => 'no-irb-snippet', | |
| @@ | @@ -239,11 +239,11 @@ class TagTest < ActiveSupport::TestCase |
| :blocks_attributes => [ | |
| { :label => 'content', | |
| :content => 'text {{ cms:snippet:no-irb-snippet }} {{ cms:partial:path/to }} {{ cms:helper:method }} text' }, | |
| - | { :label => 'cms/snippet', |
| + | { :label => 'snippet', |
| :content => snippet.id } | |
| ] | |
| ) | |
| - | 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 |
| + | assert_equal "<% 1 + 1 %> text <% 2 + 2 %> snippet <%= 2 + 2 %> <%= render :partial => 'path/to' %> <%= method() %> text <%= render :partial => 'cms/snippets', :locals => {:model => 'Cms::Snippet', :identifier => '#{snippet.id}'} %> <%= 1 + 1 %>", page.content |
| end | |
| def test_content_with_irb_enabled | |
| @@ | @@ -252,7 +252,7 @@ class TagTest < ActiveSupport::TestCase |
| site = cms_sites(:default) | |
| layout = site.layouts.create!( | |
| :slug => 'irb-layout', | |
| - | :content => '<% 1 + 1 %> {{cms:page:content}} {{cms:collection:cms/snippet:snippets/show}} <%= 1 + 1 %>' |
| + | :content => '<% 1 + 1 %> {{cms:page:content}} {{cms:collection:snippet:cms/snippet}} <%= 1 + 1 %>' |
| ) | |
| snippet = site.snippets.create!( | |
| :slug => 'irb-snippet', | |
| @@ | @@ -265,11 +265,11 @@ class TagTest < ActiveSupport::TestCase |
| :blocks_attributes => [ | |
| { :label => 'content', | |
| :content => 'text {{ cms:snippet:irb-snippet }} {{ cms:partial:path/to }} {{ cms:helper:method }} text' }, | |
| - | { :label => 'cms/snippet', |
| + | { :label => 'snippet', |
| :content => snippet.id } | |
| ] | |
| ) | |
| - | 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 |
| + | assert_equal "<% 1 + 1 %> text <% 2 + 2 %> snippet <%= 2 + 2 %> <%= render :partial => 'path/to' %> <%= method() %> text <%= render :partial => 'cms/snippets', :locals => {:model => 'Cms::Snippet', :identifier => '#{snippet.id}'} %> <%= 1 + 1 %>", page.content |
| end | |
| end | |
test/unit/tags/collection_test.rb
+12
-12
| @@ | @@ -4,11 +4,11 @@ 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 }}' |
| + | cms_pages(:default), '{{ cms:collection:snippet:cms/snippet }}' |
| ) | |
| - | assert_equal 'cms/snippet', tag.label |
| + | assert_equal 'snippet', tag.label |
| assert_equal 'Cms::Snippet', tag.collection_class | |
| - | assert_equal 'path/to/partial', tag.collection_partial |
| + | assert_equal 'cms/snippets', tag.collection_partial |
| assert_equal 'label', tag.collection_title | |
| assert_equal 'id', tag.collection_identifier | |
| assert_equal [], tag.collection_params | |
| @@ | @@ -16,9 +16,9 @@ class CollectionTagTest < ActiveSupport::TestCase |
| 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 }}' |
| + | cms_pages(:default), '{{ cms:collection:snippet:cms/snippet:path/to/partial:title:slug:param_a:param_b }}' |
| ) | |
| - | assert_equal 'cms/snippet', tag.label |
| + | assert_equal 'snippet', tag.label |
| assert_equal 'Cms::Snippet', tag.collection_class | |
| assert_equal 'path/to/partial', tag.collection_partial | |
| assert_equal 'title', tag.collection_title | |
| @@ | @@ -30,7 +30,7 @@ class CollectionTagTest < ActiveSupport::TestCase |
| [ | |
| '{{cms:collection}}', | |
| '{{cms:collection:label}}', | |
| - | '{{cms:not_collection:label:partial}}', |
| + | '{{cms:not_collection:label:class:partial}}', |
| '{not_a_tag}' | |
| ].each do |tag_signature| | |
| assert_nil ComfortableMexicanSofa::Tag::Collection.initialize_tag( | |
| @@ | @@ -40,8 +40,8 @@ class CollectionTagTest < ActiveSupport::TestCase |
| end | |
| def test_content_and_render | |
| - | tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag( |
| - | cms_pages(:default), '{{ cms:collection:cms/snippet:path/to/partial }}' |
| + | assert tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag( |
| + | cms_pages(:default), '{{ cms:collection:snippet:cms/snippet:path/to/partial }}' |
| ) | |
| assert tag.content.blank? | |
| @@ | @@ -53,8 +53,8 @@ class CollectionTagTest < ActiveSupport::TestCase |
| 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 = ComfortableMexicanSofa::Tag::Collection.initialize_tag( |
| + | cms_pages(:default), '{{ cms:collection:snippet:cms/snippet:path/to/partial:label:slug:param_a:param_b }}' |
| ) | |
| assert tag.content.blank? | |
| @@ | @@ -66,8 +66,8 @@ class CollectionTagTest < ActiveSupport::TestCase |
| 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 = ComfortableMexicanSofa::Tag::Collection.initialize_tag( |
| + | cms_pages(:default), '{{ cms:collection:snippet:cms/snippet:path/to/partial }}' |
| ) | |
| assert tag.content.blank? | |
| assert_equal '', tag.render | |