Class: GroupedCheckBoxesInput

Inherits:
Formtastic::Inputs::CheckBoxesInput
  • Object
show all
Defined in:
app/inputs/grouped_check_boxes_input.rb

Instance Method Summary collapse

Instance Method Details

#asObject

> “<li class="grouped_check_boxes check_boxes input optional" >…</li>”

we want to reuse existing CSS from regular checkboxes



8
9
10
# File 'app/inputs/grouped_check_boxes_input.rb', line 8

def as
	super + " check_boxes"
end

#choice_wrapping_html_options(choice) ⇒ Object



80
81
82
83
84
85
# File 'app/inputs/grouped_check_boxes_input.rb', line 80

def choice_wrapping_html_options(choice)
	classes = ["choice", "grouped_check_boxes__choice"]
	classes << "#{sanitized_method_name.singularize}_#{choice_html_safe_value(choice)}" if value_as_class?

	{ class: classes.join(" ") }
end

#choices_group_contents(group_choice_records) ⇒ String

Returns of concatenated <li>s.

Returns:

  • (String)

    of concatenated <li>s



69
70
71
72
73
74
75
76
77
# File 'app/inputs/grouped_check_boxes_input.rb', line 69

def choices_group_contents(group_choice_records)
	normalized_collection(collection, group_choice_records).map { |choice|
		choice_wrapping(choice_wrapping_html_options(choice)) { # <li class="choice">
			choice_html(choice)                                   #   <label for="project_technology_ids_49">
																														#     <input type="checkbox" name="project[technology_ids][]" id="project_technology_ids_49" value="49" />AWS Route53
																														#   </label>
		}                                                       # </li>
	}.join("\n").html_safe
end

#choices_group_wrapping_html_optionsObject



62
63
64
# File 'app/inputs/grouped_check_boxes_input.rb', line 62

def choices_group_wrapping_html_options
	{ class: ["choices-group", "grouped_check_boxes__choices-group"] }
end

#choices_wrapping_html_optionsObject



36
37
38
# File 'app/inputs/grouped_check_boxes_input.rb', line 36

def choices_wrapping_html_options
	{ class: ["choices", "grouped_check_boxes__choices"] }
end

#fieldsets_for_groupsObject

legend_html # => “<legend class="label"><label>Technologies</label></legend>” choices_wrapping # => “<fieldset class="choices"></fieldset>”



26
27
28
29
30
31
32
33
# File 'app/inputs/grouped_check_boxes_input.rb', line 26

def fieldsets_for_groups
	grouped_collection.
	map { |group_method_call_return_value, group_records|
		choices_wrapping do
			legend_html(group_method_call_return_value) << lis_with_checkboxes(group_records)
		end
	}.join("\n").html_safe
end

#group_name(group_record_or_label) ⇒ Object



112
113
114
115
116
117
118
119
120
# File 'app/inputs/grouped_check_boxes_input.rb', line 112

def group_name(group_record_or_label)
	return "No subgroup" unless group_record_or_label

	if (label_method = input_options[:group_label_method].presence)
		group_record_or_label.public_send(label_method)
	else
		group_record_or_label
	end
end

#grouped_collectionObject



88
89
90
91
92
93
94
95
96
# File 'app/inputs/grouped_check_boxes_input.rb', line 88

def grouped_collection
	fail "You must provide a `:collection` input option" unless collection_from_options

	case (predicat = input_options.fetch(:group_method))
	when Proc   then collection_from_options.group_by { |record| predicat.call(record) }
	when Symbol then collection_from_options.group_by { |record| record.public_send(predicat) }
	else fail "You must provide either a Proc or a Symbol in the `:group_method` input option"
	end
end

#legend_html(group_method_call_return_value) ⇒ Object



41
42
43
44
45
46
47
48
49
50
# File 'app/inputs/grouped_check_boxes_input.rb', line 41

def legend_html(group_method_call_return_value)
	template. :legend, label_html_options.merge(class: ["label", "grouped_check_boxes__legend"]) do
		template. :label, class: "grouped_check_boxes__legend__label" do
			acc = []
			acc << (label_text + " / ") if input_options[:group_label_parent]
			acc << group_name(group_method_call_return_value)
			acc.join("\n").html_safe
		end
	end
end

#lis_with_checkboxes(records) ⇒ Object

choices_group_wrapping # => <ol class=“choices-group”>…</ol>



55
56
57
58
59
# File 'app/inputs/grouped_check_boxes_input.rb', line 55

def lis_with_checkboxes(records)
	choices_group_wrapping do
		choices_group_contents(records)
	end
end

#normalized_collection(collection, group_records) ⇒ Object

‘collection` is an Array of [label, value] Arrays, so `arr_el.second` will return the <option value=“”> `group_records` is an Array of ActiveRecord objects `value_method` is a Symbol of the method name called on each record to get what to insert into the <option value=“”>



103
104
105
106
107
108
109
# File 'app/inputs/grouped_check_boxes_input.rb', line 103

def normalized_collection(collection, group_records)
	collection.select do |arr_el|
		group_records
			.map(&value_method.to_sym)
			.include?(arr_el.second)
	end
end

#to_htmlObject

input_wrapping # => “<li class="grouped_check_boxes input optional" id="project_technologies_input">nn</li>” hidden_field_for_all # => “<input type="hidden" name="project[]" id="project_technologies_none" value="" autocomplete="off" />”



16
17
18
19
20
# File 'app/inputs/grouped_check_boxes_input.rb', line 16

def to_html
	input_wrapping do
		hidden_field_for_all << fieldsets_for_groups
	end
end