adding scope support for collections

Oleg committed Sep 26, 2011
commit 96cc621638f442a2288b1806c1eb52354000a0c7
Showing 3 changed files with 46 additions and 5 deletions
comfortable_mexican_sofa/form_builder.rb b/lib/comfortable_mexican_sofa/form_builder.rb +2 -2
@@ @@ -120,7 +120,7 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder
def collection(tag)
options = [["---- Select #{tag.collection_class.titleize} ----", nil]] +
- tag.collection_class.constantize.all.collect do |m|
+ tag.collection_objects.collect do |m|
[m.send(tag.collection_title), m.send(tag.collection_identifier)]
end
@@ @@ -130,7 +130,7 @@ class ComfortableMexicanSofa::FormBuilder < ActionView::Helpers::FormBuilder
: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 )
+ simple_field(tag.label, content, :class => tag.class.to_s.demodulize.underscore )
end
end
comfortable_mexican_sofa/tags/collection.rb b/lib/comfortable_mexican_sofa/tags/collection.rb +11 -1
@@ @@ -3,7 +3,9 @@ class ComfortableMexicanSofa::Tag::Collection
# 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:
+ # Most minimal tag can look like this:
+ # {{ cms:collection:album:foo/my_album }}
+ # A more complete 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\/\-]+/
@@ @@ -37,6 +39,14 @@ class ComfortableMexicanSofa::Tag::Collection
self.params[4..-1] || []
end
+ # Array of objects used in the collection
+ # You may set up a scope on the model `scope :cms_collection, lambda|*args| do ... end `
+ # `args` will be the set of `collection_params`
+ def collection_objects
+ klass = self.collection_class.constantize
+ klass.respond_to?(:cms_collection) ? klass.cms_collection(*collection_params).all : klass.all
+ end
+
def content=(value)
block.content = value
end
test/unit/tags/collection_test.rb +33 -2
@@ @@ -2,6 +2,13 @@ require File.expand_path('../../test_helper', File.dirname(__FILE__))
class CollectionTagTest < ActiveSupport::TestCase
+ module TestCollectionScope
+ def self.included(base)
+ base.scope :cms_collection, lambda{|*args| base.where(:slug => args.first) if args.first }
+ end
+ end
+ Cms::Snippet.send(:include, TestCollectionScope)
+
def test_initialize_tag
assert tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag(
cms_pages(:default), '{{ cms:collection:snippet:cms/snippet }}'
@@ @@ -39,9 +46,33 @@ class CollectionTagTest < ActiveSupport::TestCase
end
end
+ def test_collection_objects
+ assert tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag(
+ cms_pages(:default), '{{ cms:collection:snippet:cms/snippet }}'
+ )
+ assert snippets = tag.collection_objects
+ assert_equal 1, snippets.count
+ assert snippets.first.is_a?(Cms::Snippet)
+ end
+
+ def test_collection_objects_with_scope
+ assert tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag(
+ cms_pages(:default), "{{ cms:collection:snippet:cms/snippet:path/to/partial:label:slug:#{cms_snippets(:default).slug} }}"
+ )
+ assert snippets = tag.collection_objects
+ assert_equal 1, snippets.count
+ assert snippets.first.is_a?(Cms::Snippet)
+
+ assert tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag(
+ cms_pages(:default), "{{ cms:collection:snippet:cms/snippet:path/to/partial:label:slug:invalid }}"
+ )
+ assert snippets = tag.collection_objects
+ assert_equal 0, snippets.count
+ end
+
def test_content_and_render
assert tag = ComfortableMexicanSofa::Tag::Collection.initialize_tag(
- cms_pages(:default), '{{ cms:collection:snippet:cms/snippet:path/to/partial }}'
+ cms_pages(:default), '{{ cms:collection:snippet:cms/snippet }}'
)
assert tag.content.blank?
@@ @@ -49,7 +80,7 @@ class CollectionTagTest < ActiveSupport::TestCase
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
+ assert_equal "<%= render :partial => 'cms/snippets', :locals => {:model => 'Cms::Snippet', :identifier => '#{snippet.id}'} %>", tag.render
end
def test_content_and_render_detailed