Class: ActionForm::SubformsCollection
- Inherits:
-
Phlex::HTML
- Object
- Phlex::HTML
- ActionForm::SubformsCollection
- Extended by:
- Forwardable
- Includes:
- Rendering
- Defined in:
- lib/action_form/subforms_collection.rb
Overview
Collection of subforms that can be iterated and rendered
Class Attribute Summary collapse
-
.default ⇒ Object
Returns the value of attribute default.
-
.host_class ⇒ Object
Returns the value of attribute host_class.
-
.subform_definition ⇒ Object
readonly
Returns the value of attribute subform_definition.
Instance Attribute Summary collapse
-
#helpers ⇒ Object
Returns the value of attribute helpers.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#subforms ⇒ Object
readonly
Returns the value of attribute subforms.
-
#tags ⇒ Object
readonly
Returns the value of attribute tags.
Class Method Summary collapse
Instance Method Summary collapse
- #add_subform_js ⇒ Object
- #each(&block) ⇒ Object
-
#initialize(name) ⇒ SubformsCollection
constructor
A new instance of SubformsCollection.
- #remove_subform_js ⇒ Object
- #render? ⇒ Boolean
- #render_subform_template(subform) ⇒ Object
- #template_html_id ⇒ Object
-
#view_template ⇒ Object
rubocop:disable Metrics/AbcSize.
Methods included from Rendering
#render_element, #render_elements, #render_form, #render_inline_errors, #render_input, #render_label, #render_many_subforms, #render_new_subform_button, #render_remove_subform_button, #render_subform, #render_submit
Constructor Details
#initialize(name) ⇒ SubformsCollection
Returns a new instance of SubformsCollection.
24 25 26 27 28 29 |
# File 'lib/action_form/subforms_collection.rb', line 24 def initialize(name) super() @name = name @subforms = [] @tags = {} end |
Class Attribute Details
.default ⇒ Object
Returns the value of attribute default.
16 17 18 |
# File 'lib/action_form/subforms_collection.rb', line 16 def default @default end |
.host_class ⇒ Object
Returns the value of attribute host_class.
16 17 18 |
# File 'lib/action_form/subforms_collection.rb', line 16 def host_class @host_class end |
.subform_definition ⇒ Object (readonly)
Returns the value of attribute subform_definition.
15 16 17 |
# File 'lib/action_form/subforms_collection.rb', line 15 def subform_definition @subform_definition end |
Instance Attribute Details
#helpers ⇒ Object
Returns the value of attribute helpers.
12 13 14 |
# File 'lib/action_form/subforms_collection.rb', line 12 def helpers @helpers end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
11 12 13 |
# File 'lib/action_form/subforms_collection.rb', line 11 def name @name end |
#subforms ⇒ Object (readonly)
Returns the value of attribute subforms.
11 12 13 |
# File 'lib/action_form/subforms_collection.rb', line 11 def subforms @subforms end |
#tags ⇒ Object (readonly)
Returns the value of attribute tags.
11 12 13 |
# File 'lib/action_form/subforms_collection.rb', line 11 def @tags end |
Class Method Details
.subform(subform_class = nil, &block) ⇒ Object
18 19 20 21 |
# File 'lib/action_form/subforms_collection.rb', line 18 def subform(subform_class = nil, &block) @subform_definition = subform_class || Class.new(host_class.subform_class) @subform_definition.class_eval(&block) if block end |
Instance Method Details
#add_subform_js ⇒ Object
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 |
# File 'lib/action_form/subforms_collection.rb', line 45 def add_subform_js <<~JS function easyFormAddSubform(event) { event.preventDefault() var template = document.querySelector("##{template_html_id}") const content = template.innerHTML.replace(/NEW_RECORD/g, new Date().getTime().toString()) var beforeElement = event.target.closest(event.target.dataset.insertBeforeSelector) if (beforeElement) { beforeElement.insertAdjacentHTML("beforebegin", content) } else { event.target.parentElement.insertAdjacentHTML("beforebegin", content) } } JS end |
#each(&block) ⇒ Object
31 32 33 34 35 |
# File 'lib/action_form/subforms_collection.rb', line 31 def each(&block) return to_enum(:each) unless block @subforms.each(&block) end |
#remove_subform_js ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/action_form/subforms_collection.rb', line 61 def remove_subform_js <<~JS function easyFormRemoveSubform(event) { event.preventDefault() var subform = event.target.closest(".new_#{name}") if (subform) { subform.remove() } var subform = event.target.closest(".#{name}_subform") if (subform) { subform.style.display = "none" var input = subform.querySelector("input[name*='_destroy']") if (input) { input.value = "1" } } } JS end |
#render? ⇒ Boolean
37 38 39 |
# File 'lib/action_form/subforms_collection.rb', line 37 def render? true end |
#render_subform_template(subform) ⇒ Object
90 91 92 93 94 |
# File 'lib/action_form/subforms_collection.rb', line 90 def render_subform_template(subform) template(id: template_html_id) do div(class: "new_#{name}") { render_subform(subform) } end end |
#template_html_id ⇒ Object
41 42 43 |
# File 'lib/action_form/subforms_collection.rb', line 41 def template_html_id "#{name}_template" end |
#view_template ⇒ Object
rubocop:disable Metrics/AbcSize
77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/action_form/subforms_collection.rb', line 77 def view_template # rubocop:disable Metrics/AbcSize script(type: "text/javascript") { raw safe(remove_subform_js) } script(type: "text/javascript") { raw safe(add_subform_js) } subforms.each do |subform| subform.helpers = helpers if subform.[:template] render_subform_template(subform) else div(id: subform.html_id, class: subform.html_class) { render_subform(subform) } end end end |