Class: ComfyPress::Tag::PageFile

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

Constant Summary

Constants included from ComfyPress::Tag

IDENTIFIER_REGEX, TOKENIZER_REGEX

Instance Attribute Summary

Attributes included from ComfyPress::Tag

#identifier, #namespace, #page, #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/comfypress/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/comfypress/tags/page_file.rb', line 23

def content
  block.files.first
end

#dimensionsObject



19
20
21
# File 'lib/comfypress/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
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/comfypress/tags/page_file.rb', line 27

def render
  file = block.files.first
  case self.type
  when 'url'
    return '' unless file
    file.file.url
  when 'link'
    return '' unless file
    text = params[1] || identifier
    "<a href='#{file.file.url}' target='_blank'>#{text}</a>"
  when 'image'
    return '' unless file
    text = params[1] || identifier
    "<img src='#{file.file.url}' alt='#{text}' />"
  when 'partial'
    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}} %>"
  when 'field'
    ''
  end
end

#typeObject

Type of the tag controls how file is rendered



14
15
16
17
# File 'lib/comfypress/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