/
form_helper.rb
105 lines (93 loc) · 4.58 KB
/
form_helper.rb
1
2
3
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
module FilepickerRails
module FormHelper
FILEPICKER_OPTIONS_TO_DASHERIZE = [:button_text, :button_class, :mimetypes,
:extensions, :container, :services,
:drag_text, :drag_class, :store_path,
:store_location, :store_access,
:store_container, :multiple]
FILEPICKER_OPTIONS_TO_CAMELIZE = [:max_size, :max_files, :open_to]
# Creates a filepicker field, accepts optional `options` hash for configuration.
#
# #### Options
#
# - `:button_text` - The text of the upload button.
# - `:button_class` - The class of the upload button.
# - `:extensions` - The extensions of file types you want to support for this upload. Ex: `.png,.jpg`.
# - `:mimetypes` - The file types you want to support for this upload. Ex: `image/png,text/*`.
# - `:container` - Where to show the file picker dialog can be `modal`, `window` or the id of an iframe on the page.
# - `:multiple` - (true or false) Whether or not multiple uploads can be saved at once.
# - `:services` - What services your users can upload to. Ex: `BOX, COMPUTER, FACEBOOK`.
# - `:store_path` - The path to store the file at within the specified file store.
# - `:store_location` - The file is not copied by default. It remains in the original location. If you wish you have the file copied onto your own storage, you can specify where we should put the copy. The only value at the moment is `S3`.
# - `:store_container` - The bucket or container in your specified `store_location`. Defaults to the container specified in the developer portal. Does not apply to Dropbox storage.
# - `:store_access` - Should the underlying file be publicly available on its S3 link. Options are `public` and `private`, defaults to 'private'.
# - `:dragdrop` - (`true` or `false`) Whether or not to allow drag-and-drop uploads.
# - `:drag_text` - The text of the dragdrop pane.
# - `:drag_class` - The class of the dragdrop pane.
# - `:onchange` - The onchange event.
# - `:max_size` - The maximum file size allowed, in bytes.
# - `:max_files` - The maximum number of files.
# - `:open_to` - Open the picker to the given service. Ex: `COMPUTER`.
# - `:class` - Add a class to the input.
# - `:value` - Define the value of the input
#
# #### Examples
#
# filepicker_field(:filepicker_url)
# # => <input data-fp-apikey="..." id="user_filepicker_url" name="user[filepicker_url]" type="filepicker" />
#
# This is mixed on form for to be used like.
#
# <%= form_for @user do |f| %>
# <%= f.filepicker_field :filepicker_url %>
# <%= f.submit %>
# <% end %>
#
def filepicker_field(method, options = {})
define_input_options(options)
@method = method
if rails_greater_than_4?
rails_greater_than_4_input
else
rails_input
end
end
private
attr_reader :input_options, :method, :type, :object_name, :template
def define_input_options(options)
@type = options.delete(:dragdrop) ? 'filepicker-dragdrop' : 'filepicker'
@input_options = retrieve_legacy_filepicker_options(options)
@input_options['data-fp-apikey'] ||= ::Rails.application.config.filepicker_rails.api_key
@input_options.merge!(secure_filepicker) unless @input_options['data-fp-policy'].present?
@input_options['type'] = @type
@input_options
end
def rails_greater_than_4_input
tag = ActionView::Helpers::Tags::TextField.new(object_name, method, template, objectify_options(input_options))
tag.send(:add_default_name_and_id, input_options)
tag.render
end
def rails_input
ActionView::Helpers::InstanceTag.new(object_name, method, template).to_input_field_tag(type, input_options)
end
def rails_greater_than_4?
::Rails.version.to_i >= 4
end
def filepicker_prefix
'data-fp-'
end
def retrieve_legacy_filepicker_options(options)
mappings = {}
FILEPICKER_OPTIONS_TO_DASHERIZE.each do |option|
mappings[option] = "#{filepicker_prefix}#{option.to_s.dasherize}"
end
FILEPICKER_OPTIONS_TO_CAMELIZE.each do |option|
mappings[option] = "#{filepicker_prefix}#{option.to_s.camelize(:lower)}"
end
Hash[options.map {|k, v| [mappings[k] || k, v] }]
end
def secure_filepicker
Policy.apply([:pick, :store], ['data-fp-policy', 'data-fp-signature'])
end
end
end