-
Notifications
You must be signed in to change notification settings - Fork 101
/
Copy pathapplication_helper.rb
113 lines (96 loc) · 3.36 KB
/
application_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
106
107
108
109
110
111
112
113
require 'json'
include FilestackRails::Transform
include FilestackRails::Version
module FilestackRails
module ApplicationHelper
def filestack_js_include_tag
v2 = -> { javascript_include_tag('https://static.filestackapi.com/v3/filestack.js', type: 'text/javascript') }
v3 = -> { javascript_include_tag('https://static.filestackapi.com/filestack-js/1.x.x/filestack.min.js', type: 'text/javascript') }
get_filestack_js_result(v2: v2, v3: v3)
end
def filestack_js_init_tag
client_name, apikey = get_client_and_api_key
signature_and_policy = get_policy_and_signature_string
javascript_string = "var #{client_name} = filestack.init('#{apikey}', #{signature_and_policy}, '#{cname}');"
javascript_tag javascript_string
end
def filestack_picker_element(content, callback, options = {})
picker_options = options[:pickerOptions]
options.delete(:pickerOptions)
options[:onclick] = create_javascript_for_picker(callback, picker_options)
options[:type] = 'button'
button_tag content, options
end
def filestack_transform
_, apikey = get_client_and_api_key
get_transform(apikey)
end
def filestack_image(url, options = {})
transform_object = options[:transform]
options.delete(:transform)
if transform_object
transform_object.add_external_url url
image_tag transform_object.fs_url, options
else
image_tag url
end
end
private
def cname
::Rails.application.config.filestack_rails.cname
end
def create_javascript_for_picker(callback, options)
client_name, _api_key = get_client_and_api_key
json_string = if options.nil?
''
else
options.to_json
end
v2 = -> do
<<~HTML
(function(){
#{client_name}.pick(#{json_string}).then(function(data){#{callback}(data)})
})()
HTML
end
v3 = -> do
json_string = json_string[1..-2] # removed curly brackets help to generate pickerOptions in js
<<~HTML
(function(){
#{client_name}.picker({ onUploadDone: data => #{callback}(data), #{json_string} }).open()
})()
HTML
end
get_filestack_js_result(v2: v2, v3: v3)
end
def get_client_and_api_key
client_name = ::Rails.application.config.filestack_rails.client_name
apikey = ::Rails.application.config.filestack_rails.api_key
[client_name, apikey]
end
def get_policy_and_signature
if security
signature = security.signature
policy = security.policy
end
[signature, policy]
end
def get_policy_and_signature_string
signature, policy = get_policy_and_signature
if policy && signature
signature_and_policy = { signature: signature, policy: policy }
v2 = -> { signature_and_policy.to_json }
v3 = -> { { security: signature_and_policy }.to_json }
get_filestack_js_result(v2: v2, v3: v3)
else
"''"
end
end
def security
security_options = ::Rails.application.config.filestack_rails.security
app_secret = ::Rails.application.config.filestack_rails.app_secret
return nil unless security_options
FilestackSecurity.new(app_secret, options: security_options)
end
end
end