diff --git a/app/helpers/filestack_rails/application_helper.rb b/app/helpers/filestack_rails/application_helper.rb index 35bbdaa..6d86e21 100644 --- a/app/helpers/filestack_rails/application_helper.rb +++ b/app/helpers/filestack_rails/application_helper.rb @@ -1,6 +1,8 @@ +include FilestackRails::Transform + module FilestackRails module ApplicationHelper - + def filestack_js_include_tag javascript_include_tag "https://static.filestackapi.com/v3/filestack.js", type: "text/javascript" end @@ -14,6 +16,22 @@ def filestack_js_init_tag def filestack_picker_element(content, callback, options = {}) button_tag content, onclick: create_javascript_for_picker(callback, options), type: 'button' end + + def filestack_transform + _, apikey = get_client_and_api_key + get_transform(apikey) + end + + def filestack_image(url, options = {}) + transform_object = options[:transform] + options.delete(:transform) + if transform_object + transform_object.add_external_url url + image_tag transform_object.fs_url, options + else + image_tag url + end + end private @@ -34,5 +52,6 @@ def get_client_and_api_key apikey = ::Rails::application.config.filestack_rails.api_key [client_name, apikey] end + end end diff --git a/filestack_rails.gemspec b/filestack_rails.gemspec index 208f220..edc2b7c 100644 --- a/filestack_rails.gemspec +++ b/filestack_rails.gemspec @@ -17,6 +17,7 @@ Gem::Specification.new do |s| s.files = Dir["{app,config,db,lib}/**/*", "MIT-LICENSE", "Rakefile", "README.md"] s.add_dependency "rails", "~> 5.1.2" + s.add_dependency "filestack", "~> 2.0.1" s.add_development_dependency 'coveralls' s.add_development_dependency 'sqlite3' diff --git a/lib/filestack_rails.rb b/lib/filestack_rails.rb index 5e24b21..fc5f01c 100644 --- a/lib/filestack_rails.rb +++ b/lib/filestack_rails.rb @@ -1,4 +1,5 @@ require "filestack_rails/configuration" +require "filestack_rails/transform" require "filestack_rails/engine" module FilestackRails diff --git a/lib/filestack_rails/transform.rb b/lib/filestack_rails/transform.rb new file mode 100644 index 0000000..c5338e4 --- /dev/null +++ b/lib/filestack_rails/transform.rb @@ -0,0 +1,41 @@ +require 'filestack' + +class FilestackTransform + def initialize(apikey) + @transform = Transform.new(apikey:apikey) + end + + def method_missing(method_name, **args) + if defined? @transform.send(method_name) + raise "Invalid transformation for filestack_image" unless scrub_bad_transforms(method_name) + @transform = @transform.send(method_name, **args) + self + else + super + end + end + + def add_external_url(url) + @transform.instance_variable_set(:@external_url, url) + end + + def fs_url + @transform.url + end +end + +module FilestackRails + module Transform + + def get_transform(apikey) + FilestackTransform.new(apikey) + end + + end +end + +private + +def scrub_bad_transforms(method_name) + !['av_convert', 'debug', 'store', 'url'].include? method_name.to_s +end \ No newline at end of file diff --git a/spec/dummy/app/controllers/hello_controller.rb b/spec/dummy/app/controllers/hello_controller.rb index 6ae981c..8e272a6 100644 --- a/spec/dummy/app/controllers/hello_controller.rb +++ b/spec/dummy/app/controllers/hello_controller.rb @@ -1,4 +1,5 @@ class HelloController < ApplicationController + include FilestackRails::ApplicationHelper def index @user = User.new end diff --git a/spec/dummy/app/views/hello/index.html.erb b/spec/dummy/app/views/hello/index.html.erb index 71fef95..a33b11c 100644 --- a/spec/dummy/app/views/hello/index.html.erb +++ b/spec/dummy/app/views/hello/index.html.erb @@ -1,5 +1,6 @@