Class: Lifeform::Libraries::Default::Input
- Inherits:
-
Object
- Object
- Lifeform::Libraries::Default::Input
- Includes:
- Streamlined::Renderable
- Defined in:
- lib/lifeform/libraries/default/input.rb
Direct Known Subclasses
Constant Summary collapse
- WRAPPER_TAG =
:form_field
- INPUT_TAG =
:input
Instance Attribute Summary collapse
-
#attributes ⇒ Object
readonly
Returns the value of attribute attributes.
-
#field_definition ⇒ Object
readonly
Returns the value of attribute field_definition.
-
#form ⇒ Object
readonly
Returns the value of attribute form.
Instance Method Summary collapse
- #handle_labels ⇒ Object
-
#initialize(form, field_definition, **attributes) ⇒ Input
constructor
A new instance of Input.
- #model_name ⇒ Object
-
#template(&block) ⇒ Object
rubocop:disable Metrics/AbcSize.
- #value_for_model ⇒ Object
-
#verify_attributes ⇒ Object
rubocop:disable Metrics.
Constructor Details
#initialize(form, field_definition, **attributes) ⇒ Input
Returns a new instance of Input.
14 15 16 17 18 19 20 21 |
# File 'lib/lifeform/libraries/default/input.rb', line 14 def initialize(form, field_definition, **attributes) @form = form @field_definition = field_definition @attributes = Lifeform::Form.parameters_to_attributes(field_definition.parameters).merge(attributes) @field_type = field_definition.type verify_attributes end |
Instance Attribute Details
#attributes ⇒ Object (readonly)
Returns the value of attribute attributes.
9 10 11 |
# File 'lib/lifeform/libraries/default/input.rb', line 9 def attributes @attributes end |
#field_definition ⇒ Object (readonly)
Returns the value of attribute field_definition.
9 10 11 |
# File 'lib/lifeform/libraries/default/input.rb', line 9 def field_definition @field_definition end |
#form ⇒ Object (readonly)
Returns the value of attribute form.
9 10 11 |
# File 'lib/lifeform/libraries/default/input.rb', line 9 def form @form end |
Instance Method Details
#handle_labels ⇒ Object
43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/lifeform/libraries/default/input.rb', line 43 def handle_labels label_text = attributes[:label] label_name = (attributes[:id] || attributes[:name]).to_s @attributes = attributes.filter_map { |k, v| [k, v] unless k == :label }.to_h -> { <<~HTML <label for="#{text -> { label_name }}">#{text label_text}</label> HTML } end |
#model_name ⇒ Object
35 36 37 38 39 |
# File 'lib/lifeform/libraries/default/input.rb', line 35 def model_name name_of_model = @form.class.name_of_model(@model) form.parent_name ? "#{form.parent_name}[#{name_of_model}]" : name_of_model end |
#template(&block) ⇒ Object
rubocop:disable Metrics/AbcSize
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/lifeform/libraries/default/input.rb', line 55 def template(&block) # rubocop:disable Metrics/AbcSize return "" if !@if.nil? && !@if input_tag = dashed self.class.const_get(:INPUT_TAG) closing_tag = input_tag != "input" field_body = html -> { <<~HTML # rubocop:disable Bridgetown/InsecureHeredoc #{html(@label || -> {}).to_s.strip} <#{input_tag} #{html_attributes type: @field_type.to_s, **@attributes}>#{ "</#{input_tag}>" if closing_tag } #{html -> { capture(self, &block) } if block} HTML } return field_body unless self.class.const_get(:WRAPPER_TAG) wrapper_tag = dashed self.class.const_get(:WRAPPER_TAG) html -> { <<~HTML # rubocop:disable Bridgetown/InsecureHeredoc <#{wrapper_tag} #{html_attributes name: @attributes[:name]}>#{field_body.to_s.strip}</#{wrapper_tag}> HTML } end |
#value_for_model ⇒ Object
41 |
# File 'lib/lifeform/libraries/default/input.rb', line 41 def value_for_model = @model.send(attributes[:name]) |
#verify_attributes ⇒ Object
rubocop:disable Metrics
23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/lifeform/libraries/default/input.rb', line 23 def verify_attributes # rubocop:disable Metrics @model = attributes.delete(:model) @model = form.model if form.model && @model.nil? @if = attributes.delete(:if) attributes[:value] ||= value_for_model if form.model attributes[:name] = "#{model_name}[#{attributes[:name]}]" if @model # TODO: validate if this is enough attributes[:id] ||= attributes[:name].tr("[]", "_").gsub("__", "_").chomp("_") if attributes[:name] @label = handle_labels if attributes[:label] end |