Module: Abyme::ViewHelpers
- Defined in:
- lib/abyme/view_helpers.rb
Instance Method Summary collapse
-
#abyme_for(association, form, options = {}, &block) ⇒ Object
(also: #abymize)
<div data-controller=“abyme” data-limit=“3” id=“abyme–tasks”> …
-
#add_associated_record(options = {}, &block) ⇒ Object
(also: #add_association)
these helpers will call the #create_button method to generate the buttons for add and remove associations with the right action and a default content text for each button.
-
#new_records_for(association, form, options = {}, &block) ⇒ Object
-
wrapper_html (Hash) allows you to pass any html attributes to the the html element wrapping all the fields.
-
-
#persisted_records_for(association, form, options = {}) ⇒ Object
-
wrapper_html (Hash) allows you to pass any html attributes to the the html element wrapping all the fields.
-
- #remove_associated_record(options = {}, &block) ⇒ Object (also: #remove_association)
Instance Method Details
#abyme_for(association, form, options = {}, &block) ⇒ Object Also known as: abymize
<div data-controller=“abyme” data-limit=“3” id=“abyme–tasks”>
...
</div>
40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/abyme/view_helpers.rb', line 40 def abyme_for(association, form, = {}, &block) content_tag(:div, data: {controller: "abyme", limit: [:limit], min_count: [:min_count]}, id: "abyme--#{association}") do if block yield( Abyme::AbymeBuilder.new( association: association, form: form, context: self, partial: [:partial] ) ) else # model = association.to_s.singularize.classify.constantize model = association.to_s.singularize concat(persisted_records_for(association, form, )) concat(new_records_for(association, form, )) concat(add_associated_record(content: [:button_text] || "Add #{model}")) end end end |
#add_associated_record(options = {}, &block) ⇒ Object Also known as: add_association
these helpers will call the #create_button method to generate the buttons for add and remove associations with the right action and a default content text for each button
199 200 201 202 203 |
# File 'lib/abyme/view_helpers.rb', line 199 def add_associated_record( = {}, &block) action = "click->abyme#add_association" [:content] ||= "Add Association" (action, , &block) end |
#new_records_for(association, form, options = {}, &block) ⇒ Object
-
wrapper_html (Hash)
allows you to pass any html attributes to the the html element wrapping all the fields
101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/abyme/view_helpers.rb', line 101 def new_records_for(association, form, = {}, &block) [:wrapper_html] ||= {} wrapper_default = { data: { abyme_target: "associations", association: association, abyme_position: [:position] || :end } } fields_default = {data: {target: "abyme.fields abyme.newFields"}} content_tag(:div, build_attributes(wrapper_default, [:wrapper_html])) do content_tag(:template, class: "abyme--#{association.to_s.singularize}_template", data: {abyme_target: "template"}) do form.fields_for association, association.to_s.classify.constantize.new, child_index: "NEW_RECORD" do |f| content_tag(:div, build_attributes(fields_default, basic_fields_markup([:fields_html], association))) do # Here, if a block is passed, we're passing the association fields to it, rather than the form itself # block_given? ? yield(f) : render(options[:partial] || "abyme/#{association.to_s.singularize}_fields", f: f) block ? yield(f) : render_association_partial(association, f, [:partial]) end end end end end |
#persisted_records_for(association, form, options = {}) ⇒ Object
-
wrapper_html (Hash)
allows you to pass any html attributes to the the html element wrapping all the fields
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/abyme/view_helpers.rb', line 168 def persisted_records_for(association, form, = {}) records = [:collection] || form.object.send(association) # return if records.empty? [:wrapper_html] ||= {} fields_default = {data: {abyme_target: "fields"}} if [:order].present? records = records.order([:order]) # by calling the order method on the AR collection # we get rid of the records with errors # so we have to get them back with the 2 lines below invalids = form.object.send(association).reject(&:persisted?) records = records.to_a.concat(invalids) if invalids.any? end content_tag(:div, [:wrapper_html]) do form.fields_for(association, records) do |f| content_tag(:div, build_attributes(fields_default, basic_fields_markup([:fields_html], association))) do block_given? ? yield(f) : render_association_partial(association, f, [:partial]) end end end end |
#remove_associated_record(options = {}, &block) ⇒ Object Also known as: remove_association
205 206 207 208 209 |
# File 'lib/abyme/view_helpers.rb', line 205 def remove_associated_record( = {}, &block) action = "click->abyme#remove_association" [:content] ||= "Remove Association" (action, , &block) end |