Module: EffectiveFormBuilderHelper

Defined in:
app/helpers/effective_form_builder_helper.rb

Instance Method Summary collapse

Instance Method Details

#effective_form_with(**options, &block) ⇒ Object



2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'app/helpers/effective_form_builder_helper.rb', line 2

def effective_form_with(**options, &block)

  # Compute the default ID
  subject = Array(options[:scope] || options[:model]).last
  class_name = subject.class.name.underscore

  html_id = if subject.kind_of?(Symbol)
    subject.to_s
  elsif subject.respond_to?(:persisted?) && subject.persisted?
    "edit_#{class_name}_#{subject.to_param}"
  else
    "new_#{class_name}"
  end

  options[:class] = [
    options[:class],
    'needs-validation',
    ('form-inline' if options[:layout] == :inline),
    ('with-errors' if subject.respond_to?(:errors) && subject.errors.present?)
  ].compact.join(' ')

  options[:html] = (options[:html] || {}).merge(novalidate: true, onsubmit: 'return EffectiveForm.validate(this);')

  remote_index = options.except(:model).hash.abs

  options[:remote] = true if respond_to?(:inline_datatable?) && inline_datatable?

  if options.delete(:remote) == true
    @_effective_remote_index ||= {}
    remote_index = remote_index + 1 if @_effective_remote_index[remote_index]

    if options[:html][:data].kind_of?(Hash)
      options[:html][:data][:remote] = true
      options[:html][:data]['data-remote-index'] = remote_index
    else
      options[:html]['data-remote'] = true
      options[:html]['data-remote-index'] = remote_index
    end

    html_id = "#{html_id}_#{remote_index}"

    @_effective_remote_index[remote_index] = true
  end

  # Assign default ID
  options[:id] ||= (options[:html].delete(:id) || html_id) unless options.key?(:id)

  without_error_proc do
    form_with(**options.merge(builder: Effective::FormBuilder), &block)
  end
end