Class: ActionView::Helpers::FormBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/uploadcare/rails/action_view/form_builder.rb

Instance Method Summary collapse

Instance Method Details

#uploadcare_file_field(method, ctx_name: nil, solution: "regular", **options) ⇒ Object



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
# File 'lib/uploadcare/rails/action_view/form_builder.rb', line 8

def uploadcare_file_field(method, ctx_name: nil, solution: "regular", **options)
  unless options.key?(:multiple)
    model = object&.class || object_name.to_s.camelize.safe_constantize
    if model
      checker = "has_uploadcare_files_for_#{method}?"
      options[:multiple] = true if model.respond_to?(checker) && model.public_send(checker)
    end
  end

  ctx_name ||= SecureRandom.uuid
  field_name = "#{object_name}[#{method}]"
  options[:value] = object.public_send(method) if !options.key?(:value) && object&.respond_to?(method)

  field_html = @template.uploadcare_file_field_tag(
    field_name,
    ctx_name: ctx_name,
    solution: solution,
    **options
  )

  if object && uploadcare_object_has_errors?(method)
    uploadcare_error_wrapping(field_html)
  else
    field_html
  end
end

#uploadcare_files_field(method, ctx_name: nil, solution: "regular", **options) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/uploadcare/rails/action_view/form_builder.rb', line 35

def uploadcare_files_field(method, ctx_name: nil, solution: "regular", **options)
  uploadcare_file_field(
    method,
    ctx_name: ctx_name,
    solution: solution,
    **uploadcare_group_field_options(options)
  )
end