Class: Bootstrap3Helper::Component

Inherits:
Object
  • Object
show all
Defined in:
lib/bootstrap3_helper/component.rb

Overview

Note:

Every component that inherits from this class, needs to call the parent initialization method! In order to properly render erb blocks within the proper context, we need the template. The only way to get this, is to pass in the template.

Note:

The ‘context` mentioned above, refers to the context of `@template` and not to be confused with `@context` that can be found in the sub classes. `@context` refers to the Bootstrap class context of the component.

This super class is meant to contain commonly used methods that all sub classes can leverage.

Instance Method Summary collapse

Constructor Details

#initialize(template) ⇒ Component

Used to ensure that the helpers always have the propert context for rendering and bindings.

Parameters:

  • template (ActionView)

    the context of the bindings



20
21
22
# File 'lib/bootstrap3_helper/component.rb', line 20

def initialize(template)
  @template = template
end

Instance Method Details

#concat(text) ⇒ Object

Used to pass all concat references to the template. This ensures proper binding. Concat adds a String to the template Output buffer. Useful when trying to add a String with no block.

Parameters:

  • text (String)


51
52
53
# File 'lib/bootstrap3_helper/component.rb', line 51

def concat(text)
  @template.concat(text)
end

#config(setting, fallback) ⇒ Mixed

Used to get config settings inside of components quicker.

Parameters:

  • setting (Symbol|String|Hash)

Returns:

  • (Mixed)


119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/bootstrap3_helper/component.rb', line 119

def config(setting, fallback)
  object = Bootstrap3Helper.config

  value  = (
    case setting
    when Hash
      object.send(setting.keys[0])[setting.values[0]] if object.send(setting.keys[0])
    when Symbol, String
      object.send(setting) if object.respond_to?(setting)
    end
  )

  value || fallback
end

#content_tag(name, content_or_options_with_block = nil, options = nil, escape = true, &block) ⇒ String

Used to pass all context of content_tag to the template. This ensures proper template binding of variables and methods!

Returns:

  • (String)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/bootstrap3_helper/component.rb', line 29

def (
  name,
  content_or_options_with_block = nil,
  options = nil,
  escape = true,
  &block
)
  @template.(
    name,
    content_or_options_with_block,
    options,
    escape,
    &block
  )
end

#parse_arguments(param_or_options, options, default) ⇒ Array #parse_arguments(options, default) ⇒ Array

Used to parse method arguments. If the first argument is a Hash, then it is assumed that the user left off the bootstrap contectual class. So we will assign it to ‘default` and return the Hash to be used as options.

Overloads:

  • #parse_arguments(param_or_options, options, default) ⇒ Array

    Parameters:

    • param_or_options (NilClass|Hash|Symbol|String)
    • options (Hash)
    • default (NilClass|String|Symbol)
  • #parse_arguments(options, default) ⇒ Array

    Parameters:

    • options (Hash)
    • default (NilClass|String|Symbol)

Returns:

  • (Array)


95
96
97
98
99
100
101
102
103
# File 'lib/bootstrap3_helper/component.rb', line 95

def parse_arguments(*args, default)
  first, second = *args
  case first
  when Hash, NilClass
    [default, first || second]
  when Symbol, String
    [first, second]
  end
end

#parse_context_or_options(*args) ⇒ Array

Used to parse method arguments. If the first argument is a Hash, then it is assumed that the user left off the bootstrap contectual class. So we will assign it to ‘default` and return the Hash to be used as options.

Parameters:

  • args (Hash|NilClass|String|Symbol)

Returns:

  • (Array)


63
64
65
# File 'lib/bootstrap3_helper/component.rb', line 63

def parse_context_or_options(*args)
  parse_arguments(*args, 'default')
end

#parse_tag_or_options(*args) ⇒ Array

Used to parse method arguments. If the first argument is a Hash, then it is assumed that the user left off the tag element. So we will assign it to NilClass and return the Hash to be used as options.

Parameters:

  • args (Hash|NilClass|String|Symbol)

Returns:

  • (Array)


75
76
77
# File 'lib/bootstrap3_helper/component.rb', line 75

def parse_tag_or_options(*args)
  parse_arguments(*args, nil)
end

#uuidString

Used to generate a (hopefully) unique ID for DOM elements. Used as a fallback if the user doesn’t specify one.

Returns:

  • (String)


110
111
112
# File 'lib/bootstrap3_helper/component.rb', line 110

def uuid
  (0...10).map { rand(65..90).chr }.join
end