Module: Abyme::ViewHelpers

Defined in:
lib/abyme/view_helpers.rb

Instance Method Summary collapse

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, options = {}, &block)
  (:div, data: {controller: "abyme", limit: options[:limit], min_count: options[:min_count]}, id: "abyme--#{association}") do
    if block
      yield(
        Abyme::AbymeBuilder.new(
          association: association, form: form, context: self, partial: options[:partial]
        )
      )
    else
      # model = association.to_s.singularize.classify.constantize
      model = association.to_s.singularize
      concat(persisted_records_for(association, form, options))
      concat(new_records_for(association, form, options))
      concat(add_associated_record(content: options[: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(options = {}, &block)
  action = "click->abyme#add_association"
  options[:content] ||= "Add Association"
  create_button(action, options, &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, options = {}, &block)
  options[:wrapper_html] ||= {}

  wrapper_default = {
    data: {
      abyme_target: "associations",
      association: association,
      abyme_position: options[:position] || :end
    }
  }

  fields_default = {data: {target: "abyme.fields abyme.newFields"}}

  (:div, build_attributes(wrapper_default, options[:wrapper_html])) do
    (: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|
        (:div, build_attributes(fields_default, basic_fields_markup(options[: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, options[: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, options = {})
  records = options[:collection] || form.object.send(association)
  # return if records.empty?

  options[:wrapper_html] ||= {}
  fields_default = {data: {abyme_target: "fields"}}

  if options[:order].present?
    records = records.order(options[: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

  (:div, options[:wrapper_html]) do
    form.fields_for(association, records) do |f|
      (:div, build_attributes(fields_default, basic_fields_markup(options[:fields_html], association))) do
        block_given? ? yield(f) : render_association_partial(association, f, options[: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(options = {}, &block)
  action = "click->abyme#remove_association"
  options[:content] ||= "Remove Association"
  create_button(action, options, &block)
end