Commit 1655bf29 by Overbryd

Merge branch 'master' into kreuzwerker

parents 4b746b39 94d35f98
......@@ -8,3 +8,6 @@ capybara*.html
*.rbc
.bundle
*SPIKE*
.rvmrc
*emfile.lock
.rbx
rvm:
- 1.8.7
- 1.9.2
- ree
- rbx-2.0
before_script: "sudo ntpdate -ub ntp.ubuntu.com pool.ntp.org; true"
script: "bundle exec rake clean test cucumber"
gemfile:
- gemfiles/rails2.gemfile
- gemfiles/rails3.gemfile
- gemfiles/rails3_1.gemfile
appraise "rails2" do
gem "rails", "~>2.3.0"
gem "rails", "~> 2.3.14"
gem "paperclip", :path => "../"
end
appraise "rails3" do
gem "rails", "~>3.0.0"
gem "rails", "~> 3.0.10"
gem "paperclip", :path => "../"
end
appraise "rails3_1" do
gem "rails", "~> 3.1.0"
gem "paperclip", :path => "../"
end
We love pull requests. Here's a quick guide:
1. Fork the repo.
2. Run the tests. We only take pull requests with passing tests, and it's great
to know that you have a clean slate: `bundle && rake`
3. Add a test for your change. Only refactoring and documentation changes
require no new tests. If you are adding functionality or fixing a bug, we need
a test!
4. Make the test pass.
5. Push to your fork and submit a pull request.
At this point you're waiting on us. We like to at least comment on, if not
accept, pull requests within three business days (and, typically, one business
day). We may suggest some changes or improvements or alternatives.
Some things that will increase the chance that your pull request is accepted,
taken straight from the Ruby on Rails guide:
* Use Rails idioms and helpers
* Include tests that fail without your code, and pass with it
* Update the documentation, the surrounding one, examples elsewhere, guides,
whatever is affected by your contribution
Syntax:
* Two spaces, no tabs.
* No trailing whitespace. Blank lines should not have any space.
* Prefer &&/|| over and/or.
* MyClass.my_method(my_arg) not my_method( my_arg ) or my_method my_arg.
* a = b and not a=b.
* Follow the conventions you see used in the source already.
And in case we didn't emphasize it enough: we love tests!
source "http://rubygems.org"
gem "shoulda"
gem "activerecord", :require => "active_record"
gem "appraisal"
gem "aruba"
gem "aws-s3", :require => "aws/s3"
gem "bundler"
gem "cocaine", "~>0.2"
gem "fog"
gem "jruby-openssl", :platform => :jruby
gem "mime-types"
gem "mocha"
gem "rake"
gem "ruby-debug"
gem "aws-s3", :require => "aws/s3"
gem "sqlite3-ruby", "~>1.3.0"
gem "appraisal"
gem "rdoc", :require => false
gem "capybara"
gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "sqlite3", "~>1.3.4"
gem "fakeweb", :require => false
gem 'pry'
GEM
remote: http://rubygems.org/
specs:
appraisal (0.1)
bundler
rake
aws-s3 (0.6.2)
builder
mime-types
xml-simple
builder (3.0.0)
columnize (0.3.2)
linecache (0.43)
mime-types (1.16)
mocha (0.9.9)
rake
rake (0.8.7)
ruby-debug (0.10.4)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
shoulda (2.11.3)
sqlite3-ruby (1.3.2)
xml-simple (1.0.12)
PLATFORMS
ruby
DEPENDENCIES
appraisal
aws-s3
mocha
rake
ruby-debug
shoulda
sqlite3-ruby (~> 1.3.0)
Paperclip
=========
# Paperclip [![Build Status](https://secure.travis-ci.org/thoughtbot/paperclip.png?branch=master)](http://travis-ci.org/thoughtbot/paperclip)
Paperclip is intended as an easy file attachment library for ActiveRecord. The
intent behind it was to keep setup as easy as possible and to treat files as
......@@ -33,16 +32,40 @@ In development mode, you might add this line to `config/environments/development
Paperclip.options[:command_path] = "/usr/local/bin/"
If you're on Mac OSX, you'll want to run the following with Homebrew:
brew install imagemagick
If you are dealing with pdf uploads or running the test suite, also run:
brew install gs
Installation
------------
Paperclip is distributed as a gem, which is how it should be used in your app. It's
technically still installable as a plugin, but that's discouraged, as Rails plays
well with gems.
Include the gem in your Gemfile:
gem "paperclip", "~> 2.3"
gem "paperclip", "~> 2.4"
Or, if you don't use Bundler (though you probably should, even in Rails 2), with config.gem
Or as a plugin:
# In config/environment.rb
...
Rails::Initializer.run do |config|
...
config.gem "paperclip", :version => "~> 2.4"
...
end
For Non-Rails usage:
ruby script/plugin install git://github.com/thoughtbot/paperclip.git
class ModuleName < ActiveRecord::Base
include Paperclip::Glue
...
end
Quick Start
-----------
......@@ -73,7 +96,7 @@ In your migrations:
In your edit and new views:
<% form_for :user, @user, :url => user_path, :html => { :multipart => true } do |form| %>
<%= form_for :user, @user, :url => user_path, :html => { :multipart => true } do |form| %>
<%= form.file_field :avatar %>
<% end %>
......@@ -89,13 +112,18 @@ In your show view:
<%= image_tag @user.avatar.url(:medium) %>
<%= image_tag @user.avatar.url(:thumb) %>
To detach a file, simply set the attribute to `nil`:
@user.avatar = nil
@user.save
Usage
-----
The basics of paperclip are quite simple: Declare that your model has an
attachment with the has_attached_file method, and give it a name. Paperclip
will wrap up up to four attributes (all prefixed with that attachment's name,
so you can have multiple attachments per model if you wish) and give the a
so you can have multiple attachments per model if you wish) and give them a
friendly front end. The attributes are `<attachment>_file_name`,
`<attachment>_file_size`, `<attachment>_content_type`, and `<attachment>_updated_at`.
Only `<attachment>_file_name` is required for paperclip to operate. More
......@@ -178,6 +206,12 @@ or more or the processors, and they are expected to ignore it.
_NOTE: Because processors operate by turning the original attachment into the
styles, no processors will be run if there are no styles defined._
If you're interested in caching your thumbnail's width, height and size in the
database, take a look at the [paperclip-meta](https://github.com/y8/paperclip-meta) gem.
Also, if you're interested in generating the thumbnail on-the-fly, you might want
to look into the [attachment_on_the_fly](https://github.com/drpentode/Attachment-on-the-Fly) gem.
Events
------
......@@ -196,11 +230,157 @@ _NOTE: Post processing will not even *start* if the attachment is not valid
according to the validations. Your callbacks and processors will *only* be
called with valid attachments._
URI Obfuscation
---------------
Paperclip has an interpolation called `:hash` for obfuscating filenames of
publicly-available files.
Example Usage:
has_attached_file :avatar, {
:url => "/system/:hash.:extension",
:hash_secret => "longSecretString"
}
The `:hash` interpolation will be replaced with a unique hash made up of whatever
is specified in `:hash_data`. The default value for `:hash_data` is `":class/:attachment/:id/:style/:updated_at"`.
`:hash_secret` is required, an exception will be raised if `:hash` is used without `:hash_secret` present.
For more on this feature read the author's own explanation. [https://github.com/thoughtbot/paperclip/pull/416](https://github.com/thoughtbot/paperclip/pull/416)
MD5 Checksum / Fingerprint
-------
A MD5 checksum of the original file assigned will be placed in the model if it
has an attribute named fingerprint. Following the user model migration example
above, the migration would look like the following.
class AddAvatarFingerprintColumnToUser < ActiveRecord::Migration
def self.up
add_column :users, :avatar_fingerprint, :string
end
def self.down
remove_column :users, :avatar_fingerprint
end
end
Custom Attachment Processors
-------
Custom attachment processors can be implemented and their only requirement is
to inherit from `Paperclip::Processor` (see `lib/paperclip/processor.rb`).
For example, when `:styles` are specified for an image attachment, the
thumbnail processor (see `lib/paperclip/thumbnail.rb`) is loaded without having
to specify it as a `:processor` parameter to `has_attached_file`. When any
other processor is defined it must be called out in the `:processors`
parameter if it is to be applied to the attachment. The thumbnail processor
uses the imagemagick `convert` command to do the work of resizing image
thumbnails. It would be easy to create a custom processor that watermarks
an image using imagemagick's `composite` command. Following the
implementation pattern of the thumbnail processor would be a way to implement a
watermark processor. All kinds of attachment processors can be created;
a few utility examples would be compression and encryption processors.
Dynamic Configuration
---------------------
Callable objects (lambdas, Procs) can be used in a number of places for dynamic
configuration throughout Paperclip. This strategy exists in a number of
components of the library but is most significant in the possibilities for
allowing custom styles and processors to be applied for specific model
instances, rather than applying defined styles and processors across all
instances.
Dynamic Styles:
Imagine a user model that had different styles based on the role of the user.
Perhaps some users are bosses (e.g. a User model instance responds to #boss?)
and merit a bigger avatar thumbnail than regular users. The configuration to
determine what style parameters are to be used based on the user role might
look as follows where a boss will receive a `300x300` thumbnail otherwise a
`100x100` thumbnail will be created.
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => lambda { |attachment| { :thumb => (attachment.instance.boss? ? "300x300>" : "100x100>") }
end
Dynamic Processors:
Another contrived example is a user model that is aware of which file processors
should be applied to it (beyond the implied `thumbnail` processor invoked when
`:styles` are defined). Perhaps we have a watermark processor available and it is
only used on the avatars of certain models. The configuration for this might be
where the instance is queried for which processors should be applied to it.
Presumably some users might return `[:thumbnail, :watermark]` for its
processors, where a defined `watermark` processor is invoked after the
`thumbnail` processor already defined by Paperclip.
class User < ActiveRecord::Base
has_attached_file :avatar, :processors => lambda { |instance| instance.processors }
attr_accessor :watermark
end
Deploy
------
Paperclip is aware of new attachment styles you have added in previous deploy. The only thing you should do after each deployment is to call
`rake paperclip:refresh:missing_styles`. It will store current attachment styles in `RAILS_ROOT/public/system/paperclip_attachments.yml`
by default. You can change it by:
Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml'
Here is an example for Capistrano:
namespace :deploy do
desc "build missing paperclip styles"
task :build_missing_paperclip_styles, :roles => :app do
run "cd #{release_path}; RAILS_ENV=production bundle exec rake paperclip:refresh:missing_styles"
end
end
after("deploy:update_code", "deploy:build_missing_paperclip_styles")
Now you don't have to remember to refresh thumbnails in production everytime you add new style.
Unfortunately it does not work with dynamic styles - it just ignores them.
If you already have working app and don't want `rake paperclip:refresh:missing_styles` to refresh old pictures, you need to tell
Paperclip about existing styles. Simply create paperclip_attachments.yml file by hand. For example:
class User < ActiveRecord::Base
has_attached_file :avatar, :styles => {:thumb => 'x100', :croppable => '600x600>', :big => '1000x1000>'}
end
class Book < ActiveRecord::Base
has_attached_file :cover, :styles => {:small => 'x100', :large => '1000x1000>'}
has_attached_file :sample, :styles => {:thumb => 'x100'}
end
Then in `RAILS_ROOT/public/system/paperclip_attachments.yml`:
---
:User:
:avatar:
- :thumb
- :croppable
- :big
:Book:
:cover:
- :small
- :large
:sample:
- :thumb
Testing
-------
Paperclip provides rspec-compatible matchers for testing attachments. See the
documentation on Paperclip::Shoulda::Matchers for more information.
documentation on [Paperclip::Shoulda::Matchers](http://rubydoc.info/gems/paperclip/Paperclip/Shoulda/Matchers)
for more information.
Contributing
------------
......@@ -215,6 +395,8 @@ guidelines:
It's a rare time when explicit tests aren't needed. If you have questions
about writing tests for paperclip, please ask the mailing list.
Please see CONTRIBUTING.md for details.
Credits
-------
......
require 'rubygems'
require 'appraisal'
require 'bundler/setup'
require 'appraisal'
require 'rake'
require 'rake/testtask'
require 'rake/rdoctask'
require 'rdoc/task'
require 'cucumber/rake/task'
$LOAD_PATH << File.join(File.dirname(__FILE__), 'lib')
require 'paperclip'
desc 'Default: run unit tests.'
task :default => [:clean, :all]
task :default => [:clean, 'appraisal:install', :all]
desc 'Test the paperclip plugin under all supported Rails versions.'
task :all do |t|
exec('rake appraisal test')
exec('rake appraisal test cucumber')
end
desc 'Test the paperclip plugin.'
......@@ -24,6 +25,11 @@ Rake::TestTask.new(:test) do |t|
t.verbose = true
end
desc 'Run integration test'
Cucumber::Rake::Task.new do |t|
t.cucumber_opts = %w{--format progress}
end
desc 'Start an IRB session with all necessary files required.'
task :shell do |t|
chdir File.dirname(__FILE__)
......@@ -31,7 +37,7 @@ task :shell do |t|
end
desc 'Generate documentation for the paperclip plugin.'
Rake::RDocTask.new(:rdoc) do |rdoc|
RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'doc'
rdoc.title = 'Paperclip'
rdoc.options << '--line-numbers' << '--inline-source'
......
Feature: Running paperclip in a Rails app
Scenario: Basic utilization
Given I have a rails application
And I save the following as "app/models/user.rb"
"""
class User < ActiveRecord::Base
has_attached_file :avatar
end
"""
When I visit /users/new
And I fill in "user_name" with "something"
And I attach the file "test/fixtures/5k.png" to "user_avatar"
And I press "Submit"
Then I should see "Name: something"
And I should see an image with a path of "/system/avatars/1/original/5k.png"
And the file at "/system/avatars/1/original/5k.png" is the same as "test/fixtures/5k.png"
Feature: Rails integration
Background:
Given I generate a new rails application
And I run a rails generator to generate a "User" scaffold with "name:string"
And I run a paperclip generator to add a paperclip "attachment" to the "User" model
And I run a migration
And I update my new user view to include the file upload field
And I update my user view to include the attachment
Scenario: Filesystem integration test
Given I add this snippet to the User model:
"""
has_attached_file :attachment
"""
And I start the rails application
When I go to the new user page
And I fill in "Name" with "something"
And I attach the file "test/fixtures/5k.png" to "Attachment"
And I press "Submit"
Then I should see "Name: something"
And I should see an image with a path of "/system/attachments/1/original/5k.png"
And the file at "/system/attachments/1/original/5k.png" should be the same as "test/fixtures/5k.png"
Scenario: S3 Integration test
Given I add this snippet to the User model:
"""
has_attached_file :attachment,
:storage => :s3,
:path => "/:attachment/:id/:style/:filename",
:s3_credentials => Rails.root.join("config/s3.yml")
"""
And I write to "config/s3.yml" with:
"""
bucket: paperclip
access_key_id: access_key
secret_access_key: secret_key
"""
And I start the rails application
When I go to the new user page
And I fill in "Name" with "something"
And I attach the file "test/fixtures/5k.png" to "Attachment" on S3
And I press "Submit"
Then I should see "Name: something"
And I should see an image with a path of "http://s3.amazonaws.com/paperclip/attachments/1/original/5k.png"
And the file at "http://s3.amazonaws.com/paperclip/attachments/1/original/5k.png" should be uploaded to S3
Feature: Rake tasks
Background:
Given I generate a new rails application
And I run a rails generator to generate a "User" scaffold with "name:string"
And I run a paperclip generator to add a paperclip "attachment" to the "User" model
And I run a migration
And I prepare my old Rails application for rake task
And I add this snippet to the User model:
"""
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
"""
Scenario: Paperclip refresh thumbnails task
When I modify my attachment definition to:
"""
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
:styles => { :medium => "200x200#" }
"""
And I upload the fixture "5k.png"
Then the attachment "medium/5k.png" should have a dimension of 200x200
When I modify my attachment definition to:
"""
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
:styles => { :medium => "100x100#" }
"""
When I successfully run `bundle exec rake paperclip:refresh:thumbnails CLASS=User --trace`
Then the attachment "original/5k.png" should exist
And the attachment "medium/5k.png" should have a dimension of 100x100
Scenario: Paperclip refresh metadata task
When I upload the fixture "5k.png"
And I swap the attachment "original/5k.png" with the fixture "12k.png"
And I successfully run `bundle exec rake paperclip:refresh:metadata CLASS=User --trace`
Then the attachment should have the same content type as the fixture "12k.png"
And the attachment should have the same file size as the fixture "12k.png"
Scenario: Paperclip refresh missing styles task
When I upload the fixture "5k.png"
Then the attachment file "original/5k.png" should exist
And the attachment file "medium/5k.png" should not exist
When I modify my attachment definition to:
"""
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
:styles => { :medium => "200x200#" }
"""
When I successfully run `bundle exec rake paperclip:refresh:missing_styles --trace`
Then the attachment file "original/5k.png" should exist
And the attachment file "medium/5k.png" should exist
Scenario: Paperclip clean task
When I upload the fixture "5k.png"
And I upload the fixture "12k.png"
Then the attachment file "original/5k.png" should exist
And the attachment file "original/12k.png" should exist
When I modify my attachment definition to:
"""
has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
validates_attachment_size :attachment, :less_than => 10.kilobytes
"""
And I successfully run `bundle exec rake paperclip:clean CLASS=User --trace`
Then the attachment file "original/5k.png" should exist
But the attachment file "original/12k.png" should not exist
Feature: Running paperclip in a Rails app using basic S3 support
Scenario: Basic utilization
Given I have a rails application
And I save the following as "app/models/user.rb"
"""
class User < ActiveRecord::Base
has_attached_file :avatar,
:storage => :s3,
:path => "/:attachment/:id/:style/:filename",
:s3_credentials => Rails.root.join("config/s3.yml")
end
"""
And I validate my S3 credentials
And I save the following as "config/s3.yml"
"""
bucket: <%= ENV['PAPERCLIP_TEST_BUCKET'] || 'paperclip' %>
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
"""
When I visit /users/new
And I fill in "user_name" with "something"
And I attach the file "test/fixtures/5k.png" to "user_avatar"
And I press "Submit"
Then I should see "Name: something"
And I should see an image with a path of "http://s3.amazonaws.com/paperclip/avatars/1/original/5k.png"
And the file at "http://s3.amazonaws.com/paperclip/avatars/1/original/5k.png" is the same as "test/fixtures/5k.png"
module AttachmentHelpers
def fixture_path(filename)
File.expand_path("#{PROJECT_ROOT}/test/fixtures/#{filename}")
end
def attachment_path(filename)
File.expand_path("public/system/attachments/#{filename}")
end
end
World(AttachmentHelpers)
When /^I modify my attachment definition to:$/ do |definition|
in_current_dir do
File.open("app/models/user.rb", "w") do |file|
file.write <<-FILE
class User < ActiveRecord::Base
#{definition}
end
FILE
end
end
end
When /^I upload the fixture "([^"]*)"$/ do |filename|
run_simple %(bundle exec #{runner_command} "User.create!(:attachment => File.open('#{fixture_path(filename)}'))")
end
Then /^the attachment "([^"]*)" should have a dimension of (\d+x\d+)$/ do |filename, dimension|
in_current_dir do
geometry = `identify -format "%wx%h" "#{attachment_path(filename)}"`.strip
geometry.should == dimension
end
end
Then /^the attachment "([^"]*)" should exist$/ do |filename|
in_current_dir do
File.exists?(attachment_path(filename)).should be
end
end
When /^I swap the attachment "([^"]*)" with the fixture "([^"]*)"$/ do |attachment_filename, fixture_filename|
in_current_dir do
require 'fileutils'
FileUtils.rm_f attachment_path(attachment_filename)
FileUtils.cp fixture_path(fixture_filename), attachment_path(attachment_filename)
end
end
Then /^the attachment should have the same content type as the fixture "([^"]*)"$/ do |filename|
in_current_dir do
require 'mime/types'
attachment_content_type = `bundle exec #{runner_command} "puts User.last.attachment_content_type"`.strip
attachment_content_type.should == MIME::Types.type_for(filename).first.content_type
end
end
Then /^the attachment should have the same file size as the fixture "([^"]*)"$/ do |filename|
in_current_dir do
attachment_file_size = `bundle exec #{runner_command} "puts User.last.attachment_file_size"`.strip
attachment_file_size.should == File.size(fixture_path(filename)).to_s
end
end
Then /^the attachment file "([^"]*)" should (not )?exist$/ do |filename, not_exist|
in_current_dir do
check_file_presence([attachment_path(filename)], !not_exist)
end
end
......@@ -10,5 +10,6 @@ Then %r{^the file at "([^"]*)" is the same as "([^"]*)"$} do |web_file, path|
visit(web_file)
page.body
end
actual.force_encoding("UTF-8") if actual.respond_to?(:force_encoding)
actual.should == expected
end
Given "I have a rails application" do
Given /^I generate a new rails application$/ do
steps %{
Given I generate a rails application
And this plugin is available
And I have a "users" resource with "name:string"
When I run `bundle exec #{new_application_command} #{APP_NAME}`
And I cd to "#{APP_NAME}"
And I turn off class caching
Given I save the following as "app/models/user.rb"
And I write to "Gemfile" with:
"""
class User < ActiveRecord::Base
end
source "http://rubygems.org"
gem "rails", "#{framework_version}"
gem "sqlite3"
gem "capybara"
gem "gherkin"
gem "aws-s3"
"""
And I save the following as "config/s3.yml"
And I configure the application to use "paperclip" from this project
And I reset Bundler environment variable
And I successfully run `bundle install --local`
}
end
Given /^I run a rails generator to generate a "([^"]*)" scaffold with "([^"]*)"$/ do |model_name, attributes|
Given %[I successfully run `bundle exec #{generator_command} scaffold #{model_name} #{attributes}`]
end
Given /^I run a paperclip generator to add a paperclip "([^"]*)" to the "([^"]*)" model$/ do |attachment_name, model_name|
Given %[I successfully run `bundle exec #{generator_command} paperclip #{model_name} #{attachment_name}`]
end
Given /^I run a migration$/ do
Given %[I successfully run `bundle exec rake db:migrate`]
end
Given /^I update my new user view to include the file upload field$/ do
if framework_version?("3")
steps %{
Given I overwrite "app/views/users/new.html.erb" with:
"""
access_key_id: <%= ENV['AWS_ACCESS_KEY_ID'] %>
secret_access_key: <%= ENV['AWS_SECRET_ACCESS_KEY'] %>
bucket: paperclip
<%= form_for @user, :html => { :multipart => true } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<%= submit_tag "Submit" %>
<% end %>
"""
And I save the following as "app/views/users/new.html.erb"
}
else
steps %{
Given I overwrite "app/views/users/new.html.erb" with:
"""
<% form_for @user, :html => { :multipart => true } do |f| %>
<%= f.label :name %>
<%= f.text_field :name %>
<%= f.file_field :avatar %>
<%= f.label :attachment %>
<%= f.file_field :attachment %>
<%= submit_tag "Submit" %>
<% end %>
"""
And I save the following as "app/views/users/show.html.erb"
}
end
end
Given /^I update my user view to include the attachment$/ do
steps %{
Given I overwrite "app/views/users/show.html.erb" with:
"""
<p>Name: <%= @user.name %></p>
<p>Avatar: <%= image_tag @user.avatar.url %></p>
<p>Attachment: <%= image_tag @user.attachment.url %></p>
"""
And I run "script/generate paperclip user avatar"
And the rails application is prepped and running
}
end
Given %r{I generate a rails application} do
FileUtils.rm_rf TEMP_ROOT
FileUtils.mkdir_p TEMP_ROOT
Dir.chdir(TEMP_ROOT) do
`rails _2.3.8_ #{APP_NAME}`
Given /^I add this snippet to the User model:$/ do |snippet|
file_name = "app/models/user.rb"
in_current_dir do
content = File.read(file_name)
File.open(file_name, 'w') { |f| f << content.sub(/end\Z/, "#{snippet}\nend") }
end
end
When %r{I save the following as "([^"]*)"} do |path, string|
FileUtils.mkdir_p(File.join(CUC_RAILS_ROOT, File.dirname(path)))
File.open(File.join(CUC_RAILS_ROOT, path), 'w') { |file| file.write(string) }
Given /^I start the rails application$/ do
in_current_dir do
require "./config/environment"
require "capybara/rails"
end
end
Given /^I reload my application$/ do
Rails::Application.reload!
end
When %r{I turn off class caching} do
Dir.chdir(CUC_RAILS_ROOT) do
in_current_dir do
file = "config/environments/test.rb"
config = IO.read(file)
config.gsub!(%r{^\s*config.cache_classes.*$},
......@@ -56,35 +99,84 @@ When %r{I turn off class caching} do
end
end
When %r{the rails application is prepped and running$} do
When "I reset the database"
When "the rails application is running"
Given /^I update my application to use Bundler$/ do
if framework_version?("2")
boot_config_template = File.read('features/support/fixtures/boot_config.txt')
preinitializer_template = File.read('features/support/fixtures/preinitializer.txt')
gemfile_template = File.read('features/support/fixtures/gemfile.txt')
in_current_dir do
content = File.read("config/boot.rb").sub(/Rails\.boot!/, boot_config_template)
File.open("config/boot.rb", "w") { |file| file.write(content) }
File.open("config/preinitializer.rb", "w") { |file| file.write(preinitializer_template) }
File.open("Gemfile", "w") { |file| file.write(gemfile_template.sub(/RAILS_VERSION/, framework_version)) }
end
end
end
When %r{I reset the database} do
When %{I run "rake db:drop db:create db:migrate"}
Given /^I prepare my old Rails application for rake task$/ do
if framework_version?("2.3")
require 'fileutils'
source = File.expand_path('lib/tasks/paperclip.rake')
destination = in_current_dir { File.expand_path("lib/tasks") }
FileUtils.cp source, destination
append_to "Rakefile", "require 'paperclip'"
end
end
When %r{the rails application is running} do
Dir.chdir(CUC_RAILS_ROOT) do
require "config/environment"
require "capybara/rails"
Then /^the file at "([^"]*)" should be the same as "([^"]*)"$/ do |web_file, path|
expected = IO.read(path)
actual = if web_file.match %r{^https?://}
Net::HTTP.get(URI.parse(web_file))
else
visit(web_file)
page.source
end
actual.force_encoding("UTF-8") if actual.respond_to?(:force_encoding)
actual.should == expected
end
When /^I configure the application to use "([^\"]+)" from this project$/ do |name|
append_to_gemfile "gem '#{name}', :path => '#{PROJECT_ROOT}'"
steps %{And I run `bundle install --local`}
end
When %r{this plugin is available} do
$LOAD_PATH << "#{PROJECT_ROOT}/lib"
require 'paperclip'
When %{I save the following as "vendor/plugins/paperclip/rails/init.rb"},
IO.read("#{PROJECT_ROOT}/rails/init.rb")
When /^I configure the application to use "([^\"]+)"$/ do |gem_name|
append_to_gemfile "gem '#{gem_name}'"
end
When %r{I run "([^"]*)"} do |command|
Dir.chdir(CUC_RAILS_ROOT) do
`#{command}`
When /^I append gems from Appraisal Gemfile$/ do
File.read(ENV['BUNDLE_GEMFILE']).split(/\n/).each do |line|
if line =~ /^gem "(?!rails|appraisal)/
append_to_gemfile line.strip
end
end
end
When %r{I have a "([^"]*)" resource with "([^"]*)"} do |resource, fields|
When %{I run "script/generate scaffold #{resource} #{fields}"}
When /^I comment out the gem "([^"]*)" from the Gemfile$/ do |gemname|
comment_out_gem_in_gemfile gemname
end
module FileHelpers
def append_to(path, contents)
in_current_dir do
File.open(path, "a") do |file|
file.puts
file.puts contents
end
end
end
def append_to_gemfile(contents)
append_to('Gemfile', contents)
end
def comment_out_gem_in_gemfile(gemname)
in_current_dir do
gemfile = File.read("Gemfile")
gemfile.sub!(/^(\s*)(gem\s*['"]#{gemname})/, "\\1# \\2")
File.open("Gemfile", 'w'){ |file| file.write(gemfile) }
end
end
end
World(FileHelpers)
Given /I validate my S3 credentials/ do
key = ENV['AWS_ACCESS_KEY_ID']
secret = ENV['AWS_SECRET_ACCESS_KEY']
key.should_not be_nil
secret.should_not be_nil
When /^I attach the file "([^"]*)" to "([^"]*)" on S3$/ do |file_path, field|
definition = User.attachment_definitions[field.downcase.to_sym]
path = "http://s3.amazonaws.com/paperclip#{definition[:path]}"
path.gsub!(':filename', File.basename(file_path))
path.gsub!(/:([^\/\.]+)/) do |match|
"([^\/\.]+)"
end
FakeWeb.register_uri(:put, Regexp.new(path), :body => "OK")
When "I attach the file \"#{file_path}\" to \"#{field}\""
end
assert_credentials(key, secret)
Then /^the file at "([^"]*)" should be uploaded to S3$/ do |url|
FakeWeb.registered_uri?(:put, url)
end
# IMPORTANT: This file is generated by cucumber-rails - edit at your own peril.
# It is recommended to regenerate this file in the future when you upgrade to a
# newer version of cucumber-rails. Consider adding your own code to a new file
# instead of editing this one. Cucumber will automatically load all features/**/*.rb
# files.
# TL;DR: YOU SHOULD DELETE THIS FILE
#
# This file was generated by Cucumber-Rails and is only here to get you a head start
# These step definitions are thin wrappers around the Capybara/Webrat API that lets you
# visit pages, interact with widgets and make assertions about page content.
#
# If you use these step definitions as basis for your features you will quickly end up
# with features that are:
#
# * Hard to maintain
# * Verbose to read
#
# A much better approach is to write your own higher level step definitions, following
# the advice in the following blog posts:
#
# * http://benmabey.com/2008/05/19/imperative-vs-declarative-scenarios-in-user-stories.html
# * http://dannorth.net/2011/01/31/whose-domain-is-it-anyway/
# * http://elabs.se/blog/15-you-re-cuking-it-wrong
#
require 'uri'
require 'cgi'
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "paths"))
require File.expand_path(File.join(File.dirname(__FILE__), "..", "support", "selectors"))
module WithinHelpers
def with_scope(locator)
locator ? within(locator) { yield } : yield
locator ? within(*selector_for(locator)) { yield } : yield
end
end
World(WithinHelpers)
# Single-line step scoper
When /^(.*) within (.*[^:])$/ do |step, parent|
with_scope(parent) { When step }
end
# Multi-line step scoper
When /^(.*) within (.*[^:]):$/ do |step, parent, table_or_string|
with_scope(parent) { When "#{step}:", table_or_string }
end
Given /^(?:|I )am on (.+)$/ do |page_name|
visit path_to(page_name)
end
......@@ -24,32 +49,20 @@ When /^(?:|I )go to (.+)$/ do |page_name|
visit path_to(page_name)
end
When /^(?:|I )visit (\/.+)$/ do |page_path|
visit page_path
end
When /^(?:|I )press "([^"]*)"(?: within "([^"]*)")?$/ do |button, selector|
with_scope(selector) do
When /^(?:|I )press "([^"]*)"$/ do |button|
click_button(button)
end
end
When /^(?:|I )follow "([^"]*)"(?: within "([^"]*)")?$/ do |link, selector|
with_scope(selector) do
When /^(?:|I )follow "([^"]*)"$/ do |link|
click_link(link)
end
end
When /^(?:|I )fill in "([^"]*)" with "([^"]*)"(?: within "([^"]*)")?$/ do |field, value, selector|
with_scope(selector) do
When /^(?:|I )fill in "([^"]*)" with "([^"]*)"$/ do |field, value|
fill_in(field, :with => value)
end
end
When /^(?:|I )fill in "([^"]*)" for "([^"]*)"(?: within "([^"]*)")?$/ do |value, field, selector|
with_scope(selector) do
When /^(?:|I )fill in "([^"]*)" for "([^"]*)"$/ do |value, field|
fill_in(field, :with => value)
end
end
# Use this to fill in an entire form with data from a table. Example:
......@@ -63,119 +76,92 @@ end
# TODO: Add support for checkbox, select og option
# based on naming conventions.
#
When /^(?:|I )fill in the following(?: within "([^"]*)")?:$/ do |selector, fields|
with_scope(selector) do
When /^(?:|I )fill in the following:$/ do |fields|
fields.rows_hash.each do |name, value|
When %{I fill in "#{name}" with "#{value}"}
end
end
end
When /^(?:|I )select "([^"]*)" from "([^"]*)"(?: within "([^"]*)")?$/ do |value, field, selector|
with_scope(selector) do
When /^(?:|I )select "([^"]*)" from "([^"]*)"$/ do |value, field|
select(value, :from => field)
end
end
When /^(?:|I )check "([^"]*)"(?: within "([^"]*)")?$/ do |field, selector|
with_scope(selector) do
When /^(?:|I )check "([^"]*)"$/ do |field|
check(field)
end
end
When /^(?:|I )uncheck "([^"]*)"(?: within "([^"]*)")?$/ do |field, selector|
with_scope(selector) do
When /^(?:|I )uncheck "([^"]*)"$/ do |field|
uncheck(field)
end
end
When /^(?:|I )choose "([^"]*)"(?: within "([^"]*)")?$/ do |field, selector|
with_scope(selector) do
When /^(?:|I )choose "([^"]*)"$/ do |field|
choose(field)
end
end
When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"(?: within "([^"]*)")?$/ do |path, field, selector|
with_scope(selector) do
attach_file(field, path)
end
end
Then /^(?:|I )should see JSON:$/ do |expected_json|
require 'json'
expected = JSON.pretty_generate(JSON.parse(expected_json))
actual = JSON.pretty_generate(JSON.parse(response.body))
expected.should == actual
When /^(?:|I )attach the file "([^"]*)" to "([^"]*)"$/ do |path, field|
attach_file(field, File.expand_path(path))
end
Then /^(?:|I )should see "([^"]*)"(?: within "([^"]*)")?$/ do |text, selector|
with_scope(selector) do
Then /^(?:|I )should see "([^"]*)"$/ do |text|
if page.respond_to? :should
page.should have_content(text)
else
assert page.has_content?(text)
end
end
end
Then /^(?:|I )should see \/([^\/]*)\/(?: within "([^"]*)")?$/ do |regexp, selector|
Then /^(?:|I )should see \/([^\/]*)\/$/ do |regexp|
regexp = Regexp.new(regexp)
with_scope(selector) do
if page.respond_to? :should
page.should have_xpath('//*', :text => regexp)
else
assert page.has_xpath?('//*', :text => regexp)
end
end
end
Then /^(?:|I )should not see "([^"]*)"(?: within "([^"]*)")?$/ do |text, selector|
with_scope(selector) do
Then /^(?:|I )should not see "([^"]*)"$/ do |text|
if page.respond_to? :should
page.should have_no_content(text)
else
assert page.has_no_content?(text)
end
end
end
Then /^(?:|I )should not see \/([^\/]*)\/(?: within "([^"]*)")?$/ do |regexp, selector|
Then /^(?:|I )should not see \/([^\/]*)\/$/ do |regexp|
regexp = Regexp.new(regexp)
with_scope(selector) do
if page.respond_to? :should
page.should have_no_xpath('//*', :text => regexp)
else
assert page.has_no_xpath?('//*', :text => regexp)
end
end
end
Then /^the "([^"]*)" field(?: within "([^"]*)")? should contain "([^"]*)"$/ do |field, selector, value|
with_scope(selector) do
Then /^the "([^"]*)" field(?: within (.*))? should contain "([^"]*)"$/ do |field, parent, value|
with_scope(parent) do
field = find_field(field)
field_value = (field.tag_name == 'textarea') ? field.text : field.value
if field_value.respond_to? :should
field_value.should =~ /#{value}/
if field.value.respond_to? :should
field.value.should =~ /#{value}/
else
assert_match(/#{value}/, field_value)
assert_match(/#{value}/, field.value)
end
end
end
Then /^the "([^"]*)" field(?: within "([^"]*)")? should not contain "([^"]*)"$/ do |field, selector, value|
with_scope(selector) do
Then /^the "([^"]*)" field(?: within (.*))? should not contain "([^"]*)"$/ do |field, parent, value|
with_scope(parent) do
field = find_field(field)
field_value = (field.tag_name == 'textarea') ? field.text : field.value
if field_value.respond_to? :should_not
field_value.should_not =~ /#{value}/
if field.value.respond_to? :should_not
field.value.should_not =~ /#{value}/
else
assert_no_match(/#{value}/, field_value)
assert_no_match(/#{value}/, field.value)
end
end
end
Then /^the "([^"]*)" checkbox(?: within "([^"]*)")? should be checked$/ do |label, selector|
with_scope(selector) do
Then /^the "([^"]*)" checkbox(?: within (.*))? should be checked$/ do |label, parent|
with_scope(parent) do
field_checked = find_field(label)['checked']
if field_checked.respond_to? :should
field_checked.should be_true
......@@ -185,8 +171,8 @@ Then /^the "([^"]*)" checkbox(?: within "([^"]*)")? should be checked$/ do |labe
end
end
Then /^the "([^"]*)" checkbox(?: within "([^"]*)")? should not be checked$/ do |label, selector|
with_scope(selector) do
Then /^the "([^"]*)" checkbox(?: within (.*))? should not be checked$/ do |label, parent|
with_scope(parent) do
field_checked = find_field(label)['checked']
if field_checked.respond_to? :should
field_checked.should be_false
......@@ -218,10 +204,6 @@ Then /^(?:|I )should have the following query string:$/ do |expected_pairs|
end
end
Then /^I save and open the page$/ do
save_and_open_page
end
Then /^show me the page$/ do
save_and_open_page
end
require 'aruba/cucumber'
require 'capybara/cucumber'
require 'test/unit/assertions'
World(Test::Unit::Assertions)
Before do
@aruba_timeout_seconds = 120
end
require 'fake_web'
FakeWeb.allow_net_connect = false
class Rails::Boot
def run
load_initializer
Rails::Initializer.class_eval do
def load_gems
@bundler_loaded ||= Bundler.require :default, Rails.env
end
end
Rails::Initializer.run(:set_load_path)
end
end
Rails.boot!
source "http://rubygems.org"
gem "rails", "RAILS_VERSION"
gem "rdoc"
gem "sqlite3"
begin
require "rubygems"
require "bundler"
rescue LoadError
raise "Could not load the bundler gem. Install it with `gem install bundler`."
end
if Gem::Version.new(Bundler::VERSION) <= Gem::Version.new("0.9.24")
raise RuntimeError, "Your bundler version is too old for Rails 2.3." +
"Run `gem install bundler` to upgrade."
end
begin
# Set up load paths for all bundled gems
ENV["BUNDLE_GEMFILE"] = File.expand_path("../../Gemfile", __FILE__)
Bundler.setup
rescue Bundler::GemNotFound
raise RuntimeError, "Bundler couldn't find some gems." +
"Did you run `bundle install`?"
end
......@@ -8,17 +8,10 @@ module NavigationHelpers
def path_to(page_name)
case page_name
when /the new user page/
'/users/new'
when /the home\s?page/
'/'
# Add more mappings here.
# Here is an example that pulls values out of the Regexp:
#
# when /^(.*)'s profile page$/i
# user_profile_path(User.find_by_login($1))
when /the new user page/
'/users/new'
else
begin
page_name =~ /the (.*) page/
......
PROJECT_ROOT = File.expand_path(File.join(File.dirname(__FILE__), '..', '..')).freeze
TEMP_ROOT = File.join(PROJECT_ROOT, 'tmp').freeze
APP_NAME = 'testapp'.freeze
CUC_RAILS_ROOT = File.join(TEMP_ROOT, APP_NAME).freeze
BUNDLE_ENV_VARS = %w(RUBYOPT BUNDLE_PATH BUNDLE_BIN_PATH BUNDLE_GEMFILE)
ORIGINAL_BUNDLE_VARS = Hash[ENV.select{ |key,value| BUNDLE_ENV_VARS.include?(key) }]
ENV['RAILS_ENV'] = 'test'
Before do
ENV['BUNDLE_GEMFILE'] = File.join(Dir.pwd, ENV['BUNDLE_GEMFILE']) unless ENV['BUNDLE_GEMFILE'].start_with?(Dir.pwd)
@framework_version = nil
end
After do
ORIGINAL_BUNDLE_VARS.each_pair do |key, value|
ENV[key] = value
end
end
When /^I reset Bundler environment variable$/ do
BUNDLE_ENV_VARS.each do |key|
ENV[key] = nil
end
end
module RailsCommandHelpers
def framework_version?(version_string)
framework_version =~ /^#{version_string}/
end
def framework_version
@framework_version ||= `rails -v`[/^Rails (.+)$/, 1]
end
def new_application_command
framework_version?("3") ? "rails new" : "rails"
end
def generator_command
framework_version?("3") ? "script/rails generate" : "script/generate"
end
def runner_command
framework_version?("3") ? "script/rails runner" : "script/runner"
end
end
World(RailsCommandHelpers)
module AWSS3Methods
def load_s3
begin
require 'aws/s3'
rescue LoadError => e
fail "You do not have aws-s3 installed."
end
end
def assert_credentials(key, secret)
load_s3
begin
AWS::S3::Base.establish_connection!(
:access_key_id => key,
:secret_access_key => secret
)
AWS::S3::Service.buckets
rescue AWS::S3::ResponseError => e
fail "Could not connect using AWS credentials in AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY. " +
"Please make sure these are set in your environment."
end
end
end
World(AWSS3Methods)
module HtmlSelectorsHelpers
# Maps a name to a selector. Used primarily by the
#
# When /^(.+) within (.+)$/ do |step, scope|
#
# step definitions in web_steps.rb
#
def selector_for(locator)
case locator
when "the page"
"html > body"
else
raise "Can't find mapping from \"#{locator}\" to a selector.\n" +
"Now, go and add a mapping in #{__FILE__}"
end
end
end
World(HtmlSelectorsHelpers)
# This file was generated by Appraisal
source "http://rubygems.org"
gem "ruby-debug"
gem "rails", "~>2.3.0"
gem "activerecord", :require=>"active_record"
gem "appraisal"
gem "aruba"
gem "aws-s3", :require=>"aws/s3"
gem "bundler"
gem "cocaine", "~>0.2"
gem "fog"
gem "jruby-openssl", :platform=>:jruby
gem "mime-types"
gem "mocha"
gem "rake"
gem "sqlite3-ruby", "~>1.3.0"
gem "rdoc", :require=>false
gem "capybara"
gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "mocha"
gem "aws-s3", {:require=>"aws/s3"}
gem "appraisal"
\ No newline at end of file
gem "sqlite3", "~>1.3.4"
gem "fakeweb", :require=>false
gem "pry"
gem "rails", "~> 2.3.14"
gem "paperclip", :path=>"../"
GEM
remote: http://rubygems.org/
specs:
actionmailer (2.3.10)
actionpack (= 2.3.10)
actionpack (2.3.10)
activesupport (= 2.3.10)
rack (~> 1.1.0)
activerecord (2.3.10)
activesupport (= 2.3.10)
activeresource (2.3.10)
activesupport (= 2.3.10)
activesupport (2.3.10)
appraisal (0.1)
bundler
rake
aws-s3 (0.6.2)
builder
mime-types
xml-simple
builder (3.0.0)
columnize (0.3.2)
linecache (0.43)
mime-types (1.16)
mocha (0.9.9)
rake
rack (1.1.0)
rails (2.3.10)
actionmailer (= 2.3.10)
actionpack (= 2.3.10)
activerecord (= 2.3.10)
activeresource (= 2.3.10)
activesupport (= 2.3.10)
rake (>= 0.8.3)
rake (0.8.7)
ruby-debug (0.10.4)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
shoulda (2.11.3)
sqlite3-ruby (1.3.2)
xml-simple (1.0.12)
PLATFORMS
ruby
DEPENDENCIES
appraisal
aws-s3
mocha
rails (~> 2.3.0)
rake
ruby-debug
shoulda
sqlite3-ruby (~> 1.3.0)
# This file was generated by Appraisal
source "http://rubygems.org"
gem "ruby-debug"
gem "rails", "~>3.0.0"
gem "activerecord", :require=>"active_record"
gem "appraisal"
gem "aruba"
gem "aws-s3", :require=>"aws/s3"
gem "bundler"
gem "cocaine", "~>0.2"
gem "fog"
gem "jruby-openssl", :platform=>:jruby
gem "mime-types"
gem "mocha"
gem "rake"
gem "sqlite3-ruby", "~>1.3.0"
gem "rdoc", :require=>false
gem "capybara"
gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "mocha"
gem "aws-s3", {:require=>"aws/s3"}
gem "appraisal"
\ No newline at end of file
gem "sqlite3", "~>1.3.4"
gem "fakeweb", :require=>false
gem "pry"
gem "rails", "~> 3.0.10"
gem "paperclip", :path=>"../"
GEM
remote: http://rubygems.org/
specs:
abstract (1.0.0)
actionmailer (3.0.3)
actionpack (= 3.0.3)
mail (~> 2.2.9)
actionpack (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
builder (~> 2.1.2)
erubis (~> 2.6.6)
i18n (~> 0.4)
rack (~> 1.2.1)
rack-mount (~> 0.6.13)
rack-test (~> 0.5.6)
tzinfo (~> 0.3.23)
activemodel (3.0.3)
activesupport (= 3.0.3)
builder (~> 2.1.2)
i18n (~> 0.4)
activerecord (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
arel (~> 2.0.2)
tzinfo (~> 0.3.23)
activeresource (3.0.3)
activemodel (= 3.0.3)
activesupport (= 3.0.3)
activesupport (3.0.3)
appraisal (0.1)
bundler
rake
arel (2.0.4)
aws-s3 (0.6.2)
builder
mime-types
xml-simple
builder (2.1.2)
columnize (0.3.2)
erubis (2.6.6)
abstract (>= 1.0.0)
i18n (0.4.2)
linecache (0.43)
mail (2.2.10)
activesupport (>= 2.3.6)
i18n (~> 0.4.1)
mime-types (~> 1.16)
treetop (~> 1.4.8)
mime-types (1.16)
mocha (0.9.9)
rake
polyglot (0.3.1)
rack (1.2.1)
rack-mount (0.6.13)
rack (>= 1.0.0)
rack-test (0.5.6)
rack (>= 1.0)
rails (3.0.3)
actionmailer (= 3.0.3)
actionpack (= 3.0.3)
activerecord (= 3.0.3)
activeresource (= 3.0.3)
activesupport (= 3.0.3)
bundler (~> 1.0)
railties (= 3.0.3)
railties (3.0.3)
actionpack (= 3.0.3)
activesupport (= 3.0.3)
rake (>= 0.8.7)
thor (~> 0.14.4)
rake (0.8.7)
ruby-debug (0.10.4)
columnize (>= 0.1)
ruby-debug-base (~> 0.10.4.0)
ruby-debug-base (0.10.4)
linecache (>= 0.3)
shoulda (2.11.3)
sqlite3-ruby (1.3.2)
thor (0.14.6)
treetop (1.4.9)
polyglot (>= 0.3.1)
tzinfo (0.3.23)
xml-simple (1.0.12)
PLATFORMS
ruby
DEPENDENCIES
appraisal
aws-s3
mocha
rails (~> 3.0.0)
rake
ruby-debug
shoulda
sqlite3-ruby (~> 1.3.0)
# This file was generated by Appraisal
source "http://rubygems.org"
gem "activerecord", :require=>"active_record"
gem "appraisal"
gem "aruba"
gem "aws-s3", :require=>"aws/s3"
gem "bundler"
gem "cocaine", "~>0.2"
gem "fog"
gem "jruby-openssl", :platform=>:jruby
gem "mime-types"
gem "mocha"
gem "rake"
gem "rdoc", :require=>false
gem "capybara"
gem "cucumber", "~> 1.0.0"
gem "shoulda"
gem "sqlite3", "~>1.3.4"
gem "fakeweb", :require=>false
gem "pry"
gem "rails", "~> 3.1.0"
gem "paperclip", :path=>"../"
require File.join(File.dirname(__FILE__), "lib", "paperclip")
require 'paperclip/railtie'
Paperclip::Railtie.insert
require 'rails/generators/active_record'
class PaperclipGenerator < ActiveRecord::Generators::Base
desc "Create a migration to add paperclip-specific fields to your model."
desc "Create a migration to add paperclip-specific fields to your model. " +
"The NAME argument is the name of your model, and the following " +
"arguments are the name of the attachments"
argument :attachment_names, :required => true, :type => :array, :desc => "The names of the attachment(s) to add.",
:banner => "attachment_one attachment_two attachment_three ..."
......
module Paperclip
class CommandLine
class << self
attr_accessor :path
end
def initialize(binary, params = "", options = {})
@binary = binary.dup
@params = params.dup
@options = options.dup
@swallow_stderr = @options.has_key?(:swallow_stderr) ? @options.delete(:swallow_stderr) : Paperclip.options[:swallow_stderr]
@expected_outcodes = @options.delete(:expected_outcodes)
@expected_outcodes ||= [0]
end
def command
cmd = []
cmd << full_path(@binary)
cmd << interpolate(@params, @options)
cmd << bit_bucket if @swallow_stderr
cmd.join(" ")
end
def run
Paperclip.log(command)
begin
output = self.class.send(:'`', command)
rescue Errno::ENOENT
raise Paperclip::CommandNotFoundError
end
if $?.exitstatus == 127
raise Paperclip::CommandNotFoundError
end
unless @expected_outcodes.include?($?.exitstatus)
raise Paperclip::PaperclipCommandLineError, "Command '#{command}' returned #{$?.exitstatus}. Expected #{@expected_outcodes.join(", ")}"
end
output
end
private
def full_path(binary)
[self.class.path, binary].compact.join("/")
end
def interpolate(pattern, vars)
# interpolates :variables and :{variables}
pattern.gsub(%r#:(?:\w+|\{\w+\})#) do |match|
key = match[1..-1]
key = key[1..-2] if key[0,1] == '{'
if invalid_variables.include?(key)
raise PaperclipCommandLineError,
"Interpolation of #{key} isn't allowed."
end
interpolation(vars, key) || match
end
end
def invalid_variables
%w(expected_outcodes swallow_stderr)
end
def interpolation(vars, key)
if vars.key?(key.to_sym)
shell_quote(vars[key.to_sym])
end
end
def shell_quote(string)
return "" if string.nil? or string.blank?
if self.class.unix?
string.split("'").map{|m| "'#{m}'" }.join("\\'")
else
%{"#{string}"}
end
end
def bit_bucket
self.class.unix? ? "2>/dev/null" : "2>NUL"
end
def self.unix?
File.exist?("/dev/null")
end
end
end
......@@ -13,15 +13,20 @@ module Paperclip
# Uses ImageMagick to determing the dimensions of a file, passed in as either a
# File or path.
# NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be
# a Tempfile object, which would be eligible for file deletion when no longer referenced.
def self.from_file file
file = file.path if file.respond_to? "path"
file_path = file.respond_to?(:path) ? file.path : file
raise(Paperclip::NotIdentifiedByImageMagickError.new("Cannot find the geometry of a file with a blank name")) if file_path.blank?
geometry = begin
Paperclip.run("identify", "-format %wx%h :file", :file => "#{file}[0]")
rescue PaperclipCommandLineError
Paperclip.run("identify", "-format %wx%h :file", :file => "#{file_path}[0]")
rescue Cocaine::ExitStatusError
""
rescue Cocaine::CommandNotFoundError => e
raise Paperclip::CommandNotFoundError.new("Could not run the `identify` command. Please install ImageMagick.")
end
parse(geometry) ||
raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command."))
raise(NotIdentifiedByImageMagickError.new("#{file_path} is not recognized by the 'identify' command."))
end
# Parses a "WxH" formatted string, where W is the width and H is the height.
......
require 'uri'
module Paperclip
class InterpolatedString < String
def escaped?
!!@escaped
end
def escape
if !escaped?
escaped_string = self.class.new(URI.escape(self))
escaped_string.instance_variable_set(:@escaped, true)
escaped_string
else
self
end
end
def unescape
if escaped?
escaped_string = self.class.new(URI.unescape(self))
escaped_string.instance_variable_set(:@escaped, false)
escaped_string
else
self
end
end
def force_escape
@escaped = true
end
end
end
require 'paperclip/interpolated_string'
module Paperclip
# This module contains all the methods that are available for interpolation
# in paths and urls. To add your own (or override an existing one), you
......@@ -6,13 +8,13 @@ module Paperclip
module Interpolations
extend self
# Hash assignment of interpolations. Included only for compatability,
# Hash assignment of interpolations. Included only for compatibility,
# and is not intended for normal use.
def self.[]= name, block
define_method(name, &block)
end
# Hash access of interpolations. Included only for compatability,
# Hash access of interpolations. Included only for compatibility,
# and is not intended for normal use.
def self.[] name
method(name)
......@@ -25,17 +27,22 @@ module Paperclip
# Perform the actual interpolation. Takes the pattern to interpolate
# and the arguments to pass, which are the attachment and style name.
# You can pass a method name on your record as a symbol, which should turn
# an interpolation pattern for Paperclip to use.
def self.interpolate pattern, *args
all.reverse.inject( pattern.dup ) do |result, tag|
pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol
interpolated_string = all.reverse.inject(InterpolatedString.new(pattern)) do |result, tag|
result.gsub(/:#{tag}/) do |match|
send( tag, *args )
end
end
interpolated_string.force_escape if pattern =~ /:url/
interpolated_string
end
# Returns the filename, the same way as ":basename.:extension" would.
def filename attachment, style_name
"#{basename(attachment, style_name)}.#{extension(attachment, style_name)}"
[ basename(attachment, style_name), extension(attachment, style_name) ].reject(&:blank?).join(".")
end
# Returns the interpolated URL. Will raise an error if the url itself
......@@ -83,7 +90,7 @@ module Paperclip
# Returns the basename of the file. e.g. "file" for "file.jpg"
def basename attachment, style_name
attachment.original_filename.gsub(/#{File.extname(attachment.original_filename)}$/, "")
attachment.original_filename.gsub(/#{Regexp.escape(File.extname(attachment.original_filename))}$/, "")
end
# Returns the extension of the file. e.g. "jpg" for "file.jpg"
......@@ -94,11 +101,41 @@ module Paperclip
File.extname(attachment.original_filename).gsub(/^\.+/, "")
end
# Returns an extension based on the content type. e.g. "jpeg" for "image/jpeg".
# Each mime type generally has multiple extensions associated with it, so
# if the extension from teh original filename is one of these extensions,
# that extension is used, otherwise, the first in the list is used.
def content_type_extension attachment, style_name
mime_type = MIME::Types[attachment.content_type]
extensions_for_mime_type = unless mime_type.empty?
mime_type.first.extensions
else
[]
end
original_extension = extension(attachment, style_name)
if extensions_for_mime_type.include? original_extension
original_extension
elsif !extensions_for_mime_type.empty?
extensions_for_mime_type.first
else
# It's possible, though unlikely, that the mime type is not in the
# database, so just use the part after the '/' in the mime type as the
# extension.
%r{/([^/]*)$}.match(attachment.content_type)[1]
end
end
# Returns the id of the instance.
def id attachment, style_name
attachment.instance.id
end
# Returns the #to_param of the instance.
def param attachment, style_name
attachment.instance.to_param
end
# Returns the fingerprint of the instance.
def fingerprint attachment, style_name
attachment.fingerprint
......@@ -106,14 +143,22 @@ module Paperclip
# Returns a the attachment hash. See Paperclip::Attachment#hash for
# more details.
def hash attachment, style_name
def hash attachment=nil, style_name=nil
if attachment && style_name
attachment.hash(style_name)
else
super()
end
end
# Returns the id of the instance in a split path form. e.g. returns
# 000/001/234 for an id of 1234.
def id_partition attachment, style_name
("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
if (id = attachment.instance.id).is_a?(Integer)
("%09d" % id).scan(/\d{3}/).join("/")
else
id.scan(/.{3}/).first(3).join("/")
end
end
# Returns the pluralized form of the attachment name. e.g.
......
......@@ -30,7 +30,7 @@ module IOStream
end
# Corrects a bug in Windows when asking for Tempfile size.
if defined? Tempfile
if defined?(Tempfile) && RUBY_PLATFORM !~ /java/
class Tempfile
def size
if @tmpfile
......
......@@ -17,6 +17,8 @@ module Paperclip
class ValidateAttachmentContentTypeMatcher
def initialize attachment_name
@attachment_name = attachment_name
@allowed_types = []
@rejected_types = []
end
def allowing *types
......@@ -37,13 +39,19 @@ module Paperclip
end
def failure_message
"Content types #{@allowed_types.join(", ")} should be accepted" +
" and #{@rejected_types.join(", ")} rejected by #{@attachment_name}"
"".tap do |str|
str << "Content types #{@allowed_types.join(", ")} should be accepted" if @allowed_types.present?
str << "\n" if @allowed_types.present? && @rejected_types.present?
str << "Content types #{@rejected_types.join(", ")} should be rejected by #{@attachment_name}" if @rejected_types.present?
end
end
def negative_failure_message
"Content types #{@allowed_types.join(", ")} should be rejected" +
" and #{@rejected_types.join(", ")} accepted by #{@attachment_name}"
"".tap do |str|
str << "Content types #{@allowed_types.join(", ")} should be rejected" if @allowed_types.present?
str << "\n" if @allowed_types.present? && @rejected_types.present?
str << "Content types #{@rejected_types.join(", ")} should be accepted by #{@attachment_name}" if @rejected_types.present?
end
end
def description
......@@ -52,22 +60,20 @@ module Paperclip
protected
def allow_types?(types)
types.all? do |type|
def type_allowed?(type)
file = StringIO.new(".")
file.content_type = type
(subject = @subject.new).attachment_for(@attachment_name).assign(file)
subject.valid?
subject.errors[:"#{@attachment_name}_content_type"].blank?
end
end
def allowed_types_allowed?
allow_types?(@allowed_types)
@allowed_types.all? { |type| type_allowed?(type) }
end
def rejected_types_rejected?
not allow_types?(@rejected_types)
!@rejected_types.any? { |type| type_allowed?(type) }
end
end
end
......
require 'set'
module Paperclip
class << self
attr_accessor :classes_with_attachments
attr_writer :registered_attachments_styles_path
def registered_attachments_styles_path
@registered_attachments_styles_path ||= Rails.root.join('public/system/paperclip_attachments.yml').to_s
end
end
self.classes_with_attachments = Set.new
# Get list of styles saved on previous deploy (running rake paperclip:refresh:missing_styles)
def self.get_registered_attachments_styles
YAML.load_file(Paperclip.registered_attachments_styles_path)
rescue Errno::ENOENT
nil
end
private_class_method :get_registered_attachments_styles
def self.save_current_attachments_styles!
File.open(Paperclip.registered_attachments_styles_path, 'w') do |f|
YAML.dump(current_attachments_styles, f)
end
end
# Returns hash with styles for all classes using Paperclip.
# Unfortunately current version does not work with lambda styles:(
# {
# :User => {:avatar => [:small, :big]},
# :Book => {
# :cover => [:thumb, :croppable]},
# :sample => [:thumb, :big]},
# }
# }
def self.current_attachments_styles
Hash.new.tap do |current_styles|
Paperclip.classes_with_attachments.each do |klass_name|
klass = Paperclip.class_for(klass_name)
klass.attachment_definitions.each do |attachment_name, attachment_attributes|
# TODO: is it even possible to take into account Procs?
next if attachment_attributes[:styles].kind_of?(Proc)
attachment_attributes[:styles].try(:keys).try(:each) do |style_name|
klass_sym = klass.to_s.to_sym
current_styles[klass_sym] ||= Hash.new
current_styles[klass_sym][attachment_name.to_sym] ||= Array.new
current_styles[klass_sym][attachment_name.to_sym] << style_name.to_sym
current_styles[klass_sym][attachment_name.to_sym].map!(&:to_s).sort!.map!(&:to_sym).uniq!
end
end
end
end
end
private_class_method :current_attachments_styles
# Returns hash with styles missing from recent run of rake paperclip:refresh:missing_styles
# {
# :User => {:avatar => [:big]},
# :Book => {
# :cover => [:croppable]},
# }
# }
def self.missing_attachments_styles
current_styles = current_attachments_styles
registered_styles = get_registered_attachments_styles
Hash.new.tap do |missing_styles|
current_styles.each do |klass, attachment_definitions|
attachment_definitions.each do |attachment_name, styles|
registered = registered_styles[klass][attachment_name] rescue []
missed = styles - registered
if missed.present?
klass_sym = klass.to_s.to_sym
missing_styles[klass_sym] ||= Hash.new
missing_styles[klass_sym][attachment_name.to_sym] ||= Array.new
missing_styles[klass_sym][attachment_name.to_sym].concat(missed.to_a)
missing_styles[klass_sym][attachment_name.to_sym].map!(&:to_s).sort!.map!(&:to_sym).uniq!
end
end
end
end
end
end
module Paperclip
class Options
attr_accessor :url, :path, :only_process, :normalized_styles, :default_url, :default_style,
:storage, :use_timestamp, :whiny, :use_default_time_zone, :hash_digest, :hash_secret,
:convert_options, :source_file_options, :preserve_files, :http_proxy
attr_accessor :s3_credentials, :s3_host_name, :s3_options, :s3_permissions, :s3_protocol,
:s3_headers, :s3_host_alias, :bucket
attr_accessor :fog_directory, :fog_credentials, :fog_host, :fog_public, :fog_file
def initialize(attachment, hash)
@attachment = attachment
@url = hash[:url]
@url = @url.call(@attachment) if @url.is_a?(Proc)
@path = hash[:path]
@path = @path.call(@attachment) if @path.is_a?(Proc)
@styles = hash[:styles]
@only_process = hash[:only_process]
@normalized_styles = nil
@default_url = hash[:default_url]
@default_style = hash[:default_style]
@storage = hash[:storage]
@use_timestamp = hash[:use_timestamp]
@whiny = hash[:whiny_thumbnails] || hash[:whiny]
@use_default_time_zone = hash[:use_default_time_zone]
@hash_digest = hash[:hash_digest]
@hash_data = hash[:hash_data]
@hash_secret = hash[:hash_secret]
@convert_options = hash[:convert_options]
@source_file_options = hash[:source_file_options]
@processors = hash[:processors]
@preserve_files = hash[:preserve_files]
@http_proxy = hash[:http_proxy]
#s3 options
@s3_credentials = hash[:s3_credentials]
@s3_host_name = hash[:s3_host_name]
@bucket = hash[:bucket]
@s3_options = hash[:s3_options]
@s3_permissions = hash[:s3_permissions]
@s3_protocol = hash[:s3_protocol]
@s3_headers = hash[:s3_headers]
@s3_host_alias = hash[:s3_host_alias]
#fog options
@fog_directory = hash[:fog_directory]
@fog_credentials = hash[:fog_credentials]
@fog_host = hash[:fog_host]
@fog_public = hash[:fog_public]
@fog_file = hash[:fog_file]
end
def method_missing(method, *args, &blk)
if method.to_s[-1,1] == "="
instance_variable_set("@#{method[0..-2]}", args[0])
else
instance_variable_get("@#{method}")
end
end
def processors
@processors.respond_to?(:call) ? @processors.call(@attachment.instance) : @processors
end
def styles
if @styles.respond_to?(:call) || !@normalized_styles
@normalized_styles = ActiveSupport::OrderedHash.new
(@styles.respond_to?(:call) ? @styles.call(@attachment) : @styles).each do |name, args|
normalized_styles[name] = Paperclip::Style.new(name, args.dup, @attachment)
end
end
@normalized_styles
end
end
end
......@@ -41,7 +41,7 @@ module Paperclip
# http://marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions
class Tempfile < ::Tempfile
# This is Ruby 1.8.7's implementation.
if RUBY_VERSION <= "1.8.6"
if RUBY_VERSION <= "1.8.6" || RUBY_PLATFORM =~ /java/
def make_tmpname(basename, n)
case basename
when Array
......
......@@ -19,6 +19,8 @@ module Paperclip
def self.insert
ActiveRecord::Base.send(:include, Paperclip::Glue)
File.send(:include, Paperclip::Upfile)
Paperclip.options[:logger] = defined?(ActiveRecord) ? ActiveRecord::Base.logger : Rails.logger
end
end
end
require "paperclip/storage/filesystem"
require "paperclip/storage/fog"
require "paperclip/storage/s3"
......@@ -38,9 +38,17 @@ module Paperclip
file.close
FileUtils.mkdir_p(File.dirname(path(style_name)))
log("saving #{path(style_name)}")
begin
FileUtils.mv(file.path, path(style_name))
FileUtils.chmod(0644, path(style_name))
rescue SystemCallError
FileUtils.cp(file.path, path(style_name))
FileUtils.rm(file.path)
end
FileUtils.chmod(0666&~File.umask, path(style_name))
end
after_flush_writes # allows attachment to clean up temp files
@queued_for_write = {}
end
......@@ -58,7 +66,7 @@ module Paperclip
FileUtils.rmdir(path)
break if File.exists?(path) # Ruby 1.9.2 does not raise if the removal failed.
end
rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR
rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR, Errno::EACCES
# Stop trying to remove parent directories
rescue SystemCallError => e
log("There was an unexpected error while deleting directories: #{e.class}")
......
module Paperclip
module Storage
# fog is a modern and versatile cloud computing library for Ruby.
# Among others, it supports Amazon S3 to store your files. In
# contrast to the outdated AWS-S3 gem it is actively maintained and
# supports multiple locations.
# Amazon's S3 file hosting service is a scalable, easy place to
# store files for distribution. You can find out more about it at
# http://aws.amazon.com/s3 There are a few fog-specific options for
# has_attached_file, which will be explained using S3 as an example:
# * +fog_credentials+: Takes a Hash with your credentials. For S3,
# you can use the following format:
# aws_access_key_id: '<your aws_access_key_id>'
# aws_secret_access_key: '<your aws_secret_access_key>'
# provider: 'AWS'
# region: 'eu-west-1'
# * +fog_directory+: This is the name of the S3 bucket that will
# store your files. Remember that the bucket must be unique across
# all of Amazon S3. If the bucket does not exist, Paperclip will
# attempt to create it.
# * +path+: This is the key under the bucket in which the file will
# be stored. The URL will be constructed from the bucket and the
# path. This is what you will want to interpolate. Keys should be
# unique, like filenames, and despite the fact that S3 (strictly
# speaking) does not support directories, you can still use a / to
# separate parts of your file name.
# * +fog_public+: (optional, defaults to true) Should the uploaded
# files be public or not? (true/false)
# * +fog_host+: (optional) The fully-qualified domain name (FQDN)
# that is the alias to the S3 domain of your bucket, e.g.
# 'http://images.example.com'. This can also be used in
# conjunction with Cloudfront (http://aws.amazon.com/cloudfront)
module Fog
def self.extended base
begin
require 'fog'
rescue LoadError => e
e.message << " (You may need to install the fog gem)"
raise e
end unless defined?(Fog)
base.instance_eval do
unless @options.url.to_s.match(/^:fog.*url$/)
@options.path = @options.path.gsub(/:url/, @options.url)
@options.url = ':fog_public_url'
end
Paperclip.interpolates(:fog_public_url) do |attachment, style|
attachment.public_url(style)
end unless Paperclip::Interpolations.respond_to? :fog_public_url
end
end
def exists?(style = default_style)
if original_filename
!!directory.files.head(path(style))
else
false
end
end
def fog_credentials
@fog_credentials ||= parse_credentials(@options.fog_credentials)
end
def fog_file
@fog_file ||= @options.fog_file || {}
end
def fog_public
@fog_public ||= @options.fog_public || true
end
def flush_writes
for style, file in @queued_for_write do
log("saving #{path(style)}")
retried = false
begin
directory.files.create(fog_file.merge(
:body => file,
:key => path(style),
:public => fog_public
))
rescue Excon::Errors::NotFound
raise if retried
retried = true
directory.save
retry
end
end
after_flush_writes # allows attachment to clean up temp files
@queued_for_write = {}
end
def flush_deletes
for path in @queued_for_delete do
log("deleting #{path}")
directory.files.new(:key => path).destroy
end
@queued_for_delete = []
end
# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
def to_file(style = default_style)
if @queued_for_write[style]
@queued_for_write[style]
else
body = directory.files.get(path(style)).body
filename = path(style)
extname = File.extname(filename)
basename = File.basename(filename, extname)
file = Tempfile.new([basename, extname])
file.binmode
file.write(body)
file.rewind
file
end
end
def public_url(style = default_style)
if @options.fog_host
host = (@options.fog_host =~ /%d/) ? @options.fog_host % (path(style).hash % 4) : @options.fog_host
"#{host}/#{path(style)}"
else
directory.files.new(:key => path(style)).public_url
end
end
def parse_credentials(creds)
creds = find_credentials(creds).stringify_keys
env = Object.const_defined?(:Rails) ? Rails.env : nil
(creds[env] || creds).symbolize_keys
end
private
def find_credentials(creds)
case creds
when File
YAML::load(ERB.new(File.read(creds.path)).result)
when String, Pathname
YAML::load(ERB.new(File.read(creds)).result)
when Hash
creds
else
raise ArgumentError, "Credentials are not a path, file, or hash."
end
end
def connection
@connection ||= ::Fog::Storage.new(fog_credentials)
end
def directory
@directory ||= connection.directories.new(:key => @options.fog_directory)
end
end
end
end
......@@ -25,8 +25,16 @@ module Paperclip
# development versus production.
# * +s3_permissions+: This is a String that should be one of the "canned" access
# policies that S3 provides (more information can be found here:
# http://docs.amazonwebservices.com/AmazonS3/2006-03-01/RESTAccessPolicy.html#RESTCannedAccessPolicies)
# http://docs.amazonwebservices.com/AmazonS3/latest/dev/index.html?RESTAccessPolicy.html)
# The default for Paperclip is :public_read.
#
# You can set permission on a per style bases by doing the following:
# :s3_permissions => {
# :original => :private
# }
# Or globaly:
# :s3_permissions => :private
#
# * +s3_protocol+: The protocol for the URLs generated to your S3 assets. Can be either
# 'http' or 'https'. Defaults to 'http' when your :s3_permissions are :public_read (the
# default), and 'https' when your :s3_permissions are anything else.
......@@ -39,9 +47,9 @@ module Paperclip
# * +s3_host_alias+: The fully-qualified domain name (FQDN) that is the alias to the
# S3 domain of your bucket. Used with the :s3_alias_url url interpolation. See the
# link in the +url+ entry for more information about S3 domains and buckets.
# * +url+: There are three options for the S3 url. You can choose to have the bucket's name
# * +url+: There are four options for the S3 url. You can choose to have the bucket's name
# placed domain-style (bucket.s3.amazonaws.com) or path-style (s3.amazonaws.com/bucket).
# Lastly, you can specify a CNAME (which requires the CNAME to be specified as
# You can also specify a CNAME (which requires the CNAME to be specified as
# :s3_alias_url. You can read more about CNAMEs and S3 at
# http://docs.amazonwebservices.com/AmazonS3/latest/index.html?VirtualHosting.html
# Normally, this won't matter in the slightest and you can leave the default (which is
......@@ -50,12 +58,15 @@ module Paperclip
# NOTE: If you use a CNAME for use with CloudFront, you can NOT specify https as your
# :s3_protocol; This is *not supported* by S3/CloudFront. Finally, when using the host
# alias, the :bucket parameter is ignored, as the hostname is used as the bucket name
# by S3.
# by S3. The fourth option for the S3 url is :asset_host, which uses Rails' built-in
# asset_host settings. NOTE: To get the full url from a paperclip'd object, use the
# image_path helper; this is what image_tag uses to generate the url for an img tag.
# * +path+: This is the key under the bucket in which the file will be stored. The
# URL will be constructed from the bucket and the path. This is what you will want
# to interpolate. Keys should be unique, like filenames, and despite the fact that
# S3 (strictly speaking) does not support directories, you can still use a / to
# separate parts of your file name.
# * +s3_host_name+: If you are using your bucket in Tokyo region etc, write host_name.
module S3
def self.extended base
begin
......@@ -66,49 +77,101 @@ module Paperclip
end unless defined?(AWS::S3)
base.instance_eval do
@s3_credentials = parse_credentials(@options[:s3_credentials])
@bucket = @options[:bucket] || @s3_credentials[:bucket]
@bucket = @bucket.call(self) if @bucket.is_a?(Proc)
@s3_options = @options[:s3_options] || {}
@s3_permissions = @options[:s3_permissions] || :public_read
@s3_protocol = @options[:s3_protocol] || (@s3_permissions == :public_read ? 'http' : 'https')
@s3_headers = @options[:s3_headers] || {}
@s3_host_alias = @options[:s3_host_alias]
unless @url.to_s.match(/^:s3.*url$/)
@path = @path.gsub(/:url/, @url)
@url = ":s3_path_url"
@s3_options = @options.s3_options || {}
@s3_permissions = set_permissions(@options.s3_permissions)
@s3_protocol = @options.s3_protocol ||
Proc.new do |style|
(@s3_permissions[style.to_sym] || @s3_permissions[:default]) == :public_read ? 'http' : 'https'
end
@s3_headers = @options.s3_headers || {}
unless @options.url.to_s.match(/^:s3.*url$/) || @options.url == ":asset_host"
@options.path = @options.path.gsub(/:url/, @options.url).gsub(/^:rails_root\/public\/system/, '')
@options.url = ":s3_path_url"
end
@options.url = @options.url.inspect if @options.url.is_a?(Symbol)
@http_proxy = @options.http_proxy || nil
if @http_proxy
@s3_options.merge!({:proxy => @http_proxy})
end
AWS::S3::Base.establish_connection!( @s3_options.merge(
:access_key_id => @s3_credentials[:access_key_id],
:secret_access_key => @s3_credentials[:secret_access_key]
:access_key_id => s3_credentials[:access_key_id],
:secret_access_key => s3_credentials[:secret_access_key]
))
end
Paperclip.interpolates(:s3_alias_url) do |attachment, style|
"#{attachment.s3_protocol}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
"#{attachment.s3_protocol(style)}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}"
end unless Paperclip::Interpolations.respond_to? :s3_alias_url
Paperclip.interpolates(:s3_path_url) do |attachment, style|
"#{attachment.s3_protocol}://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
"#{attachment.s3_protocol(style)}://#{attachment.s3_host_name}/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
end unless Paperclip::Interpolations.respond_to? :s3_path_url
Paperclip.interpolates(:s3_domain_url) do |attachment, style|
"#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}"
"#{attachment.s3_protocol(style)}://#{attachment.bucket_name}.#{attachment.s3_host_name}/#{attachment.path(style).gsub(%r{^/}, "")}"
end unless Paperclip::Interpolations.respond_to? :s3_domain_url
Paperclip.interpolates(:asset_host) do |attachment, style|
"#{attachment.path(style).gsub(%r{^/}, "")}"
end unless Paperclip::Interpolations.respond_to? :asset_host
end
def expiring_url(time = 3600)
AWS::S3::S3Object.url_for(path, bucket_name, :expires_in => time )
def expiring_url(time = 3600, style_name = default_style)
AWS::S3::S3Object.url_for(path(style_name), bucket_name, :expires_in => time, :use_ssl => (s3_protocol(style_name) == 'https'))
end
def bucket_name
@bucket
def s3_credentials
@s3_credentials ||= parse_credentials(@options.s3_credentials)
end
def s3_host_name
@options.s3_host_name || s3_credentials[:s3_host_name] || "s3.amazonaws.com"
end
def s3_host_alias
@s3_host_alias = @options.s3_host_alias
@s3_host_alias = @s3_host_alias.call(self) if @s3_host_alias.is_a?(Proc)
@s3_host_alias
end
def bucket_name
@bucket = @options.bucket || s3_credentials[:bucket]
@bucket = @bucket.call(self) if @bucket.is_a?(Proc)
@bucket
end
def using_http_proxy?
!!@http_proxy
end
def http_proxy_host
using_http_proxy? ? @http_proxy[:host] : nil
end
def http_proxy_port
using_http_proxy? ? @http_proxy[:port] : nil
end
def http_proxy_user
using_http_proxy? ? @http_proxy[:user] : nil
end
def http_proxy_password
using_http_proxy? ? @http_proxy[:password] : nil
end
def set_permissions permissions
if permissions.is_a?(Hash)
permissions[:default] = permissions[:default] || :public_read
else
permissions = { :default => permissions || :public_read }
end
permissions
end
def parse_credentials creds
creds = find_credentials(creds).stringify_keys
(creds[Rails.env] || creds).symbolize_keys
env = Object.const_defined?(:Rails) ? Rails.env : nil
(creds[env] || creds).symbolize_keys
end
def exists?(style = default_style)
......@@ -119,9 +182,13 @@ module Paperclip
end
end
def s3_protocol
def s3_protocol(style = default_style)
if @s3_protocol.is_a?(Proc)
@s3_protocol.call(style)
else
@s3_protocol
end
end
# Returns representation of the data of the file assigned to the given
# style, in the format most representative of the current storage.
......@@ -148,8 +215,8 @@ module Paperclip
AWS::S3::S3Object.store(path(style),
file,
bucket_name,
{:content_type => instance_read(:content_type),
:access => @s3_permissions,
{:content_type => file.content_type.to_s.strip,
:access => (@s3_permissions[style] || @s3_permissions[:default]),
}.merge(@s3_headers))
rescue AWS::S3::NoSuchBucket => e
create_bucket
......@@ -158,6 +225,9 @@ module Paperclip
raise
end
end
after_flush_writes # allows attachment to clean up temp files
@queued_for_write = {}
end
......
......@@ -30,13 +30,14 @@ module Paperclip
# (which method (in the attachment) will call any supplied procs)
# There is an important change of interface here: a style rule can set its own processors
# by default we behave as before, though.
# if a proc has been supplied, we call it here
def processors
@processors || attachment.processors
@processors.respond_to?(:call) ? @processors.call(attachment.instance) : (@processors || attachment.options.processors)
end
# retrieves from the attachment the whiny setting
def whiny
attachment.whiny
attachment.options.whiny
end
# returns true if we're inclined to grumble
......@@ -48,6 +49,10 @@ module Paperclip
attachment.send(:extra_options_for, name)
end
def source_file_options
attachment.send(:extra_source_file_options_for, name)
end
# returns the geometry string for this style
# if a proc has been supplied, we call it here
def geometry
......@@ -62,16 +67,16 @@ module Paperclip
@other_args.each do |k,v|
args[k] = v.respond_to?(:call) ? v.call(attachment) : v
end
[:processors, :geometry, :format, :whiny, :convert_options].each do |k|
[:processors, :geometry, :format, :whiny, :convert_options, :source_file_options].each do |k|
(arg = send(k)) && args[k] = arg
end
args
end
# Supports getting and setting style properties with hash notation to ensure backwards-compatibility
# eg. @attachment.styles[:large][:geometry]@ will still work
# eg. @attachment.options.styles[:large][:geometry]@ will still work
def [](key)
if [:name, :convert_options, :whiny, :processors, :geometry, :format].include?(key)
if [:name, :convert_options, :whiny, :processors, :geometry, :format, :animated, :source_file_options].include?(key)
send(key)
elsif defined? @other_args[key]
@other_args[key]
......@@ -79,7 +84,7 @@ module Paperclip
end
def []=(key, value)
if [:name, :convert_options, :whiny, :processors, :geometry, :format].include?(key)
if [:name, :convert_options, :whiny, :processors, :geometry, :format, :animated, :source_file_options].include?(key)
send("#{key}=".intern, value)
else
@other_args[key] = value
......
......@@ -2,7 +2,11 @@ module Paperclip
# Handles thumbnailing images that are uploaded.
class Thumbnail < Processor
attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options, :source_file_options
attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options,
:source_file_options, :animated
# List of formats that we need to preserve animation
ANIMATED_FORMATS = %w(gif)
# Creates a Thumbnail object set to work on the +file+ given. It
# will attempt to transform the image into one defined by +target_geometry+
......@@ -10,18 +14,30 @@ module Paperclip
# unless specified. Thumbnail creation will raise no errors unless
# +whiny+ is true (which it is, by default. If +convert_options+ is
# set, the options will be appended to the convert command upon image conversion
def initialize file, options = {}, attachment = nil
#
# Options include:
#
# +geometry+ - the desired width and height of the thumbnail (required)
# +file_geometry_parser+ - an object with a method named +from_file+ that takes an image file and produces its geometry and a +transformation_to+. Defaults to Paperclip::Geometry
# +string_geometry_parser+ - an object with a method named +parse+ that takes a string and produces an object with +width+, +height+, and +to_s+ accessors. Defaults to Paperclip::Geometry
# +source_file_options+ - flags passed to the +convert+ command that influence how the source file is read
# +convert_options+ - flags passed to the +convert+ command that influence how the image is processed
# +whiny+ - whether to raise an error when processing fails. Defaults to true
# +format+ - the desired filename extension
# +animated+ - whether to merge all the layers in the image. Defaults to true
def initialize(file, options = {}, attachment = nil)
super
geometry = options[:geometry]
geometry = options[:geometry] # this is not an option
@file = file
@crop = geometry[-1,1] == '#'
@target_geometry = Geometry.parse geometry
@current_geometry = Geometry.from_file @file
@target_geometry = (options[:string_geometry_parser] || Geometry).parse(geometry)
@current_geometry = (options[:file_geometry_parser] || Geometry).from_file(@file)
@source_file_options = options[:source_file_options]
@convert_options = options[:convert_options]
@whiny = options[:whiny].nil? ? true : options[:whiny]
@format = options[:format]
@animated = options[:animated].nil? ? true : options[:animated]
@source_file_options = @source_file_options.split(/\s+/) if @source_file_options.respond_to?(:split)
@convert_options = @convert_options.split(/\s+/) if @convert_options.respond_to?(:split)
......@@ -58,9 +74,11 @@ module Paperclip
parameters = parameters.flatten.compact.join(" ").strip.squeeze(" ")
success = Paperclip.run("convert", parameters, :source => "#{File.expand_path(src.path)}[0]", :dest => File.expand_path(dst.path))
rescue PaperclipCommandLineError => e
success = Paperclip.run("convert", parameters, :source => "#{File.expand_path(src.path)}#{'[0]' unless animated?}", :dest => File.expand_path(dst.path))
rescue Cocaine::ExitStatusError => e
raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny
rescue Cocaine::CommandNotFoundError => e
raise Paperclip::CommandNotFoundError.new("Could not run the `convert` command. Please install ImageMagick.")
end
dst
......@@ -71,9 +89,17 @@ module Paperclip
def transformation_command
scale, crop = @current_geometry.transformation_to(@target_geometry, crop?)
trans = []
trans << "-coalesce" if animated?
trans << "-resize" << %["#{scale}"] unless scale.nil? || scale.empty?
trans << "-crop" << %["#{crop}"] << "+repage" if crop
trans
end
protected
# Return true if the format is animated
def animated?
@animated && ANIMATED_FORMATS.include?(@current_format[1..-1]) && (ANIMATED_FORMATS.include?(@format.to_s) || @format.blank?)
end
end
end
require 'mime/types'
module Paperclip
# The Upfile module is a convenience module for adding uploaded-file-type methods
# to the +File+ class. Useful for testing.
......@@ -6,23 +8,28 @@ module Paperclip
# Infer the MIME-type of the file from the extension.
def content_type
type = (self.path.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
case type
when %r"jp(e|g|eg)" then "image/jpeg"
when %r"tiff?" then "image/tiff"
when %r"png", "gif", "bmp" then "image/#{type}"
when "txt" then "text/plain"
when %r"html?" then "text/html"
when "js" then "application/js"
when "csv", "xml", "css" then "text/#{type}"
types = MIME::Types.type_for(self.original_filename)
if types.length == 0
type_from_file_command
elsif types.length == 1
types.first.content_type
else
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
content_type = (Paperclip.run("file", "-b --mime-type :file", :file => self.path).split(':').last.strip rescue "application/x-#{type}")
content_type = "application/x-#{type}" if content_type.match(/\(.*?\)/)
content_type
iterate_over_array_to_find_best_option(types)
end
end
def iterate_over_array_to_find_best_option(types)
types.reject {|type| type.content_type.match(/\/x-/) }.first
end
def type_from_file_command
# On BSDs, `file` doesn't give a result code of 1 if the file doesn't exist.
type = (self.original_filename.match(/\.(\w+)$/)[1] rescue "octet-stream").downcase
mime_type = (Paperclip.run("file", "-b --mime :file", :file => self.path).split(/[:;]\s+/)[0] rescue "application/x-#{type}")
mime_type = "application/x-#{type}" if mime_type.match(/\(.*?\)/)
mime_type
end
# Returns the file's normal name.
def original_filename
File.basename(self.path)
......
module Paperclip
VERSION = "2.3.8" unless defined? Paperclip::VERSION
VERSION = "2.4.4" unless defined? Paperclip::VERSION
end
def obtain_class
module Paperclip
module Task
def self.obtain_class
class_name = ENV['CLASS'] || ENV['class']
raise "Must specify CLASS" unless class_name
class_name
end
end
def obtain_attachments(klass)
klass = Object.const_get(klass.to_s)
def self.obtain_attachments(klass)
klass = Paperclip.class_for(klass.to_s)
name = ENV['ATTACHMENT'] || ENV['attachment']
raise "Class #{klass.name} has no attachments specified" unless klass.respond_to?(:attachment_definitions)
if !name.blank? && klass.attachment_definitions.keys.include?(name)
......@@ -13,6 +15,8 @@ def obtain_attachments(klass)
else
klass.attachment_definitions.keys
end
end
end
end
namespace :paperclip do
......@@ -20,14 +24,15 @@ namespace :paperclip do
task :refresh => ["paperclip:refresh:metadata", "paperclip:refresh:thumbnails"]
namespace :refresh do
desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)."
desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT and STYLES splitted by comma)."
task :thumbnails => :environment do
errors = []
klass = obtain_class
names = obtain_attachments(klass)
klass = Paperclip::Task.obtain_class
names = Paperclip::Task.obtain_attachments(klass)
styles = (ENV['STYLES'] || ENV['styles'] || '').split(',').map(&:to_sym)
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
result = instance.send(name).reprocess!
instance.send(name).reprocess!(*styles)
errors << [instance.id, instance.errors] unless instance.errors.blank?
end
end
......@@ -36,35 +41,59 @@ namespace :paperclip do
desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)."
task :metadata => :environment do
klass = obtain_class
names = obtain_attachments(klass)
klass = Paperclip::Task.obtain_class
names = Paperclip::Task.obtain_attachments(klass)
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
if file = instance.send(name).to_file
if file = instance.send(name).to_file(:original)
instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
instance.send("#{name}_content_type=", file.content_type.strip)
instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
if Rails.version >= "3.0.0"
instance.save(:validate => false)
else
instance.save(false)
end
else
true
end
end
end
end
desc "Regenerates missing thumbnail styles for all classes using Paperclip."
task :missing_styles => :environment do
# Force loading all model classes to never miss any has_attached_file declaration:
Dir[Rails.root + 'app/models/**/*.rb'].each { |path| load path }
Paperclip.missing_attachments_styles.each do |klass, attachment_definitions|
attachment_definitions.each do |attachment_name, missing_styles|
puts "Regenerating #{klass} -> #{attachment_name} -> #{missing_styles.inspect}"
ENV['CLASS'] = klass.to_s
ENV['ATTACHMENT'] = attachment_name.to_s
ENV['STYLES'] = missing_styles.join(',')
Rake::Task['paperclip:refresh:thumbnails'].execute
end
end
Paperclip.save_current_attachments_styles!
end
end
desc "Cleans out invalid attachments. Useful after you've added new validations."
task :clean => :environment do
klass = obtain_class
names = obtain_attachments(klass)
klass = Paperclip::Task.obtain_class
names = Paperclip::Task.obtain_attachments(klass)
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
instance.send(name).send(:validate)
if instance.send(name).valid?
true
else
unless instance.valid?
attributes = %w(file_size file_name content_type).map{ |suffix| "#{name}_#{suffix}".to_sym }
if attributes.any?{ |attribute| instance.errors[attribute].present? }
instance.send("#{name}=", nil)
instance.save
if Rails.version >= "3.0.0"
instance.save(:validate => false)
else
instance.save(false)
end
end
end
end
end
......
......@@ -13,7 +13,7 @@ spec = Gem::Specification.new do |s|
s.version = Paperclip::VERSION
s.author = "Jon Yurek"
s.email = "jyurek@thoughtbot.com"
s.homepage = "http://www.thoughtbot.com/projects/paperclip"
s.homepage = "https://github.com/thoughtbot/paperclip"
s.description = "Easy upload management for ActiveRecord"
s.platform = Gem::Platform::RUBY
s.summary = "File attachments as attributes for ActiveRecord"
......@@ -21,15 +21,18 @@ spec = Gem::Specification.new do |s|
s.require_path = "lib"
s.test_files = Dir["test/**/test_*.rb"]
s.rubyforge_project = "paperclip"
s.has_rdoc = true
s.extra_rdoc_files = Dir["README*"]
s.rdoc_options << '--line-numbers' << '--inline-source'
s.requirements << "ImageMagick"
s.add_dependency 'activerecord'
s.add_dependency 'activesupport'
s.add_dependency 'activerecord', '>=2.3.0'
s.add_dependency 'activesupport', '>=2.3.2'
s.add_dependency 'cocaine', '>=0.0.2'
s.add_dependency 'mime-types'
s.add_development_dependency 'shoulda'
s.add_development_dependency 'appraisal'
s.add_development_dependency 'mocha'
s.add_development_dependency 'aws-s3'
s.add_development_dependency 'sqlite3-ruby'
s.add_development_dependency 'sqlite3'
s.add_development_dependency 'cucumber'
s.add_development_dependency 'capybara'
end
......@@ -109,8 +109,14 @@ if defined?(ActionController::Integration::Session)
end
end
class Factory
if defined?(FactoryGirl::Factory)
class FactoryGirl::Factory
include Paperclip::Shoulda #:nodoc:
end
else
class Factory
include Paperclip::Shoulda #:nodoc:
end
end
class Test::Unit::TestCase #:nodoc:
......
require './test/helper'
class CommandLineTest < Test::Unit::TestCase
def setup
Paperclip::CommandLine.path = nil
File.stubs(:exist?).with("/dev/null").returns(true)
end
should "allow colons in parameters" do
cmd = Paperclip::CommandLine.new("convert", "'a.jpg' -resize 175x220> -size 175x220 xc:black +swap -gravity center -composite 'b.jpg'", :swallow_stderr => false)
assert_equal "convert 'a.jpg' -resize 175x220> -size 175x220 xc:black +swap -gravity center -composite 'b.jpg'", cmd.command
end
should "take a command and parameters and produce a shell command for bash" do
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
assert_equal "convert a.jpg b.png", cmd.command
end
should "be able to set a path and produce commands with that path" do
Paperclip::CommandLine.path = "/opt/bin"
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
assert_equal "/opt/bin/convert a.jpg b.png", cmd.command
end
should "be able to interpolate quoted variables into the parameters" do
cmd = Paperclip::CommandLine.new("convert",
":one :{two}",
:one => "a.jpg",
:two => "b.png",
:swallow_stderr => false)
assert_equal "convert 'a.jpg' 'b.png'", cmd.command
end
should "quote command line options differently if we're on windows" do
File.stubs(:exist?).with("/dev/null").returns(false)
cmd = Paperclip::CommandLine.new("convert",
":one :{two}",
:one => "a.jpg",
:two => "b.png",
:swallow_stderr => false)
assert_equal 'convert "a.jpg" "b.png"', cmd.command
end
should "be able to quote and interpolate dangerous variables" do
cmd = Paperclip::CommandLine.new("convert",
":one :two",
:one => "`rm -rf`.jpg",
:two => "ha'ha.png",
:swallow_stderr => false)
assert_equal "convert '`rm -rf`.jpg' 'ha'\\''ha.png'", cmd.command
end
should "be able to quote and interpolate dangerous variables even on windows" do
File.stubs(:exist?).with("/dev/null").returns(false)
cmd = Paperclip::CommandLine.new("convert",
":one :two",
:one => "`rm -rf`.jpg",
:two => "ha'ha.png",
:swallow_stderr => false)
assert_equal %{convert "`rm -rf`.jpg" "ha'ha.png"}, cmd.command
end
should "add redirection to get rid of stderr in bash" do
File.stubs(:exist?).with("/dev/null").returns(true)
cmd = Paperclip::CommandLine.new("convert",
"a.jpg b.png",
:swallow_stderr => true)
assert_equal "convert a.jpg b.png 2>/dev/null", cmd.command
end
should "add redirection to get rid of stderr in cmd.exe" do
File.stubs(:exist?).with("/dev/null").returns(false)
cmd = Paperclip::CommandLine.new("convert",
"a.jpg b.png",
:swallow_stderr => true)
assert_equal "convert a.jpg b.png 2>NUL", cmd.command
end
should "raise if trying to interpolate :swallow_stderr or :expected_outcodes" do
cmd = Paperclip::CommandLine.new("convert",
":swallow_stderr :expected_outcodes",
:swallow_stderr => false,
:expected_outcodes => [0, 1])
assert_raise(Paperclip::PaperclipCommandLineError) do
cmd.command
end
end
should "run the #command it's given and return the output" do
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(0) do
assert_equal :correct_value, cmd.run
end
end
should "raise a PaperclipCommandLineError if the result code isn't expected" do
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(1) do
assert_raises(Paperclip::PaperclipCommandLineError) do
cmd.run
end
end
end
should "not raise a PaperclipCommandLineError if the result code is expected" do
cmd = Paperclip::CommandLine.new("convert",
"a.jpg b.png",
:expected_outcodes => [0, 1],
:swallow_stderr => false)
cmd.class.stubs(:"`").with("convert a.jpg b.png").returns(:correct_value)
with_exitstatus_returning(1) do
assert_nothing_raised do
cmd.run
end
end
end
should "log the command" do
cmd = Paperclip::CommandLine.new("convert", "a.jpg b.png", :swallow_stderr => false)
cmd.class.stubs(:'`')
Paperclip.expects(:log).with("convert a.jpg b.png")
cmd.run
end
should "detect that the system is unix or windows based on presence of /dev/null" do
File.stubs(:exist?).returns(true)
assert Paperclip::CommandLine.unix?
end
should "detect that the system is not unix or windows based on absence of /dev/null" do
File.stubs(:exist?).returns(false)
assert ! Paperclip::CommandLine.unix?
end
end
development:
provider: AWS
aws_access_key_id: AWS_ID
aws_secret_access_key: AWS_SECRET
test:
provider: AWS
aws_access_key_id: AWS_ID
aws_secret_access_key: AWS_SECRET
require './test/helper'
require 'fog'
Fog.mock!
class FogTest < Test::Unit::TestCase
context "" do
context "with credentials provided in a path string" do
setup do
rebuild_model :styles => { :medium => "300x300>", :thumb => "100x100>" },
:storage => :fog,
:url => '/:attachment/:filename',
:fog_directory => "paperclip",
:fog_credentials => File.join(File.dirname(__FILE__), 'fixtures', 'fog.yml')
@dummy = Dummy.new
@dummy.avatar = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
end
should "have the proper information loading credentials from a file" do
assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
end
end
context "with credentials provided in a File object" do
setup do
rebuild_model :styles => { :medium => "300x300>", :thumb => "100x100>" },
:storage => :fog,
:url => '/:attachment/:filename',
:fog_directory => "paperclip",
:fog_credentials => File.open(File.join(File.dirname(__FILE__), 'fixtures', 'fog.yml'))
@dummy = Dummy.new
@dummy.avatar = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
end
should "have the proper information loading credentials from a file" do
assert_equal @dummy.avatar.fog_credentials[:provider], 'AWS'
end
end
context "with default values for path and url" do
setup do
rebuild_model :styles => { :medium => "300x300>", :thumb => "100x100>" },
:storage => :fog,
:url => '/:attachment/:filename',
:fog_directory => "paperclip",
:fog_credentials => {
:provider => 'AWS',
:aws_access_key_id => 'AWS_ID',
:aws_secret_access_key => 'AWS_SECRET'
}
@dummy = Dummy.new
@dummy.avatar = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
end
should "be able to interpolate the path without blowing up" do
assert_equal File.expand_path(File.join(File.dirname(__FILE__), "../public/avatars/5k.png")),
@dummy.avatar.path
end
should "clean up file objects" do
File.stubs(:exist?).returns(true)
Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
@dummy.save!
end
end
setup do
@fog_directory = 'papercliptests'
@credentials = {
:provider => 'AWS',
:aws_access_key_id => 'ID',
:aws_secret_access_key => 'SECRET'
}
@connection = Fog::Storage.new(@credentials)
@connection.directories.create(
:key => @fog_directory
)
@options = {
:fog_directory => @fog_directory,
:fog_credentials => @credentials,
:fog_host => nil,
:fog_file => {:cache_control => 1234},
:path => ":attachment/:basename.:extension",
:storage => :fog
}
rebuild_model(@options)
end
should "be extended by the Fog module" do
assert Dummy.new.avatar.is_a?(Paperclip::Storage::Fog)
end
context "when assigned" do
setup do
@file = File.new(File.join(File.dirname(__FILE__), 'fixtures', '5k.png'), 'rb')
@dummy = Dummy.new
@dummy.avatar = @file
end
teardown do
@file.close
directory = @connection.directories.new(:key => @fog_directory)
directory.files.each {|file| file.destroy}
directory.destroy
end
context "without a bucket" do
setup do
@connection.directories.get(@fog_directory).destroy
end
should "create the bucket" do
assert @dummy.save
assert @connection.directories.get(@fog_directory)
end
end
context "with a bucket" do
should "succeed" do
assert @dummy.save
end
end
context "without a fog_host" do
setup do
rebuild_model(@options.merge(:fog_host => nil))
@dummy = Dummy.new
@dummy.avatar = StringIO.new('.')
@dummy.save
end
should "provide a public url" do
assert !@dummy.avatar.url.nil?
end
end
context "with a fog_host" do
setup do
rebuild_model(@options.merge(:fog_host => 'http://example.com'))
@dummy = Dummy.new
@dummy.avatar = StringIO.new('.')
@dummy.save
end
should "provide a public url" do
assert @dummy.avatar.url =~ /^http:\/\/example\.com\/avatars\/stringio\.txt\?\d*$/
end
end
context "with a fog_host that includes a wildcard placeholder" do
setup do
rebuild_model(
:fog_directory => @fog_directory,
:fog_credentials => @credentials,
:fog_host => 'http://img%d.example.com',
:path => ":attachment/:basename.:extension",
:storage => :fog
)
@dummy = Dummy.new
@dummy.avatar = StringIO.new('.')
@dummy.save
end
should "provide a public url" do
assert @dummy.avatar.url =~ /^http:\/\/img[0123]\.example\.com\/avatars\/stringio\.txt\?\d*$/
end
end
context "with fog_public set to false" do
setup do
rebuild_model(@options.merge(:fog_public => false))
@dummy = Dummy.new
@dummy.avatar = StringIO.new('.')
@dummy.save
end
should 'set the @fog_public instance variable to false' do
assert_equal false, @dummy.avatar.options.fog_public
end
end
end
end
end
......@@ -120,6 +120,35 @@ class GeometryTest < Test::Unit::TestCase
assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
end
should "not generate from a blank filename" do
file = ""
assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
end
should "not generate from a nil file" do
file = nil
assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
end
should "not generate from a file with no path" do
file = mock("file", :path => "")
file.stubs(:respond_to?).with(:path).returns(true)
assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
end
should "let us know when a command isn't found versus a processing error" do
old_path = ENV['PATH']
begin
ENV['PATH'] = ''
assert_raises(Paperclip::CommandNotFoundError) do
file = File.join(File.dirname(__FILE__), "fixtures", "5k.png")
@geo = Paperclip::Geometry.from_file(file)
end
ensure
ENV['PATH'] = old_path
end
end
[['vertical', 900, 1440, true, false, false, 1440, 900, 0.625],
['horizontal', 1024, 768, false, true, false, 1024, 768, 1.3333],
['square', 100, 100, false, false, true, 100, 100, 1]].each do |args|
......
......@@ -8,6 +8,8 @@ require 'mocha'
require 'active_record'
require 'active_record/version'
require 'active_support'
require 'mime/types'
require 'pry'
puts "Testing against version #{ActiveRecord::VERSION::STRING}"
......@@ -19,7 +21,7 @@ rescue LoadError => e
puts "debugger disabled"
end
ROOT = File.join(File.dirname(__FILE__), '..')
ROOT = Pathname(File.expand_path(File.join(File.dirname(__FILE__), '..')))
def silence_warnings
old_verbose, $VERBOSE = $VERBOSE, nil
......@@ -47,6 +49,7 @@ FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures")
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = ActiveSupport::BufferedLogger.new(File.dirname(__FILE__) + "/debug.log")
ActiveRecord::Base.establish_connection(config['test'])
Paperclip.options[:logger] = ActiveRecord::Base.logger
def reset_class class_name
ActiveRecord::Base.send(:include, Paperclip::Glue)
......@@ -67,6 +70,7 @@ end
def rebuild_model options = {}
ActiveRecord::Base.connection.create_table :dummies, :force => true do |table|
table.column :title, :string
table.column :other, :string
table.column :avatar_file_name, :string
table.column :avatar_content_type, :string
......@@ -81,10 +85,12 @@ def rebuild_class options = {}
ActiveRecord::Base.send(:include, Paperclip::Glue)
Object.send(:remove_const, "Dummy") rescue nil
Object.const_set("Dummy", Class.new(ActiveRecord::Base))
Paperclip.reset_duplicate_clash_check!
Dummy.class_eval do
include Paperclip::Glue
has_attached_file :avatar, options
end
Dummy.reset_column_information
end
class FakeModel
......
......@@ -58,6 +58,7 @@ class IntegrationTest < Test::Unit::TestCase
has_attached_file :avatar, :styles => { :thumb => "150x25#", :dynamic => lambda { |a| '50x50#' } }
end
@d2 = Dummy.find(@dummy.id)
@original_timestamp = @d2.avatar_updated_at
@d2.avatar.reprocess!
@d2.save
end
......@@ -66,6 +67,26 @@ class IntegrationTest < Test::Unit::TestCase
assert_match /\b150x25\b/, `identify "#{@dummy.avatar.path(:thumb)}"`
assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:dynamic)}"`
end
should "change the timestamp" do
assert_not_equal @original_timestamp, @d2.avatar_updated_at
end
should "clean up the old original if it is a tempfile" do
original = @d2.avatar.to_file(:original)
tf = Paperclip::Tempfile.new('original')
tf.binmode
original.binmode
tf.write(original.read)
original.close
tf.rewind
File.expects(:unlink).with(tf.instance_variable_get(:@tmpname))
@d2.avatar.expects(:to_file).with(:original).returns(tf)
@d2.avatar.reprocess!
end
end
end
......@@ -278,6 +299,38 @@ class IntegrationTest < Test::Unit::TestCase
end
end
context "A model with no source_file_options setting" do
setup do
rebuild_model :styles => { :large => "300x300>",
:medium => "100x100",
:thumb => ["32x32#", :gif] },
:default_style => :medium,
:url => "/:attachment/:class/:style/:id/:basename.:extension",
:path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
@dummy = Dummy.new
end
should "have its definition return nil when asked about source_file_options" do
assert ! Dummy.attachment_definitions[:avatar][:source_file_options]
end
context "redefined to have source_file_options setting" do
setup do
rebuild_model :styles => { :large => "300x300>",
:medium => "100x100",
:thumb => ["32x32#", :gif] },
:source_file_options => "-density 400",
:default_style => :medium,
:url => "/:attachment/:class/:style/:id/:basename.:extension",
:path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
end
should "have its definition return source_file_options value when asked about source_file_options" do
assert_equal "-density 400", Dummy.attachment_definitions[:avatar][:source_file_options]
end
end
end
context "A model with a filesystem attachment" do
setup do
rebuild_model :styles => { :large => "300x300>",
......@@ -383,6 +436,24 @@ class IntegrationTest < Test::Unit::TestCase
assert_equal "5k.png", @dummy.avatar_file_name
end
[000,002,022].each do |umask|
context "when the umask is #{umask}" do
setup do
@umask = File.umask umask
end
teardown do
File.umask @umask
end
should "respect the current umask" do
@dummy.avatar = @file
@dummy.save
assert_equal 0666&~umask, 0666&File.stat(@dummy.avatar.path).mode
end
end
end
context "that is assigned its file from another Paperclip attachment" do
setup do
@dummy2 = Dummy.new
......@@ -399,6 +470,7 @@ class IntegrationTest < Test::Unit::TestCase
@dummy.save
assert_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`,
`identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"`
assert_equal @dummy.avatar_file_name, @dummy2.avatar_file_name
end
end
......@@ -466,6 +538,18 @@ class IntegrationTest < Test::Unit::TestCase
@files_on_s3 = s3_files_for @dummy.avatar
end
context 'assigning itself to a new model' do
setup do
@d2 = Dummy.new
@d2.avatar = @dummy.avatar
@d2.save
end
should "have the same name as the old file" do
assert_equal @d2.avatar.original_filename, @dummy.avatar.original_filename
end
end
should "have the same contents as the original" do
@file.rewind
assert_equal @file.read, @files_on_s3[:original].read
......
require './test/helper'
class InterpolatedStringTest < Test::Unit::TestCase
context "inheritance" do
should "inherited from String" do
assert Paperclip::InterpolatedString.new("paperclip").is_a? String
end
end
context "#escape" do
subject { Paperclip::InterpolatedString.new("paperclip foo").escape }
should "returns an InterpolatedString object" do
assert subject.is_a? Paperclip::InterpolatedString
end
should "escape the output string" do
assert_equal "paperclip%20foo", subject
end
should "not double escape output string" do
assert_equal "paperclip%20foo", subject.escape
end
end
context "#unescape" do
subject { Paperclip::InterpolatedString.new("paperclip%20foo").escape.unescape }
should "returns an InterpolatedString object" do
assert subject.is_a? Paperclip::InterpolatedString
end
should "unescape the output string" do
assert_equal "paperclip%20foo", subject
end
should "not double unescape output string" do
assert_equal "paperclip%20foo", subject.unescape
end
end
context "#escaped?" do
subject { Paperclip::InterpolatedString.new("paperclip") }
should "returns true if string was escaped" do
assert subject.escape.escaped?
end
should "returns false if string wasn't escaped" do
assert !subject.escaped?
end
end
context "#force_escape" do
subject { Paperclip::InterpolatedString.new("paperclip") }
setup { subject.force_escape }
should "sets escaped flag to true" do
assert subject.escaped?
end
end
end
......@@ -50,6 +50,37 @@ class InterpolationsTest < Test::Unit::TestCase
assert_equal "png", Paperclip::Interpolations.extension(attachment, :style)
end
should "return the extension of the file based on the content type" do
attachment = mock
attachment.expects(:content_type).returns('image/jpeg')
interpolations = Paperclip::Interpolations
interpolations.expects(:extension).returns('random')
assert_equal "jpeg", interpolations.content_type_extension(attachment, :style)
end
should "return the original extension of the file if it matches a content type extension" do
attachment = mock
attachment.expects(:content_type).returns('image/jpeg')
interpolations = Paperclip::Interpolations
interpolations.expects(:extension).returns('jpe')
assert_equal "jpe", interpolations.content_type_extension(attachment, :style)
end
should "return the latter half of the content type of the extension if no match found" do
attachment = mock
attachment.expects(:content_type).at_least_once().returns('not/found')
interpolations = Paperclip::Interpolations
interpolations.expects(:extension).returns('random')
assert_equal "found", interpolations.content_type_extension(attachment, :style)
end
should "return the #to_param of the attachment" do
attachment = mock
attachment.expects(:to_param).returns("23-awesome")
attachment.expects(:instance).returns(attachment)
assert_equal "23-awesome", Paperclip::Interpolations.param(attachment, :style)
end
should "return the id of the attachment" do
attachment = mock
attachment.expects(:id).returns(23)
......@@ -57,13 +88,20 @@ class InterpolationsTest < Test::Unit::TestCase
assert_equal 23, Paperclip::Interpolations.id(attachment, :style)
end
should "return the partitioned id of the attachment" do
should "return the partitioned id of the attachment when the id is an integer" do
attachment = mock
attachment.expects(:id).returns(23)
attachment.expects(:instance).returns(attachment)
assert_equal "000/000/023", Paperclip::Interpolations.id_partition(attachment, :style)
end
should "return the partitioned id of the attachment when the id is a string" do
attachment = mock
attachment.expects(:id).returns("32fnj23oio2f")
attachment.expects(:instance).returns(attachment)
assert_equal "32f/nj2/3oi", Paperclip::Interpolations.id_partition(attachment, :style)
end
should "return the name of the attachment" do
attachment = mock
attachment.expects(:name).returns("file")
......@@ -110,6 +148,20 @@ class InterpolationsTest < Test::Unit::TestCase
assert_equal "one.png", Paperclip::Interpolations.filename(attachment, :style)
end
should "return the filename as basename when extension is blank" do
attachment = mock
attachment.stubs(:styles).returns({})
attachment.stubs(:original_filename).returns("one")
assert_equal "one", Paperclip::Interpolations.filename(attachment, :style)
end
should "return the basename when the extension contains regexp special characters" do
attachment = mock
attachment.stubs(:styles).returns({})
attachment.stubs(:original_filename).returns("one.ab)")
assert_equal "one", Paperclip::Interpolations.basename(attachment, :style)
end
should "return the timestamp" do
now = Time.now
zone = 'UTC'
......@@ -126,13 +178,20 @@ class InterpolationsTest < Test::Unit::TestCase
assert_equal seconds_since_epoch, Paperclip::Interpolations.updated_at(attachment, :style)
end
should "return hash" do
should "return attachment's hash when passing both arguments" do
attachment = mock
fake_hash = "a_wicked_secure_hash"
attachment.expects(:hash).returns(fake_hash)
assert_equal fake_hash, Paperclip::Interpolations.hash(attachment, :style)
end
should "return Object#hash when passing no argument" do
attachment = mock
fake_hash = "a_wicked_secure_hash"
attachment.expects(:hash).never.returns(fake_hash)
assert_not_equal fake_hash, Paperclip::Interpolations.hash
end
should "call all expected interpolations with the given arguments" do
Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234)
Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments")
......
......@@ -43,5 +43,45 @@ class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase
should_accept_dummy_class
end
context "given a class that matches and a matcher that only specifies 'allowing'" do
setup do
@dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
allowing(%w(image/png image/jpeg))
end
should_accept_dummy_class
end
context "given a class that does not match and a matcher that only specifies 'allowing'" do
setup do
@dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
allowing(%w(image/png image/jpeg))
end
should_reject_dummy_class
end
context "given a class that matches and a matcher that only specifies 'rejecting'" do
setup do
@dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
rejecting(%w(audio/mp3 application/octet-stream))
end
should_accept_dummy_class
end
context "given a class that does not match and a matcher that only specifies 'rejecting'" do
setup do
@dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*}
@matcher = self.class.validate_attachment_content_type(:avatar).
rejecting(%w(audio/mp3 application/octet-stream))
end
should_reject_dummy_class
end
end
end
# encoding: utf-8
require './test/helper'
class MockAttachment < Struct.new(:one, :two)
def instance
self
end
end
class OptionsTest < Test::Unit::TestCase
should "be able to set a value" do
@options = Paperclip::Options.new(nil, {})
assert_nil @options.path
@options.path = "this/is/a/path"
assert_equal "this/is/a/path", @options.path
end
context "#styles with a plain hash" do
setup do
@attachment = MockAttachment.new(nil, nil)
@options = Paperclip::Options.new(@attachment,
:styles => {
:something => ["400x400", :png]
})
end
should "return the right data for the style's geometry" do
assert_equal "400x400", @options.styles[:something][:geometry]
end
should "return the right data for the style's format" do
assert_equal :png, @options.styles[:something][:format]
end
end
context "#styles is a proc" do
setup do
@attachment = MockAttachment.new("123x456", :doc)
@options = Paperclip::Options.new(@attachment,
:styles => lambda {|att|
{:something => {:geometry => att.one, :format => att.two}}
})
end
should "return the right data for the style's geometry" do
assert_equal "123x456", @options.styles[:something][:geometry]
end
should "return the right data for the style's format" do
assert_equal :doc, @options.styles[:something][:format]
end
should "run the proc each time, giving dynamic results" do
assert_equal :doc, @options.styles[:something][:format]
@attachment.two = :pdf
assert_equal :pdf, @options.styles[:something][:format]
end
end
context "#processors" do
setup do
@attachment = MockAttachment.new(nil, nil)
end
should "return processors if not a proc" do
@options = Paperclip::Options.new(@attachment, :processors => [:one])
assert_equal [:one], @options.processors
end
should "return processors if it is a proc" do
@options = Paperclip::Options.new(@attachment, :processors => lambda{|att| [att.one]})
assert_equal [nil], @options.processors
@attachment.one = :other
assert_equal [:other], @options.processors
end
end
end
require './test/helper'
class PaperclipMissingAttachmentStylesTest < Test::Unit::TestCase
context "Paperclip" do
setup do
Paperclip.classes_with_attachments = Set.new
end
teardown do
File.unlink(Paperclip.registered_attachments_styles_path) rescue nil
end
should "be able to keep list of models using it" do
assert_kind_of Set, Paperclip.classes_with_attachments
assert Paperclip.classes_with_attachments.empty?, 'list should be empty'
rebuild_model
assert_equal ['Dummy'].to_set, Paperclip.classes_with_attachments
end
should "enable to get and set path to registered styles file" do
assert_equal ROOT.join('public/system/paperclip_attachments.yml').to_s, Paperclip.registered_attachments_styles_path
Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml'
assert_equal '/tmp/config/paperclip_attachments.yml', Paperclip.registered_attachments_styles_path
Paperclip.registered_attachments_styles_path = nil
assert_equal ROOT.join('public/system/paperclip_attachments.yml').to_s, Paperclip.registered_attachments_styles_path
end
should "be able to get current attachment styles" do
assert_equal Hash.new, Paperclip.send(:current_attachments_styles)
rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
expected_hash = { :Dummy => {:avatar => [:big, :croppable]}}
assert_equal expected_hash, Paperclip.send(:current_attachments_styles)
end
should "be able to save current attachment styles for further comparison" do
rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
Paperclip.save_current_attachments_styles!
expected_hash = { :Dummy => {:avatar => [:big, :croppable]}}
assert_equal expected_hash, YAML.load_file(Paperclip.registered_attachments_styles_path)
end
should "be able to read registered attachment styles from file" do
rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
Paperclip.save_current_attachments_styles!
expected_hash = { :Dummy => {:avatar => [:big, :croppable]}}
assert_equal expected_hash, Paperclip.send(:get_registered_attachments_styles)
end
should "be able to calculate differences between registered styles and current styles" do
rebuild_model :styles => {:croppable => '600x600>', :big => '1000x1000>'}
Paperclip.save_current_attachments_styles!
rebuild_model :styles => {:thumb => 'x100', :export => 'x400>', :croppable => '600x600>', :big => '1000x1000>'}
expected_hash = { :Dummy => {:avatar => [:export, :thumb]} }
assert_equal expected_hash, Paperclip.missing_attachments_styles
ActiveRecord::Base.connection.create_table :books, :force => true
class ::Book < ActiveRecord::Base
has_attached_file :cover, :styles => {:small => 'x100', :large => '1000x1000>'}
has_attached_file :sample, :styles => {:thumb => 'x100'}
end
expected_hash = {
:Dummy => {:avatar => [:export, :thumb]},
:Book => {:sample => [:thumb], :cover => [:large, :small]}
}
assert_equal expected_hash, Paperclip.missing_attachments_styles
Paperclip.save_current_attachments_styles!
assert_equal Hash.new, Paperclip.missing_attachments_styles
end
# It's impossible to build styles hash without loading from database whole bunch of records
should "skip lambda-styles" do
rebuild_model :styles => lambda{ |attachment| attachment.instance.other == 'a' ? {:thumb => "50x50#"} : {:large => "400x400"} }
assert_equal Hash.new, Paperclip.send(:current_attachments_styles)
end
end
end
......@@ -3,43 +3,32 @@ require './test/helper'
class PaperclipTest < Test::Unit::TestCase
context "Calling Paperclip.run" do
setup do
Paperclip.options[:image_magick_path] = nil
Paperclip.options[:command_path] = nil
Paperclip::CommandLine.stubs(:'`')
Paperclip.options[:log_command] = false
Cocaine::CommandLine.expects(:new).with("convert", "stuff", {}).returns(stub(:run))
@original_command_line_path = Cocaine::CommandLine.path
end
should "execute the right command with :image_magick_path" do
Paperclip.options[:image_magick_path] = "/usr/bin"
Paperclip.expects(:log).with(includes('[DEPRECATION]'))
Paperclip.expects(:log).with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]}))
Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]}))
Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg")
end
should "execute the right command with :command_path" do
Paperclip.options[:command_path] = "/usr/bin"
Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{/usr/bin/convert ['"]one.jpg['"] ['"]two.jpg['"]}))
Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg")
teardown do
Paperclip.options[:log_command] = true
Cocaine::CommandLine.path = @original_command_line_path
end
should "execute the right command with no path" do
Paperclip::CommandLine.expects(:"`").with(regexp_matches(%r{convert ['"]one.jpg['"] ['"]two.jpg['"]}))
Paperclip.run("convert", ":one :two", :one => "one.jpg", :two => "two.jpg")
should "run the command with Cocaine" do
Paperclip.run("convert", "stuff")
end
should "tell you the command isn't there if the shell returns 127" do
with_exitstatus_returning(127) do
assert_raises(Paperclip::CommandNotFoundError) do
Paperclip.run("command")
end
should "save Cocaine::CommandLine.path that set before" do
Cocaine::CommandLine.path = "/opt/my_app/bin"
Paperclip.run("convert", "stuff")
assert_equal [Cocaine::CommandLine.path].flatten.include?("/opt/my_app/bin"), true
end
end
should "tell you the command isn't there if an ENOENT is raised" do
assert_raises(Paperclip::CommandNotFoundError) do
Paperclip::CommandLine.stubs(:"`").raises(Errno::ENOENT)
Paperclip.run("command")
end
context "Calling Paperclip.run with a logger" do
should "pass the defined logger if :log_command is set" do
Paperclip.options[:log_command] = true
Cocaine::CommandLine.expects(:new).with("convert", "stuff", :logger => Paperclip.logger).returns(stub(:run))
Paperclip.run("convert", "stuff")
end
end
......@@ -60,10 +49,6 @@ class PaperclipTest < Test::Unit::TestCase
end
end
should "raise when sent #processor and the name of a class that exists but isn't a subclass of Processor" do
assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) }
end
should "raise when sent #processor and the name of a class that doesn't exist" do
assert_raises(NameError){ Paperclip.processor(:boogey_man) }
end
......@@ -72,6 +57,37 @@ class PaperclipTest < Test::Unit::TestCase
assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail)
end
should "get a class from a namespaced class name" do
class ::One; class Two; end; end
assert_equal ::One::Two, Paperclip.class_for("One::Two")
end
should "raise when class doesn't exist in specified namespace" do
class ::Three; end
class ::Four; end
assert_raise NameError do
Paperclip.class_for("Three::Four")
end
end
context "Attachments with clashing URLs should raise error" do
setup do
class Dummy2 < ActiveRecord::Base
include Paperclip::Glue
end
end
should "generate warning if attachment is redefined with the same url string" do
Paperclip.expects(:log).with("Duplicate URL for blah with /system/:attachment/:id/:style/:filename. This will clash with attachment defined in Dummy class")
Dummy.class_eval do
has_attached_file :blah
end
Dummy2.class_eval do
has_attached_file :blah
end
end
end
context "An ActiveRecord model with an 'avatar' attachment" do
setup do
rebuild_model :path => "tmp/:class/omg/:style.:extension"
......@@ -223,7 +239,7 @@ class PaperclipTest < Test::Unit::TestCase
@dummy.valid?
end
should "not have an error when assigned a valid file" do
assert_equal 0, @dummy.errors.length, @dummy.errors.full_messages.join(", ")
assert_equal 0, @dummy.errors.size, @dummy.errors.full_messages.join(", ")
end
end
context "and assigned an invalid file" do
......@@ -232,7 +248,7 @@ class PaperclipTest < Test::Unit::TestCase
@dummy.valid?
end
should "have an error when assigned a valid file" do
assert @dummy.errors.length > 0
assert @dummy.errors.size > 0
end
end
end
......@@ -298,4 +314,26 @@ class PaperclipTest < Test::Unit::TestCase
end
end
context "configuring a custom processor" do
setup do
@freedom_processor = Class.new do
def make(file, options = {}, attachment = nil)
file
end
end.new
Paperclip.configure do |config|
config.register_processor(:freedom, @freedom_processor)
end
end
should "be able to find the custom processor" do
assert_equal @freedom_processor, Paperclip.processor(:freedom)
end
teardown do
Paperclip.clear_processors!
end
end
end
require './test/helper'
class FileSystemTest < Test::Unit::TestCase
context "Filesystem" do
setup do
rebuild_model :styles => { :thumbnail => "25x25#" }
@dummy = Dummy.create!
@dummy.avatar = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "5k.png"))
end
should "allow file assignment" do
assert @dummy.save
end
should "store the original" do
@dummy.save
assert File.exists?(@dummy.avatar.path)
end
should "store the thumbnail" do
@dummy.save
assert File.exists?(@dummy.avatar.path(:thumbnail))
end
should "clean up file objects" do
File.stubs(:exist?).returns(true)
Paperclip::Tempfile.any_instance.expects(:close).at_least_once()
Paperclip::Tempfile.any_instance.expects(:unlink).at_least_once()
@dummy.save!
end
context "with file that has space in file name" do
setup do
rebuild_model :styles => { :thumbnail => "25x25#" }
@dummy = Dummy.create!
@dummy.avatar = File.open(File.join(File.dirname(__FILE__), "..", "fixtures", "spaced file.png"))
@dummy.save
end
should "store the file" do
assert File.exists?(@dummy.avatar.path)
end
should "store the path unescaped" do
assert_match /\/spaced file\.png/, @dummy.avatar.path
end
should "return an escaped version of URL" do
assert_match /\/spaced%20file\.png/, @dummy.avatar.url
end
end
end
end
require './test/helper'
require 'aws/s3'
unless ENV["S3_TEST_BUCKET"].blank?
class S3LiveTest < Test::Unit::TestCase
context "Using S3 for real, an attachment with S3 storage" do
setup do
rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
:storage => :s3,
:bucket => ENV["S3_TEST_BUCKET"],
:path => ":class/:attachment/:id/:style.:extension",
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "..", "s3.yml"))
Dummy.delete_all
@dummy = Dummy.new
end
should "be extended by the S3 module" do
assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3)
end
context "when assigned" do
setup do
@file = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', '5k.png'), 'rb')
@dummy.avatar = @file
end
teardown do
@file.close
@dummy.destroy
end
should "still return a Tempfile when sent #to_file" do
assert_equal Paperclip::Tempfile, @dummy.avatar.to_file.class
end
context "and saved" do
setup do
@dummy.save
end
should "be on S3" do
assert true
end
should "generate a tempfile with the right name" do
file = @dummy.avatar.to_file
assert_match /^original.*\.png$/, File.basename(file.path)
end
end
end
end
context "An attachment that uses S3 for storage and has spaces in file name" do
setup do
rebuild_model :styles => { :thumb => "100x100", :square => "32x32#" },
:storage => :s3,
:bucket => ENV["S3_TEST_BUCKET"],
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "..", "s3.yml"))
Dummy.delete_all
@dummy = Dummy.new
@dummy.avatar = File.new(File.join(File.dirname(__FILE__), '..', 'fixtures', 'spaced file.png'), 'rb')
@dummy.save
end
teardown { @dummy.destroy }
should "return an unescaped version for path" do
assert_match /.+\/spaced file\.png/, @dummy.avatar.path
end
should "return an escaped version for url" do
assert_match /.+\/spaced%20file\.png/, @dummy.avatar.url
end
should "be accessible" do
assert_match /200 OK/, `curl -I #{@dummy.avatar.url}`
end
should "be destoryable" do
url = @dummy.avatar.url
@dummy.destroy
assert_match /404 Not Found/, `curl -I #{url}`
end
end
end
end
......@@ -6,8 +6,9 @@ class StyleTest < Test::Unit::TestCase
context "A style rule" do
setup do
@attachment = attachment :path => ":basename.:extension",
:styles => { :foo => {:geometry => "100x100#", :format => :png} }
@style = @attachment.styles[:foo]
:styles => { :foo => {:geometry => "100x100#", :format => :png} },
:whiny => true
@style = @attachment.options.styles[:foo]
end
should "be held as a Style object" do
......@@ -23,7 +24,6 @@ class StyleTest < Test::Unit::TestCase
end
should "be whiny if the attachment is" do
@attachment.expects(:whiny).returns(true)
assert @style.whiny?
end
......@@ -46,52 +46,49 @@ class StyleTest < Test::Unit::TestCase
}
end
should "defer processing of procs until they are needed" do
assert_kind_of Proc, @attachment.styles[:foo].instance_variable_get("@geometry")
assert_kind_of Proc, @attachment.styles[:bar].instance_variable_get("@geometry")
assert_kind_of Proc, @attachment.instance_variable_get("@processors")
end
should "call procs when they are needed" do
assert_equal "300x300#", @attachment.styles[:foo].geometry
assert_equal "300x300#", @attachment.styles[:bar].geometry
assert_equal [:test], @attachment.styles[:foo].processors
assert_equal [:test], @attachment.styles[:bar].processors
assert_equal "300x300#", @attachment.options.styles[:foo].geometry
assert_equal "300x300#", @attachment.options.styles[:bar].geometry
assert_equal [:test], @attachment.options.styles[:foo].processors
assert_equal [:test], @attachment.options.styles[:bar].processors
end
end
context "An attachment with style rules in various forms" do
setup do
styles = ActiveSupport::OrderedHash.new
styles[:aslist] = ["100x100", :png]
styles[:ashash] = {:geometry => "100x100", :format => :png}
styles[:asstring] = "100x100"
@attachment = attachment :path => ":basename.:extension",
:styles => {
:aslist => ["100x100", :png],
:ashash => {:geometry => "100x100", :format => :png},
:asstring => "100x100"
}
:styles => styles
end
should "have the right number of styles" do
assert_kind_of Hash, @attachment.styles
assert_equal 3, @attachment.styles.size
assert_kind_of Hash, @attachment.options.styles
assert_equal 3, @attachment.options.styles.size
end
should "have styles as Style objects" do
[:aslist, :ashash, :aslist].each do |s|
assert_kind_of Paperclip::Style, @attachment.styles[s]
assert_kind_of Paperclip::Style, @attachment.options.styles[s]
end
end
should "have the right geometries" do
[:aslist, :ashash, :aslist].each do |s|
assert_equal @attachment.styles[s].geometry, "100x100"
assert_equal @attachment.options.styles[s].geometry, "100x100"
end
end
should "have the right formats" do
assert_equal @attachment.styles[:aslist].format, :png
assert_equal @attachment.styles[:ashash].format, :png
assert_nil @attachment.styles[:asstring].format
assert_equal @attachment.options.styles[:aslist].format, :png
assert_equal @attachment.options.styles[:ashash].format, :png
assert_nil @attachment.options.styles[:asstring].format
end
should "retain order" do
assert_equal [:aslist, :ashash, :asstring], @attachment.options.styles.keys
end
end
context "An attachment with :convert_options" do
......@@ -99,7 +96,7 @@ class StyleTest < Test::Unit::TestCase
@attachment = attachment :path => ":basename.:extension",
:styles => {:thumb => "100x100", :large => "400x400"},
:convert_options => {:all => "-do_stuff", :thumb => "-thumbnailize"}
@style = @attachment.styles[:thumb]
@style = @attachment.options.styles[:thumb]
@file = StringIO.new("...")
@file.stubs(:original_filename).returns("file.jpg")
end
......@@ -110,7 +107,27 @@ class StyleTest < Test::Unit::TestCase
should "call extra_options_for(:thumb/:large) when convert options are requested" do
@attachment.expects(:extra_options_for).with(:thumb)
@attachment.styles[:thumb].convert_options
@attachment.options.styles[:thumb].convert_options
end
end
context "An attachment with :source_file_options" do
setup do
@attachment = attachment :path => ":basename.:extension",
:styles => {:thumb => "100x100", :large => "400x400"},
:source_file_options => {:all => "-density 400", :thumb => "-depth 8"}
@style = @attachment.options.styles[:thumb]
@file = StringIO.new("...")
@file.stubs(:original_filename).returns("file.jpg")
end
before_should "not have called extra_source_file_options_for(:thumb/:large) on initialization" do
@attachment.expects(:extra_source_file_options_for).never
end
should "call extra_options_for(:thumb/:large) when convert options are requested" do
@attachment.expects(:extra_source_file_options_for).with(:thumb)
@attachment.options.styles[:thumb].source_file_options
end
end
......@@ -125,7 +142,7 @@ class StyleTest < Test::Unit::TestCase
}
},
:processors => [:thumbnail]
@style = @attachment.styles[:foo]
@style = @attachment.options.styles[:foo]
end
should "not get processors from the attachment" do
......@@ -138,4 +155,26 @@ class StyleTest < Test::Unit::TestCase
end
end
context "A style rule with :processors supplied as procs" do
setup do
@attachment = attachment :path => ":basename.:extension",
:styles => {
:foo => {
:geometry => "100x100#",
:format => :png,
:processors => lambda{|a| [:test]}
}
},
:processors => [:thumbnail]
end
should "defer processing of procs until they are needed" do
assert_kind_of Proc, @attachment.options.styles[:foo].instance_variable_get("@processors")
end
should "call procs when they are needed" do
assert_equal [:test], @attachment.options.styles[:foo].processors
end
end
end
......@@ -73,6 +73,18 @@ class ThumbnailTest < Test::Unit::TestCase
@thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x50#")
end
should "let us know when a command isn't found versus a processing error" do
old_path = ENV['PATH']
begin
ENV['PATH'] = ''
assert_raises(Paperclip::CommandNotFoundError) do
@thumb.make
end
ensure
ENV['PATH'] = old_path
end
end
should "report its correct current and target geometries" do
assert_equal "100x50#", @thumb.target_geometry.to_s
assert_equal "434x66", @thumb.current_geometry.to_s
......@@ -90,9 +102,15 @@ class ThumbnailTest < Test::Unit::TestCase
assert_equal nil, @thumb.convert_options
end
should "have source_file_options set to nil by default" do
assert_equal nil, @thumb.source_file_options
end
should "send the right command to convert when sent #make" do
Paperclip::CommandLine.expects(:"`").with do |arg|
arg.match %r{convert ["']#{File.expand_path(@thumb.file.path)}\[0\]["'] -resize ["']x50["'] -crop ["']100x50\+114\+0["'] \+repage ["'].*?["']}
Paperclip.expects(:run).with do |*arg|
arg[0] == 'convert' &&
arg[1] == ':source -resize "x50" -crop "100x50+114+0" +repage :dest' &&
arg[2][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end
@thumb.make
end
......@@ -115,8 +133,10 @@ class ThumbnailTest < Test::Unit::TestCase
end
should "send the right command to convert when sent #make" do
Paperclip::CommandLine.expects(:"`").with do |arg|
arg.match %r{convert -strip ["']#{File.expand_path(@thumb.file.path)}\[0\]["'] -resize ["']x50["'] -crop ["']100x50\+114\+0["'] \+repage ["'].*?["']}
Paperclip.expects(:run).with do |*arg|
arg[0] == 'convert' &&
arg[1] == '-strip :source -resize "x50" -crop "100x50+114+0" +repage :dest' &&
arg[2][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end
@thumb.make
end
......@@ -153,8 +173,10 @@ class ThumbnailTest < Test::Unit::TestCase
end
should "send the right command to convert when sent #make" do
Paperclip::CommandLine.expects(:"`").with do |arg|
arg.match %r{convert ["']#{File.expand_path(@thumb.file.path)}\[0\]["'] -resize ["']x50["'] -crop ["']100x50\+114\+0["'] \+repage -strip -depth 8 ["'].*?["']}
Paperclip.expects(:run).with do |*arg|
arg[0] == 'convert' &&
arg[1] == ':source -resize "x50" -crop "100x50+114+0" +repage -strip -depth 8 :dest' &&
arg[2][:source] == "#{File.expand_path(@thumb.file.path)}[0]"
end
@thumb.make
end
......@@ -176,6 +198,18 @@ class ThumbnailTest < Test::Unit::TestCase
@thumb.make
end
end
should "let us know when a command isn't found versus a processing error" do
old_path = ENV['PATH']
begin
ENV['PATH'] = ''
assert_raises(Paperclip::CommandNotFoundError) do
@thumb.make
end
ensure
ENV['PATH'] = old_path
end
end
end
end
......@@ -190,6 +224,53 @@ class ThumbnailTest < Test::Unit::TestCase
assert !@thumb.transformation_command.include?("-resize")
end
end
context "passing a custom file geometry parser" do
should "produce the appropriate transformation_command" do
GeoParser = Class.new do
def self.from_file(file)
new
end
def transformation_to(target, should_crop)
["SCALE", "CROP"]
end
end
thumb = Paperclip::Thumbnail.new(@file, :geometry => '50x50', :file_geometry_parser => GeoParser)
transformation_command = thumb.transformation_command
assert transformation_command.include?('-crop'),
%{expected #{transformation_command.inspect} to include '-crop'}
assert transformation_command.include?('"CROP"'),
%{expected #{transformation_command.inspect} to include '"CROP"'}
assert transformation_command.include?('-resize'),
%{expected #{transformation_command.inspect} to include '-resize'}
assert transformation_command.include?('"SCALE"'),
%{expected #{transformation_command.inspect} to include '"SCALE"'}
end
end
context "passing a custom geometry string parser" do
should "produce the appropriate transformation_command" do
GeoParser = Class.new do
def self.parse(s)
new
end
def to_s
"151x167"
end
end
thumb = Paperclip::Thumbnail.new(@file, :geometry => '50x50', :string_geometry_parser => GeoParser)
transformation_command = thumb.transformation_command
assert transformation_command.include?('"151x167"'),
%{expected #{transformation_command.inspect} to include '151x167'}
end
end
end
context "A multipage PDF" do
......@@ -224,4 +305,79 @@ class ThumbnailTest < Test::Unit::TestCase
end
end
end
context "An animated gif" do
setup do
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "animated.gif"), 'rb')
end
teardown { @file.close }
should "start with 12 frames with size 100x100" do
cmd = %Q[identify -format "%wx%h" "#{@file.path}"]
assert_equal "100x100"*12, `#{cmd}`.chomp
end
context "with static output" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, :geometry => "50x50", :format => :jpg)
end
should "create the single frame thumbnail when sent #make" do
dst = @thumb.make
cmd = %Q[identify -format "%wx%h" "#{dst.path}"]
assert_equal "50x50", `#{cmd}`.chomp
end
end
context "with animated output format" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, :geometry => "50x50", :format => :gif)
end
should "create the 12 frames thumbnail when sent #make" do
dst = @thumb.make
cmd = %Q[identify -format "%wx%h" "#{dst.path}"]
assert_equal "50x50"*12, `#{cmd}`.chomp
end
should "use the -coalesce option" do
assert_equal @thumb.transformation_command.first, "-coalesce"
end
end
context "with omitted output format" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, :geometry => "50x50")
end
should "create the 12 frames thumbnail when sent #make" do
dst = @thumb.make
cmd = %Q[identify -format "%wx%h" "#{dst.path}"]
assert_equal "50x50"*12, `#{cmd}`.chomp
end
should "use the -coalesce option" do
assert_equal @thumb.transformation_command.first, "-coalesce"
end
end
context "with animated option set to false" do
setup do
@thumb = Paperclip::Thumbnail.new(@file, :geometry => "50x50", :animated => false)
end
should "output the gif format" do
dst = @thumb.make
cmd = %Q[identify "#{dst.path}"]
assert_match /GIF/, `#{cmd}`.chomp
end
should "create the single frame thumbnail when sent #make" do
dst = @thumb.make
cmd = %Q[identify -format "%wx%h" "#{dst.path}"]
assert_equal "50x50", `#{cmd}`.chomp
end
end
end
end
......@@ -6,12 +6,13 @@ class UpfileTest < Test::Unit::TestCase
%w(png) => 'image/png',
%w(gif) => 'image/gif',
%w(bmp) => 'image/bmp',
%w(svg) => 'image/svg+xml',
%w(txt) => 'text/plain',
%w(htm html) => 'text/html',
%w(csv) => 'text/csv',
%w(xml) => 'text/xml',
%w(xml) => 'application/xml',
%w(css) => 'text/css',
%w(js) => 'application/js',
%w(js) => 'application/javascript',
%w(foo) => 'application/x-foo'
}.each do |extensions, content_type|
extensions.each do |extension|
......@@ -33,4 +34,20 @@ class UpfileTest < Test::Unit::TestCase
end
assert_equal 'text/plain', file.content_type
end
{ '5k.png' => 'image/png',
'animated.gif' => 'image/gif',
'text.txt' => 'text/plain',
'twopage.pdf' => 'application/pdf'
}.each do |filename, content_type|
should "return a content type of #{content_type} from a file command for file #{filename}" do
file = File.new(File.join(File.dirname(__FILE__), "fixtures", filename))
class << file
include Paperclip::Upfile
end
assert_equal content_type, file.type_from_file_command
end
end
end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment