Sha256: 31f1e2a93d827a02000441d0f7cd418c8e9fef278b5cd140adbf9f2dcba3c08a

Contents?: true

Size: 1.38 KB

Versions: 24

Compression:

Stored size: 1.38 KB

Contents

class ComfortableMexicanSofa::Tag::PageFile
  include ComfortableMexicanSofa::Tag
  
  # Signature of a tag:
  #   {{ cms:page_file:some_label:type:params }}
  # Simple tag can be:
  #   {{ cms:page_file:some_label }}
  def self.regex_tag_signature(identifier = nil)
    identifier ||= /[\w\-]+/
    /\{\{\s*cms:page_file:(#{identifier}):?(.*?)\s*\}\}/
  end
  
  # Type of the tag controls how file is rendered
  def type
    s = params[0].to_s.gsub(/\[.*?\]/, '')
    %w(partial url image link field).member?(s) ? s : 'url'
  end
  
  def dimensions
    params[0].to_s.match(/\[(.*?)\]/)[1] rescue nil
  end
  
  def content
    block.files.first
  end
  
  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
  
end

Version data entries

24 entries across 24 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-1.6.23 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.22 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.21 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.20 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.19 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.18 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.17 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.16 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.15 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.14 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.13 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.12 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.11 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.10 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.9 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.8 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.7 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.6 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.5 lib/comfortable_mexican_sofa/tags/page_file.rb
comfortable_mexican_sofa-1.6.4 lib/comfortable_mexican_sofa/tags/page_file.rb