Class: ComfortableMexicanSofa::Tag::PageFile

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

Direct Known Subclasses

PageFiles

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

Signature of a tag:

{{ cms:page_file:some_label:type:params }}

Simple tag can be:

{{ cms:page_file:some_label }}


8
9
10
11
# File 'lib/comfortable_mexican_sofa/tags/page_file.rb', line 8

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

Instance Method Details

#contentObject



23
24
25
# File 'lib/comfortable_mexican_sofa/tags/page_file.rb', line 23

def content
  block.files.first
end

#dimensionsObject



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

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

#renderObject



27
28
29
30
31
32
33
34
35
36
# File 'lib/comfortable_mexican_sofa/tags/page_file.rb', line 27

def render
  file = block.files.first
  case self.type
    when 'url'      then render_url(file)
    when 'link'     then render_link(file)
    when 'image'    then render_image(file)
    when 'partial'  then render_partial(file)
    else ''
  end
end

#render_image(file) ⇒ Object



49
50
51
52
53
# File 'lib/comfortable_mexican_sofa/tags/page_file.rb', line 49

def render_image(file)
  return '' unless file
  text = params[1] || file.label
  "<img src='#{file.file.url}' alt='#{text}' />"
end


43
44
45
46
47
# File 'lib/comfortable_mexican_sofa/tags/page_file.rb', line 43

def render_link(file)
  return '' unless file
  text = params[1] || identifier
  "<a href='#{file.file.url}' target='_blank'>#{text}</a>"
end

#render_partial(file) ⇒ Object



55
56
57
58
59
60
# File 'lib/comfortable_mexican_sofa/tags/page_file.rb', line 55

def render_partial(file)
  path = params[1] || 'partials/page_file'
  ps = (self.params[2..-1] || []).collect_with_index{|p, i| ":param_#{i+1} => '#{p}'"}.join(', ')
  ps = ps.present?? ", #{ps}" : ''
  "<%= render :partial => '#{path}', :locals => {:identifier => #{file.try(:id) || 'nil'}#{ps}} %>"
end

#render_url(file) ⇒ Object



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

def render_url(file)
  return '' unless file
  file.file.url
end

#typeObject

Type of the tag controls how file is rendered



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

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