Skip to content
Merged
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
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,13 @@ Each file that is uploaded will be represented as a single object within the fil
```javascript
url = data.filesUploaded[0].url
```

For version `v3`, you can add following callbacks: `onOpen`, `onClose`, `onFileUploadFinished`, `onFileSelected`, `onUploadStarted`, to `pickerOptions`.

```erb
<%= filestack_picker_element 'button test', 'callbackForButton', id: 'someuniqueid', input_id: 'someuniqueinputid', pickerOptions: { onClose: 'callbackOnClose', onOpen: 'callbackOnOpen' } %>
```

### Filestack Form Helper
The form helper wraps the generic Pick element and adds the value of the returned file to an invisible text element, in order to attach to the form. It accepts the same options as the Pick element and renders the same button.

Expand Down
13 changes: 12 additions & 1 deletion app/helpers/filestack_rails/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ def cname

def create_javascript_for_picker(callback, options)
client_name, _api_key = get_client_and_api_key
other_callbacks = build_callbacks_js(options) if options
json_string = if options.nil?
''
else
Expand All @@ -68,13 +69,23 @@ def create_javascript_for_picker(callback, options)

<<~HTML
(function(){
#{client_name}.picker({ onUploadDone: data => #{callback}(data), #{json_string} }).open()
#{client_name}.picker({ onUploadDone: data => #{callback}(data)#{other_callbacks}, #{json_string} }).open()
})()
HTML
end
get_filestack_js_result(v2: v2, v3: v3)
end

def build_callbacks_js(options)
string = ""
string << ", onOpen: data => #{options.delete(:onOpen)}(data)" unless options[:onOpen].blank?
string << ", onClose: () => #{options.delete(:onClose)}()" unless options[:onClose].blank?
string << ", onFileUploadFinished: data => #{options.delete(:onFileUploadFinished)}(data)" unless options[:onFileUploadFinished].blank?
string << ", onFileSelected: data => #{options.delete(:onFileSelected)}(data)" unless options[:onFileSelected].blank?
string << ", onUploadStarted: data => #{options.delete(:onUploadStarted)}(data)" unless options[:onUploadStarted].blank?
string
end

def get_client_and_api_key
client_name = ::Rails.application.config.filestack_rails.client_name
apikey = ::Rails.application.config.filestack_rails.api_key
Expand Down
22 changes: 22 additions & 0 deletions spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@
end
end

describe "#get_policy_and_signature" do
it "returns empty data" do
expect(get_policy_and_signature).to eq([nil, nil])
end
end

describe "#get_policy_and_signature_string" do
let(:signature) { "signature123" }
let(:policy) { "policy321" }
Expand Down Expand Up @@ -80,4 +86,20 @@
expect(security).to be(nil)
end
end

describe "#build_callbacks_js" do
expected_values = {
{ onClose: 'callbackOnClose' } => ', onClose: () => callbackOnClose()',
{ onOpen: 'callbackOnOpen' } => ', onOpen: data => callbackOnOpen(data)',
{ onFileUploadFinished: 'callbackOnFileUploadFinished' } => ', onFileUploadFinished: data => callbackOnFileUploadFinished(data)',
{ onFileSelected: 'callbackOnFileSelected' } => ', onFileSelected: data => callbackOnFileSelected(data)',
{ onUploadStarted: 'callbackOnUploadStarted' } => ', onUploadStarted: data => callbackOnUploadStarted(data)'
}
expected_values.each do |val, expected|
it "returns correct string for #{val}" do
result = build_callbacks_js(val)
expect(result).to eq(expected)
end
end
end
end