Class: Booties::Panel

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Utils
Defined in:
lib/booties/panel.rb

Overview

:nodoc:

Constant Summary collapse

CSS_CLASSES =
{
  heading: 'panel-heading',
  title: 'panel-title',
  body: 'panel-body',
  footer: 'panel-footer',
}.freeze

Instance Method Summary collapse

Methods included from Utils

merge_classes

Constructor Details

#initialize(template, context: :default, wrapper_tag: 'div', **options) ⇒ Panel

Instantiates a new Panel. Several helper methods like #content_tag will be delegated to template.

The optional context argument can be passed in to specify the panel context; otherwise, the context :default is used.

The default behavior is to use a div tag for to top-level panel container, but you may specify a different tag using the wrapper_tag keyword. (There are no guarantees on whether or not other tags will work correctly with Bootstrap panels.

Any additional options will be included as attributes on the top-level panel container.



31
32
33
34
35
36
37
# File 'lib/booties/panel.rb', line 31

def initialize(template, context: :default, wrapper_tag: 'div', **options)
  @template    = template
  @wrapper_tag = wrapper_tag
  @context     = context
  # TODO: pass options to #render instead of constructor
  @options     = options
end

Instance Method Details

#body(content = nil, class: nil, **options, &block) ⇒ Object

Renders the panel body. The content of the body can be passed in through the content parameter or as a block. If class is given, it will be merged with the required panel body class. Additional keyword arguments will be passed as an options Hash to #content_tag.



83
84
85
86
87
88
# File 'lib/booties/panel.rb', line 83

def body(content = nil, class: nil, **options, &block)
  content ||= capture(&block)
  classes   = merge_classes CSS_CLASSES[:body],
                            binding.local_variable_get(:class)
   :div, content, class: classes, **options
end

Renders the panel footer. The content of the footer can be passed in through the content parameter or as a block. If class is given, it will be merged with the required panel footer class. Additional keyword arguments will be passed as an options Hash to #content_tag.



95
96
97
98
99
100
# File 'lib/booties/panel.rb', line 95

def footer(content = nil, class: nil, **options, &block)
  content ||= capture(&block)
  classes   = merge_classes CSS_CLASSES[:footer],
                            binding.local_variable_get(:class)
   :div, content, class: classes, **options
end

#heading(content = nil, class: nil, **options, &block) ⇒ Object

Renders the panel heading. The content of the heading can be passed in through the content parameter or as a block. If class is given, it will be merged with the required panel heading class. Additional keyword arguments will be passed as an options Hash to #content_tag.



59
60
61
62
63
64
# File 'lib/booties/panel.rb', line 59

def heading(content = nil, class: nil, **options, &block)
  content ||= capture(&block)
  classes   = merge_classes CSS_CLASSES[:heading],
                            binding.local_variable_get(:class)
   :div, content, class: classes, **options
end

#render(&block) ⇒ Object

Renders the top-level div for the panel. @context is used to specify the panel’s context. The content is captured from block. The Panel object will be passed as a parameter to block.



45
46
47
48
49
50
51
52
# File 'lib/booties/panel.rb', line 45

def render(&block)
  options = @options.dup
  classes = merge_classes %W[panel panel-#{@context}],
                          options.delete(:class)
   @wrapper_tag, class: classes, **options do
    capture self, &block
  end
end

#title(content = nil, class: nil, **options, &block) ⇒ Object

Renders the panel title. The content of the title can be passed in through the content paramter or as a block. If class is given, it will be merged with the required panel title class. Additional keyword arguments will be passed as an options Hash to #content_tag.



71
72
73
74
75
76
# File 'lib/booties/panel.rb', line 71

def title(content = nil, class: nil, **options, &block)
  content ||= capture(&block)
  classes   = merge_classes CSS_CLASSES[:title],
                            binding.local_variable_get(:class)
   :h3, content, class: classes, **options
end