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})
if pb_exists
pb_id = pb.id
else
nextId = Polyblock::Block.count + 1
nextId += 1 while Polyblock::Block.where(:id => nextId).any?
pb_id = nextId
end
end
# Build output tag
content = pb_exists ? pb.content : "
This block is content managed by Polyblock!
Click to edit me!
"
tag_options = options[:tag_options].deep_merge({:id => "pb-#{pb_id}", :data => {:pbid => pb_id, :pbname => name}})
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("
", "
").gsub(' ', ' '))
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, options={})
options = {
:label => false,
:input_html => {}
}.merge(options)
render :partial => "polyblock/simple_fields_for", :locals => {:f => f, :name => name, :options => options}
end
def polyblock_editor_bar
render :partial => "polyblock/editor_bar"
end
end
end