Class: Formular::Element

Inherits:
Object
  • Object
show all
Extended by:
Uber::InheritableAttr
Defined in:
lib/formular/element.rb,
lib/formular/elements.rb,
lib/formular/element/module.rb,
lib/formular/element/bootstrap3.rb,
lib/formular/element/bootstrap4.rb,
lib/formular/element/foundation6.rb,
lib/formular/element/modules/hint.rb,
lib/formular/element/modules/error.rb,
lib/formular/element/modules/label.rb,
lib/formular/element/modules/control.rb,
lib/formular/element/modules/checkable.rb,
lib/formular/element/modules/container.rb,
lib/formular/element/modules/collection.rb,
lib/formular/element/bootstrap3/horizontal.rb,
lib/formular/element/bootstrap4/horizontal.rb,
lib/formular/element/bootstrap3/input_group.rb,
lib/formular/element/foundation6/input_group.rb,
lib/formular/element/modules/wrapped_control.rb,
lib/formular/element/bootstrap3/column_control.rb,
lib/formular/element/bootstrap4/custom_control.rb,
lib/formular/element/foundation6/wrapped_control.rb,
lib/formular/element/bootstrap3/checkable_control.rb,
lib/formular/element/bootstrap4/checkable_control.rb,
lib/formular/element/foundation6/checkable_control.rb

Overview

The Element class is responsible for defining what the html should look like. This includes default attributes, and the function to use to render the html actual rendering is done via a HtmlBlock class

Direct Known Subclasses

ErrorNotification, Submit

Defined Under Namespace

Modules: Bootstrap3, Bootstrap4, Foundation6, Module, Modules Classes: Button, Checkbox, Error, ErrorNotification, Form, Hidden, Input, Label, Radio, Select, Submit, Textarea

Constant Summary collapse

Container =

These three are really just provided for convenience when creating other elements

Class.new(Formular::Element) { include Formular::Element::Modules::Container }
Control =
Class.new(Formular::Element) { include Formular::Element::Modules::Control }
WrappedControl =
Class.new(Formular::Element) { include Formular::Element::Modules::WrappedControl }
Option =

define some base classes to build from or easily use elsewhere

Class.new(Container) { tag :option }
OptGroup =
Class.new(Container) { tag :optgroup }
Fieldset =
Class.new(Container) { tag :fieldset }
Legend =
Class.new(Container) { tag :legend }
Div =
Class.new(Container) { tag :div }
P =
Class.new(Container) { tag :p }
Span =
Class.new(Container) { tag :span }
Small =
Class.new(Container) { tag :small }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options, &block) ⇒ Element

Returns a new instance of Element.



64
65
66
67
68
69
70
# File 'lib/formular/element.rb', line 64

def initialize(**options, &block)
  @builder = options.delete(:builder)
  normalize_attributes(options)
  @block = block
  @tag = self.class.tag_name
  @html_blocks = define_html_blocks
end

Instance Attribute Details

#attributesObject (readonly)

Returns the value of attribute attributes.



71
72
73
# File 'lib/formular/element.rb', line 71

def attributes
  @attributes
end

#builderObject (readonly)

Returns the value of attribute builder.



71
72
73
# File 'lib/formular/element.rb', line 71

def builder
  @builder
end

#html_blocksObject (readonly)

Returns the value of attribute html_blocks.



71
72
73
# File 'lib/formular/element.rb', line 71

def html_blocks
  @html_blocks
end

#optionsObject (readonly)

Returns the value of attribute options.



71
72
73
# File 'lib/formular/element.rb', line 71

def options
  @options
end

#tagObject (readonly)

Returns the value of attribute tag.



71
72
73
# File 'lib/formular/element.rb', line 71

def tag
  @tag
end

Class Method Details

.add_option_keys(*keys) ⇒ Object

blacklist the keys that should NOT end up as html attributes



45
46
47
# File 'lib/formular/element.rb', line 45

def self.add_option_keys(*keys)
  self.option_keys += keys
end

.call(**options, &block) ⇒ Object



60
61
62
# File 'lib/formular/element.rb', line 60

def self.call(**options, &block)
  new(**options, &block)
end

.html(context = :default, &block) ⇒ Object

define what your html should look like this block is executed in the context of an HtmlBlock instance



33
34
35
# File 'lib/formular/element.rb', line 33

def self.html(context = :default, &block)
  self.html_blocks[context] = block
end

.rename_html_context(old_context, new_context) ⇒ Object

a convenient way of changing the key for a context useful for inheritance if you want to replace a context but still access the original function



40
41
42
# File 'lib/formular/element.rb', line 40

def self.rename_html_context(old_context, new_context)
  self.html_blocks[new_context] = self.html_blocks.delete(old_context)
end

.set_default(key, value, condition = {}) ⇒ Object

set the default value of an option or attribute you can make this conditional by providing a condition e.g. if: :some_method or unless: :some_method



27
28
29
# File 'lib/formular/element.rb', line 27

def self.set_default(key, value, condition = {})
  self.default_hash[key] = { value: value, condition: condition }
end

.tag(name) ⇒ Object

define the name of the html tag for the element e.g. tag :span tag ‘input’ Note that if you leave this out, the tag will be inferred based on the name of your class Also, this is not inherited



56
57
58
# File 'lib/formular/element.rb', line 56

def self.tag(name)
  self.tag_name = name
end

Instance Method Details

#to_html(context: nil) ⇒ Object Also known as: to_s



73
74
75
76
# File 'lib/formular/element.rb', line 73

def to_html(context: nil)
  context ||= self.class.html_context
  html_blocks[context].call
end