Sha256: 2f5ca1b712fd240b2d2f8fea2072d71d2991759e9a1dc28c9b4a7051344ff8ea

Contents?: true

Size: 1.35 KB

Versions: 5

Compression:

Stored size: 1.35 KB

Contents

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

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
comfortable_mexican_sofa-1.5.11 lib/comfortable_mexican_sofa/tags/page_files.rb
comfortable_mexican_sofa-1.5.10 lib/comfortable_mexican_sofa/tags/page_files.rb
comfortable_mexican_sofa-1.5.9 lib/comfortable_mexican_sofa/tags/page_files.rb
comfortable_mexican_sofa-1.5.8 lib/comfortable_mexican_sofa/tags/page_files.rb
comfortable_mexican_sofa-1.5.7 lib/comfortable_mexican_sofa/tags/page_files.rb