Class: ComfortableMexicanSofa::Tag::PageFiles

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

Constant Summary

Constants included from ComfortableMexicanSofa::Tag

TOKENIZER_REGEX

Instance Attribute Summary

Attributes included from ComfortableMexicanSofa::Tag

#identifier, #page, #params, #parent

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.regex_tag_signature(identifier = nil) ⇒ Object



4
5
6
7
# File 'lib/comfortable_mexican_sofa/tags/page_files.rb', line 4

def self.regex_tag_signature(identifier = nil)
  identifier ||= /[\w\-]+/
  /\{\{\s*cms:page_files:(#{identifier}):?(.*?)\s*\}\}/
end

Instance Method Details

#contentObject



19
20
21
# File 'lib/comfortable_mexican_sofa/tags/page_files.rb', line 19

def content
  block.files
end

#dimensionsObject



15
16
17
# File 'lib/comfortable_mexican_sofa/tags/page_files.rb', line 15

def dimensions
  params[0].to_s.match(/\[(.*?)\]/)[1] rescue nil
end

#renderObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/comfortable_mexican_sofa/tags/page_files.rb', line 23

def render
  files = block.files
  case self.type
  when 'url'
    return '' if files.blank?
    files.collect do |file|
      file.file.url
    end.join(', ')
  when 'link'
    return '' if files.blank?
    files.collect do |file|
      "<a href='#{file.file.url}' target='_blank'>#{file.label}</a>"
    end.join(' ')
  when 'image'
    return '' if files.blank?
    files.collect do |file|
      "<img src='#{file.file.url}' alt='#{file.label}' />"
    end.join(' ')
  when 'partial'
    path = params[1] || 'partials/page_files'
    ps = (self.params[2..-1] || []).collect_with_index{|p, i| ":param_#{i+1} => '#{p}'"}.join(', ')
    ps = ps.present?? ", #{ps}" : ''
    ids = files.present?? files.collect(&:id).join(',') : ''
    "<%= render :partial => '#{path}', :locals => {:identifier => [#{ids}]#{ps}} %>"
  when 'field'
    ''
  end
end

#typeObject

Type of the tag controls how file is rendered



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

def type
  s = params[0].to_s.gsub(/\[.*?\]/, '')
  %w(partial url image link field).member?(s) ? s : 'url'
end