Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 20 additions & 9 deletions app/helpers/filestack_rails/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,20 +48,31 @@ def cname
end

def create_javascript_for_picker(callback, options)
client_name, = get_client_and_api_key
client_name, _api_key = get_client_and_api_key
json_string = if options.nil?
''
else
options.to_json
end
v2 = -> { "(function(){
#{client_name}.pick(#{json_string}).then(function(data){#{callback}(data)})
})()" }

v3 = -> { json_string = "#{json_string}".slice!(1, json_string.length-2) # removed curly brackets help to generate pickerOptions in js
"(function(){
#{client_name}.picker({#{json_string}, onUploadDone: data => #{callback}(data)}).open()
})()" }

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

Expand Down
8 changes: 5 additions & 3 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,12 @@

describe "#filestack_picker_element" do
it "has the right picker element" do
html_string = filestack_picker_element "hello!", "console.log('hello!')"
html_string = filestack_picker_element("hello!", "console.log('hello!')").gsub(/\s+/, ' ')

correct_string = '<button name="button" type="button" onclick="(function(){
rich_client.picker({, onUploadDone: data =&gt; console.log(&#39;hello!&#39;)(data)}).open()
})()">hello!</button>'
rich_client.picker({ onUploadDone: data =&gt; console.log(&#39;hello!&#39;)(data), }).open()
})() ">hello!</button>'.gsub(/\s+/, ' ')

expect(html_string).to eq(correct_string)
end
end
Expand Down