Module: Polyblock::ApplicationHelper

Defined in:
app/helpers/polyblock/application_helper.rb

Instance Method Summary collapse

Instance Method Details

#pb(name, options = {}) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'app/helpers/polyblock/application_helper.rb', line 4

def pb(name, options={})
  options = {
      :editable => false,
      :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 = if pb_exists
      pb.id
    else
      Polyblock::Block.any? ? Polyblock::Block.order(:id).last.id + 1 : 1
    end
  end

  # Generate a random string for id
  o = [('a'..'z'), ('A'..'Z')].map { |i| i.to_a }.flatten
  random_id = (0...50).map { o[rand(o.length)] }.join

  # Build output tag
  content = pb_exists ? pb.content : "<p>This block is content managed by Polyblock!<br />Click to edit me!</p>"
  tag_options = options[:tag_options].deep_merge({:id => "pb-#{pb_id}-#{random_id}", :data => {:pbid => pb_id, :pbname => name}})
  tag_options[:contenteditable] = if options[:condensed]
                                    "false"
                                  elsif options[:editable]
                                    "true"
                                  elsif respond_to?(:can?) && can?(:manage, pb)
                                    "true"
                                  elsif respond_to?(:user_signed_in?) && user_signed_in?
                                    if current_user.respond_to?(:admin?) && current_user.admin?
                                      "true"
                                    elsif pb.contentable.present? &&
                                        pb.contentable.respond_to?(:user_id) &&
                                        pb.contentable.user_id == current_user.id
                                      "true"
                                    else
                                      "false"
                                    end
                                  else
                                    "false"
                                  end
  tag_options.delete(:contenteditable) if tag_options[:contenteditable] == "false"
  tag_options[:class] = (tag_options.has_key?(:class) ? "#{tag_options[:class]} ":"") + "polyblock"
  tag_options[:data][:pb_exists] = pb_exists

  # Truncation
  if options[:condensed]
    tag_options[:class] += " polyblock-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

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

#polyblock_editor_barObject



79
80
81
# File 'app/helpers/polyblock/application_helper.rb', line 79

def polyblock_editor_bar
  render :partial => "polyblock/editor_bar"
end

#simple_fields_for_polyblock(name, f, options = {}) ⇒ Object



71
72
73
74
75
76
77
# File 'app/helpers/polyblock/application_helper.rb', line 71

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