Class: Effective::FormInputs::CollectionInput
Constant Summary
Effective::FormInput::BLANK, Effective::FormInput::EXCLUSIVE_CLASS_PREFIXES, Effective::FormInput::EXCLUSIVE_CLASS_SUFFIXES
Instance Attribute Summary
#name, #options
Instance Method Summary
collapse
-
#assign_options_collection! ⇒ Object
This is a grouped polymorphic collection [[“Clinics”, [[“Clinc 50”, “Clinic_50”], [“Clinic 43”, “Clinic_43”]]], [“Contacts”, [[“Contact 544”, “Contact_544”]]]].
-
#assign_options_collection_methods! ⇒ Object
-
#collection_options ⇒ Object
-
#custom? ⇒ Boolean
-
#group_label_method ⇒ Object
-
#group_method ⇒ Object
-
#grouped? ⇒ Boolean
-
#html_options ⇒ Object
-
#initialize(name, options, builder:, html_options: nil) ⇒ CollectionInput
constructor
A new instance of CollectionInput.
-
#inline? ⇒ Boolean
-
#label_method ⇒ Object
-
#option_key_method ⇒ Object
-
#option_value_method ⇒ Object
-
#options_collection ⇒ Object
-
#polymorphic? ⇒ Boolean
-
#polymorphic_id_method ⇒ Object
-
#polymorphic_id_value ⇒ Object
-
#polymorphic_type_method ⇒ Object
-
#polymorphic_type_value ⇒ Object
-
#polymorphic_value ⇒ Object
-
#value_method ⇒ Object
#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”]]]
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 80
def assign_options_collection!
collection = options[:input].delete(:collection)
grouped = collection.kind_of?(Hash) && collection.values.all? { |v| v.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 grouped
collection.transform_values! { |group| group.to_a }
end
if polymorphic?
if grouped
collection.values.each do |group|
group.transform_values! { |obj| [obj.to_s, "#{obj.class.model_name}_#{obj.id}"] }
end
else
collection.transform_values! { |obj| [obj.to_s, "#{obj.class.model_name}_#{obj.id}"] }
end
end
@options_collection = collection.to_a
end
|
#assign_options_collection_methods! ⇒ Object
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 106
def assign_options_collection_methods!
options[:input].reverse_merge!(
if grouped? && 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[0].kind_of?(Array)
{ label_method: :first, value_method: :second }
elsif options_collection[0].kind_of?(ActiveRecord::Base)
{ label_method: :to_s, value_method: :id }
else
{ label_method: :to_s, value_method: :to_s }
end
)
end
|
#collection_options ⇒ Object
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
# 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)
passed_value = options[:input].delete(:value)
include_blank = options[:input].delete(:include_blank)
@collection_options = {
checked: (checked || selected || passed_value || value),
selected: (selected || checked || passed_value || value),
include_blank: include_blank
}.compact
end
|
#custom? ⇒ Boolean
20
21
22
23
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 20
def custom? return @custom unless @custom.nil?
@custom = (options.delete(:custom) != false)
end
|
#group_label_method ⇒ Object
62
63
64
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 62
def group_label_method
@group_label_method ||= options[:input].delete(:group_label_method)
end
|
#group_method ⇒ Object
58
59
60
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 58
def group_method
@group_method ||= options[:input].delete(:group_method)
end
|
#grouped? ⇒ 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_options ⇒ Object
45
46
47
48
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 45
def html_options
options[:input].merge(skip_default_ids: nil, allow_method_names_outside_object: nil)
end
|
#inline? ⇒ Boolean
25
26
27
28
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 25
def inline? return @inline unless @inline.nil?
@inline = (options[:input].delete(:inline) == true)
end
|
#label_method ⇒ Object
54
55
56
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 54
def label_method
@label_method ||= options[:input].delete(:label_method)
end
|
#option_key_method ⇒ Object
66
67
68
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 66
def option_key_method
@option_key_method ||= options[:input].delete(:option_key_method)
end
|
#option_value_method ⇒ Object
70
71
72
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 70
def option_value_method
@option_value_method ||= options[:input].delete(:option_value_method)
end
|
#options_collection ⇒ Object
74
75
76
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 74
def options_collection
@options_collection || []
end
|
#polymorphic? ⇒ 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_method ⇒ Object
126
127
128
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 126
def polymorphic_id_method
name.to_s.sub('_id', '') + '_id'
end
|
#polymorphic_id_value ⇒ Object
138
139
140
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 138
def polymorphic_id_value
value.try(:id)
end
|
#polymorphic_type_method ⇒ Object
122
123
124
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 122
def polymorphic_type_method
name.to_s.sub('_id', '') + '_type'
end
|
#polymorphic_type_value ⇒ Object
134
135
136
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 134
def polymorphic_type_value
value.try(:class).try(:model_name)
end
|
#polymorphic_value ⇒ Object
130
131
132
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 130
def polymorphic_value
"#{object.class.model_name}_#{object.id}" if object
end
|
#value_method ⇒ Object
50
51
52
|
# File 'app/models/effective/form_inputs/collection_input.rb', line 50
def value_method
@value_method ||= options[:input].delete(:value_method)
end
|