filepickerio_rails 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +5 -0
- data/.rspec +2 -0
- data/Gemfile +4 -0
- data/README.md +44 -0
- data/Rakefile +1 -0
- data/filepickerio_rails.gemspec +28 -0
- data/lib/filepickerio_rails/action_view/form_builder.rb +14 -0
- data/lib/filepickerio_rails/action_view/form_tag_helper.rb +111 -0
- data/lib/filepickerio_rails/configuration.rb +27 -0
- data/lib/filepickerio_rails/railtie.rb +23 -0
- data/lib/filepickerio_rails/version.rb +3 -0
- data/lib/filepickerio_rails.rb +18 -0
- data/spec/form_tag_helper_spec.rb +69 -0
- data/spec/spec_helper.rb +21 -0
- data/spec/support/form_helpers.rb +14 -0
- metadata +113 -0
data/.rspec
ADDED
data/Gemfile
ADDED
data/README.md
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
filepickerio_rails
|
2
|
+
==================
|
3
|
+
|
4
|
+
Installation
|
5
|
+
------------
|
6
|
+
|
7
|
+
Add your Filepicker.io API Key to an initialiser (`config/initializer/filepickerio_rails.rb`):
|
8
|
+
|
9
|
+
FilepickerioRails.configure do |config|
|
10
|
+
config.api_key = '[YOUR API KEY FROM FILEPICKER.IO]'
|
11
|
+
end
|
12
|
+
|
13
|
+
Include the filepicker.io JavaScript library in your page (such as your application.html.erb layout):
|
14
|
+
|
15
|
+
<%= javascript_include_tag "//api.filepicker.io/v0/filepicker.js" %>
|
16
|
+
|
17
|
+
... or, if you're not using the asset pipeline, you can use the expansion:
|
18
|
+
|
19
|
+
<%= javascript_include_tag :filepickerio %>
|
20
|
+
|
21
|
+
Render a Filepicker.io file upload and save fields using the form tag helper:
|
22
|
+
|
23
|
+
<%= fp_file_field_tag :image_url, 'Pick file', 'http://example.com/existing-upload.jpg' %>
|
24
|
+
<%= fp_file_field_tag :image_url, 'Pick file', 'http://example.com/existing-upload.jpg', 'image/jpg' %>
|
25
|
+
<%= fp_file_field_tag :image_url, 'Pick file', 'http://example.com/existing-upload.jpg', 'image/jpg', { class: 'primary btn' } %>
|
26
|
+
|
27
|
+
<%= fp_save_button 'Save to Dropbox', 'http://www.filepicker.io/static/img/success.png', 'image/jpg' %>
|
28
|
+
<%= fp_save_button 'Save to Dropbox', 'http://www.filepicker.io/static/img/success.png', 'image/jpg', data: { 'fp-option-services' => 'DROPBOX' } %>
|
29
|
+
|
30
|
+
Or use the data-bound form builder methods, fp_file_field, fp_save_button:
|
31
|
+
|
32
|
+
<%= form_for @entry do |f| %>
|
33
|
+
<%= f.fp_file_field :image_url %>
|
34
|
+
<%= f.fp_file_field :lolcat_image_url, "Upload lolcat" %>
|
35
|
+
|
36
|
+
<%= f.fp_save_button :image_url, "Save existing image to cloud", 'image/jpg' %>
|
37
|
+
<%- end %>
|
38
|
+
|
39
|
+
Filepicker.io Data Params
|
40
|
+
-------------------------
|
41
|
+
|
42
|
+
Pass a data hash as the last parameter to the helper methods to control the Filepicker.io widget.
|
43
|
+
|
44
|
+
See the documentation at [https://developers.filepicker.io/docs/web/](https://developers.filepicker.io/docs/web/)
|
data/Rakefile
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
require "bundler/gem_tasks"
|
@@ -0,0 +1,28 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "filepickerio_rails/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "filepickerio_rails"
|
7
|
+
s.version = FilepickerioRails::VERSION
|
8
|
+
s.authors = ["Adam Burmister"]
|
9
|
+
s.email = ["adam.burmister@gmail.com"]
|
10
|
+
s.homepage = "http://adamburmister.github.com/filepickerio_rails/"
|
11
|
+
s.summary = %q{Rails view helpers for Filepicker.io widgets}
|
12
|
+
s.description = %q{Rails view helpers for Filepicker.io widgets}
|
13
|
+
|
14
|
+
s.rubyforge_project = "filepickerio_rails"
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split("\n")
|
17
|
+
s.files.reject! { |fn| fn.include? "example" } # don't include our example rails project which is just for testing
|
18
|
+
|
19
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
20
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
|
23
|
+
# specify any dependencies here; for example:
|
24
|
+
s.add_development_dependency "rspec"
|
25
|
+
s.add_development_dependency "rspec-rails"
|
26
|
+
s.add_development_dependency "rails"
|
27
|
+
s.add_dependency "railties", "~> 3.1"
|
28
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
module FilepickerioRails
|
2
|
+
class ::ActionView::Helpers::FormBuilder
|
3
|
+
|
4
|
+
# Returns a input tag tailored for a Filepicker.io upload widget to be attached for the form object
|
5
|
+
def fp_file_field(method, text=nil, options={})
|
6
|
+
@template.fp_file_field(@object_name, method, text, options)
|
7
|
+
end
|
8
|
+
|
9
|
+
def fp_save_button(method, text, mime, options={}, &block)
|
10
|
+
@template.fp_save_button(@object, method, text, mime, options, &block)
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,111 @@
|
|
1
|
+
module FilepickerioRails
|
2
|
+
module ActionView
|
3
|
+
|
4
|
+
# This module creates filepicker.io fields
|
5
|
+
#
|
6
|
+
# Examples:
|
7
|
+
#
|
8
|
+
# filepickerio_upload_tag :user, 'Pick file', 'http://example.com/existing-upload.jpg', data: { "fp-mimetypes": "image/jpg" }
|
9
|
+
#
|
10
|
+
# filepickerio_save_button_tag 'Save file to cloud', 'http://example.com/existing-upload.jpg', 'image/jpeg'
|
11
|
+
#
|
12
|
+
module FormTagHelper
|
13
|
+
|
14
|
+
def fp_file_field_tag(object_name, text_or_options=nil, value_or_options=nil, options={})
|
15
|
+
# Allow users to pass in variable length arguments
|
16
|
+
if text_or_options.is_a? Hash
|
17
|
+
text = value = nil
|
18
|
+
options = text_or_options
|
19
|
+
elsif value_or_options.is_a? Hash
|
20
|
+
value = nil
|
21
|
+
options = value_or_options
|
22
|
+
else
|
23
|
+
text = text_or_options
|
24
|
+
value = value_or_options
|
25
|
+
end
|
26
|
+
|
27
|
+
options.merge!(
|
28
|
+
# This avoids the ActiveModel naming of `object_name[method]`
|
29
|
+
id: object_name,
|
30
|
+
name: object_name,
|
31
|
+
value: value
|
32
|
+
)
|
33
|
+
|
34
|
+
fp_file_field(object_name, nil, text, options)
|
35
|
+
end
|
36
|
+
|
37
|
+
def fp_file_field(object_name, method, text, options)
|
38
|
+
dragdrop = options[:dragdrop] && options[:dragdrop] == true
|
39
|
+
|
40
|
+
input_type = if dragdrop
|
41
|
+
options.delete(:dragdrop)
|
42
|
+
'filepicker-dragdrop'
|
43
|
+
else
|
44
|
+
'filepicker'
|
45
|
+
end
|
46
|
+
|
47
|
+
options = {
|
48
|
+
type: input_type,
|
49
|
+
size: nil,
|
50
|
+
data: {
|
51
|
+
"fp-apikey" => fp_api_key,
|
52
|
+
"fp-button-text" => text || 'Pick File'
|
53
|
+
}
|
54
|
+
}.deep_merge(options)
|
55
|
+
|
56
|
+
::ActionView::Helpers::InstanceTag.new(object_name, method, self, options.delete(:object)).to_input_field_tag(input_type, options)
|
57
|
+
end
|
58
|
+
|
59
|
+
def fp_save_button_tag(content=nil, url=nil, mime=nil, options=nil, &block)
|
60
|
+
raise "URL of file to be saved must be set" if url.nil?
|
61
|
+
|
62
|
+
options.deep_merge!({
|
63
|
+
data: {
|
64
|
+
"fp-url" => url
|
65
|
+
}
|
66
|
+
})
|
67
|
+
|
68
|
+
fp_save_button(nil, nil, content, mime, options, &block)
|
69
|
+
end
|
70
|
+
|
71
|
+
def fp_save_button(object, method, content=nil, mime=nil, options={}, &block)
|
72
|
+
raise "Mime type of file to be saved must be set" if mime.nil?
|
73
|
+
|
74
|
+
value = options[:value]
|
75
|
+
if object && method
|
76
|
+
if !value
|
77
|
+
value = options.fetch(:value){ ::ActionView::Helpers::InstanceTag::value_before_type_cast(object, method.to_s) }
|
78
|
+
value &&= ERB::Util.html_escape(value)
|
79
|
+
end
|
80
|
+
end
|
81
|
+
|
82
|
+
options = {
|
83
|
+
name: nil,
|
84
|
+
type: nil,
|
85
|
+
data: {
|
86
|
+
"fp-apikey" => fp_api_key,
|
87
|
+
"fp-mimetype" => mime,
|
88
|
+
"fp-url" => value
|
89
|
+
}
|
90
|
+
}.deep_merge(options)
|
91
|
+
|
92
|
+
# Convert services array into string
|
93
|
+
if options[:data]['fp-option-services'].is_a? Array
|
94
|
+
options[:data]['fp-option-services'] = options[:data]['fp-option-services'].join(',')
|
95
|
+
end
|
96
|
+
|
97
|
+
button_tag(content || 'Save file', options, &block)
|
98
|
+
end
|
99
|
+
|
100
|
+
private
|
101
|
+
|
102
|
+
def fp_api_key
|
103
|
+
raise "Filepicker.io API Key not set. Check config/initializer/filepickerio_rails.rb" if !FilepickerioRails.config.api_key
|
104
|
+
FilepickerioRails.config.api_key
|
105
|
+
end
|
106
|
+
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
ActionView::Base.send :include, FilepickerioRails::ActionView::FormTagHelper
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'active_support/configurable'
|
2
|
+
|
3
|
+
module FilepickerioRails
|
4
|
+
# Configures global settings for FilepickerioRails
|
5
|
+
# FilepickerioRails.configure do |config|
|
6
|
+
# config.api_key = 'YOURKEY'
|
7
|
+
# end
|
8
|
+
def self.configure(&block)
|
9
|
+
yield @config ||= FilepickerioRails::Configuration.new
|
10
|
+
end
|
11
|
+
|
12
|
+
# Global settings for FilepickerioRails
|
13
|
+
def self.config
|
14
|
+
@config
|
15
|
+
end
|
16
|
+
|
17
|
+
# Do not access this class directly, use self.config
|
18
|
+
class Configuration
|
19
|
+
include ActiveSupport::Configurable
|
20
|
+
config_accessor :api_key
|
21
|
+
end
|
22
|
+
|
23
|
+
# Initialize the configuration
|
24
|
+
configure do |config|
|
25
|
+
config.api_key = nil
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
module FilepickerioRails
|
2
|
+
class Railtie < Rails::Railtie
|
3
|
+
|
4
|
+
# By running before_configuration we give users a chance to override
|
5
|
+
# within their rails config
|
6
|
+
config.before_configuration do
|
7
|
+
# If you're not using the asset pipeline you can use this javascript
|
8
|
+
# expansion to include the Filepicker.io JS lib
|
9
|
+
config.action_view.javascript_expansions[:filepickerio] = %w(//api.filepicker.io/v0/filepicker.js)
|
10
|
+
end
|
11
|
+
|
12
|
+
initializer 'filepickerio_rails.action_view' do
|
13
|
+
ActiveSupport.on_load :action_view do
|
14
|
+
# Load the helpers along with action_view
|
15
|
+
# The _tag helper renders non-model bound tags
|
16
|
+
require 'filepickerio_rails/action_view/form_tag_helper'
|
17
|
+
# The form builder renders model bound field tags
|
18
|
+
require 'filepickerio_rails/action_view/form_builder'
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# load Rails/Railtie
|
2
|
+
begin
|
3
|
+
require 'rails'
|
4
|
+
rescue LoadError
|
5
|
+
#do nothing
|
6
|
+
end
|
7
|
+
|
8
|
+
$stderr.puts <<-EOC if !defined?(Rails)
|
9
|
+
warning: no framework detected.
|
10
|
+
would you check out if your Gemfile appropriately configured?
|
11
|
+
---- e.g. ----
|
12
|
+
when Rails:
|
13
|
+
gem 'filepickerio_rails'
|
14
|
+
EOC
|
15
|
+
|
16
|
+
require "filepickerio_rails/version"
|
17
|
+
require 'filepickerio_rails/configuration'
|
18
|
+
require 'filepickerio_rails/railtie' if defined?(Rails)
|
@@ -0,0 +1,69 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe FilepickerioRails::ActionView::FormTagHelper do
|
4
|
+
describe "#filepickerio_save_button" do
|
5
|
+
pending
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#fp_file_field_tag" do
|
9
|
+
context "field" do
|
10
|
+
let :template do
|
11
|
+
<<-EOTEMPLATE
|
12
|
+
<%= fp_file_field_tag :entry, "Pick file to upload", 'value.jpg', class: 'btn' %>
|
13
|
+
EOTEMPLATE
|
14
|
+
end
|
15
|
+
|
16
|
+
it "should render successfuly" do
|
17
|
+
render(inline: template)
|
18
|
+
rendered.should_not be_nil
|
19
|
+
end
|
20
|
+
|
21
|
+
it "should have a type of filepickerio" do
|
22
|
+
render(inline: template, locals: { user: user })
|
23
|
+
rendered.should have_xpath('//input[@type="filepicker"]')
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should have the API key as a data property data-fp-apikey" do
|
27
|
+
render(inline: template, locals: { user: user })
|
28
|
+
rendered.should have_xpath("//input[@data-fp-apikey='#{test_api_key}']")
|
29
|
+
end
|
30
|
+
|
31
|
+
describe "with dragdrop:true" do
|
32
|
+
let :template do
|
33
|
+
<<-EOTEMPLATE
|
34
|
+
<%= form_for(user) do |f| %>
|
35
|
+
<%= f.fp_file_field(:avatar, dragdrop: true) %>
|
36
|
+
<%- end -%>
|
37
|
+
EOTEMPLATE
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should render the type as `filepicker-dragdrop`" do
|
41
|
+
render(inline: template, locals: { user: user })
|
42
|
+
rendered.should have_xpath('//input[@type="filepicker-dragdrop"]')
|
43
|
+
end
|
44
|
+
end
|
45
|
+
|
46
|
+
describe "with optional data params" do
|
47
|
+
let :template do
|
48
|
+
<<-EOTEMPLATE
|
49
|
+
<%= form_for(user) do |f| %>
|
50
|
+
<%= f.fp_file_field(:avatar, data: { "data-fp-apikey" => "987654321", "data-fp-class" => "myCssClass", "data-fp-button-text" => "UploadFu" }) %>
|
51
|
+
<%- end -%>
|
52
|
+
EOTEMPLATE
|
53
|
+
end
|
54
|
+
|
55
|
+
it "should render any passed Filepicker.io configuration data options" do
|
56
|
+
render(inline: template, locals: { user: user })
|
57
|
+
rendered.should have_xpath('//input[@data-fp-class="myCssClass"]')
|
58
|
+
rendered.should have_xpath('//input[@data-fp-button-text="UploadFu"]')
|
59
|
+
end
|
60
|
+
|
61
|
+
it "should override the API key outputted if passed" do
|
62
|
+
render(inline: template, locals: { user: user })
|
63
|
+
rendered.should have_xpath('//input[@data-fp-apikey="987654321"]')
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
end
|
69
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'filepickerio_rails'
|
2
|
+
|
3
|
+
Dir[ File.dirname(__FILE__) << "/support/**/*.rb"].each {|file| require file }
|
4
|
+
|
5
|
+
# This file was generated by the `rspec --init` command. Conventionally, all
|
6
|
+
# specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
|
7
|
+
# Require this file using `require "spec_helper"` to ensure that it is only
|
8
|
+
# loaded once.
|
9
|
+
#
|
10
|
+
# See http://rubydoc.info/gems/rspec-core/RSpec/Core/Configuration
|
11
|
+
RSpec.configure do |config|
|
12
|
+
config.treat_symbols_as_metadata_keys_with_true_values = true
|
13
|
+
config.run_all_when_everything_filtered = true
|
14
|
+
config.filter_run :focus
|
15
|
+
|
16
|
+
# Run specs in random order to surface order dependencies. If you find an
|
17
|
+
# order dependency and want to debug it, you can fix the order by providing
|
18
|
+
# the seed, which is printed after each run.
|
19
|
+
# --seed 1234
|
20
|
+
config.order = 'random'
|
21
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'action_view'
|
2
|
+
require 'action_view/template'
|
3
|
+
|
4
|
+
require 'filepickerio_rails/action_view/form_tag_helper'
|
5
|
+
|
6
|
+
module FormHelpers
|
7
|
+
include ActionView::Helpers::FormHelper
|
8
|
+
include ActionView::Context
|
9
|
+
include ActionController::RecordIdentifier
|
10
|
+
|
11
|
+
def protect_against_forgery?
|
12
|
+
false
|
13
|
+
end
|
14
|
+
end
|
metadata
ADDED
@@ -0,0 +1,113 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: filepickerio_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Adam Burmister
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-09-15 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rspec
|
16
|
+
requirement: &70225434735840 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70225434735840
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: rspec-rails
|
27
|
+
requirement: &70225434735260 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ! '>='
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '0'
|
33
|
+
type: :development
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70225434735260
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: rails
|
38
|
+
requirement: &70225434734180 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :development
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70225434734180
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: railties
|
49
|
+
requirement: &70225434732980 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ~>
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.1'
|
55
|
+
type: :runtime
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70225434732980
|
58
|
+
description: Rails view helpers for Filepicker.io widgets
|
59
|
+
email:
|
60
|
+
- adam.burmister@gmail.com
|
61
|
+
executables: []
|
62
|
+
extensions: []
|
63
|
+
extra_rdoc_files: []
|
64
|
+
files:
|
65
|
+
- .gitignore
|
66
|
+
- .rspec
|
67
|
+
- Gemfile
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- filepickerio_rails.gemspec
|
71
|
+
- lib/filepickerio_rails.rb
|
72
|
+
- lib/filepickerio_rails/action_view/form_builder.rb
|
73
|
+
- lib/filepickerio_rails/action_view/form_tag_helper.rb
|
74
|
+
- lib/filepickerio_rails/configuration.rb
|
75
|
+
- lib/filepickerio_rails/railtie.rb
|
76
|
+
- lib/filepickerio_rails/version.rb
|
77
|
+
- spec/form_tag_helper_spec.rb
|
78
|
+
- spec/spec_helper.rb
|
79
|
+
- spec/support/form_helpers.rb
|
80
|
+
homepage: http://adamburmister.github.com/filepickerio_rails/
|
81
|
+
licenses: []
|
82
|
+
post_install_message:
|
83
|
+
rdoc_options: []
|
84
|
+
require_paths:
|
85
|
+
- lib
|
86
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
87
|
+
none: false
|
88
|
+
requirements:
|
89
|
+
- - ! '>='
|
90
|
+
- !ruby/object:Gem::Version
|
91
|
+
version: '0'
|
92
|
+
segments:
|
93
|
+
- 0
|
94
|
+
hash: -1270750157206818046
|
95
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
96
|
+
none: false
|
97
|
+
requirements:
|
98
|
+
- - ! '>='
|
99
|
+
- !ruby/object:Gem::Version
|
100
|
+
version: '0'
|
101
|
+
segments:
|
102
|
+
- 0
|
103
|
+
hash: -1270750157206818046
|
104
|
+
requirements: []
|
105
|
+
rubyforge_project: filepickerio_rails
|
106
|
+
rubygems_version: 1.8.15
|
107
|
+
signing_key:
|
108
|
+
specification_version: 3
|
109
|
+
summary: Rails view helpers for Filepicker.io widgets
|
110
|
+
test_files:
|
111
|
+
- spec/form_tag_helper_spec.rb
|
112
|
+
- spec/spec_helper.rb
|
113
|
+
- spec/support/form_helpers.rb
|