Class: ExpressTemplates::Components::Forms::FormComponent

Inherits:
Configurable
  • Object
show all
Defined in:
lib/express_templates/components/forms/form_component.rb

Direct Known Subclasses

Checkbox, File, Hidden, Radio, Select, Submit, Textarea

Constant Summary

Constants inherited from Base

Base::MAP

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Capabilities::Configurable

included

Methods inherited from Base

abstract_component, abstract_component?, before_build, #build, builder_method, builder_method_and_class, contains, descendants, has_attributes, inherited, #initialize, require_parent, required_parent, tag

Constructor Details

This class inherits a constructor from ExpressTemplates::Components::Base

Instance Attribute Details

#input_attributesObject (readonly)

Returns the value of attribute input_attributes.



6
7
8
# File 'lib/express_templates/components/forms/form_component.rb', line 6

def input_attributes
  @input_attributes
end

Instance Method Details

#field_helper_optionsObject



62
63
64
# File 'lib/express_templates/components/forms/form_component.rb', line 62

def field_helper_options
  {id: field_id_attribute}.merge(input_attributes||nil)
end

#field_id_attributeObject



58
59
60
# File 'lib/express_templates/components/forms/form_component.rb', line 58

def field_id_attribute
  "#{resource_name.singularize}_#{field_name}"
end

#field_nameObject

Return the field_name as a string. This taken from the first argument to the component macro in the template or fragment.



44
45
46
# File 'lib/express_templates/components/forms/form_component.rb', line 44

def field_name
  (config[:id] || (@args.first.is_a?(String) && @args.first)).to_s
end

#field_name_attributeObject

Return the field name attribute. Currently handles only simple attributes on the resource. Does not handle attributes for associated resources.



54
55
56
# File 'lib/express_templates/components/forms/form_component.rb', line 54

def field_name_attribute
  "#{resource_name.singularize}[#{field_name}]"
end

#field_valueObject



48
49
50
# File 'lib/express_templates/components/forms/form_component.rb', line 48

def field_value
  resource.send(field_name)
end

#label_nameObject

Return the name attribute for the label



33
34
35
# File 'lib/express_templates/components/forms/form_component.rb', line 33

def label_name
  field_helper_options[:id]
end

#label_textObject

Return the text content for the label



38
39
40
# File 'lib/express_templates/components/forms/form_component.rb', line 38

def label_text
  config[:label] || field_name.titleize
end

#parent_formObject

Search the parent graph until we find an ExpressForm. Returns nil if none found.



67
68
69
70
71
72
73
# File 'lib/express_templates/components/forms/form_component.rb', line 67

def parent_form
  @my_form ||= parent
  until @my_form.nil? || @my_form.kind_of?(ExpressForm)
    @my_form = @my_form.parent
  end
  return @my_form
end

#resourceObject



18
19
20
# File 'lib/express_templates/components/forms/form_component.rb', line 18

def resource
  self.send(resource_name)
end

#resource_classObject



28
29
30
# File 'lib/express_templates/components/forms/form_component.rb', line 28

def resource_class
  parent_form.resource_class
end

#resource_nameObject

Lookup the resource_name from the parent ExpressForm.



23
24
25
26
# File 'lib/express_templates/components/forms/form_component.rb', line 23

def resource_name
  raise "FormComponent must have a parent form" unless parent_form
  parent_form.config[:id].to_s
end