Module: Polyblock::ViewHelpers

Defined in:
lib/polyblock/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

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



57
58
59
60
61
# File 'lib/polyblock/view_helpers.rb', line 57

def fields_for_polyblock(name, f, options={})
  options = {}.deep_merge(options)
  pb = pb_from_name(name, f)
  render :partial => 'polyblock/fields_for', :locals => {:f => f, :pb => pb, :name => name, :options => options}
end

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



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
# File 'lib/polyblock/view_helpers.rb', line 6

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

  # Fetch or create Polyblock
  pb = Block.fetch_or_initialize(name)
  pb_id = pb.new_record? ? Block.next_id : pb.id
  name = pb.name if name.is_a? Block
  random_id = Block.random_id

  # Build output tag
  content = pb.content.present? ? pb.content : pb.settings[:default_text]
  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.has_key?(:editable)
                                    options[:editable] ? 'true' : 'false'
                                  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.key?(:class) ? "#{tag_options[:class]} ": '') + 'polyblock'
  tag_options[:data][:pb_exists] = pb.new_record?

  # Truncation
  if options[:condensed]
    tag_options[:class] += ' polyblock-condensed'
    truncate(Block.strip_html(content), :length => options[:length], :omission => options[:omission])
  else
    (options[:tag], content, tag_options, false)
  end
end

#polyblock_editor_barObject



91
92
93
# File 'lib/polyblock/view_helpers.rb', line 91

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

#simple_form_for_polyblocks(options = {}) ⇒ Object

def simple_fields_for_polyblock(name, f, options={})

pb = pb_from_name(name, f)
settings = {
  :field => {
    :as => :ckeditor,
    :label => name.to_s.titleize,
    :input_html => {
      :value => pb.present? ? pb.content : nil
    }
  },
  :use_input_field => false
}.deep_merge(options)

# Merge in legacy fields
[:label, :input_html].each{|o|
  settings[:field][o] = settings[o] if settings[o].present? }

render partial: 'polyblock/simple_fields_for',
       locals: { f: f, pb: pb, name: name, options: settings }

end



84
85
86
87
88
89
# File 'lib/polyblock/view_helpers.rb', line 84

def simple_form_for_polyblocks(options={})
  options = {
      :url => '/polyblock/update'
  }.deep_merge(options)
  simple_form_for(:polyblocks, options) do |f| yield(f) end
end