Class: TaoForm::Components::SelectComponent

Inherits:
FieldComponent
  • Object
show all
Defined in:
lib/tao_form/components/select_component.rb

Instance Attribute Summary collapse

Attributes inherited from FieldComponent

#attribute_name, #builder

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(view, builder = nil, attribute_name = nil, choices = nil, options = {}) ⇒ SelectComponent



7
8
9
10
11
12
13
14
15
# File 'lib/tao_form/components/select_component.rb', line 7

def initialize view, builder = nil, attribute_name = nil, choices = nil, options = {}
  super view, builder, attribute_name, options
  @choices = choices
  @multiple = @options[:multiple].presence || false

  init_result_options
  init_list_options
  init_remote_options
end

Instance Attribute Details

#block_for_renderObject (readonly)

Returns the value of attribute block_for_render.



5
6
7
# File 'lib/tao_form/components/select_component.rb', line 5

def block_for_render
  @block_for_render
end

#choicesObject (readonly)

Returns the value of attribute choices.



5
6
7
# File 'lib/tao_form/components/select_component.rb', line 5

def choices
  @choices
end

#list_optionsObject (readonly)

Returns the value of attribute list_options.



5
6
7
# File 'lib/tao_form/components/select_component.rb', line 5

def list_options
  @list_options
end

#multipleObject (readonly)

Returns the value of attribute multiple.



5
6
7
# File 'lib/tao_form/components/select_component.rb', line 5

def multiple
  @multiple
end

#result_optionsObject (readonly)

Returns the value of attribute result_options.



5
6
7
# File 'lib/tao_form/components/select_component.rb', line 5

def result_options
  @result_options
end

Class Method Details

.component_nameObject



54
55
56
# File 'lib/tao_form/components/select_component.rb', line 54

def self.component_name
  :select
end

Instance Method Details

#clearableObject



38
39
40
# File 'lib/tao_form/components/select_component.rb', line 38

def clearable
  @clearable ||= options.delete(:clearable) || result_options[:include_blank].present? || result_options[:prompt].present?
end

#placeholderObject



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tao_form/components/select_component.rb', line 42

def placeholder
  @placeholder ||= if (text = options.delete(:placeholder))
    text
  elsif result_options[:include_blank].present? && result_options[:include_blank].is_a?(String)
    result_options[:include_blank]
  elsif result_options[:prompt].present? && result_options[:prompt].is_a?(String)
    result_options[:prompt]
  else
    t :placeholder
  end
end

#render(&block) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
# File 'lib/tao_form/components/select_component.rb', line 17

def render &block
  @block_for_render = block
  if view.request.variant.mobile?
    super
  else
    view. tag_name, html_options do
      view.concat render_result
      view.concat render_list
    end
  end
end

#render_listObject



34
35
36
# File 'lib/tao_form/components/select_component.rb', line 34

def render_list
  view.tao_select_list list_options
end

#render_resultObject



29
30
31
32
# File 'lib/tao_form/components/select_component.rb', line 29

def render_result
  result_method = multiple ? :tao_multiple_select_result : :tao_select_result
  view.send(result_method, builder, attribute_name, choices, result_options, &block_for_render)
end