Module: Sunrise::FileUpload::ViewHelper

Includes:
ActionView::Helpers::JavaScriptHelper
Defined in:
lib/sunrise/file_upload/view_helper.rb

Instance Method Summary collapse

Instance Method Details

#fileupload_tag(object_name, method, options = {}) ⇒ Object



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
# File 'lib/sunrise/file_upload/view_helper.rb', line 6

def fileupload_tag(object_name, method, options = {})
  object = options.delete(:object) if options.key?(:object)
  object ||= @template.instance_variable_get("@#{object_name}")  
  
  value = options.delete(:value) if options.key?(:value)
  value ||= object.fileupload_asset(method)
         
  element_guid = object.fileupload_guid
  element_id = dom_id(object, [method, element_guid].join('_'))
  
  script_options = (options.delete(:script) || {}).stringify_keys
  
  params = {
    :method => method, 
    :assetable_id => object.new_record? ? nil : object.id, 
    :assetable_type => object.class.name,
    :guid => element_guid
  }.merge(script_options.delete(:params) || {})
  
  script_options['action'] ||= '/sunrise/fileupload?' + Rack::Utils.build_query(params)
  script_options['allowedExtensions'] ||=  ['jpg', 'jpeg', 'png', 'gif']
  script_options['multiple'] ||= object.fileupload_multiple?(method)
  
  (:div, :class => 'fileupload', :id => element_id) do
    (:noscript) do
      fields_for object do |form|
        form.fields_for method, value do |f|
          f.file_field :data
        end
      end
    end + javascript_tag( fileupload_script(element_id, value, script_options) )
  end
end