Rails SDK for Filestack API and content management system.
**Important: This is the readme for 4.0.0+.** Note that the [Filestack::Ruby](https://github.com/filestack/filestack-ruby/) dependency has been updated to no longer interfere with namespace. However, if you were using that dependency in your Rails application, you will need to change any `Client` and `Filelink` class declarations to `FilestackClient` and `FilestackFilelink`, as per documented [here](https://github.com/filestack/filestack-ruby/blob/master/README.md). ## Overview * A multi-part uploader powered on the backend by the [Filestack CIN](https://www.filestack.com/products/content-ingestion-network). * An interface to the [Filestack Processing Engine](https://www.filestack.com/docs/image-transformations) for transforming assets via URLs. * The Filestack Picker - an upload widget for the web that integrates over a dozen cloud providers and provides pre-upload image editing. ## Installation Add this line to your application's `Gemfile`: ```ruby gem 'filestack-rails' ``` And then execute: $ bundle Or install it yourself as: $ gem install filestack-rails Add the Filestack File Picker and initialization script to your layout: ```erb <%= filestack_js_include_tag %> <%= filestack_js_init_tag %> ``` **Please note:** The scripts need to be added before your application's custom scripts, e.g. before any scripts in your assets folder, if you need access the Filestack client in your own Javascript. Set your API key and client name in `config/application.rb`: ```ruby config.filestack_rails.api_key = 'Your Filestack API Key' config.filestack_rails.client_name = 'custom_client_name' ``` The client name defaults to `"filestack_client"` and is injected into your client-side Javascript. This is because v3 of the File Picker lives in the Javascript of your web application. ### Filestack Picker Version For Filestack Rails SDK v.4.0.0+, the picker `version` is setup by default to `v3`. It means that Javascript version is 1.x.x. If you want to use older Javascript version (0.11.5), you have to configure `version` to `v2` in `config/application.rb`: ```ruby # filestack-js (0.11.5) 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' ``` Please take a look on available versions in [filestack-js](https://github.com/filestack/filestack-js) repository. ### 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. Set your CNAME in `config/application.rb`: ```ruby config.filestack_rails.cname = 'custom_cname' ``` ### Security If your account has security enabled, then you must initialize the File Picker with a signature and policy. Set up your application secret and security options in `config/application.rb`: ```ruby config.filestack_rails.app_secret = 'YOUR_APP_SECRET' config.filestack_rails.security = {'call' => %w[pick store read convert] } ``` If you set security to an empty object like so: ```ruby config.filestack_rails.security = {} ``` It will provide a policy and signature with only an expiry setting (this defaults to one hour). You can access the generated policy and signature anytime by calling their attributes on the created security object: ```ruby puts config.filestack_rails.security.policy puts config.filestack_rails.security.signature ``` You can also generate a new security object at any time, although this will only affect the filestack_image tag, and not the File Picker client. ## Usage ### Filestack Upload Button This is a generic button that can be added anywhere in your application and opens an instance of the File Picker. Once a user has chosen a file(s) and submitted, a callback will be executed, passing in the results. You can also pass in any options for the File Picker using the `pickerOptions` symbol: ```erb <%= filestack_picker_element 'button test', 'callbackForButton', id: 'someuniqueid', input_id: 'someuniqueinputid', pickerOptions: { 'fromSources': 'facebook', 'maxFiles': 50 } %> ``` File Picker options are exactly the same as in the Javscript SDK and can be found in the aforementioned documentation. The callback can be either the name of a function you've defined in your main Javascript or it can be any code that is immediately executable, e.g. `console.log` or `(function(data){console.log(data)})`. The callback should take in a response array as its only argument, which has the following structure: ```javascript { "filesUploaded": [ { "filename":"Birds", "handle":"unique_filestack_handle", "mimetype":"image/jpeg", "originalPath":"/bird/flickr/3/2849/9533051578_377332e54c_z.jpg/Birds", "size":121727, "source":"imagesearch", "url":"https://cdn.filestackcontent.com/unique_filestack_handle", "key":"fnZb1ElSMmKCLPNaErRU_bird.jpg", "container":"filestack-website-uploads" }, { "filename": ... } ], "filesFailed": [] } ``` Each file that is uploaded will be represented as a single object within the filesUploaded array. Accessing the first file uploaded in the callback would be achieved like so: ```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', onFileUploadFinished: 'callbackOnFileUploadFinished' } %> ``` where following callbacks `callbackOnClose`, `callbackOnOpen`, `callbackOnFileUploadFinished` are javascript function's name and refer to your own created function. For instance: ```js function onFileUploadFinishedCallback(data) {console.log(data);} ``` ### 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. ```erb <%= form_for @user do |f| %>