Skip to content

Commit

Permalink
Merge 3385df5 into 4e08828
Browse files Browse the repository at this point in the history
  • Loading branch information
gabifija committed Jan 22, 2019
2 parents 4e08828 + 3385df5 commit adbe06a
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 6 deletions.
18 changes: 13 additions & 5 deletions app/helpers/filestack_rails/application_helper.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
include FilestackRails::Transform
include FilestackRails::Version

module FilestackRails
module ApplicationHelper
def filestack_js_include_tag
javascript_include_tag(
'https://static.filestackapi.com/v3/filestack.js', type: 'text/javascript'
)
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') }

load_filestack_js(v2: v2, v3: v3)
end

def filestack_js_init_tag
Expand Down Expand Up @@ -52,9 +54,15 @@ def create_javascript_for_picker(callback, options)
else
options.to_json
end
"(function(){
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()
})()" }
load_filestack_js(v2: v2, v3: v3)
end

def get_client_and_api_key
Expand Down
6 changes: 5 additions & 1 deletion lib/filestack_rails/configuration.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module FilestackRails
class Configuration
attr_accessor :api_key, :client_name, :secret_key, :security, :expiry, :app_secret, :cname
attr_accessor :api_key, :client_name, :secret_key, :security, :expiry, :app_secret, :cname, :version

def api_key
@api_key or raise "Set config.filestack_rails.api_key"
Expand All @@ -10,6 +10,10 @@ def client_name
@client_name or 'filestack_client'
end

def version
@version or 'v2'
end

def expiry
@expiry or ( Time.zone.now.to_i + 600 )
end
Expand Down
20 changes: 20 additions & 0 deletions lib/filestack_rails/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
class FilestackVersion
def initialize
@version = ::Rails.application.config.filestack_rails.version
end

def determine_filestack_js(strategies)
begin
strategies[@version.to_sym].call
rescue
raise 'Set correct version in config.filestack_rails.version'
end
end
end

module FilestackRails
VERSION = '3.2.2'

module Version
def load_filestack_js(strategies)
FilestackVersion.new.determine_filestack_js(strategies)
end
end
end

0 comments on commit adbe06a

Please sign in to comment.