Skip to content

Commit

Permalink
Merge d1e577e into cecd824
Browse files Browse the repository at this point in the history
  • Loading branch information
gabifija committed Aug 1, 2019
2 parents cecd824 + d1e577e commit 6ee04b4
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 29 deletions.
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,21 @@ config.filestack_rails.version = 'v2'
# filestack-js (1.x.x)
config.filestack_rails.version = 'v3'
```

For Filestack Rails SDK v.5.0.0+, you have to provide picker version precisely. By default the picker version is setup to `3.x.x`. If you want to use older filestack-js version (0.11.5), you have to configure `version` to `0.11.5` in `config/application.rb`:
```ruby
# filestack-js (0.11.5)
config.filestack_rails.version = '0.11.5'

# filestack-js (1.x.x)
config.filestack_rails.version = '1.x.x'

# filestack-js (3.x.x)
config.filestack_rails.version = '3.x.x'
```

You can find all available filestack-js versions in [`lib/filestack_rails/version.rb`](./lib/filestack_rails/version.rb)

### CNAME

If you have set up a custom CNAME, you can add it to your configuration file. The Picker will modify all assets to formatted with your domain origin instead of Filestack's.
Expand Down
24 changes: 14 additions & 10 deletions app/helpers/filestack_rails/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
require 'json'
include FilestackRails::Transform
include FilestackRails::Version
include FilestackRails::FilestackJs

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') }
url_v0 = -> { javascript_include_tag('https://static.filestackapi.com/v3/filestack.js', type: 'text/javascript') }
url = -> { javascript_include_tag("https://static.filestackapi.com/filestack-js/#{version}/filestack.min.js", type: 'text/javascript') }

get_filestack_js_result(v2: v2, v3: v3)
determine_filestack_js_result(version_0: url_v0, version_1_2_3: url)
end

def filestack_js_init_tag
Expand Down Expand Up @@ -48,6 +48,10 @@ def cname
::Rails.application.config.filestack_rails.cname
end

def version
::Rails.application.config.filestack_rails.version
end

def create_javascript_for_picker(callback, options)
client_name, _api_key = get_client_and_api_key
other_callbacks = build_callbacks_js(options) if options
Expand All @@ -56,15 +60,15 @@ def create_javascript_for_picker(callback, options)
else
options.to_json
end
v2 = -> do
pick = -> do
<<~HTML
(function(){
#{client_name}.pick(#{json_string}).then(function(data){#{callback}(data)})
})()
HTML
end

v3 = -> do
picker = -> do
json_string = json_string[1..-2] # removed curly brackets help to generate pickerOptions in js

<<~HTML
Expand All @@ -73,7 +77,7 @@ def create_javascript_for_picker(callback, options)
})()
HTML
end
get_filestack_js_result(v2: v2, v3: v3)
determine_filestack_js_result(version_0: pick, version_1_2_3: picker)
end

def build_callbacks_js(options)
Expand Down Expand Up @@ -105,9 +109,9 @@ def get_policy_and_signature_string

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)
security_v0 = -> { signature_and_policy.to_json }
security = -> { { security: signature_and_policy }.to_json }
determine_filestack_js_result(version_0: security_v0, version_1_2_3: security)
else
"''"
end
Expand Down
8 changes: 7 additions & 1 deletion lib/filestack_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,13 @@ def client_name
end

def version
@version or 'v3'
return '3.x.x' unless @version

if FilestackRails::FilestackJs::ALL_VERSIONS.include?(@version)
@version
else
raise 'Incorrect version in config.filestack_rails.version'
end
end

def expiry
Expand Down
55 changes: 37 additions & 18 deletions lib/filestack_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,43 @@
class FilestackVersion
def initialize(results)
@version = ::Rails.application.config.filestack_rails.version
@results = results
end

def determine_filestack_js_result
begin
@results[@version.to_sym].call
rescue
raise 'Set correct version in config.filestack_rails.version'
end
end
end

module FilestackRails
VERSION = '4.0.7'

module Version
def get_filestack_js_result(results)
FilestackVersion.new(results).determine_filestack_js_result
module FilestackJs
VERSION_0 = '0.11.5'

VERSION_1_2_3 = [
'1.x.x', '1.0.0', '1.0.1', '1.0.2', '1.1.0', '1.2.0', '1.2.1', '1.3.0',
'1.3.1', '1.3.2', '1.4.0', '1.5.0', '1.5.1', '1.6.0', '1.6.1', '1.7.0',
'1.7.1', '1.7.2', '1.7.3', '1.7.4', '1.7.5', '1.7.6', '1.7.7', '1.8.0',
'1.8.1', '1.8.2', '1.8.3', '1.9.0', '1.10.0', '1.11.0', '1.12.0',
'1.12.1', '1.13.0', '1.13.1', '1.13.2', '1.13.3', '1.14.0', '1.14.1',
'1.14.2', '1.14.3', '1.14.4', '1.14.5', '1.14.6',

'2.x.x', '2.0.0', '2.0.1', '2.0.2', '2.0.3', '2.0.4', '2.0.5', '2.0.6',
'2.0.7', '2.1.0',

'3.x.x', '3.0.0', '3.1.0', '3.1.1', '3.2.0', '3.3.0', '3.3.1', '3.3.2',
'3.3.3', '3.3.4', '3.3.5', '3.4.0', '3.4.1', '3.4.2', '3.5.0'
]

ALL_VERSIONS = [
VERSION_0,
VERSION_1_2_3
]

def determine_filestack_js_result(results)
version = ::Rails.application.config.filestack_rails.version

begin
sym = case
when VERSION_0.include?(version); :version_0
when VERSION_1_2_3.include?(version); :version_1_2_3
else nil
end

results[sym].call
rescue
raise
end
end
end
end

0 comments on commit 6ee04b4

Please sign in to comment.