Class: Storefront::Components::Form::Field

Inherits:
Builder show all
Defined in:
lib/storefront/components/form/field.rb

Overview

input :name, :as => :string, :class => “string something”, :label_method => x, :value_method => y disabled, readonly, accesskey, tabindex button, checkbox, color, date, datetime, datetime-local, email, file, hidden, image, month, number, password, radio, range , reset, search, submit, tel, text, time, url, week input :phone, :mask => /ddd-ddd-dddd/, :hint => “Enter your phone number”, :tip => “This is so we can call you.”

Constant Summary

Constants included from Helpers::ContentHelper

Helpers::ContentHelper::SCOPES, Helpers::ContentHelper::SCOPES_WITH_NAMESPACE, Helpers::ContentHelper::SCOPES_WITH_NAMESPACE_AND_NESTED_MODEL, Helpers::ContentHelper::SCOPES_WITH_NESTED_MODEL

Instance Attribute Summary

Attributes inherited from Base

#options, #template

Instance Method Summary collapse

Methods inherited from Builder

#button, #field, #fields, #fields_for, #fieldset, #partial, #submit

Methods included from Helpers::DomHelper

#aria, #clone_attributes, #dom, #index_class, #merge_class, #merge_class!, #page, #resource_to_title, #title_widget, #title_widget_options

Methods included from Helpers::ContentHelper

#encoded_contents, #font_face_data_uri, #html5_time, #read_binary_file, #read_image_size, #rendered_text, #sanitize, #t?

Methods inherited from Base

#component_name, #extract_classes!, #extract_options!, #inside?, #pointer, #render_with_pointer, #to_s

Constructor Details

#initialize(options = {}) ⇒ Field

Returns a new instance of Field.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/storefront/components/form/field.rb', line 9

def initialize(options = {})
  super
  
  # input type
  options[:as]    ||= attribute.input_type(options)
  @input_type     = options[:as]
  @inputs         = []

  # class

  classes = [config.field_class, input_type]

  unless [:submit, :fieldset].include?(input_type)
    classes += [
      attribute.required? ? config.required_class : config.optional_class, 
      attribute.errors? ? config.error_class : config.valid_class,
    ]
    if options[:validate] != false && attribute.validations.present?
      classes << config.validate_class
    end
  end

  merge_class! attributes, *classes.compact.uniq.map(&:to_s)

  # id
  if attributes[:id].blank? && config.id_enabled_on.include?("field")
    attributes[:id] = attribute.to_id(:type => :field, :index => index, :parent_index => parent_index)
  end

  unless [:hidden, :submit].include?(input_type)
    # errors
    @errors           = Storefront::Components::Form::Errors.new(options.slice(:rich_input, :error_html, :error, :model, :index, :parent_index, :attribute, :template))
  
    # label
    @label            = Storefront::Components::Form::Label.new(options.slice(:rich_input, :label_html, :label, :model, :index, :parent_index, :attribute, :template))
  
    # hint
    @hints            = Storefront::Components::Form::Hint.new(options.slice(:rich_input, :hint_html, :hint, :model, :index, :parent_index, :attribute, :template))
  end

  unless input_type == :fieldset
    # inputs
    @input_attributes = default_options!.merge(attributes.except(:id, :class, :field_html, :attributes, :error_html, :label_html, :hint_html))
  end

  merge_class! options[:field_html], attributes[:class]

  @attributes       = options[:field_html].merge(:id => attributes[:id])
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Storefront::Components::Base

Instance Method Details

#input(*args) ⇒ Object



59
60
61
62
63
64
# File 'lib/storefront/components/form/field.rb', line 59

def input(*args)
  #@tabindex                    = @tabindex + 1
  options = args.extract_options!
  key     = args.shift || attribute.name
  @inputs << input_for(input_type, key, options)
end

#render(&block) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/storefront/components/form/field.rb', line 66

def render(&block)
  template.capture_haml do
    template.haml_tag :li, attributes do
      input(attribute.name) unless block_given?
      elements = extract_elements!(attributes)
    
      result = elements.map do |element|
        Array(send(element)).map(&:render)
      end
      template.haml_concat result.flatten.join.gsub(/\n$/, "") if result.present?
    
      yield(self) if block_given? # template.capture_haml(self, &block)
    end
  end
end