From bd3b20a82b7e5935a7f2b287610df8abd49e46a1 Mon Sep 17 00:00:00 2001 From: Richard Herbert Date: Thu, 20 Jul 2017 15:36:13 -0500 Subject: [PATCH 1/3] add fielstack_image tag --- .../filestack_rails/application_helper.rb | 20 ++++++++- filestack_rails.gemspec | 1 + lib/filestack_rails.rb | 1 + lib/filestack_rails/transform.rb | 41 +++++++++++++++++++ .../dummy/app/controllers/hello_controller.rb | 1 + spec/dummy/app/views/hello/index.html.erb | 1 + 6 files changed, 64 insertions(+), 1 deletion(-) create mode 100644 lib/filestack_rails/transform.rb diff --git a/app/helpers/filestack_rails/application_helper.rb b/app/helpers/filestack_rails/application_helper.rb index 35bbdaa..e8b632d 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,21 @@ 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] + if transform_object + transform_object.add_external_url url + image_tag transform_object.url, options + else + image_tag url + end + end private @@ -34,5 +51,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..c360abe --- /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 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 @@

Rails Picker Test

<%= filestack_picker_element "pick!", "logIt", {'accept' => 'image/*', 'fromSources' => 'facebook'} %> +<%= filestack_image 'https://tinyurl.com/yanvlakc', transform: filestack_transform.resize(width:100, height:100), id: 'unique-id' %> <%= form_for @user do |f| %> <%= f.label :picture %> <%= f.filestack_field :picture, 'Upload a Picture' %> From f84ceb318f5164b64c292a1c153bffd7683f252a Mon Sep 17 00:00:00 2001 From: Richard Herbert Date: Thu, 20 Jul 2017 15:58:32 -0500 Subject: [PATCH 2/3] update tests and remove transform from options --- app/helpers/filestack_rails/application_helper.rb | 3 ++- lib/filestack_rails/transform.rb | 4 ++-- spec/helpers/application_helper_spec.rb | 10 +++++++++- 3 files changed, 13 insertions(+), 4 deletions(-) diff --git a/app/helpers/filestack_rails/application_helper.rb b/app/helpers/filestack_rails/application_helper.rb index e8b632d..6d86e21 100644 --- a/app/helpers/filestack_rails/application_helper.rb +++ b/app/helpers/filestack_rails/application_helper.rb @@ -24,9 +24,10 @@ def filestack_transform 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.url, options + image_tag transform_object.fs_url, options else image_tag url end diff --git a/lib/filestack_rails/transform.rb b/lib/filestack_rails/transform.rb index c360abe..c5338e4 100644 --- a/lib/filestack_rails/transform.rb +++ b/lib/filestack_rails/transform.rb @@ -19,7 +19,7 @@ def add_external_url(url) @transform.instance_variable_set(:@external_url, url) end - def url + def fs_url @transform.url end end @@ -30,7 +30,7 @@ module Transform def get_transform(apikey) FilestackTransform.new(apikey) end - + end end diff --git a/spec/helpers/application_helper_spec.rb b/spec/helpers/application_helper_spec.rb index bff6a9f..6ed980e 100644 --- a/spec/helpers/application_helper_spec.rb +++ b/spec/helpers/application_helper_spec.rb @@ -20,7 +20,7 @@ end end - describe "filestack_picker_element" do + describe "#filestack_picker_element" do it "has the right picker element" do html_string = filestack_picker_element "hello!", "console.log('hello!')" correct_string = '