Class: Storefront::Components::Form::Select

Inherits:
Input show all
Defined in:
lib/storefront/components/form/inputs/select.rb

Overview

attributes: prompt, blank, multiple

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 Input

find, #initialize, registry, #render, resolves?

Methods inherited from Base

#initialize

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!, #initialize, #inside?, #pointer, #render, #render_with_pointer, #to_s

Constructor Details

This class inherits a constructor from Storefront::Components::Form::Input

Dynamic Method Handling

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

Instance Method Details

#boolean_collection(options = {}) ⇒ Object



49
50
51
# File 'lib/storefront/components/form/inputs/select.rb', line 49

def boolean_collection(options = {})
  [["Yes", true], ["No", false]]
end

#collectionObject



53
54
55
56
57
58
59
# File 'lib/storefront/components/form/inputs/select.rb', line 53

def collection
  if @collection.nil?
    @collection = Array(attributes.delete(:collection) || [])
  end

  @collection
end

#collection_iterator(collection, selected = nil) ⇒ Object



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
# File 'lib/storefront/components/form/inputs/select.rb', line 18

def collection_iterator(collection, selected = nil)
  collection.map do |item|
    name, options = item, {}
    optgroup = false
    case item
    when Array
      name            = item[0].to_s
      options[:value] = item[1].to_s
    when Hash
      name            = item[:name].to_s
      options[:value] = item[:value].to_s
      optgroup = item[:children].present?
    when ::String, ::Float, ::Integer, ::BigDecimal
      options[:value] = item.to_s
    else
      name            = item.name.strip.humanize
      options[:value] = item.id.to_s
    end
    if optgroup
      template.capture_haml do
        template.haml_tag :optgroup, :label => options[:name], :"data-value" => options[:value] do
          collection_iterator(item[:children], selected)
        end
      end
    else
      options[:selected] = "true" if selected.present? && options[:value] == selected
      template.haml_tag :option, name, options
    end
  end
end

#promptObject



61
62
63
64
65
66
# File 'lib/storefront/components/form/inputs/select.rb', line 61

def prompt
  if @prompt.nil? && include_blank
    @prompt     = localize(:prompts, attribute.name, attributes.delete(:prompt), :allow_blank => true)
  end
  @prompt
end

#select_input(options = {}) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/storefront/components/form/inputs/select.rb', line 8

def select_input(options = {})
  collection = self.collection
  collection = [[prompt, ""]] + collection if prompt.present?
  @value     = attributes.delete(:value).to_s

  base_input :select, attributes.merge(options) do
    collection_iterator(collection, value)
  end
end