Module: SimpleAttachments::ViewHelpers

Defined in:
lib/simple_attachments/view_helpers.rb

Defined Under Namespace

Modules: FormHelper, TagHelper

Class Method Summary collapse

Class Method Details

.helper(type, template, object, object_name, method, options) ⇒ Object

Create div with attachments.

Allowed options

:text

Text in the add button. [nil]

:id

Main div id. [nil]

:can_destroy

Show destroy link. [true]

:can_create

Show add button. [true]

:readonly

Sets default values for :can_destroy and :can_create. [false]

:destroy_remote

Destroy the attachment on the server after clicking the destroy link. [true]

:auto_associate

Associate uploaded file with container at once. Also sets default value for :destroy_remote. [true]

You can also specify any other options and then access them from JavaScript code.



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
# File 'lib/simple_attachments/view_helpers.rb', line 18

def self.helper(type, template, object, object_name, method, options)
  text = options.delete :text
  id = options.delete :id
  options[:readonly] = false if options[:readonly].nil?
  options[:can_destroy] ||= !options[:readonly]
  options[:can_create] ||= !options[:readonly]
  options.delete :readonly
  options[:auto_associate] ||= true if options[:auto_associate].nil?
  container_id = (options[:auto_associate] ? object.id : nil)
  options[:destroy_remote] ||= options[:auto_associate]
  options.delete :auto_associate
  attached = object.send(method)
  if attached.is_a? Array
    attached = attached.map{|a| a.serializable_hash}
  else
    attached = attached.serializable_hash unless attached.nil?
  end
  attachments_path = template.send object.class.reflections[method].class_name.pluralize.underscore.concat('_path')
  template.(:div,
                        template.(:div, text, :class => 'simple_attachments_add_file_div'),
                        :class => "simple_attachments_div simple_attachments_#{type}_div",
                        :id => id,
                        :data => {:container_model => object_name,
                                  :container_id => container_id,
                                  :method => method,
                                  :attachments_path => attachments_path, 
                                  :attached => attached.to_json,
                                  :options => options.to_json
                                }
                      )
end