Class: Storefront::Components::Form::Input

Inherits:
Base
  • Object
show all
Defined in:
lib/storefront/components/form/input.rb

Direct Known Subclasses

Checkbox, Date, File, Hidden, Radio, Range, Select, String, Submit, Textarea, Value

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

Class Attribute Summary collapse

Attributes inherited from Base

#options, #template

Class Method Summary collapse

Instance Method Summary collapse

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 = {}) ⇒ Input

Returns a new instance of Input.



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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
# File 'lib/storefront/components/form/input.rb', line 32

def initialize(options = {})
  super

  # input type
  options[:as]    ||= attribute.input_type(options)
  @input_type     = options[:as]
  @include_blank  = options[:include_blank] != false
  @value          = options[:value]
  @dynamic        = options[:dynamic] == true
  @rich_input     = options.has_key?(:rich_input) ? !!options[:rich_input] : config.rich_input

  @validate       = attributes.delete(:validate) != false

  classes = [input_type, self.class.name.split("::").last.underscore, attribute.name.to_s.underscore.strip.gsub(/[_\s]+/, config.separator)]

  if options[:input_html].present?
    classes << options[:input_html].delete(:class)
  end

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

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

  # id
  attributes[:id] ||= attribute.to_id(:index => index, :parent_index => parent_index) if config.id_enabled_on.include?("input")

  # validations
  if @validate
    attributes.merge!(attribute.validations) if config.inline_validations && attribute.validations.present?
  end  
  attributes[:placeholder] = options[:placeholder] if options[:placeholder].present?

  # name
  attributes[:name]     ||= attribute.to_param(:index => index, :parent_index => parent_index)

  # value
  attributes[:value]    ||= attribute.value(options[:value])

  # attributes[:tabindex]      = @tabindex
  attributes[:maxlength]       = options[:max] if options[:max].present?

  # expressions
  pattern                      = options[:match]
  pattern                      = pattern.source if pattern.is_a?(::Regexp)
  attributes[:"data-match"]    = pattern if pattern.present?
  attributes[:"aria-required"] = attribute.required.to_s if attribute.required?

  # access key
  access_key                   = attributes[:accesskey] || attribute.access_key
  attributes[:accesskey]       = access_key

  attributes.merge!(options[:input_html]) if options[:input_html]

  attributes.delete :include_blank
  attributes.delete :input_html
  attributes.delete :include_template
  attributes.delete :as
  attributes.delete :dynamic
  attributes.delete :parent_index

  attributes[:required] = "true" if attributes.delete(:required) == true
  attributes[:disabled] = "true" if attributes.delete(:disabled) == true
  attributes[:autofocus] = "true" if attributes.delete(:autofocus) == true
  attributes[:"data-dynamic"] = "true" if @dynamic

  attributes[:title] ||= attributes[:placeholder] if attributes[:placeholder].present?

  autocomplete = attributes.delete(:autocomplete)
  if autocomplete && config.include_aria
    attributes[:"aria-autocomplete"] = case autocomplete
    when :inline, :list, :both
      autocomplete.to_s
    else
      "both"
    end
  end
end

Dynamic Method Handling

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

Class Attribute Details

.resolves(*input_types) ⇒ Object

Returns the value of attribute resolves.



6
7
8
# File 'lib/storefront/components/form/input.rb', line 6

def resolves
  @resolves
end

Class Method Details

.find(name) ⇒ Object



25
26
27
28
29
# File 'lib/storefront/components/form/input.rb', line 25

def find(name)
  registry.detect do |input_class|
    input_class.resolves?(name)
  end
end

.registryObject



18
19
20
21
22
23
# File 'lib/storefront/components/form/input.rb', line 18

def registry
  @registry ||= Dir[::File.join(::File.dirname(__FILE__), "inputs", "*")].map do |path|
    next unless ::File.extname(path) == ".rb"
    "Storefront::Components::Form::#{::File.basename(path, ::File.extname(path)).camelize}".constantize
  end.compact
end

.resolves?(name) ⇒ Boolean

Returns:

  • (Boolean)


13
14
15
16
# File 'lib/storefront/components/form/input.rb', line 13

def resolves?(name)
  @resolves ||= []
  @resolves.include?(name)
end

Instance Method Details

#render(&block) ⇒ Object



121
122
123
# File 'lib/storefront/components/form/input.rb', line 121

def render(&block)
  send(:"#{@input_type}_input")
end