Module: PureAdmin::PortletHelper

Defined in:
app/helpers/pure_admin/portlet_helper.rb

Overview

Helper methods to assist in building Pure Admin portlets.

Instance Method Summary collapse

Instance Method Details

#portlet(title, options = {}) { ... } ⇒ Object

Renders a “portlet” to the view.

Parameters:

  • title (String)
  • options (Hash) (defaults to: {})

    a container for options to the portlet.

  • options (:portlet_html) (defaults to: {})

    (Hash) all options that can be passed to content_tag are respected here.

  • options (:heading_html) (defaults to: {})

    (Hash) all options that can be passed to content_tag are respected here.

  • options (:body_html) (defaults to: {})

    (Hash) all options that can be passed to content_tag are respected here.

Yields:

  • The contents of the portlet



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
70
71
72
73
74
# File 'app/helpers/pure_admin/portlet_helper.rb', line 12

def portlet(title, options = {}, &block)
  portlet_html = options[:portlet_html] || {}
  portlet_html[:class] = ['portlet clear-fix', portlet_html[:class]]
  portlet_html[:data] ||= {}
  portlet_html[:data][:source] = options[:source] unless block_given?

  heading_html = options.delete(:heading_html) || {}
  heading_html[:class] = merge_html_classes('portlet-heading', heading_html[:class])

  body_html = options.delete(:body_html) || {}
  body_html[:class] = merge_html_classes('portlet-body clear-fix', body_html[:class])

  if options[:icon]
    icon = (:i, nil, class: "portlet-heading-icon fa fa-fw fa-#{options[:icon].to_s.dasherize}")
  end

  # This determines if the portlet can be closed. If it is remote it can be closed. If not,
  # it is determined by the presence of expand in the options.
  # e.g. <%= portlet 'A title', expand: true do %>...
  closable = options.key?(:source) || options.key?(:expand)

  portlet_html[:data][:closable] = closable

  controls_content = ''.html_safe
  controls = options.delete(:controls)
  if controls.present?
    if controls.respond_to?(:each)
      controls.each do |control|
        controls_content << (:span, control, class: 'portlet-control-item')
      end
    else
      controls_content << (:span, controls, class: 'portlet-control-item')
    end
  end

  if closable
    controls_content << (:span, nil, class: 'portlet-indicator')
  end

  if controls_content.present?
    controls_wrapper = (:div, controls_content, class: 'portlet-controls')
  end

  # This determines if the portlet should be expanded by default. If it is explicitly given that
  # takes precedence. If not, by default all remote portlets are not expanded and all static
  # portlets are.
  expand = options.key?(:expand) ? options[:expand] : block_given?

  if expand
    portlet_html[:class] << 'expanded'
    portlet_html[:data][:expand] = true
  end

  heading_content = (:div, heading_html) do
    (icon || ''.html_safe) + (:h4, title) + (controls_wrapper || ''.html_safe)
  end

  body_content = (:div, (capture(&block) if block_given?), body_html)

  (:div, portlet_html) do
    heading_content + body_content
  end
end