Class: NfgUi::Bootstrap::Components::Base

Inherits:
Object
  • Object
show all
Includes:
ActionView::Context, ActionView::Helpers::AssetTagHelper, ActionView::Helpers::TagHelper, ActionView::Helpers::TextHelper
Defined in:
lib/nfg_ui/bootstrap/components/base.rb

Overview

Base Component Defines conventional, shared behavior across Bootstrap components

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(component_options, view_context) ⇒ Base

Returns a new instance of Base.



20
21
22
23
24
25
26
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 20

def initialize(component_options, view_context)
  self.options = defaults.merge!(component_options)
  self.view_context = view_context
  @body = options.fetch(:body, '')
  utility_initialize
  component_initialize
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



15
16
17
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 15

def body
  @body
end

#optionsObject

Returns the value of attribute options.



17
18
19
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 17

def options
  @options
end

#view_contextObject

Returns the value of attribute view_context.



17
18
19
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 17

def view_context
  @view_context
end

Instance Method Details

#component_familyObject

This is used to help identify where to find partials for rendering components.

Set the component family, e.g.: :breadcrumb on any sibling components.

For example: BreadcrumbItem & Breadcrumb are members of the :breadcrumb component_family



48
49
50
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 48

def component_family
  nil
end

#dataObject



52
53
54
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 52

def data
  options[:data] || {}
end

#hrefObject

Use view_context url_for method to ensure that when objects are passed into href, e.g. ‘href: @admin` will convert it to the correct path

Likewise that hashes are also parsed correctly example: href: { controller: ‘admin’, action: ‘show’, id: 7 }



76
77
78
79
80
81
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 76

def href
  options_href = options[:href]

  return if options_href.blank?
  view_context.url_for(options_href)
end

#html_optionsObject



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 56

def html_options
  options.except(*non_html_attribute_options.uniq)
         .merge!(id: id,
                 class: css_classes,
                 data: data,
                 href: href,
                 style: style,
                 **assistive_html_attributes)
         .reject { |_k, v| v.blank? } # prevent empty attributes from showing up
                                      # Example: <div class>Text</div>
end

#idObject



83
84
85
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 83

def id
  options[:id]
end

#renderObject

This base render handles many of the components and can be changed to have a different base element by overriding the base_element. in some cases, the child component can also call super with a block to have this render as the wrapping element.



35
36
37
38
39
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 35

def render
  (base_element, html_options) do
    (block_given? ? yield : body)
  end
end

#styleObject



87
88
89
# File 'lib/nfg_ui/bootstrap/components/base.rb', line 87

def style
  options[:style]
end