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
|
# File 'lib/anaconda/form_builder_helpers.rb', line 4
def anaconda( anaconda_field_name, form_options = {} )
output = ""
instance = nil
options = {}
element_id = "anaconda_file_#{anaconda_field_name}"
output += "<div class='anaconda_dropzone'>"
if self.class == SimpleForm::FormBuilder
instance = self.object
a_class = self.object.class unless self.object.kind_of? Class
begin
options = a_class.anaconda_options[anaconda_field_name.to_sym].dup
rescue
raise AnacondaError, "attribute options not set for column #{anaconda_field_name}. Did you add `anaconda_for :#{anaconda_field_name}` to the model?"
end
if form_options[:base_key]
options[:base_key] = form_options[:base_key]
else
options[:base_key] = instance.send(options[:base_key].to_s) if options[:base_key].kind_of? Symbol
end
uploader = Anaconda::S3Uploader.new(options)
output += self.input_field "file", name: "file", id: element_id, as: :file, data: {url: uploader.url, form_data: uploader.fields.to_json, media_types: Anaconda.js_file_types}
end
output += self.hidden_field "#{anaconda_field_name}_filename".to_sym, data: {"#{instance.class.to_s.underscore}_#{anaconda_field_name}_filename" => true}
output += self.hidden_field "#{anaconda_field_name}_file_path".to_sym, data: {"#{instance.class.to_s.underscore}_#{anaconda_field_name}_file_path" => true}
output += self.hidden_field "#{anaconda_field_name}_size".to_sym, data: {"#{instance.class.to_s.underscore}_#{anaconda_field_name}_size" => true}
output += self.hidden_field "#{anaconda_field_name}_original_filename".to_sym, data: {"#{instance.class.to_s.underscore}_#{anaconda_field_name}_original_filename" => true}
output += self.hidden_field "#{anaconda_field_name}_stored_privately".to_sym, data: {"#{instance.class.to_s.underscore}_#{anaconda_field_name}_stored_privately" => true}
output += self.hidden_field "#{anaconda_field_name}_type".to_sym, data: {"#{instance.class.to_s.underscore}_#{anaconda_field_name}_type" => true}
if form_options[:remove_button] && self.object.send("#{anaconda_field_name}_file_path").present?
remove_button_text = form_options[:remove_button].kind_of?(String) ? form_options[:remove_button] : "Remove"
output += "<a href='#' data-#{"remove_#{instance.class.to_s.underscore}_#{anaconda_field_name}".gsub('_', '-')}>#{remove_button_text}</a>"
end
output += "</div>"
options = options.merge(as: anaconda_field_name, form_options: form_options, element_id: element_id )
output += <<-END
<div id="#{instance.class.to_s.underscore}_#{anaconda_field_name}_details"></div>
<script>
(function() {
window.uploader = new AnacondaUploadField({
element_id: "##{options[:element_id]}",
base_key: "#{options[:base_key]}",
allowed_types: #{options[:allowed_file_types].collect{ |i| i.to_s }},
upload_details_container: "#{options[:form_options][:upload_details_container]}",
upload_automatically: #{options[:form_options][:auto_upload] ||= false},
submit_automatically: #{options[:form_options][:auto_submit] ||= false},
resource: "#{instance.class.to_s.underscore}",
attribute: "#{options[:as]}"
});
}).call(this);
</script>
END
output.html_safe
end
|