Class: ComfortableMexicanSofa::Tag::Collection

Inherits:
Object
  • Object
show all
Includes:
ComfortableMexicanSofa::Tag
Defined in:
lib/comfortable_mexican_sofa/tags/collection.rb

Constant Summary

Constants included from ComfortableMexicanSofa::Tag

IDENTIFIER_REGEX, TOKENIZER_REGEX

Instance Attribute Summary

Attributes included from ComfortableMexicanSofa::Tag

#blockable, #identifier, #namespace, #params, #parent

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.regex_tag_signature(identifier = nil) ⇒ Object

Here’s a full tag signature:

{{ cms:collection:label:collection_class:collection_partial:collection_title:collection_identifier:collection_params }}

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 }}


10
11
12
13
# File 'lib/comfortable_mexican_sofa/tags/collection.rb', line 10

def self.regex_tag_signature(identifier = nil)
  identifier ||= IDENTIFIER_REGEX
  /\{\{\s*cms:collection:(#{identifier}):(.*?)\s*\}\}/
end

Instance Method Details

#collection_classObject

Class definitition. It’s basically ‘Herp::DerpityDerp.undescore` so an example of valid definition is: `herp/derpity_derp`



17
18
19
# File 'lib/comfortable_mexican_sofa/tags/collection.rb', line 17

def collection_class
  self.params[0].classify
end

#collection_identifierObject

Identifier that will be used to find selected collection object. Defaults to ‘id`



32
33
34
# File 'lib/comfortable_mexican_sofa/tags/collection.rb', line 32

def collection_identifier
  self.params[3] || 'id'
end

#collection_objectsObject

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`



45
46
47
48
# File 'lib/comfortable_mexican_sofa/tags/collection.rb', line 45

def collection_objects
  klass = self.collection_class.constantize
  klass.respond_to?(:cms_collection) ? klass.cms_collection(*collection_params) : klass.all
end

#collection_paramsObject

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



38
39
40
# File 'lib/comfortable_mexican_sofa/tags/collection.rb', line 38

def collection_params
  self.params[4..-1] || []
end

#collection_partialObject

Path to the partial. Example: ‘path/to/partial`



22
23
24
# File 'lib/comfortable_mexican_sofa/tags/collection.rb', line 22

def collection_partial
  self.params[1] || "partials/#{self.collection_class.underscore.pluralize}"
end

#collection_titleObject

Title method for the Collection objects. Default is ‘label`



27
28
29
# File 'lib/comfortable_mexican_sofa/tags/collection.rb', line 27

def collection_title
  self.params[2] || 'label'
end

#contentObject



50
51
52
# File 'lib/comfortable_mexican_sofa/tags/collection.rb', line 50

def content
  block.content
end

#renderObject



54
55
56
57
58
59
60
61
62
# File 'lib/comfortable_mexican_sofa/tags/collection.rb', line 54

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