Sha256: 480280869b5173ebf6ff0b893cfab4aedb755db9e500af9400028c23aba299ad

Contents?: true

Size: 1.66 KB

Versions: 2

Compression:

Stored size: 1.66 KB

Contents

module Polyblock
  module ApplicationHelper

    def pb(name, options={})
      options = {
          :tag => :div,
          :tag_options => {},
          :condensed => false,
          :length => 250,
          :omission => '... (continued)'
      }.merge(options)

      # Fetch or create Polyblock
      if name.is_a? Polyblock::Block
        pb = name
        pb_id = pb.id
        name = pb.name
        pb_exists = true
      else
        matches = Polyblock::Block.where :name => name
        pb_exists = matches.any?
        pb = pb_exists ? matches.first : Polyblock::Block.new({:name => name})
        pb_id = pb_exists ? pb.id : Polyblock::Block.count + 1
      end

      # Build output tag
      content = pb_exists ? pb.content : "<p>This block is content managed by Polyblock!<br />Edit \"#{name}\" to add content here.</p>"
      tag_options = options[:tag_options].deep_merge({:id => "pb-#{pb_id}", :data => {:pbid => pb_id}})
      tag_options[:contenteditable] = (!respond_to?(:can?) || can?(:manage, pb)) ? "true" : "false"
      tag_options[:class] = (tag_options.has_key?(:class) ? "#{tag_options[:class]} ":"") + "polyblock"
      tag_options[:data][:pb_exists] = pb_exists

      # Truncation
      if options[:condensed]
        content = CGI.unescapeHTML(content.gsub("<br><br>", "<br>").gsub('&nbsp;', ' '))
        content = truncate(sanitize(strip_tags(content)), :length => options[:length], :omission => options[:omission]).html_safe
      end

      content_tag(options[:tag], content, tag_options, false)
    end

    def simple_fields_for_polyblock(name, f)
      render :partial => "polyblock/simple_fields_for", :locals => {:f => f, :name => name}
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
polyblock-0.2.5 app/helpers/polyblock/application_helper.rb
polyblock-0.2.4 app/helpers/polyblock/application_helper.rb