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

Inherits:
Base
  • Object
show all
Includes:
Capabilities::Adoptable, Capabilities::Configurable
Defined in:
lib/express_templates/components/forms/form_component.rb

Direct Known Subclasses

Checkbox, Hidden, Radio, Select, Submit, Textarea

Instance Attribute Summary

Attributes inherited from Expander

#handlers, #locals, #stack, #template

Instance Method Summary collapse

Methods included from Capabilities::Adoptable

included

Methods included from Capabilities::Configurable

included

Methods inherited from Base

inherited

Methods included from Capabilities::Iterating

included

Methods included from Capabilities::Wrapping

included

Methods included from Capabilities::Rendering

included

Methods included from Capabilities::Templating

included

Methods included from Macro

included

Methods inherited from Expander

#expand, #initialize, #initialize_expander, #method_missing, #process_children!, register_macros_for

Constructor Details

This class inherits a constructor from ExpressTemplates::Expander

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ExpressTemplates::Expander

Instance Method Details

#compile(*args) ⇒ Object



8
9
10
11
# File 'lib/express_templates/components/forms/form_component.rb', line 8

def compile(*args)
  raise "#{self.class} requires a parent ExpressForm" if parent.nil? or parent_form.nil?
  super(*args)
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.



42
43
44
# File 'lib/express_templates/components/forms/form_component.rb', line 42

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.



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

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

#field_wrapper_classObject



52
53
54
# File 'lib/express_templates/components/forms/form_component.rb', line 52

def field_wrapper_class
  @config[:wrapper_class] || 'field-wrapper'
end

#label_nameObject

Return the name attribute for the lable



31
32
33
# File 'lib/express_templates/components/forms/form_component.rb', line 31

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

#label_textObject

Return the text content for the label



36
37
38
# File 'lib/express_templates/components/forms/form_component.rb', line 36

def label_text
  @options[:label] || field_name.titleize
end

#parent_formObject

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



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

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

#resource_classObject



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

def resource_class
  if namespace = parent_form.namespace
    "#{namespace}/#{resource_name}".classify
  else
    resource_name.classify
  end
end

#resource_nameObject

Lookup the resource_name from the parent ExpressForm.



14
15
16
# File 'lib/express_templates/components/forms/form_component.rb', line 14

def resource_name
  parent_form.resource_name
end

#resource_varObject



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

def resource_var
  resource_name.to_sym
end