Skip to content

Commit

Permalink
Almost a complete rewrite
Browse files Browse the repository at this point in the history
* Multiple attachments supported
* JS simplified
* Testing against Rails 3.2 and Rails 4
* Refactored updating into Updater class
* Removed most helpers
* Convered link helper to form helper
* Updated Readme
  • Loading branch information
ScotterC committed Feb 24, 2014
1 parent 44dd31a commit 7ccb1f4
Show file tree
Hide file tree
Showing 30 changed files with 1,220 additions and 565 deletions.
11 changes: 11 additions & 0 deletions Appraisals
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
appraise "rails3_2" do
gem "rails", "~> 3.2.17"
end

appraise "rails4" do
gem "rails", "~> 4.0.3"
end

appraise "paperclip3_5" do
gem "paperclip", "~> 3.5.1"
end
2 changes: 2 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
source "http://rubygems.org"

gemspec

gem 'byebug'
16 changes: 13 additions & 3 deletions Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
PATH
remote: .
specs:
fileclip (0.3.1)
paperclip (>= 3.5.1)
fileclip (0.5.0)
paperclip (>= 3.3)
railties (>= 3.0)
rest-client

Expand Down Expand Up @@ -36,21 +36,29 @@ GEM
activesupport (3.2.14)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
appraisal (0.5.2)
bundler
rake
arel (3.0.2)
builder (3.0.4)
byebug (2.6.0)
columnize (~> 0.3)
debugger-linecache (~> 1.2)
celluloid (0.15.2)
timers (~> 1.1.0)
climate_control (0.0.3)
activesupport (>= 3.0)
cocaine (0.5.3)
climate_control (>= 0.0.3, < 1.0)
columnize (0.3.6)
connection_pool (1.2.0)
coveralls (0.7.0)
multi_json (~> 1.3)
rest-client
simplecov (>= 0.7)
term-ansicolor
thor
debugger-linecache (1.2.0)
diff-lcs (1.2.4)
docile (1.1.0)
erubis (2.7.0)
Expand All @@ -64,7 +72,7 @@ GEM
mime-types (1.23)
mono_logger (1.1.0)
multi_json (1.7.7)
paperclip (3.5.2)
paperclip (4.1.1)
activemodel (>= 3.0.0)
activesupport (>= 3.0.0)
cocaine (~> 0.5.3)
Expand Down Expand Up @@ -154,6 +162,8 @@ PLATFORMS
ruby

DEPENDENCIES
appraisal
byebug
coveralls
fileclip!
rails
Expand Down
136 changes: 79 additions & 57 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,91 +6,113 @@ A FilePicker / PaperClip mashup. Use Filepicker for uploads and paperclip to pr
### What FileClip Does

FileClip saves a filepicker url to your image table which is then
processed by paperclip after the object is saved. This allows filepicker urls with paperclip styles while the image is being processed and the possibility for seamless image handling without having to process anything on your rails servers.
processed by paperclip after the object is saved.

## Minimum Viable Setup
#### Note: Breaking Changes when upgrading to 0.5

### Pros:
* Filepicker URLs with paperclip styles as fallback
* Seamless image handling while images are processed
* Easy background processing with no risk of losing files
* Unobtrusive, Paperclip uploads still work.

## Minimal Viable Setup

### Add to Paperclip table
````
class AddFileClipToImages < ActiveRecord::Migration
def up
add_column :images, :filepicker_url, :string
end
def down
remove_column :images, :filepicker_url
end
end
````
# column prefix needs to be paperclip attachment name
class AddFileClipToImages < ActiveRecord::Migration
def up
add_column :images, :attachment_filepicker_url, :string
add_column :images, :attachment_processing, :boolean
end

def down
remove_column :images, :attachment_filepicker_url
remove_column :images, :attachment_processing
end
end

### In Initializer
````
# config/initializers/fileclip.rb
FileClip.configure do |config|
config.filepicker_key = 'XXXXXXXXXXXXXXXXXXX'
config.services = ["COMPUTER", "DROPBOX"] # Defaults to ["COMPUTER"]
config.max_size = 20 # Megabytes, defaults to 20
config.storage_path = "/assets/" # Defaults to "/fileclip/"
config.mime_types = "images/jpeg" # Defaults to "images/*"
config.file_access = "private" # Defaults to "public"
config.excluded_environments = [] # Defaults to ["test"]
config.default_service = "DROPBOX" # Defaults to "COMPUTER"
end
````
# config/initializers/fileclip.rb
# Defaults shown
FileClip.configure do |config|
config.filepicker_key = 'XXXXXXXXXXXXXXXXXXX'
config.services = ["COMPUTER"]
config.max_size = 20
config.storage_path = "/fileclip/"
config.mime_types = "images/*"
config.file_access = "public"
config.excluded_environments = ["test"]
config.default_service = "COMPUTER"
end

### In Model
````
# models/image.rb
class Image << ActiveRecord::Base
has_attached_file :attachment
# models/image.rb
class Image << ActiveRecord::Base
has_attached_file :attachment

fileclip :attachment
end
````
fileclip :attachment
end

### In View
````
# Loads Filepicker js, and FileClip js
<%= fileclip_js_include_tag %>
# Loads Filepicker js, and FileClip js
<%= fileclip_js_include_tag %>

<%= form_for(Image.new) do |f| %>
# provides a button that can be styled any way you choose

# provides a link that can be styled any way you choose
# launches filepicker dialog and saves link to hidden field
<%= link_to_fileclip "Choose a File", f %>
<%= form_for(Image.new) do |f| %>

<%= f.submit %>
<% end %>
<%= f.fileclip :attachment, "Choose a File" %>

# Specify a callback function that gets called when Filepicker completes
<%= link_to_fileclip "Choose a File", f, :callback => 'window.MyApp.filepickerCallback' %>
<%= f.submit %>
<% end %>

# For more control you can disable automatic Javascript initialization.
# You'll need to initialize the FileClip element yourself.
<%= link_to_fileclip "Choose a File", f, :class => '.my-filepicker', :js => false %>
# Specify a callback function that gets called when Filepicker completes
<%= f.fileclip :attachment, "Choose a File", :callback => 'window.MyApp.filepickerCallback' %>

# In your Javascript framework
(new FileClip).button('.my-filepicker');
### More Control
# For more control you can disable automatic JS initialization.
# You'll need to add event handlers to the button manually.
<%= f.fileclip :attachment, "Choose a File", :class => ".my-fileclip", :activate => false %>

````
# Javascript

# Using FileClip's JS work
fileclip = new Fileclip(false)
fileclip.setupButton(".my-fileclip", myCallbackFunction)

# Or completely custom
$(document).on("click", ".my-fileclip", function() {
// filepicker.pickAndStore ...
// handle setting the value to the input
})

#### Current FilePicker options hardcoded
* container modal
* location is S3

Features:
* Unobtrusive. Normal paperclip uploads still work

#### Upgrading from < 0.5
* Upgrade filepicker_url columns to use paperclip column prefixes
* {attachment_name}_processing boolean column is now required
* Change views to use formhelper `f.fileclip` instead of `link_to_fileclip`
* Option of `js: false` is now `activate: false`
* Custom code hooked into fileclip.js needs to be evaluated carefully

#### Contributing
If you'd like to contribute a feature or bugfix: Thanks!
Run tests with `rake`. Post a pull request and make sure there are tests!

#### Gotchas

This paperclip validation will return errors even if filepicker url is present:
These validations will return errors even if the filepicker url is present:
````
validates :attachment, :attachment_presence => true
validates :attachment, :presence => true
validates_attachment_presence :attachment
````

However, this will work fine. It'll skip the attachment check if a filepicker url is present and validate if it's not.
However, this will work fine. It'll skip the attachment check if a filepicker url is present and validate if it's not. There's a pending test to fix this.
````
validates_attachment :attachment, :size => { :in => 0..1000 }, :presence => true
validates :attachment, :attachment_presence => true
validates_attachment :attachment, :attachment_presence => true
````
14 changes: 12 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,8 +1,18 @@
require "rubygems"
require "bundler/setup"

require 'appraisal'

require "bundler/gem_tasks"
require "rspec/core"
require 'rspec/core/rake_task'

RSpec::Core::RakeTask.new(:spec)

# spec is default task
task :default => :spec
desc 'Default: run tests.'
task :default => ['appraisal:install', :all]

desc "Run tests for all rails versions"
task :all do
exec('rake appraisal spec')
end
17 changes: 9 additions & 8 deletions TODO.md
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
TODO:
### TODO:

Frontend:
* Eliminate need for jQuery
* Handle all filepicker options
# extensions config
# store location (s3)
# drag/drop ?
# Secure filepicker policy/signature

Backend:
* handle multiple attachments on a model, prefix column with attachment name
* create generator for migration
* create generator for intializer
* Normalize filenames

Frontend:
* Eliminate need for jQuery
* make it a form builder function that can accept a different url name

Extra features:
* Work with DelayedJob
* Fallback to filepicker url if paperclip url doesn't exist
* Filepicker converts to match paperclip styles
* FilePicker droppane support

5 changes: 3 additions & 2 deletions fileclip.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,9 @@ Gem::Specification.new do |s|
s.add_development_dependency 'resque'
s.add_development_dependency 'sidekiq'
s.add_development_dependency 'coveralls'
s.add_runtime_dependency(%q<railties>, [">= 3.0"])
s.add_development_dependency 'appraisal'

s.add_runtime_dependency "paperclip", [">= 3.5.1"]
s.add_runtime_dependency(%q<railties>, [">= 3.0"])
s.add_runtime_dependency "paperclip", [">= 3.3"]
end

8 changes: 8 additions & 0 deletions gemfiles/paperclip3_5.gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# This file was generated by Appraisal

source "http://rubygems.org"

gem "byebug"
gem "paperclip", "~> 3.5.1"

gemspec :path=>"../"
Loading

0 comments on commit 7ccb1f4

Please sign in to comment.