Class: Satis::Dropdown::Component

Inherits:
ViewComponent::Base
  • Object
show all
Defined in:
app/components/satis/dropdown/component.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(form:, attribute:, **options, &block) ⇒ Component

Returns a new instance of Component.



8
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
# File 'app/components/satis/dropdown/component.rb', line 8

def initialize(form:, attribute:, **options, &block)
  super

  @form = form
  @attribute = attribute
  @title = title
  @options = options
  @url = options[:url]
  @chain_to = options[:chain_to]
  @free_text = options[:free_text]
  @needs_exact_match = options[:needs_exact_match]
  @reset_button = options[:reset_button] || options[:include_blank]
  @toggle_button = options[:toggle_button] != false

  options[:input_html] ||= {}

  options[:input_html][:value] = hidden_value

  options[:input_html][:autofocus] ||= false
  if options[:input_html][:autofocus]
    options[:autofocus] = "autofocus"
    options[:input_html].delete(:autofocus)
  end

  unless options[:input_html]["data-reflex"]
    actions = [options[:input_html]["data-action"], "change->satis-dropdown#display",
               "focus->satis-dropdown#focus"].join(" ")
  end

  options[:input_html].merge!("data-satis-dropdown-target" => "hiddenSelect",
                              "data-action" => actions)

  @block = block
  @page_size = options[:page_size] || 25
end

Instance Attribute Details

#attributeObject (readonly)

Returns the value of attribute attribute.



6
7
8
# File 'app/components/satis/dropdown/component.rb', line 6

def attribute
  @attribute
end

#formObject (readonly)

Returns the value of attribute form.



6
7
8
# File 'app/components/satis/dropdown/component.rb', line 6

def form
  @form
end

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'app/components/satis/dropdown/component.rb', line 6

def options
  @options
end

#titleObject (readonly)

Returns the value of attribute title.



6
7
8
# File 'app/components/satis/dropdown/component.rb', line 6

def title
  @title
end

#urlObject (readonly)

Returns the value of attribute url.



6
7
8
# File 'app/components/satis/dropdown/component.rb', line 6

def url
  @url
end

Instance Method Details

#custom_item_html?Boolean

Returns:

  • (Boolean)


105
106
107
# File 'app/components/satis/dropdown/component.rb', line 105

def custom_item_html?
  !!@block
end

#hidden_valueObject

Deal with context



45
46
47
48
49
50
51
52
53
54
# File 'app/components/satis/dropdown/component.rb', line 45

def hidden_value
  value = @options[:selected]
  value ||= @options.dig(:input_html, :value)
  value ||= form.object&.send(attribute)

  value = value.id if value.respond_to?(:id)

  value = value.second if value.is_a?(Array) && value.size == 2 && value.first.casecmp?(value.second)
  value
end

#item_html(item) ⇒ Object



109
110
111
# File 'app/components/satis/dropdown/component.rb', line 109

def item_html(item)
  form.template.capture { @block.call(item) }
end

#option_value(item) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/components/satis/dropdown/component.rb', line 66

def option_value(item)
  text = value = ""

  if item.respond_to?(:id)
    value = item.send(:id)
    text = if item.respond_to?(:name)
              item.send(:name)
           else
              ""
           end
  elsif item.is_a?(Array)
    value = item.first
    text = item.second
  elsif item.is_a?(String)
    text = value = item
  end

  return nil if value.blank?
  [text, item, {selected: true}]
end

#options_array(obj) ⇒ Object



56
57
58
59
60
61
62
63
64
# File 'app/components/satis/dropdown/component.rb', line 56

def options_array(obj)
  return [[]] unless obj

  if obj.is_a?(Array)
    obj.filter_map {|item| option_value(item) }
  else
    [option_value(obj)]
  end
end

#placeholderObject



87
88
89
90
91
92
93
94
95
# File 'app/components/satis/dropdown/component.rb', line 87

def placeholder
  return title if title.present?

  if form.object.class.respond_to?(:human_attribute_name)
    form.object.class.human_attribute_name(attribute)
  else
    attribute.to_s.humanize
  end
end

#text_methodObject



101
102
103
# File 'app/components/satis/dropdown/component.rb', line 101

def text_method
  options[:text_method] || :name
end

#value_methodObject



97
98
99
# File 'app/components/satis/dropdown/component.rb', line 97

def value_method
  options[:value_method] || :id
end