Class: RefileInput

Inherits:
Formtastic::Inputs::FileInput
  • Object
show all
Defined in:
lib/inputs/refile_input.rb

Instance Method Summary collapse

Constructor Details

#initialize(*args) ⇒ RefileInput



2
3
4
5
6
# File 'lib/inputs/refile_input.rb', line 2

def initialize *args
  super

  @reference = SecureRandom.hex
end

Instance Method Details

#file_input_html_optionsObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/inputs/refile_input.rb', line 8

def file_input_html_options
  options[:data] ||= {}
  options[:data][:reference] = @reference

  attacher = object.send(:"#{method}_attacher")
  options[:accept] = attacher.definition.accept
  host =  options[:host] || Refile.cdn_host

  if options[:direct]
    backend_name = Refile.backends.key(attacher.cache)

    url = ::File.join(host, Rails.application.routes.url_helpers.refile_app_path, backend_name)
    options[:data].merge!(direct: true, as: "file", url: url)
  end

  if options[:presigned] and attacher.cache.respond_to?(:presign)
    url = ::File.join(host, Rails.application.routes.url_helpers.refile_app_path, backend_name, "presign")
    options[:data].merge!(direct: true, url: url)
  end

  options.merge(input_html_options)
end

#hidden_input_html_optionsObject



31
32
33
34
# File 'lib/inputs/refile_input.rb', line 31

def hidden_input_html_options
  attacher = object.send(:"#{method}_attacher")
  { value: attacher.data.to_json, object: object, id: nil, data: { reference: @reference } }
end

#to_htmlObject



36
37
38
39
40
41
42
43
# File 'lib/inputs/refile_input.rb', line 36

def to_html
  input_wrapping do
    label_html <<
    builder.hidden_field(method, hidden_input_html_options) <<
    builder.file_field(method, file_input_html_options) <<
    image_preview_content
  end
end