Class: Effective::FormInputs::CollectionInput

Inherits:
Effective::FormInput show all
Defined in:
app/models/effective/form_inputs/collection_input.rb

Direct Known Subclasses

Checks, Radios, Select

Constant Summary

Constants inherited from Effective::FormInput

Effective::FormInput::BLANK, Effective::FormInput::DEFAULT_FEEDBACK_OPTIONS, Effective::FormInput::DEFAULT_INPUT_GROUP_OPTIONS, Effective::FormInput::EMPTY_HASH, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES, Effective::FormInput::HORIZONTAL_LABEL_OPTIONS, Effective::FormInput::HORIZONTAL_WRAPPER_OPTIONS, Effective::FormInput::INLINE_LABEL_OPTIONS, Effective::FormInput::VERTICAL_WRAPPER_OPTIONS

Instance Attribute Summary

Attributes inherited from Effective::FormInput

#name, #options

Instance Method Summary collapse

Methods inherited from Effective::FormInput

#feedback_options, #hint_options, #input_group_options, #input_html_options, #input_js_options, #label_options, #to_html, #wrapper_options

Constructor Details

#initialize(name, options, builder:, html_options: nil) ⇒ CollectionInput

Returns a new instance of CollectionInput.



6
7
8
9
10
# File 'app/models/effective/form_inputs/collection_input.rb', line 6

def initialize(name, options, builder:, html_options: nil)
  super
  assign_options_collection!
  assign_options_collection_methods!
end

Instance Method Details

#assign_options_collection!Object

This is a grouped polymorphic collection

[“Clinics”, [[“Clinc 50”, “Clinic_50”], [“Clinic 43”, “Clinic_43”]]], [“Contacts”, [[“Contact 544”, “Contact_544”]]]


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
# File 'app/models/effective/form_inputs/collection_input.rb', line 81

def assign_options_collection!
  collection = options[:input].delete(:collection) || raise('Please include a collection')

  grouped = collection.kind_of?(Hash) && collection.values.first.respond_to?(:to_a)

  if grouped? && !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

  if polymorphic? && !grouped && collection.present?
    raise "Polymorphic collection expecting a Hash {'Posts' => Post.all, 'Events' => Event.all}"
  end

  @options_collection = (
    if polymorphic?
      collection.inject({}) { |h, (k, group)| h[k] = translate(group).map { |obj| [obj.to_s, "#{obj.class.model_name}_#{obj.id}"] }; h }
    elsif grouped
      collection.inject({}) { |h, (k, group)| h[k] = translate(group).map { |obj| obj }; h }
    elsif (collection == :boolean || collection == :booleans || collection == :boolean_collection)
      EffectiveBootstrap.boolean_collection
    else
      translate(collection).map { |obj| obj }
    end
  )
end

#assign_options_collection_methods!Object



127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'app/models/effective/form_inputs/collection_input.rb', line 127

def assign_options_collection_methods!
  options[:input].reverse_merge!(
    if polymorphic?
      { group_method: :last, group_label_method: :first, option_key_method: :second, option_value_method: :first }
    elsif grouped?
      first = Array(options_collection.values.first).first

      string_of_strings = (
        options_collection.kind_of?(Hash) && 
        options_collection.keys.all? { |key| key.kind_of?(String) } && 
        options_collection.values.all? { |values| values.kind_of?(Array) && values.all? { |value| value.kind_of?(String) } }
      )

      if first.kind_of?(ActiveRecord::Base)
        { group_method: :last, group_label_method: :first, option_value_method: :to_s, option_key_method: :id }
      elsif string_of_strings
        { group_method: :last, group_label_method: :first, option_value_method: :to_s, option_key_method: :to_s }
      else
        { group_method: :last, group_label_method: :first, option_value_method: :first, option_key_method: :second }
      end
    elsif options_collection.first.kind_of?(Array)
      { label_method: :first, value_method: :second }
    elsif options_collection.first.kind_of?(ActiveRecord::Base)
      { label_method: :to_s, value_method: :id }
    else
      { label_method: :to_s, value_method: :to_s }
    end
  )
end

#collection_optionsObject



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'app/models/effective/form_inputs/collection_input.rb', line 32

def collection_options
  return @collection_options unless @collection_options.nil?

  checked = options[:input].delete(:checked)
  selected = options[:input].delete(:selected)
  include_blank = options[:input].delete(:include_blank)

  @collection_options = {
    checked: [checked, selected, polymorphic_value, value].find { |value| value != nil },
    selected: ([selected, checked, polymorphic_value, value].find { |value| value != nil } unless kind_of?(Effective::FormInputs::Radios)),
    include_blank: include_blank
  }.compact
end

#custom?Boolean

default true

Returns:

  • (Boolean)


22
23
24
25
# File 'app/models/effective/form_inputs/collection_input.rb', line 22

def custom? # default true
  return @custom unless @custom.nil?
  @custom = (options.delete(:custom) != false)
end

#group_label_methodObject



63
64
65
# File 'app/models/effective/form_inputs/collection_input.rb', line 63

def group_label_method
  @group_label_method ||= options[:input].delete(:group_label_method)
end

#group_methodObject



59
60
61
# File 'app/models/effective/form_inputs/collection_input.rb', line 59

def group_method
  @group_method ||= options[:input].delete(:group_method)
end

#grouped?Boolean

Returns:

  • (Boolean)


17
18
19
20
# File 'app/models/effective/form_inputs/collection_input.rb', line 17

def grouped?
  return @grouped unless @grouped.nil?
  @grouped = (options.delete(:grouped) || false)
end

#html_optionsObject



46
47
48
49
# File 'app/models/effective/form_inputs/collection_input.rb', line 46

def html_options
  # Not sure why I need this. But they're merged in if I don't.
  options[:input].merge(skip_default_ids: nil, allow_method_names_outside_object: nil)
end

#inline?Boolean

default false

Returns:

  • (Boolean)


27
28
29
30
# File 'app/models/effective/form_inputs/collection_input.rb', line 27

def inline? # default false
  return @inline unless @inline.nil?
  @inline = (options[:input].delete(:inline) == true)
end

#label_methodObject



55
56
57
# File 'app/models/effective/form_inputs/collection_input.rb', line 55

def label_method
  @label_method ||= options[:input].delete(:label_method)
end

#option_key_methodObject



67
68
69
# File 'app/models/effective/form_inputs/collection_input.rb', line 67

def option_key_method
  @option_key_method ||= options[:input].delete(:option_key_method)
end

#option_value_methodObject



71
72
73
# File 'app/models/effective/form_inputs/collection_input.rb', line 71

def option_value_method
  @option_value_method ||= options[:input].delete(:option_value_method)
end

#options_collectionObject



75
76
77
# File 'app/models/effective/form_inputs/collection_input.rb', line 75

def options_collection
  @options_collection || []
end

#polymorphic?Boolean

Returns:

  • (Boolean)


12
13
14
15
# File 'app/models/effective/form_inputs/collection_input.rb', line 12

def polymorphic?
  return @polymorphic unless @polymorphic.nil?
  @polymorphic = (options.delete(:polymorphic) || false)
end

#polymorphic_id_methodObject



161
162
163
# File 'app/models/effective/form_inputs/collection_input.rb', line 161

def polymorphic_id_method
  name.to_s.sub('_id', '') + '_id'
end

#polymorphic_id_valueObject



174
175
176
# File 'app/models/effective/form_inputs/collection_input.rb', line 174

def polymorphic_id_value
  value.try(:id)
end

#polymorphic_type_methodObject



157
158
159
# File 'app/models/effective/form_inputs/collection_input.rb', line 157

def polymorphic_type_method
  name.to_s.sub('_id', '') + '_type'
end

#polymorphic_type_valueObject



170
171
172
# File 'app/models/effective/form_inputs/collection_input.rb', line 170

def polymorphic_type_value
  value.try(:class).try(:model_name)
end

#polymorphic_valueObject



165
166
167
168
# File 'app/models/effective/form_inputs/collection_input.rb', line 165

def polymorphic_value
  return nil unless polymorphic?
  "#{value.class.model_name}_#{value.id}" if value
end

#translate(collection) ⇒ Object

Apply ActsAsArchived behavior. That’s all for now.



108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
# File 'app/models/effective/form_inputs/collection_input.rb', line 108

def translate(collection)
  return collection unless object.respond_to?(:new_record?)
  return collection unless collection.respond_to?(:klass)

  if collection.klass.respond_to?(:acts_as_archived?)
    collection = if object.new_record?
      collection.unarchived
    else
      collection.unarchived.or(collection.archived.where(collection.klass.primary_key => value))
    end
  end

  if respond_to?(:ajax?) && ajax? # effective_select
    collection = collection.where(collection.klass.primary_key => value)
  end

  collection
end

#value_methodObject



51
52
53
# File 'app/models/effective/form_inputs/collection_input.rb', line 51

def value_method
  @value_method ||= options[:input].delete(:value_method)
end