Module: EffectiveFormBuilderHelper

Defined in:
app/helpers/effective_form_builder_helper.rb

Instance Method Summary collapse

Instance Method Details

#effective_form_with(model: nil, scope: nil, url: nil, format: nil, **options, &block) ⇒ Object



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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'app/helpers/effective_form_builder_helper.rb', line 4

def effective_form_with(model: nil, scope: nil, url: nil, format: nil, **options, &block)
  # This allows us to call effective_form_with inside an effective_table_with and just get the fields
  return @_effective_table_builder.capture(&block) if @_effective_table_builder

  # Compute the default ID
  subject = Array(model || scope).last
  class_name = (scope || subject.class.name.parameterize.underscore)
  unique_id = options.except(:model).hash.abs

  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[:html] = (options[:html] || {}).merge(novalidate: true, onsubmit: 'return EffectiveForm.validate(this)')
  options[:local] = true unless options.key?(:local)

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

  options[:class] = [
    options[:class],
    'needs-validation',
    ('form-inline' if options[:layout] == :inline),
    ('with-errors' if subject.respond_to?(:errors) && subject.errors.present?),
    ('show-flash-success' if options[:remote] && options[:flash_success]),
    ('hide-flash-danger' if options[:remote] && options.key?(:flash_error) && !options[:flash_error]),
    ('autosave' if options[:autosave])
  ].compact.join(' ')

  if options[:remote] || options[:unique_ids]
    @_effective_unique_id ||= {}

    if @_effective_unique_id.key?(unique_id)
      unique_id = unique_id + @_effective_unique_id.length
    end

    options[:unique_id] = unique_id
    html_id = "#{html_id}_#{unique_id}"

    @_effective_unique_id[unique_id] = true
  end

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

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

  # Assign url if engine present
  url ||= if options[:engine] && model.present?
    resource = Effective::Resource.new(model)

    if subject.respond_to?(:persisted?) && subject.persisted?
      resource.action_path(:update, subject)
    elsif subject.respond_to?(:new_record?) && subject.new_record?
      resource.action_path(:create)
    end
  end

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