Class: Inputs::EffectivePanelSelect::Input

Inherits:
Effective::FormInput show all
Defined in:
app/models/inputs/effective_panel_select/input.rb

Instance Method Summary collapse

Methods inherited from Effective::FormInput

#field_name, #html_options, #initialize, #js_options, #options, #value

Constructor Details

This class inherits a constructor from Effective::FormInput

Instance Method Details

#_initialize_group_and_optionObject



67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/models/inputs/effective_panel_select/input.rb', line 67

def _initialize_group_and_option
  collection.each do |group, items|
    items.each do |item|
      if item.send(options[:option_key_method]) == value
        @group_label = group.send(options[:group_label_method])
        @group_value = group.send(options[:group_method])
        @option_label = item.send(options[:option_value_method])
        return
      end
    end
  end

end

#ajax?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/models/inputs/effective_panel_select/input.rb', line 32

def ajax?
  return true if js_options[:ajax]
end

#collectionObject

This is a grouped polymorphic collection

[‘Active’, [[‘Post A’, 1], [‘Post B’, 2]]], [‘Past’, [[‘Post C’, 3], [‘Post D’, 4]]


83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'app/models/inputs/effective_panel_select/input.rb', line 83

def collection
  @collection ||= begin
    collection = options.delete(:collection) || []
    grouped = collection[0].kind_of?(Array) && collection[0][1].respond_to?(:to_a) && (collection[0][1] != nil) # Array or ActiveRecord_Relation

    if !grouped && collection.present?
      raise "Grouped collection expecting a Hash {'Posts' => Post.all, 'Events' => Event.all} or a Hash {'Posts' => [['Post A', 1], ['Post B', 2]], 'Events' => [['Event A', 1], ['Event B', 2]]}"
    end

    collection.each_with_index do |(name, group), index|
      collection[index][1] = group.respond_to?(:call) ? group.call : group.to_a
    end

    if collection[0][0].kind_of?(String)
      options[:group_label_method] = :to_s
      options[:group_method] = :to_s
    end

    collection.respond_to?(:call) ? collection.call : collection.to_a
  end
end

#default_input_htmlObject



20
21
22
# File 'app/models/inputs/effective_panel_select/input.rb', line 20

def default_input_html
  { class: 'effective_panel_select' }
end

#default_input_jsObject



16
17
18
# File 'app/models/inputs/effective_panel_select/input.rb', line 16

def default_input_js
  { placeholder: 'Please choose', invade: '.row', collapseOnSelect: true, resetOnCollapse: true, showCount: false }
end

#default_optionsObject



12
13
14
# File 'app/models/inputs/effective_panel_select/input.rb', line 12

def default_options
  { label_method: :to_s, value_method: :to_s, group_label_method: :first, group_method: :second, option_value_method: :first, option_key_method: :second, sidebar_class: 'nav-stacked' }
end

#group_labelObject

Active



56
57
58
59
# File 'app/models/inputs/effective_panel_select/input.rb', line 56

def group_label
  return nil unless value.present?
  @group_label || (_initialize_group_and_option; @group_label)
end

#group_valueObject

Active



62
63
64
65
# File 'app/models/inputs/effective_panel_select/input.rb', line 62

def group_value
  return nil unless value.present?
  @group_value || (_initialize_group_and_option; @group_value)
end

#method_nameObject



28
29
30
# File 'app/models/inputs/effective_panel_select/input.rb', line 28

def method_name
  @method_name ||= @method.to_s.titleize.downcase
end

#option_labelObject

Post A



50
51
52
53
# File 'app/models/inputs/effective_panel_select/input.rb', line 50

def option_label
  return nil unless value.present?
  @option_label || (_initialize_group_and_option; @option_label)
end

#option_valueObject

163



45
46
47
# File 'app/models/inputs/effective_panel_select/input.rb', line 45

def option_value
  value
end

#show_count?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'app/models/inputs/effective_panel_select/input.rb', line 36

def show_count?
  return true if js_options[:showCount]
end

#to_htmlObject



24
25
26
# File 'app/models/inputs/effective_panel_select/input.rb', line 24

def to_html
  render('effective/effective_panel_select/input', input: self)
end