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::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES

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.



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

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”]]]


79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/effective/form_inputs/collection_input.rb', line 79

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 }
    else
      translate(collection).map { |obj| obj }
    end
  )
end

#assign_options_collection_methods!Object



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'app/models/effective/form_inputs/collection_input.rb', line 123

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?
      { group_method: :last, group_label_method: :first, option_key_method: :second, option_value_method: :first }
    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



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

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 },
    include_blank: include_blank
  }.compact
end

#custom?Boolean

default true

Returns:

  • (Boolean)


20
21
22
23
# File 'app/models/effective/form_inputs/collection_input.rb', line 20

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

#group_label_methodObject



61
62
63
# File 'app/models/effective/form_inputs/collection_input.rb', line 61

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

#group_methodObject



57
58
59
# File 'app/models/effective/form_inputs/collection_input.rb', line 57

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

#grouped?Boolean

Returns:

  • (Boolean)


15
16
17
18
# File 'app/models/effective/form_inputs/collection_input.rb', line 15

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

#html_optionsObject



44
45
46
47
# File 'app/models/effective/form_inputs/collection_input.rb', line 44

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)


25
26
27
28
# File 'app/models/effective/form_inputs/collection_input.rb', line 25

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

#label_methodObject



53
54
55
# File 'app/models/effective/form_inputs/collection_input.rb', line 53

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

#option_key_methodObject



65
66
67
# File 'app/models/effective/form_inputs/collection_input.rb', line 65

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

#option_value_methodObject



69
70
71
# File 'app/models/effective/form_inputs/collection_input.rb', line 69

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

#options_collectionObject



73
74
75
# File 'app/models/effective/form_inputs/collection_input.rb', line 73

def options_collection
  @options_collection || []
end

#polymorphic?Boolean

Returns:

  • (Boolean)


10
11
12
13
# File 'app/models/effective/form_inputs/collection_input.rb', line 10

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

#polymorphic_id_methodObject



143
144
145
# File 'app/models/effective/form_inputs/collection_input.rb', line 143

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

#polymorphic_id_valueObject



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

def polymorphic_id_value
  value.try(:id)
end

#polymorphic_type_methodObject



139
140
141
# File 'app/models/effective/form_inputs/collection_input.rb', line 139

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

#polymorphic_type_valueObject



152
153
154
# File 'app/models/effective/form_inputs/collection_input.rb', line 152

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

#polymorphic_valueObject



147
148
149
150
# File 'app/models/effective/form_inputs/collection_input.rb', line 147

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.



104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'app/models/effective/form_inputs/collection_input.rb', line 104

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



49
50
51
# File 'app/models/effective/form_inputs/collection_input.rb', line 49

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