Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/helpers/filestack_rails/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ def filestack_js_init_tag
end

def filestack_picker_element(content, callback, options = {})
button_tag content, onclick: create_javascript_for_picker(callback, options)
button_tag content, onclick: create_javascript_for_picker(callback, options), type: 'button'
end

private
Expand Down
36 changes: 14 additions & 22 deletions app/helpers/filestack_rails/form_helper.rb
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
module FilestackRails
module FormHelper

def filestack_field(method, options = {})
get_filestack_field_button(method, options)
include FilestackRails::ApplicationHelper
include ActionView::Helpers
def filestack_field(method, content, options = {})
get_filestack_field_button(method, content, options)
end

private

def get_filestack_field_button(method, options)
# TODO: add filestack_field button
end
def get_filestack_field_button(method, content, options = {})
options[:id] = options[:id] || "#{@object.class.name.downcase}_#{method.downcase}"
options[:style] = 'display:none'
user_callback = options[:callback] || nil

def create_javascript_for_picker(callback, options)
client_name, apikey = get_client_and_api_key
json_string = if options.nil?
''
else
options.to_json
end
"(function(){
#{client_name}.pick(#{json_string}).then(function(data){#{callback}(data)})
})()"
end
form_field_callback_guts = "const filestack_input_field = document.getElementById('#{options[:id]}');" \
"filestack_input_field.value = data.filesUploaded[0].url;"
form_field_callback_guts = "#{form_field_callback_guts}#{user_callback}(data)" unless user_callback.nil?
form_field_callback = "(function(data){#{form_field_callback_guts}})"

def get_client_and_api_key
client_name = ::Rails.application.config.filestack_rails.client_name
apikey = ::Rails::application.config.filestack_rails.api_key
[client_name, apikey]
html_string = "#{filestack_picker_element("#{content}", form_field_callback)}#{text_field(method, options)}"
raw html_string.html_safe
end

end
end
4 changes: 4 additions & 0 deletions spec/dummy/app/controllers/hello_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,8 @@ class HelloController < ApplicationController
def index
@user = User.new
end

def show
@users = User.all
end
end
5 changes: 5 additions & 0 deletions spec/dummy/app/views/hello/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
<h1>Rails Picker Test</h1>
<%= filestack_picker_element "pick!", "logIt", {'accept' => 'image/*', 'fromSources' => 'facebook'} %>
<%= form_for @user do |f| %>
<%= f.label :picture %>
<%= f.filestack_field :picture, 'Upload a Picture' %>
<%= f.submit %>
<% end %>
2 changes: 1 addition & 1 deletion spec/dummy/config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 5.1
config.assets.compile = true
config.filestack_rails.api_key = 'Af7SDtlubQPqhByVjt4qPz'
config.filestack_rails.api_key = 'API_KEY'
config.filestack_rails.client_name = 'rich_client'

# Settings in config/environments/* take precedence over those specified here.
Expand Down
9 changes: 0 additions & 9 deletions spec/dummy/test/controllers/hello_controller_test.rb

This file was deleted.

11 changes: 0 additions & 11 deletions spec/dummy/test/fixtures/users.yml

This file was deleted.

7 changes: 0 additions & 7 deletions spec/dummy/test/models/user_test.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/helpers/application_helper_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
describe "filestack_picker_element" do
it "has the right picker element" do
html_string = filestack_picker_element "hello!", "console.log('hello!')"
correct_string = '<button name="button" type="submit" onclick="(function(){
correct_string = '<button name="button" type="button" onclick="(function(){
rich_client.pick({}).then(function(data){console.log(&#39;hello!&#39;)(data)})
})()">hello!</button>'
expect(html_string).to eq(correct_string)
Expand Down
14 changes: 14 additions & 0 deletions spec/helpers/form_helper_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
require 'rails_helper'

RSpec.describe FilestackRails::ApplicationHelper do
include FilestackRails::ApplicationHelper
include FilestackRails::FormHelper
include ActionView::Helpers
describe '#filestack_field' do
it "provides the correct form_tag" do
output = filestack_field :picture, "some content"
expect(output).to include("<button")
expect(output).to include("</button>")
end
end
end