From cf1b60e0cfa7d1a8f4a80d686649cc12e73a634e Mon Sep 17 00:00:00 2001 From: hukl Date: Fri, 24 Apr 2009 11:43:08 +0200 Subject: Integrated basic Asset upload functionality. You can upload files now and use their url in pages. --- .gitignore | 5 +- app/controllers/assets_controller.rb | 88 +++ app/helpers/assets_helper.rb | 2 + app/models/asset.rb | 10 + app/views/admin/_menu.html.erb | 1 + app/views/assets/edit.html.erb | 12 + app/views/assets/index.html.erb | 20 + app/views/assets/new.html.erb | 17 + app/views/assets/show.html.erb | 10 + app/views/layouts/assets.html.erb | 17 + config/routes.rb | 2 + db/migrate/20090424085851_create_assets.rb | 16 + test/fixtures/assets.yml | 7 + test/functional/assets_controller_test.rb | 45 ++ test/unit/asset_test.rb | 8 + test/unit/helpers/assets_helper_test.rb | 4 + vendor/plugins/paperclip/LICENSE | 26 + vendor/plugins/paperclip/README.rdoc | 172 +++++ vendor/plugins/paperclip/Rakefile | 77 +++ .../plugins/paperclip/generators/paperclip/USAGE | 5 + .../generators/paperclip/paperclip_generator.rb | 27 + .../paperclip/templates/paperclip_migration.rb.erb | 19 + vendor/plugins/paperclip/init.rb | 1 + vendor/plugins/paperclip/lib/paperclip.rb | 318 +++++++++ .../plugins/paperclip/lib/paperclip/attachment.rb | 403 +++++++++++ .../lib/paperclip/callback_compatability.rb | 33 + vendor/plugins/paperclip/lib/paperclip/geometry.rb | 115 ++++ vendor/plugins/paperclip/lib/paperclip/iostream.rb | 58 ++ vendor/plugins/paperclip/lib/paperclip/matchers.rb | 4 + .../matchers/have_attached_file_matcher.rb | 49 ++ .../validate_attachment_content_type_matcher.rb | 66 ++ .../validate_attachment_presence_matcher.rb | 48 ++ .../matchers/validate_attachment_size_matcher.rb | 83 +++ .../plugins/paperclip/lib/paperclip/processor.rb | 48 ++ vendor/plugins/paperclip/lib/paperclip/storage.rb | 236 +++++++ .../plugins/paperclip/lib/paperclip/thumbnail.rb | 70 ++ vendor/plugins/paperclip/lib/paperclip/upfile.rb | 48 ++ vendor/plugins/paperclip/paperclip.gemspec | 40 ++ .../plugins/paperclip/shoulda_macros/paperclip.rb | 68 ++ .../plugins/paperclip/tasks/paperclip_tasks.rake | 79 +++ vendor/plugins/paperclip/test/.gitignore | 1 + vendor/plugins/paperclip/test/attachment_test.rb | 742 +++++++++++++++++++++ vendor/plugins/paperclip/test/fixtures/12k.png | Bin 0 -> 12093 bytes vendor/plugins/paperclip/test/fixtures/50x50.png | Bin 0 -> 1615 bytes vendor/plugins/paperclip/test/fixtures/5k.png | Bin 0 -> 4456 bytes vendor/plugins/paperclip/test/fixtures/bad.png | 1 + vendor/plugins/paperclip/test/fixtures/text.txt | 0 vendor/plugins/paperclip/test/fixtures/twopage.pdf | Bin 0 -> 8775 bytes vendor/plugins/paperclip/test/geometry_test.rb | 168 +++++ vendor/plugins/paperclip/test/helper.rb | 82 +++ vendor/plugins/paperclip/test/integration_test.rb | 481 +++++++++++++ vendor/plugins/paperclip/test/iostream_test.rb | 71 ++ .../matchers/have_attached_file_matcher_test.rb | 21 + ...alidate_attachment_content_type_matcher_test.rb | 30 + .../validate_attachment_presence_matcher_test.rb | 21 + .../validate_attachment_size_matcher_test.rb | 50 ++ vendor/plugins/paperclip/test/paperclip_test.rb | 233 +++++++ vendor/plugins/paperclip/test/processor_test.rb | 10 + vendor/plugins/paperclip/test/storage_test.rb | 277 ++++++++ vendor/plugins/paperclip/test/thumbnail_test.rb | 177 +++++ 60 files changed, 4721 insertions(+), 1 deletion(-) create mode 100644 app/controllers/assets_controller.rb create mode 100644 app/helpers/assets_helper.rb create mode 100644 app/models/asset.rb create mode 100644 app/views/assets/edit.html.erb create mode 100644 app/views/assets/index.html.erb create mode 100644 app/views/assets/new.html.erb create mode 100644 app/views/assets/show.html.erb create mode 100644 app/views/layouts/assets.html.erb create mode 100644 db/migrate/20090424085851_create_assets.rb create mode 100644 test/fixtures/assets.yml create mode 100644 test/functional/assets_controller_test.rb create mode 100644 test/unit/asset_test.rb create mode 100644 test/unit/helpers/assets_helper_test.rb create mode 100644 vendor/plugins/paperclip/LICENSE create mode 100644 vendor/plugins/paperclip/README.rdoc create mode 100644 vendor/plugins/paperclip/Rakefile create mode 100644 vendor/plugins/paperclip/generators/paperclip/USAGE create mode 100644 vendor/plugins/paperclip/generators/paperclip/paperclip_generator.rb create mode 100644 vendor/plugins/paperclip/generators/paperclip/templates/paperclip_migration.rb.erb create mode 100644 vendor/plugins/paperclip/init.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/attachment.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/callback_compatability.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/geometry.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/iostream.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/matchers.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/matchers/have_attached_file_matcher.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_presence_matcher.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_size_matcher.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/processor.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/storage.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/thumbnail.rb create mode 100644 vendor/plugins/paperclip/lib/paperclip/upfile.rb create mode 100644 vendor/plugins/paperclip/paperclip.gemspec create mode 100644 vendor/plugins/paperclip/shoulda_macros/paperclip.rb create mode 100644 vendor/plugins/paperclip/tasks/paperclip_tasks.rake create mode 100644 vendor/plugins/paperclip/test/.gitignore create mode 100644 vendor/plugins/paperclip/test/attachment_test.rb create mode 100644 vendor/plugins/paperclip/test/fixtures/12k.png create mode 100644 vendor/plugins/paperclip/test/fixtures/50x50.png create mode 100644 vendor/plugins/paperclip/test/fixtures/5k.png create mode 100644 vendor/plugins/paperclip/test/fixtures/bad.png create mode 100644 vendor/plugins/paperclip/test/fixtures/text.txt create mode 100644 vendor/plugins/paperclip/test/fixtures/twopage.pdf create mode 100644 vendor/plugins/paperclip/test/geometry_test.rb create mode 100644 vendor/plugins/paperclip/test/helper.rb create mode 100644 vendor/plugins/paperclip/test/integration_test.rb create mode 100644 vendor/plugins/paperclip/test/iostream_test.rb create mode 100644 vendor/plugins/paperclip/test/matchers/have_attached_file_matcher_test.rb create mode 100644 vendor/plugins/paperclip/test/matchers/validate_attachment_content_type_matcher_test.rb create mode 100644 vendor/plugins/paperclip/test/matchers/validate_attachment_presence_matcher_test.rb create mode 100644 vendor/plugins/paperclip/test/matchers/validate_attachment_size_matcher_test.rb create mode 100644 vendor/plugins/paperclip/test/paperclip_test.rb create mode 100644 vendor/plugins/paperclip/test/processor_test.rb create mode 100644 vendor/plugins/paperclip/test/storage_test.rb create mode 100644 vendor/plugins/paperclip/test/thumbnail_test.rb diff --git a/.gitignore b/.gitignore index b8d3ec2..ed05711 100644 --- a/.gitignore +++ b/.gitignore @@ -11,4 +11,7 @@ lib/chaos_calendar/Makefile lib/chaos_calendar/ical_occurrences.bundle lib/chaos_calendar/ical_occurrences.o lib/chaos_calendar/ical_occurrences_wrap.c -lib/chaos_calendar/ical_occurrences_wrap.o \ No newline at end of file +lib/chaos_calendar/ical_occurrences_wrap.o +db/authors.csv +public/system +vendor/plugins/paperclip/tmp diff --git a/app/controllers/assets_controller.rb b/app/controllers/assets_controller.rb new file mode 100644 index 0000000..4734a54 --- /dev/null +++ b/app/controllers/assets_controller.rb @@ -0,0 +1,88 @@ +class AssetsController < ApplicationController + + layout 'admin' + + # GET /assets + # GET /assets.xml + def index + @assets = Asset.all + + respond_to do |format| + format.html # index.html.erb + format.xml { render :xml => @assets } + end + end + + # GET /assets/1 + # GET /assets/1.xml + def show + @asset = Asset.find(params[:id]) + + respond_to do |format| + format.html # show.html.erb + format.xml { render :xml => @asset } + end + end + + # GET /assets/new + # GET /assets/new.xml + def new + @asset = Asset.new + + respond_to do |format| + format.html # new.html.erb + format.xml { render :xml => @asset } + end + end + + # GET /assets/1/edit + def edit + @asset = Asset.find(params[:id]) + end + + # POST /assets + # POST /assets.xml + def create + @asset = Asset.new(params[:asset]) + + respond_to do |format| + if @asset.save + flash[:notice] = 'Asset was successfully created.' + format.html { redirect_to(@asset) } + format.xml { render :xml => @asset, :status => :created, :location => @asset } + else + format.html { render :action => "new" } + format.xml { render :xml => @asset.errors, :status => :unprocessable_entity } + end + end + end + + # PUT /assets/1 + # PUT /assets/1.xml + def update + @asset = Asset.find(params[:id]) + + respond_to do |format| + if @asset.update_attributes(params[:asset]) + flash[:notice] = 'Asset was successfully updated.' + format.html { redirect_to(@asset) } + format.xml { head :ok } + else + format.html { render :action => "edit" } + format.xml { render :xml => @asset.errors, :status => :unprocessable_entity } + end + end + end + + # DELETE /assets/1 + # DELETE /assets/1.xml + def destroy + @asset = Asset.find(params[:id]) + @asset.destroy + + respond_to do |format| + format.html { redirect_to(assets_url) } + format.xml { head :ok } + end + end +end diff --git a/app/helpers/assets_helper.rb b/app/helpers/assets_helper.rb new file mode 100644 index 0000000..9824bb4 --- /dev/null +++ b/app/helpers/assets_helper.rb @@ -0,0 +1,2 @@ +module AssetsHelper +end diff --git a/app/models/asset.rb b/app/models/asset.rb new file mode 100644 index 0000000..c9c6296 --- /dev/null +++ b/app/models/asset.rb @@ -0,0 +1,10 @@ +class Asset < ActiveRecord::Base + has_attached_file( + :upload, + :styles => { + :normal => "450x450", + :medium => "300x300", + :thumb => "100x100", + } + ) +end diff --git a/app/views/admin/_menu.html.erb b/app/views/admin/_menu.html.erb index 8f48822..5ae4307 100644 --- a/app/views/admin/_menu.html.erb +++ b/app/views/admin/_menu.html.erb @@ -2,4 +2,5 @@ <%= link_to 'Overview', :controller => :admin, :action => 'index' %> <%= link_to 'Nodes', nodes_path, selected?('nodes') %> +<%= link_to 'Assets', assets_path, selected?('assets') %> <%= link_to 'User', users_path, selected?('users') %> > \ No newline at end of file diff --git a/app/views/assets/edit.html.erb b/app/views/assets/edit.html.erb new file mode 100644 index 0000000..d60db94 --- /dev/null +++ b/app/views/assets/edit.html.erb @@ -0,0 +1,12 @@ +

Editing asset

+ +<% form_for(@asset) do |f| %> + <%= f.error_messages %> + +

+ <%= f.submit 'Update' %> +

+<% end %> + +<%= link_to 'Show', @asset %> | +<%= link_to 'Back', assets_path %> \ No newline at end of file diff --git a/app/views/assets/index.html.erb b/app/views/assets/index.html.erb new file mode 100644 index 0000000..c9be684 --- /dev/null +++ b/app/views/assets/index.html.erb @@ -0,0 +1,20 @@ +<% content_for :subnavigation do %> + <%= link_to 'New asset', new_asset_path %> +<% end %> + + + + + + +<% @assets.each do |asset| %> + + + + + + + + +<% end %> +
<%= image_tag asset.upload.url(:thumb) %><%= link_to asset.name, asset.upload.url %><%= asset.upload.content_type %><%= link_to 'Show', asset %><%= link_to 'Edit', edit_asset_path(asset) %><%= link_to 'Destroy', asset, :confirm => 'Are you sure?', :method => :delete %>
\ No newline at end of file diff --git a/app/views/assets/new.html.erb b/app/views/assets/new.html.erb new file mode 100644 index 0000000..366488f --- /dev/null +++ b/app/views/assets/new.html.erb @@ -0,0 +1,17 @@ +

New asset

+ +<% form_for(@asset, :html => { :multipart => true }) do |f| %> + <%= f.error_messages %> + +

+ <%= f.label :name %>
+ <%= f.text_field :name %> +

+ +

+ <%= f.file_field :upload %> + <%= f.submit 'Create' %> +

+<% end %> + +<%= link_to 'Back', assets_path %> \ No newline at end of file diff --git a/app/views/assets/show.html.erb b/app/views/assets/show.html.erb new file mode 100644 index 0000000..dd657d3 --- /dev/null +++ b/app/views/assets/show.html.erb @@ -0,0 +1,10 @@ +<% content_for :subnavigation do %> + <%= link_to 'Edit', edit_asset_path(@asset) %> + <%= link_to 'Back', assets_path %> +<% end %> + + + +<%= image_tag @asset.upload.url(:medium) %>
+<%= @asset.upload.content_type %>
+<%= "#{@asset.upload.size/1024} KB" %>
\ No newline at end of file diff --git a/app/views/layouts/assets.html.erb b/app/views/layouts/assets.html.erb new file mode 100644 index 0000000..9d629c8 --- /dev/null +++ b/app/views/layouts/assets.html.erb @@ -0,0 +1,17 @@ + + + + + + Assets: <%= controller.action_name %> + <%= stylesheet_link_tag 'scaffold' %> + + + +

<%= flash[:notice] %>

+ +<%= yield %> + + + diff --git a/config/routes.rb b/config/routes.rb index 38f508a..e96ea13 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,4 +1,6 @@ ActionController::Routing::Routes.draw do |map| + map.resources :assets + map.filter :locale diff --git a/db/migrate/20090424085851_create_assets.rb b/db/migrate/20090424085851_create_assets.rb new file mode 100644 index 0000000..c7f1796 --- /dev/null +++ b/db/migrate/20090424085851_create_assets.rb @@ -0,0 +1,16 @@ +class CreateAssets < ActiveRecord::Migration + def self.up + create_table :assets do |t| + t.string :name + t.string :upload_file_name + t.string :upload_content_type + t.integer :upload_file_size + t.datetime :upload_updated_at + t.timestamps + end + end + + def self.down + drop_table :assets + end +end diff --git a/test/fixtures/assets.yml b/test/fixtures/assets.yml new file mode 100644 index 0000000..5bf0293 --- /dev/null +++ b/test/fixtures/assets.yml @@ -0,0 +1,7 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html + +# one: +# column: value +# +# two: +# column: value diff --git a/test/functional/assets_controller_test.rb b/test/functional/assets_controller_test.rb new file mode 100644 index 0000000..7b838cc --- /dev/null +++ b/test/functional/assets_controller_test.rb @@ -0,0 +1,45 @@ +require 'test_helper' + +class AssetsControllerTest < ActionController::TestCase + test "should get index" do + get :index + assert_response :success + assert_not_nil assigns(:assets) + end + + test "should get new" do + get :new + assert_response :success + end + + test "should create asset" do + assert_difference('Asset.count') do + post :create, :asset => { } + end + + assert_redirected_to asset_path(assigns(:asset)) + end + + test "should show asset" do + get :show, :id => assets(:one).to_param + assert_response :success + end + + test "should get edit" do + get :edit, :id => assets(:one).to_param + assert_response :success + end + + test "should update asset" do + put :update, :id => assets(:one).to_param, :asset => { } + assert_redirected_to asset_path(assigns(:asset)) + end + + test "should destroy asset" do + assert_difference('Asset.count', -1) do + delete :destroy, :id => assets(:one).to_param + end + + assert_redirected_to assets_path + end +end diff --git a/test/unit/asset_test.rb b/test/unit/asset_test.rb new file mode 100644 index 0000000..58aba76 --- /dev/null +++ b/test/unit/asset_test.rb @@ -0,0 +1,8 @@ +require 'test_helper' + +class AssetTest < ActiveSupport::TestCase + # Replace this with your real tests. + test "the truth" do + assert true + end +end diff --git a/test/unit/helpers/assets_helper_test.rb b/test/unit/helpers/assets_helper_test.rb new file mode 100644 index 0000000..ae50bff --- /dev/null +++ b/test/unit/helpers/assets_helper_test.rb @@ -0,0 +1,4 @@ +require 'test_helper' + +class AssetsHelperTest < ActionView::TestCase +end diff --git a/vendor/plugins/paperclip/LICENSE b/vendor/plugins/paperclip/LICENSE new file mode 100644 index 0000000..299b9ed --- /dev/null +++ b/vendor/plugins/paperclip/LICENSE @@ -0,0 +1,26 @@ + +LICENSE + +The MIT License + +Copyright (c) 2008 Jon Yurek and thoughtbot, inc. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. + + diff --git a/vendor/plugins/paperclip/README.rdoc b/vendor/plugins/paperclip/README.rdoc new file mode 100644 index 0000000..85fcf4b --- /dev/null +++ b/vendor/plugins/paperclip/README.rdoc @@ -0,0 +1,172 @@ +=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 +much like other attributes as possible. This means they aren't saved to their +final locations on disk, nor are they deleted if set to nil, until +ActiveRecord::Base#save is called. It manages validations based on size and +presence, if required. It can transform its assigned image into thumbnails if +needed, and the prerequisites are as simple as installing ImageMagick (which, +for most modern Unix-based systems, is as easy as installing the right +packages). Attached files are saved to the filesystem and referenced in the +browser by an easily understandable specification, which has sensible and +useful defaults. + +See the documentation for +has_attached_file+ in Paperclip::ClassMethods for +more detailed options. + +==Quick Start + +In your model: + + class User < ActiveRecord::Base + has_attached_file :avatar, :styles => { :medium => "300x300>", :thumb => "100x100>" } + end + +In your migrations: + + class AddAvatarColumnsToUser < ActiveRecord::Migration + def self.up + add_column :users, :avatar_file_name, :string + add_column :users, :avatar_content_type, :string + add_column :users, :avatar_file_size, :integer + add_column :users, :avatar_updated_at, :datetime + end + + def self.down + remove_column :users, :avatar_file_name + remove_column :users, :avatar_content_type + remove_column :users, :avatar_file_size + remove_column :users, :avatar_updated_at + end + end + +In your edit and new views: + + <% form_for :user, @user, :url => user_path, :html => { :multipart => true } do |form| %> + <%= form.file_field :avatar %> + <% end %> + +In your controller: + + def create + @user = User.create( params[:user] ) + end + +In your show view: + + <%= image_tag @user.avatar.url %> + <%= image_tag @user.avatar.url(:medium) %> + <%= image_tag @user.avatar.url(:thumb) %> + +==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 +friendly front end. The attributes are _file_name, +_file_size, _content_type, and _updated_at. +Only _file_name is required for paperclip to operate. More +information about the options to has_attached_file is available in the +documentation of Paperclip::ClassMethods. + +Attachments can be validated with Paperclip's validation methods, +validates_attachment_presence, validates_attachment_content_type, and +validates_attachment_size. + +==Storage + +The files that are assigned as attachments are, by default, placed in the +directory specified by the :path option to has_attached_file. By default, this +location is +":rails_root/public/system/:attachment/:id/:style/:basename.:extension". This +location was chosen because on standard Capistrano deployments, the +public/system directory is symlinked to the app's shared directory, meaning it +will survive between deployments. For example, using that :path, you may have a +file at + + /data/myapp/releases/20081229172410/public/system/avatars/13/small/my_pic.png + +NOTE: This is a change from previous versions of Paperclip, but is overall a +safer choice for the defaul file store. + +You may also choose to store your files using Amazon's S3 service. You can find +more information about S3 storage at the description for +Paperclip::Storage::S3. + +Files on the local filesystem (and in the Rails app's public directory) will be +available to the internet at large. If you require access control, it's +possible to place your files in a different location. You will need to change +both the :path and :url options in order to make sure the files are unavailable +to the public. Both :path and :url allow the same set of interpolated +variables. + +==Post Processing + +Paperclip supports an extendible selection of post-processors. When you define +a set of styles for an attachment, by default it is expected that those +"styles" are actually "thumbnails". However, you can do more than just +thumbnail images. By defining a subclass of Paperclip::Processor, you can +perform any processing you want on the files that are attached. Any file in +your Rails app's lib/paperclip_processors directory is automatically loaded by +paperclip, allowing you to easily define custom processors. You can specify a +processor with the :processors option to has_attached_file: + + has_attached_file :scan, :styles => { :text => { :quality => :better } }, + :processors => [:ocr] + +This would load the hypothetical class Paperclip::Ocr, which would have the +hash "{ :quality => :better }" passed to it along with the uploaded file. For +more information about defining processors, see Paperclip::Processor. + +The default processor is Paperclip::Thumbnail. For backwards compatability +reasons, you can pass a single geometry string or an array containing a +geometry and a format, which the file will be converted to, like so: + + has_attached_file :avatar, :styles => { :thumb => ["32x32#", :png] } + +This will convert the "thumb" style to a 32x32 square in png format, regardless +of what was uploaded. If the format is not specified, it is kept the same (i.e. +jpgs will remain jpgs). + +Multiple processors can be specified, and they will be invoked in the order +they are defined in the :processors array. Each successive processor will +be given the result of the previous processor's execution. All processors will +receive the same parameters, which are what you define in the :styles hash. +For example, assuming we had this definition: + + has_attached_file :scan, :styles => { :text => { :quality => :better } }, + :processors => [:rotator, :ocr] + +then both the :rotator processor and the :ocr processor would receive the +options "{ :quality => :better }". This parameter may not mean anything to one +or more or the processors, and they are free to ignore it. + +==Events + +Before and after the Post Processing step, Paperclip calls back to the model +with a few callbacks, allowing the model to change or cancel the processing +step. The callbacks are "before_post_process" and "after_post_process" (which +are called before and after the processing of each attachment), and the +attachment-specific "before__post_process" and +"after__post_process". The callbacks are intended to be as close to +normal ActiveRecord callbacks as possible, so if you return false (specifically +- returning nil is not the same) in a before_ filter, the post processing step +will halt. Returning false in an after_ filter will not halt anything, but you +can access the model and the attachment if necessary. + +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. + +==Contributing + +If you'd like to contribute a feature or bugfix: Thanks! To make sure your +fix/feature has a high chance of being included, please read the following +guidelines: + +1. Ask on the mailing list, or post a ticket in Lighthouse. +2. Make sure there are tests! We will not accept any patch that is not tested. + 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. diff --git a/vendor/plugins/paperclip/Rakefile b/vendor/plugins/paperclip/Rakefile new file mode 100644 index 0000000..91f1687 --- /dev/null +++ b/vendor/plugins/paperclip/Rakefile @@ -0,0 +1,77 @@ +require 'rake' +require 'rake/testtask' +require 'rake/rdoctask' + +$LOAD_PATH << File.join(File.dirname(__FILE__), 'lib') +require 'paperclip' + +desc 'Default: run unit tests.' +task :default => [:clean, :test] + +desc 'Test the paperclip plugin.' +Rake::TestTask.new(:test) do |t| + t.libs << 'lib' << 'profile' + t.pattern = 'test/**/*_test.rb' + t.verbose = true +end + +desc 'Start an IRB session with all necessary files required.' +task :shell do |t| + chdir File.dirname(__FILE__) + exec 'irb -I lib/ -I lib/paperclip -r rubygems -r active_record -r tempfile -r init' +end + +desc 'Generate documentation for the paperclip plugin.' +Rake::RDocTask.new(:rdoc) do |rdoc| + rdoc.rdoc_dir = 'doc' + rdoc.title = 'Paperclip' + rdoc.options << '--line-numbers' << '--inline-source' + rdoc.rdoc_files.include('README*') + rdoc.rdoc_files.include('lib/**/*.rb') +end + +desc 'Update documentation on website' +task :sync_docs => 'rdoc' do + `rsync -ave ssh doc/ dev@dev.thoughtbot.com:/home/dev/www/dev.thoughtbot.com/paperclip` +end + +desc 'Clean up files.' +task :clean do |t| + FileUtils.rm_rf "doc" + FileUtils.rm_rf "tmp" + FileUtils.rm_rf "pkg" + FileUtils.rm "test/debug.log" rescue nil + FileUtils.rm "test/paperclip.db" rescue nil +end + +spec = Gem::Specification.new do |s| + s.name = "paperclip" + s.version = Paperclip::VERSION + s.author = "Jon Yurek" + s.email = "jyurek@thoughtbot.com" + s.homepage = "http://www.thoughtbot.com/projects/paperclip" + s.platform = Gem::Platform::RUBY + s.summary = "File attachments as attributes for ActiveRecord" + s.files = FileList["README*", + "LICENSE", + "Rakefile", + "init.rb", + "{generators,lib,tasks,test,shoulda_macros}/**/*"].to_a + s.require_path = "lib" + s.test_files = FileList["test/**/test_*.rb"].to_a + s.rubyforge_project = "paperclip" + s.has_rdoc = true + s.extra_rdoc_files = FileList["README*"].to_a + s.rdoc_options << '--line-numbers' << '--inline-source' + s.requirements << "ImageMagick" + s.add_runtime_dependency 'right_aws' + s.add_development_dependency 'thoughtbot-shoulda' + s.add_development_dependency 'mocha' +end + +desc "Generate a gemspec file for GitHub" +task :gemspec do + File.open("#{spec.name}.gemspec", 'w') do |f| + f.write spec.to_ruby + end +end diff --git a/vendor/plugins/paperclip/generators/paperclip/USAGE b/vendor/plugins/paperclip/generators/paperclip/USAGE new file mode 100644 index 0000000..2d611d7 --- /dev/null +++ b/vendor/plugins/paperclip/generators/paperclip/USAGE @@ -0,0 +1,5 @@ +Usage: + + script/generate paperclip Class attachment1 (attachment2 ...) + +This will create a migration that will add the proper columns to your class's table. \ No newline at end of file diff --git a/vendor/plugins/paperclip/generators/paperclip/paperclip_generator.rb b/vendor/plugins/paperclip/generators/paperclip/paperclip_generator.rb new file mode 100644 index 0000000..77ee5a2 --- /dev/null +++ b/vendor/plugins/paperclip/generators/paperclip/paperclip_generator.rb @@ -0,0 +1,27 @@ +class PaperclipGenerator < Rails::Generator::NamedBase + attr_accessor :attachments, :migration_name + + def initialize(args, options = {}) + super + @class_name, @attachments = args[0], args[1..-1] + end + + def manifest + file_name = generate_file_name + @migration_name = file_name.camelize + record do |m| + m.migration_template "paperclip_migration.rb.erb", + File.join('db', 'migrate'), + :migration_file_name => file_name + end + end + + private + + def generate_file_name + names = attachments.map{|a| a.underscore } + names = names[0..-2] + ["and", names[-1]] if names.length > 1 + "add_attachments_#{names.join("_")}_to_#{@class_name.underscore}" + end + +end diff --git a/vendor/plugins/paperclip/generators/paperclip/templates/paperclip_migration.rb.erb b/vendor/plugins/paperclip/generators/paperclip/templates/paperclip_migration.rb.erb new file mode 100644 index 0000000..eebb0e5 --- /dev/null +++ b/vendor/plugins/paperclip/generators/paperclip/templates/paperclip_migration.rb.erb @@ -0,0 +1,19 @@ +class <%= migration_name %> < ActiveRecord::Migration + def self.up +<% attachments.each do |attachment| -%> + add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_name, :string + add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_content_type, :string + add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_size, :integer + add_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at, :datetime +<% end -%> + end + + def self.down +<% attachments.each do |attachment| -%> + remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_name + remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_content_type + remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_file_size + remove_column :<%= class_name.underscore.camelize.tableize %>, :<%= attachment %>_updated_at +<% end -%> + end +end diff --git a/vendor/plugins/paperclip/init.rb b/vendor/plugins/paperclip/init.rb new file mode 100644 index 0000000..5a07dda --- /dev/null +++ b/vendor/plugins/paperclip/init.rb @@ -0,0 +1 @@ +require File.join(File.dirname(__FILE__), "lib", "paperclip") diff --git a/vendor/plugins/paperclip/lib/paperclip.rb b/vendor/plugins/paperclip/lib/paperclip.rb new file mode 100644 index 0000000..74c3d79 --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip.rb @@ -0,0 +1,318 @@ +# Paperclip allows file attachments that are stored in the filesystem. All graphical +# transformations are done using the Graphics/ImageMagick command line utilities and +# are stored in Tempfiles until the record is saved. Paperclip does not require a +# separate model for storing the attachment's information, instead adding a few simple +# columns to your table. +# +# Author:: Jon Yurek +# Copyright:: Copyright (c) 2008 thoughtbot, inc. +# License:: MIT License (http://www.opensource.org/licenses/mit-license.php) +# +# Paperclip defines an attachment as any file, though it makes special considerations +# for image files. You can declare that a model has an attached file with the +# +has_attached_file+ method: +# +# class User < ActiveRecord::Base +# has_attached_file :avatar, :styles => { :thumb => "100x100" } +# end +# +# user = User.new +# user.avatar = params[:user][:avatar] +# user.avatar.url +# # => "/users/avatars/4/original_me.jpg" +# user.avatar.url(:thumb) +# # => "/users/avatars/4/thumb_me.jpg" +# +# See the +has_attached_file+ documentation for more details. + +require 'tempfile' +require 'paperclip/upfile' +require 'paperclip/iostream' +require 'paperclip/geometry' +require 'paperclip/processor' +require 'paperclip/thumbnail' +require 'paperclip/storage' +require 'paperclip/attachment' +if defined? RAILS_ROOT + Dir.glob(File.join(File.expand_path(RAILS_ROOT), "lib", "paperclip_processors", "*.rb")).each do |processor| + require processor + end +end + +# The base module that gets included in ActiveRecord::Base. See the +# documentation for Paperclip::ClassMethods for more useful information. +module Paperclip + + VERSION = "2.2.8" + + class << self + # Provides configurability to Paperclip. There are a number of options available, such as: + # * whiny_thumbnails: Will raise an error if Paperclip cannot process thumbnails of + # an uploaded image. Defaults to true. + # * log: Logs progress to the Rails log. Uses ActiveRecord's logger, so honors + # log levels, etc. Defaults to true. + # * command_path: Defines the path at which to find the command line + # programs if they are not visible to Rails the system's search path. Defaults to + # nil, which uses the first executable found in the user's search path. + # * image_magick_path: Deprecated alias of command_path. + def options + @options ||= { + :whiny_thumbnails => true, + :image_magick_path => nil, + :command_path => nil, + :log => true, + :swallow_stderr => true + } + end + + def path_for_command command #:nodoc: + if options[:image_magick_path] + warn("[DEPRECATION] :image_magick_path is deprecated and will be removed. Use :command_path instead") + end + path = [options[:command_path] || options[:image_magick_path], command].compact + File.join(*path) + end + + def interpolates key, &block + Paperclip::Attachment.interpolations[key] = block + end + + # The run method takes a command to execute and a string of parameters + # that get passed to it. The command is prefixed with the :command_path + # option from Paperclip.options. If you have many commands to run and + # they are in different paths, the suggested course of action is to + # symlink them so they are all in the same directory. + # + # If the command returns with a result code that is not one of the + # expected_outcodes, a PaperclipCommandLineError will be raised. Generally + # a code of 0 is expected, but a list of codes may be passed if necessary. + def run cmd, params = "", expected_outcodes = 0 + command = %Q<#{%Q[#{path_for_command(cmd)} #{params}].gsub(/\s+/, " ")}> + command = "#{command} 2>#{bit_bucket}" if Paperclip.options[:swallow_stderr] + output = `#{command}` + unless [expected_outcodes].flatten.include?($?.exitstatus) + raise PaperclipCommandLineError, "Error while running #{cmd}" + end + output + end + + def bit_bucket #:nodoc: + File.exists?("/dev/null") ? "/dev/null" : "NUL" + end + + def included base #:nodoc: + base.extend ClassMethods + unless base.respond_to?(:define_callbacks) + base.send(:include, Paperclip::CallbackCompatability) + end + end + + def processor name #:nodoc: + name = name.to_s.camelize + processor = Paperclip.const_get(name) + unless processor.ancestors.include?(Paperclip::Processor) + raise PaperclipError.new("Processor #{name} was not found") + end + processor + end + end + + class PaperclipError < StandardError #:nodoc: + end + + class PaperclipCommandLineError < StandardError #:nodoc: + end + + class NotIdentifiedByImageMagickError < PaperclipError #:nodoc: + end + + module ClassMethods + # +has_attached_file+ gives the class it is called on an attribute that maps to a file. This + # is typically a file stored somewhere on the filesystem and has been uploaded by a user. + # The attribute returns a Paperclip::Attachment object which handles the management of + # that file. The intent is to make the attachment as much like a normal attribute. The + # thumbnails will be created when the new file is assigned, but they will *not* be saved + # until +save+ is called on the record. Likewise, if the attribute is set to +nil+ is + # called on it, the attachment will *not* be deleted until +save+ is called. See the + # Paperclip::Attachment documentation for more specifics. There are a number of options + # you can set to change the behavior of a Paperclip attachment: + # * +url+: The full URL of where the attachment is publically accessible. This can just + # as easily point to a directory served directly through Apache as it can to an action + # that can control permissions. You can specify the full domain and path, but usually + # just an absolute path is sufficient. The leading slash *must* be included manually for + # absolute paths. The default value is + # "/system/:attachment/:id/:style/:basename.:extension". See + # Paperclip::Attachment#interpolate for more information on variable interpolaton. + # :url => "/:class/:attachment/:id/:style_:basename.:extension" + # :url => "http://some.other.host/stuff/:class/:id_:extension" + # * +default_url+: The URL that will be returned if there is no attachment assigned. + # This field is interpolated just as the url is. The default value is + # "/:attachment/:style/missing.png" + # has_attached_file :avatar, :default_url => "/images/default_:style_avatar.png" + # User.new.avatar_url(:small) # => "/images/default_small_avatar.png" + # * +styles+: A hash of thumbnail styles and their geometries. You can find more about + # geometry strings at the ImageMagick website + # (http://www.imagemagick.org/script/command-line-options.php#resize). Paperclip + # also adds the "#" option (e.g. "50x50#"), which will resize the image to fit maximally + # inside the dimensions and then crop the rest off (weighted at the center). The + # default value is to generate no thumbnails. + # * +default_style+: The thumbnail style that will be used by default URLs. + # Defaults to +original+. + # has_attached_file :avatar, :styles => { :normal => "100x100#" }, + # :default_style => :normal + # user.avatar.url # => "/avatars/23/normal_me.png" + # * +whiny_thumbnails+: Will raise an error if Paperclip cannot post_process an uploaded file due + # to a command line error. This will override the global setting for this attachment. + # Defaults to true. + # * +convert_options+: When creating thumbnails, use this free-form options + # field to pass in various convert command options. Typical options are "-strip" to + # remove all Exif data from the image (save space for thumbnails and avatars) or + # "-depth 8" to specify the bit depth of the resulting conversion. See ImageMagick + # convert documentation for more options: (http://www.imagemagick.org/script/convert.php) + # Note that this option takes a hash of options, each of which correspond to the style + # of thumbnail being generated. You can also specify :all as a key, which will apply + # to all of the thumbnails being generated. If you specify options for the :original, + # it would be best if you did not specify destructive options, as the intent of keeping + # the original around is to regenerate all the thumbnails when requirements change. + # has_attached_file :avatar, :styles => { :large => "300x300", :negative => "100x100" } + # :convert_options => { + # :all => "-strip", + # :negative => "-negate" + # } + # * +storage+: Chooses the storage backend where the files will be stored. The current + # choices are :filesystem and :s3. The default is :filesystem. Make sure you read the + # documentation for Paperclip::Storage::Filesystem and Paperclip::Storage::S3 + # for backend-specific options. + def has_attached_file name, options = {} + include InstanceMethods + + write_inheritable_attribute(:attachment_definitions, {}) if attachment_definitions.nil? + attachment_definitions[name] = {:validations => {}}.merge(options) + + after_save :save_attached_files + before_destroy :destroy_attached_files + + define_callbacks :before_post_process, :after_post_process + define_callbacks :"before_#{name}_post_process", :"after_#{name}_post_process" + + define_method name do |*args| + a = attachment_for(name) + (args.length > 0) ? a.to_s(args.first) : a + end + + define_method "#{name}=" do |file| + attachment_for(name).assign(file) + end + + define_method "#{name}?" do + attachment_for(name).file? + end + + validates_each(name) do |record, attr, value| + attachment = record.attachment_for(name) + attachment.send(:flush_errors) unless attachment.valid? + end + end + + # Places ActiveRecord-style validations on the size of the file assigned. The + # possible options are: + # * +in+: a Range of bytes (i.e. +1..1.megabyte+), + # * +less_than+: equivalent to :in => 0..options[:less_than] + # * +greater_than+: equivalent to :in => options[:greater_than]..Infinity + # * +message+: error message to display, use :min and :max as replacements + def validates_attachment_size name, options = {} + min = options[:greater_than] || (options[:in] && options[:in].first) || 0 + max = options[:less_than] || (options[:in] && options[:in].last) || (1.0/0) + range = (min..max) + message = options[:message] || "file size must be between :min and :max bytes." + + attachment_definitions[name][:validations][:size] = lambda do |attachment, instance| + if attachment.file? && !range.include?(attachment.size.to_i) + message.gsub(/:min/, min.to_s).gsub(/:max/, max.to_s) + end + end + end + + # Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true. + def validates_attachment_thumbnails name, options = {} + attachment_definitions[name][:whiny_thumbnails] = true + end + + # Places ActiveRecord-style validations on the presence of a file. + def validates_attachment_presence name, options = {} + message = options[:message] || "must be set." + attachment_definitions[name][:validations][:presence] = lambda do |attachment, instance| + message unless attachment.file? + end + end + + # Places ActiveRecord-style validations on the content type of the file + # assigned. The possible options are: + # * +content_type+: Allowed content types. Can be a single content type + # or an array. Each type can be a String or a Regexp. It should be + # noted that Internet Explorer upload files with content_types that you + # may not expect. For example, JPEG images are given image/pjpeg and + # PNGs are image/x-png, so keep that in mind when determining how you + # match. Allows all by default. + # * +message+: The message to display when the uploaded file has an invalid + # content type. + # NOTE: If you do not specify an [attachment]_content_type field on your + # model, content_type validation will work _ONLY upon assignment_ and + # re-validation after the instance has been reloaded will always succeed. + def validates_attachment_content_type name, options = {} + attachment_definitions[name][:validations][:content_type] = lambda do |attachment, instance| + valid_types = [options[:content_type]].flatten + + unless attachment.original_filename.blank? + unless valid_types.blank? + content_type = attachment.instance_read(:content_type) + unless valid_types.any?{|t| content_type.nil? || t === content_type } + options[:message] || "is not one of the allowed file types." + end + end + end + end + end + + # Returns the attachment definitions defined by each call to + # has_attached_file. + def attachment_definitions + read_inheritable_attribute(:attachment_definitions) + end + end + + module InstanceMethods #:nodoc: + def attachment_for name + @_paperclip_attachments ||= {} + @_paperclip_attachments[name] ||= Attachment.new(name, self, self.class.attachment_definitions[name]) + end + + def each_attachment + self.class.attachment_definitions.each do |name, definition| + yield(name, attachment_for(name)) + end + end + + def save_attached_files + logger.info("[paperclip] Saving attachments.") + each_attachment do |name, attachment| + attachment.send(:save) + end + end + + def destroy_attached_files + logger.info("[paperclip] Deleting attachments.") + each_attachment do |name, attachment| + attachment.send(:queue_existing_for_delete) + attachment.send(:flush_deletes) + end + end + end + +end + +# Set it all up. +if Object.const_defined?("ActiveRecord") + ActiveRecord::Base.send(:include, Paperclip) + File.send(:include, Paperclip::Upfile) +end diff --git a/vendor/plugins/paperclip/lib/paperclip/attachment.rb b/vendor/plugins/paperclip/lib/paperclip/attachment.rb new file mode 100644 index 0000000..897c67e --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/attachment.rb @@ -0,0 +1,403 @@ +module Paperclip + # The Attachment class manages the files for a given attachment. It saves + # when the model saves, deletes when the model is destroyed, and processes + # the file upon assignment. + class Attachment + + def self.default_options + @default_options ||= { + :url => "/system/:attachment/:id/:style/:basename.:extension", + :path => ":rails_root/public/system/:attachment/:id/:style/:basename.:extension", + :styles => {}, + :default_url => "/:attachment/:style/missing.png", + :default_style => :original, + :validations => {}, + :storage => :filesystem + } + end + + attr_reader :name, :instance, :styles, :default_style, :convert_options, :queued_for_write + + # Creates an Attachment object. +name+ is the name of the attachment, + # +instance+ is the ActiveRecord object instance it's attached to, and + # +options+ is the same as the hash passed to +has_attached_file+. + def initialize name, instance, options = {} + @name = name + @instance = instance + + options = self.class.default_options.merge(options) + + @url = options[:url] + @url = @url.call(self) if @url.is_a?(Proc) + @path = options[:path] + @path = @path.call(self) if @path.is_a?(Proc) + @styles = options[:styles] + @styles = @styles.call(self) if @styles.is_a?(Proc) + @default_url = options[:default_url] + @validations = options[:validations] + @default_style = options[:default_style] + @storage = options[:storage] + @whiny = options[:whiny_thumbnails] + @convert_options = options[:convert_options] || {} + @processors = options[:processors] || [:thumbnail] + @options = options + @queued_for_delete = [] + @queued_for_write = {} + @errors = {} + @validation_errors = nil + @dirty = false + + normalize_style_definition + initialize_storage + end + + # What gets called when you call instance.attachment = File. It clears + # errors, assigns attributes, processes the file, and runs validations. It + # also queues up the previous file for deletion, to be flushed away on + # #save of its host. In addition to form uploads, you can also assign + # another Paperclip attachment: + # new_user.avatar = old_user.avatar + # If the file that is assigned is not valid, the processing (i.e. + # thumbnailing, etc) will NOT be run. + def assign uploaded_file + %w(file_name).each do |field| + unless @instance.class.column_names.include?("#{name}_#{field}") + raise PaperclipError.new("#{@instance.class} model does not have required column '#{name}_#{field}'") + end + end + + if uploaded_file.is_a?(Paperclip::Attachment) + uploaded_file = uploaded_file.to_file(:original) + close_uploaded_file = uploaded_file.respond_to?(:close) + end + + return nil unless valid_assignment?(uploaded_file) + + uploaded_file.binmode if uploaded_file.respond_to? :binmode + self.clear + + return nil if uploaded_file.nil? + + @queued_for_write[:original] = uploaded_file.to_tempfile + instance_write(:file_name, uploaded_file.original_filename.strip.gsub(/[^\w\d\.\-]+/, '_')) + instance_write(:content_type, uploaded_file.content_type.to_s.strip) + instance_write(:file_size, uploaded_file.size.to_i) + instance_write(:updated_at, Time.now) + + @dirty = true + + post_process if valid? + + # Reset the file size if the original file was reprocessed. + instance_write(:file_size, @queued_for_write[:original].size.to_i) + ensure + uploaded_file.close if close_uploaded_file + validate + end + + # Returns the public URL of the attachment, with a given style. Note that + # this does not necessarily need to point to a file that your web server + # can access and can point to an action in your app, if you need fine + # grained security. This is not recommended if you don't need the + # security, however, for performance reasons. set + # include_updated_timestamp to false if you want to stop the attachment + # update time appended to the url + def url style = default_style, include_updated_timestamp = true + url = original_filename.nil? ? interpolate(@default_url, style) : interpolate(@url, style) + include_updated_timestamp && updated_at ? [url, updated_at].compact.join(url.include?("?") ? "&" : "?") : url + end + + # Returns the path of the attachment as defined by the :path option. If the + # file is stored in the filesystem the path refers to the path of the file + # on disk. If the file is stored in S3, the path is the "key" part of the + # URL, and the :bucket option refers to the S3 bucket. + def path style = nil #:nodoc: + original_filename.nil? ? nil : interpolate(@path, style) + end + + # Alias to +url+ + def to_s style = nil + url(style) + end + + # Returns true if there are no errors on this attachment. + def valid? + validate + errors.empty? + end + + # Returns an array containing the errors on this attachment. + def errors + @errors + end + + # Returns true if there are changes that need to be saved. + def dirty? + @dirty + end + + # Saves the file, if there are no errors. If there are, it flushes them to + # the instance's errors and returns false, cancelling the save. + def save + if valid? + flush_deletes + flush_writes + @dirty = false + true + else + flush_errors + false + end + end + + # Clears out the attachment. Has the same effect as previously assigning + # nil to the attachment. Does NOT save. If you wish to clear AND save, + # use #destroy. + def clear + queue_existing_for_delete + @errors = {} + @validation_errors = nil + end + + # Destroys the attachment. Has the same effect as previously assigning + # nil to the attachment *and saving*. This is permanent. If you wish to + # wipe out the existing attachment but not save, use #clear. + def destroy + clear + save + end + + # Returns the name of the file as originally assigned, and lives in the + # _file_name attribute of the model. + def original_filename + instance_read(:file_name) + end + + # Returns the size of the file as originally assigned, and lives in the + # _file_size attribute of the model. + def size + instance_read(:file_size) || (@queued_for_write[:original] && @queued_for_write[:original].size) + end + + # Returns the content_type of the file as originally assigned, and lives + # in the _content_type attribute of the model. + def content_type + instance_read(:content_type) + end + + # Returns the last modified time of the file as originally assigned, and + # lives in the _updated_at attribute of the model. + def updated_at + time = instance_read(:updated_at) + time && time.to_i + end + + # A hash of procs that are run during the interpolation of a path or url. + # A variable of the format :name will be replaced with the return value of + # the proc named ":name". Each lambda takes the attachment and the current + # style as arguments. This hash can be added to with your own proc if + # necessary. + def self.interpolations + @interpolations ||= { + :rails_root => lambda{|attachment,style| RAILS_ROOT }, + :rails_env => lambda{|attachment,style| RAILS_ENV }, + :class => lambda do |attachment,style| + attachment.instance.class.name.underscore.pluralize + end, + :basename => lambda do |attachment,style| + attachment.original_filename.gsub(/#{File.extname(attachment.original_filename)}$/, "") + end, + :extension => lambda do |attachment,style| + ((style = attachment.styles[style]) && style[:format]) || + File.extname(attachment.original_filename).gsub(/^\.+/, "") + end, + :id => lambda{|attachment,style| attachment.instance.id }, + :id_partition => lambda do |attachment, style| + ("%09d" % attachment.instance.id).scan(/\d{3}/).join("/") + end, + :attachment => lambda{|attachment,style| attachment.name.to_s.downcase.pluralize }, + :style => lambda{|attachment,style| style || attachment.default_style }, + } + end + + # This method really shouldn't be called that often. It's expected use is + # in the paperclip:refresh rake task and that's it. It will regenerate all + # thumbnails forcefully, by reobtaining the original file and going through + # the post-process again. + def reprocess! + new_original = Tempfile.new("paperclip-reprocess") + new_original.binmode + if old_original = to_file(:original) + new_original.write( old_original.read ) + new_original.rewind + + @queued_for_write = { :original => new_original } + post_process + + old_original.close if old_original.respond_to?(:close) + + save + else + true + end + end + + # Returns true if a file has been assigned. + def file? + !original_filename.blank? + end + + # Writes the attachment-specific attribute on the instance. For example, + # instance_write(:file_name, "me.jpg") will write "me.jpg" to the instance's + # "avatar_file_name" field (assuming the attachment is called avatar). + def instance_write(attr, value) + setter = :"#{name}_#{attr}=" + responds = instance.respond_to?(setter) + self.instance_variable_set("@_#{setter.to_s.chop}", value) + instance.send(setter, value) if responds || attr.to_s == "file_name" + end + + # Reads the attachment-specific attribute on the instance. See instance_write + # for more details. + def instance_read(attr) + getter = :"#{name}_#{attr}" + responds = instance.respond_to?(getter) + cached = self.instance_variable_get("@_#{getter}") + return cached if cached + instance.send(getter) if responds || attr.to_s == "file_name" + end + + private + + def logger #:nodoc: + instance.logger + end + + def log message #:nodoc: + logger.info("[paperclip] #{message}") if logging? + end + + def logging? #:nodoc: + Paperclip.options[:log] + end + + def valid_assignment? file #:nodoc: + file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type)) + end + + def validate #:nodoc: + unless @validation_errors + @validation_errors = @validations.inject({}) do |errors, validation| + name, block = validation + errors[name] = block.call(self, instance) if block + errors + end + @validation_errors.reject!{|k,v| v == nil } + @errors.merge!(@validation_errors) + end + @validation_errors + end + + def normalize_style_definition #:nodoc: + @styles.each do |name, args| + unless args.is_a? Hash + dimensions, format = *args + @styles[name] = { + :processors => @processors, + :geometry => dimensions, + :format => format, + :whiny => @whiny, + :convert_options => extra_options_for(name) + } + else + @styles[name] = { + :processors => @processors, + :whiny => @whiny, + :convert_options => extra_options_for(name) + }.merge(@styles[name]) + end + end + end + + def solidify_style_definitions #:nodoc: + @styles.each do |name, args| + @styles[name][:geometry] = @styles[name][:geometry].call(instance) if @styles[name][:geometry].respond_to?(:call) + @styles[name][:processors] = @styles[name][:processors].call(instance) if @styles[name][:processors].respond_to?(:call) + end + end + + def initialize_storage #:nodoc: + @storage_module = Paperclip::Storage.const_get(@storage.to_s.capitalize) + self.extend(@storage_module) + end + + def extra_options_for(style) #:nodoc: + all_options = convert_options[:all] + all_options = all_options.call(instance) if all_options.respond_to?(:call) + style_options = convert_options[style] + style_options = style_options.call(instance) if style_options.respond_to?(:call) + + [ style_options, all_options ].compact.join(" ") + end + + def post_process #:nodoc: + return if @queued_for_write[:original].nil? + solidify_style_definitions + return if fire_events(:before) + post_process_styles + return if fire_events(:after) + end + + def fire_events(which) + return true if callback(:"#{which}_post_process") == false + return true if callback(:"#{which}_#{name}_post_process") == false + end + + def callback which #:nodoc: + instance.run_callbacks(which, @queued_for_write){|result, obj| result == false } + end + + def post_process_styles + @styles.each do |name, args| + begin + raise RuntimeError.new("Style #{name} has no processors defined.") if args[:processors].blank? + @queued_for_write[name] = args[:processors].inject(@queued_for_write[:original]) do |file, processor| + Paperclip.processor(processor).make(file, args, self) + end + rescue PaperclipError => e + log("An error was received while processing: #{e.inspect}") + (@errors[:processing] ||= []) << e.message if @whiny + end + end + end + + def interpolate pattern, style = default_style #:nodoc: + interpolations = self.class.interpolations.sort{|a,b| a.first.to_s <=> b.first.to_s } + interpolations.reverse.inject( pattern.dup ) do |result, interpolation| + tag, blk = interpolation + result.gsub(/:#{tag}/) do |match| + blk.call( self, style ) + end + end + end + + def queue_existing_for_delete #:nodoc: + return unless file? + @queued_for_delete += [:original, *@styles.keys].uniq.map do |style| + path(style) if exists?(style) + end.compact + instance_write(:file_name, nil) + instance_write(:content_type, nil) + instance_write(:file_size, nil) + instance_write(:updated_at, nil) + end + + def flush_errors #:nodoc: + @errors.each do |error, message| + [message].flatten.each {|m| instance.errors.add(name, m) } + end + end + + end +end + diff --git a/vendor/plugins/paperclip/lib/paperclip/callback_compatability.rb b/vendor/plugins/paperclip/lib/paperclip/callback_compatability.rb new file mode 100644 index 0000000..10b08fc --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/callback_compatability.rb @@ -0,0 +1,33 @@ +module Paperclip + # This module is intended as a compatability shim for the differences in + # callbacks between Rails 2.0 and Rails 2.1. + module CallbackCompatability + def self.included(base) + base.extend(ClassMethods) + base.send(:include, InstanceMethods) + end + + module ClassMethods + # The implementation of this method is taken from the Rails 1.2.6 source, + # from rails/activerecord/lib/active_record/callbacks.rb, line 192. + def define_callbacks(*args) + args.each do |method| + self.class_eval <<-"end_eval" + def self.#{method}(*callbacks, &block) + callbacks << block if block_given? + write_inheritable_array(#{method.to_sym.inspect}, callbacks) + end + end_eval + end + end + end + + module InstanceMethods + # The callbacks in < 2.1 don't worry about the extra options or the + # block, so just run what we have available. + def run_callbacks(meth, opts = nil, &blk) + callback(meth) + end + end + end +end diff --git a/vendor/plugins/paperclip/lib/paperclip/geometry.rb b/vendor/plugins/paperclip/lib/paperclip/geometry.rb new file mode 100644 index 0000000..7fbe038 --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/geometry.rb @@ -0,0 +1,115 @@ +module Paperclip + + # Defines the geometry of an image. + class Geometry + attr_accessor :height, :width, :modifier + + # Gives a Geometry representing the given height and width + def initialize width = nil, height = nil, modifier = nil + @height = height.to_f + @width = width.to_f + @modifier = modifier + end + + # Uses ImageMagick to determing the dimensions of a file, passed in as either a + # File or path. + def self.from_file file + file = file.path if file.respond_to? "path" + geometry = begin + Paperclip.run("identify", %Q[-format "%wx%h" "#{file}"[0]]) + rescue PaperclipCommandLineError + "" + end + parse(geometry) || + raise(NotIdentifiedByImageMagickError.new("#{file} is not recognized by the 'identify' command.")) + end + + # Parses a "WxH" formatted string, where W is the width and H is the height. + def self.parse string + if match = (string && string.match(/\b(\d*)x?(\d*)\b([\>\<\#\@\%^!])?/)) + Geometry.new(*match[1,3]) + end + end + + # True if the dimensions represent a square + def square? + height == width + end + + # True if the dimensions represent a horizontal rectangle + def horizontal? + height < width + end + + # True if the dimensions represent a vertical rectangle + def vertical? + height > width + end + + # The aspect ratio of the dimensions. + def aspect + width / height + end + + # Returns the larger of the two dimensions + def larger + [height, width].max + end + + # Returns the smaller of the two dimensions + def smaller + [height, width].min + end + + # Returns the width and height in a format suitable to be passed to Geometry.parse + def to_s + s = "" + s << width.to_i.to_s if width > 0 + s << "x#{height.to_i}" if height > 0 + s << modifier.to_s + s + end + + # Same as to_s + def inspect + to_s + end + + # Returns the scaling and cropping geometries (in string-based ImageMagick format) + # neccessary to transform this Geometry into the Geometry given. If crop is true, + # then it is assumed the destination Geometry will be the exact final resolution. + # In this case, the source Geometry is scaled so that an image containing the + # destination Geometry would be completely filled by the source image, and any + # overhanging image would be cropped. Useful for square thumbnail images. The cropping + # is weighted at the center of the Geometry. + def transformation_to dst, crop = false + if crop + ratio = Geometry.new( dst.width / self.width, dst.height / self.height ) + scale_geometry, scale = scaling(dst, ratio) + crop_geometry = cropping(dst, ratio, scale) + else + scale_geometry = dst.to_s + end + + [ scale_geometry, crop_geometry ] + end + + private + + def scaling dst, ratio + if ratio.horizontal? || ratio.square? + [ "%dx" % dst.width, ratio.width ] + else + [ "x%d" % dst.height, ratio.height ] + end + end + + def cropping dst, ratio, scale + if ratio.horizontal? || ratio.square? + "%dx%d+%d+%d" % [ dst.width, dst.height, 0, (self.height * scale - dst.height) / 2 ] + else + "%dx%d+%d+%d" % [ dst.width, dst.height, (self.width * scale - dst.width) / 2, 0 ] + end + end + end +end diff --git a/vendor/plugins/paperclip/lib/paperclip/iostream.rb b/vendor/plugins/paperclip/lib/paperclip/iostream.rb new file mode 100644 index 0000000..74e2014 --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/iostream.rb @@ -0,0 +1,58 @@ +# Provides method that can be included on File-type objects (IO, StringIO, Tempfile, etc) to allow stream copying +# and Tempfile conversion. +module IOStream + + # Returns a Tempfile containing the contents of the readable object. + def to_tempfile + tempfile = Tempfile.new("stream") + tempfile.binmode + self.stream_to(tempfile) + end + + # Copies one read-able object from one place to another in blocks, obviating the need to load + # the whole thing into memory. Defaults to 8k blocks. If this module is included in both + # StringIO and Tempfile, then either can have its data copied anywhere else without typing + # worries or memory overhead worries. Returns a File if a String is passed in as the destination + # and returns the IO or Tempfile as passed in if one is sent as the destination. + def stream_to path_or_file, in_blocks_of = 8192 + dstio = case path_or_file + when String then File.new(path_or_file, "wb+") + when IO then path_or_file + when Tempfile then path_or_file + end + buffer = "" + self.rewind + while self.read(in_blocks_of, buffer) do + dstio.write(buffer) + end + dstio.rewind + dstio + end +end + +class IO #:nodoc: + include IOStream +end + +%w( Tempfile StringIO ).each do |klass| + if Object.const_defined? klass + Object.const_get(klass).class_eval do + include IOStream + end + end +end + +# Corrects a bug in Windows when asking for Tempfile size. +if defined? Tempfile + class Tempfile + def size + if @tmpfile + @tmpfile.fsync + @tmpfile.flush + @tmpfile.stat.size + else + 0 + end + end + end +end diff --git a/vendor/plugins/paperclip/lib/paperclip/matchers.rb b/vendor/plugins/paperclip/lib/paperclip/matchers.rb new file mode 100644 index 0000000..ca24b5e --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/matchers.rb @@ -0,0 +1,4 @@ +require 'paperclip/matchers/have_attached_file_matcher' +require 'paperclip/matchers/validate_attachment_presence_matcher' +require 'paperclip/matchers/validate_attachment_content_type_matcher' +require 'paperclip/matchers/validate_attachment_size_matcher' diff --git a/vendor/plugins/paperclip/lib/paperclip/matchers/have_attached_file_matcher.rb b/vendor/plugins/paperclip/lib/paperclip/matchers/have_attached_file_matcher.rb new file mode 100644 index 0000000..da0dd8b --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/matchers/have_attached_file_matcher.rb @@ -0,0 +1,49 @@ +module Paperclip + module Shoulda + module Matchers + def have_attached_file name + HaveAttachedFileMatcher.new(name) + end + + class HaveAttachedFileMatcher + def initialize attachment_name + @attachment_name = attachment_name + end + + def matches? subject + @subject = subject + responds? && has_column? && included? + end + + def failure_message + "Should have an attachment named #{@attachment_name}" + end + + def negative_failure_message + "Should not have an attachment named #{@attachment_name}" + end + + def description + "have an attachment named #{@attachment_name}" + end + + protected + + def responds? + methods = @subject.instance_methods + methods.include?("#{@attachment_name}") && + methods.include?("#{@attachment_name}=") && + methods.include?("#{@attachment_name}?") + end + + def has_column? + @subject.column_names.include?("#{@attachment_name}_file_name") + end + + def included? + @subject.ancestors.include?(Paperclip::InstanceMethods) + end + end + end + end +end diff --git a/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb b/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb new file mode 100644 index 0000000..b4e97fd --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_content_type_matcher.rb @@ -0,0 +1,66 @@ +module Paperclip + module Shoulda + module Matchers + def validate_attachment_content_type name + ValidateAttachmentContentTypeMatcher.new(name) + end + + class ValidateAttachmentContentTypeMatcher + def initialize attachment_name + @attachment_name = attachment_name + end + + def allowing *types + @allowed_types = types.flatten + self + end + + def rejecting *types + @rejected_types = types.flatten + self + end + + def matches? subject + @subject = subject + @allowed_types && @rejected_types && + allowed_types_allowed? && rejected_types_rejected? + end + + def failure_message + "Content types #{@allowed_types.join(", ")} should be accepted" + + " and #{@rejected_types.join(", ")} rejected by #{@attachment_name}" + end + + def negative_failure_message + "Content types #{@allowed_types.join(", ")} should be rejected" + + " and #{@rejected_types.join(", ")} accepted by #{@attachment_name}" + end + + def description + "validate the content types allowed on attachment #{@attachment_name}" + end + + protected + + def allow_types?(types) + types.all? do |type| + file = StringIO.new(".") + file.content_type = type + attachment = @subject.new.attachment_for(@attachment_name) + attachment.assign(file) + attachment.errors[:content_type].nil? + end + end + + def allowed_types_allowed? + allow_types?(@allowed_types) + end + + def rejected_types_rejected? + not allow_types?(@rejected_types) + end + end + end + end +end + diff --git a/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_presence_matcher.rb b/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_presence_matcher.rb new file mode 100644 index 0000000..61dc0ea --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_presence_matcher.rb @@ -0,0 +1,48 @@ +module Paperclip + module Shoulda + module Matchers + def validate_attachment_presence name + ValidateAttachmentPresenceMatcher.new(name) + end + + class ValidateAttachmentPresenceMatcher + def initialize attachment_name + @attachment_name = attachment_name + end + + def matches? subject + @subject = subject + error_when_not_valid? && no_error_when_valid? + end + + def failure_message + "Attachment #{@attachment_name} should be required" + end + + def negative_failure_message + "Attachment #{@attachment_name} should not be required" + end + + def description + "require presence of attachment #{@attachment_name}" + end + + protected + + def error_when_not_valid? + @attachment = @subject.new.send(@attachment_name) + @attachment.assign(nil) + not @attachment.errors[:presence].nil? + end + + def no_error_when_valid? + @file = StringIO.new(".") + @attachment = @subject.new.send(@attachment_name) + @attachment.assign(@file) + @attachment.errors[:presence].nil? + end + end + end + end +end + diff --git a/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_size_matcher.rb b/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_size_matcher.rb new file mode 100644 index 0000000..f84c479 --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/matchers/validate_attachment_size_matcher.rb @@ -0,0 +1,83 @@ +module Paperclip + module Shoulda + module Matchers + def validate_attachment_size name + ValidateAttachmentSizeMatcher.new(name) + end + + class ValidateAttachmentSizeMatcher + def initialize attachment_name + @attachment_name = attachment_name + @low, @high = 0, (1.0/0) + end + + def less_than size + @high = size + self + end + + def greater_than size + @low = size + self + end + + def in range + @low, @high = range.first, range.last + self + end + + def matches? subject + @subject = subject + lower_than_low? && higher_than_low? && lower_than_high? && higher_than_high? + end + + def failure_message + "Attachment #{@attachment_name} must be between #{@low} and #{@high} bytes" + end + + def negative_failure_message + "Attachment #{@attachment_name} cannot be between #{@low} and #{@high} bytes" + end + + def description + "validate the size of attachment #{@attachment_name}" + end + + protected + + def override_method object, method, &replacement + (class << object; self; end).class_eval do + define_method(method, &replacement) + end + end + + def passes_validation_with_size(new_size) + file = StringIO.new(".") + override_method(file, :size){ new_size } + attachment = @subject.new.attachment_for(@attachment_name) + attachment.assign(file) + attachment.errors[:size].nil? + end + + def lower_than_low? + not passes_validation_with_size(@low - 1) + end + + def higher_than_low? + passes_validation_with_size(@low + 1) + end + + def lower_than_high? + return true if @high == (1.0/0) + passes_validation_with_size(@high - 1) + end + + def higher_than_high? + return true if @high == (1.0/0) + not passes_validation_with_size(@high + 1) + end + end + end + end +end + diff --git a/vendor/plugins/paperclip/lib/paperclip/processor.rb b/vendor/plugins/paperclip/lib/paperclip/processor.rb new file mode 100644 index 0000000..9082bfd --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/processor.rb @@ -0,0 +1,48 @@ +module Paperclip + # Paperclip processors allow you to modify attached files when they are + # attached in any way you are able. Paperclip itself uses command-line + # programs for its included Thumbnail processor, but custom processors + # are not required to follow suit. + # + # Processors are required to be defined inside the Paperclip module and + # are also required to be a subclass of Paperclip::Processor. There are + # only two methods you must implement to properly be a subclass: + # #initialize and #make. Initialize's arguments are the file that will + # be operated on (which is an instance of File), and a hash of options + # that were defined in has_attached_file's style hash. + # + # All #make needs to do is return an instance of File (Tempfile is + # acceptable) which contains the results of the processing. + # + # See Paperclip.run for more information about using command-line + # utilities from within Processors. + class Processor + attr_accessor :file, :options, :attachment + + def initialize file, options = {}, attachment = nil + @file = file + @options = options + @attachment = attachment + end + + def make + end + + def self.make file, options = {}, attachment = nil + new(file, options, attachment).make + end + end + + # Due to how ImageMagick handles its image format conversion and how Tempfile + # handles its naming scheme, it is necessary to override how Tempfile makes + # its names so as to allow for file extensions. Idea taken from the comments + # on this blog post: + # http://marsorange.com/archives/of-mogrify-ruby-tempfile-dynamic-class-definitions + class Tempfile < ::Tempfile + # Replaces Tempfile's +make_tmpname+ with one that honors file extensions. + def make_tmpname(basename, n) + extension = File.extname(basename) + sprintf("%s,%d,%d%s", File.basename(basename, extension), $$, n, extension) + end + end +end diff --git a/vendor/plugins/paperclip/lib/paperclip/storage.rb b/vendor/plugins/paperclip/lib/paperclip/storage.rb new file mode 100644 index 0000000..58913ad --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/storage.rb @@ -0,0 +1,236 @@ +module Paperclip + module Storage + + # The default place to store attachments is in the filesystem. Files on the local + # filesystem can be very easily served by Apache without requiring a hit to your app. + # They also can be processed more easily after they've been saved, as they're just + # normal files. There is one Filesystem-specific option for has_attached_file. + # * +path+: The location of the repository of attachments on disk. This can (and, in + # almost all cases, should) be coordinated with the value of the +url+ option to + # allow files to be saved into a place where Apache can serve them without + # hitting your app. Defaults to + # ":rails_root/public/:attachment/:id/:style/:basename.:extension" + # By default this places the files in the app's public directory which can be served + # directly. If you are using capistrano for deployment, a good idea would be to + # make a symlink to the capistrano-created system directory from inside your app's + # public directory. + # See Paperclip::Attachment#interpolate for more information on variable interpolaton. + # :path => "/var/app/attachments/:class/:id/:style/:basename.:extension" + module Filesystem + def self.extended base + end + + def exists?(style = default_style) + if original_filename + File.exist?(path(style)) + else + false + end + 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 + @queued_for_write[style] || (File.new(path(style), 'rb') if exists?(style)) + end + alias_method :to_io, :to_file + + def flush_writes #:nodoc: + @queued_for_write.each do |style, file| + file.close + FileUtils.mkdir_p(File.dirname(path(style))) + logger.info("[paperclip] saving #{path(style)}") + FileUtils.mv(file.path, path(style)) + FileUtils.chmod(0644, path(style)) + end + @queued_for_write = {} + end + + def flush_deletes #:nodoc: + @queued_for_delete.each do |path| + begin + logger.info("[paperclip] deleting #{path}") + FileUtils.rm(path) if File.exist?(path) + rescue Errno::ENOENT => e + # ignore file-not-found, let everything else pass + end + begin + while(true) + path = File.dirname(path) + FileUtils.rmdir(path) + end + rescue Errno::EEXIST, Errno::ENOTEMPTY, Errno::ENOENT, Errno::EINVAL, Errno::ENOTDIR + # Stop trying to remove parent directories + rescue SystemCallError => e + logger.info("[paperclip] There was an unexpected error while deleting directories: #{e.class}") + # Ignore it + end + end + @queued_for_delete = [] + end + end + + # 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 S3-specific options for has_attached_file: + # * +s3_credentials+: Takes a path, a File, or a Hash. The path (or File) must point + # to a YAML file containing the +access_key_id+ and +secret_access_key+ that Amazon + # gives you. You can 'environment-space' this just like you do to your + # database.yml file, so different environments can use different accounts: + # development: + # access_key_id: 123... + # secret_access_key: 123... + # test: + # access_key_id: abc... + # secret_access_key: abc... + # production: + # access_key_id: 456... + # secret_access_key: 456... + # This is not required, however, and the file may simply look like this: + # access_key_id: 456... + # secret_access_key: 456... + # In which case, those access keys will be used in all environments. You can also + # put your bucket name in this file, instead of adding it to the code directly. + # This is useful when you want the same account but a different bucket for + # 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) + # The default for Paperclip is "public-read". + # * +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. + # * +s3_headers+: A hash of headers such as {'Expires' => 1.year.from_now.httpdate} + # * +bucket+: 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. The bucket name will not be interpolated. + # You can define the bucket as a Proc if you want to determine it's name at runtime. + # Paperclip will call that Proc with attachment as the only argument. + # * +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 + # 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 + # :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 + # path-style, or :s3_path_url). But in some cases paths don't work and you need to use + # the domain-style (:s3_domain_url). Anything else here will be treated like path-style. + # 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. + # * +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. + module S3 + def self.extended base + require 'right_aws' + 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] + @url = ":s3_path_url" unless @url.to_s.match(/^:s3.*url$/) + end + base.class.interpolations[:s3_alias_url] = lambda do |attachment, style| + "#{attachment.s3_protocol}://#{attachment.s3_host_alias}/#{attachment.path(style).gsub(%r{^/}, "")}" + end + base.class.interpolations[:s3_path_url] = lambda do |attachment, style| + "#{attachment.s3_protocol}://s3.amazonaws.com/#{attachment.bucket_name}/#{attachment.path(style).gsub(%r{^/}, "")}" + end + base.class.interpolations[:s3_domain_url] = lambda do |attachment, style| + "#{attachment.s3_protocol}://#{attachment.bucket_name}.s3.amazonaws.com/#{attachment.path(style).gsub(%r{^/}, "")}" + end + end + + def s3 + @s3 ||= RightAws::S3.new(@s3_credentials[:access_key_id], + @s3_credentials[:secret_access_key], + @s3_options) + end + + def s3_bucket + @s3_bucket ||= s3.bucket(@bucket, true, @s3_permissions) + end + + def bucket_name + @bucket + end + + def s3_host_alias + @s3_host_alias + end + + def parse_credentials creds + creds = find_credentials(creds).stringify_keys + (creds[ENV['RAILS_ENV']] || creds).symbolize_keys + end + + def exists?(style = default_style) + s3_bucket.key(path(style)) ? true : false + end + + def s3_protocol + @s3_protocol + 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 + @queued_for_write[style] || s3_bucket.key(path(style)) + end + alias_method :to_io, :to_file + + def flush_writes #:nodoc: + @queued_for_write.each do |style, file| + begin + logger.info("[paperclip] saving #{path(style)}") + key = s3_bucket.key(path(style)) + key.data = file + key.put(nil, @s3_permissions, {'Content-type' => instance_read(:content_type)}.merge(@s3_headers)) + rescue RightAws::AwsError => e + raise + end + end + @queued_for_write = {} + end + + def flush_deletes #:nodoc: + @queued_for_delete.each do |path| + begin + logger.info("[paperclip] deleting #{path}") + if file = s3_bucket.key(path) + file.delete + end + rescue RightAws::AwsError + # Ignore this. + end + end + @queued_for_delete = [] + end + + def find_credentials creds + case creds + when File + YAML.load_file(creds.path) + when String + YAML.load_file(creds) + when Hash + creds + else + raise ArgumentError, "Credentials are not a path, file, or hash." + end + end + private :find_credentials + + end + end +end diff --git a/vendor/plugins/paperclip/lib/paperclip/thumbnail.rb b/vendor/plugins/paperclip/lib/paperclip/thumbnail.rb new file mode 100644 index 0000000..2178a9c --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/thumbnail.rb @@ -0,0 +1,70 @@ +module Paperclip + # Handles thumbnailing images that are uploaded. + class Thumbnail < Processor + + attr_accessor :current_geometry, :target_geometry, :format, :whiny, :convert_options + + # Creates a Thumbnail object set to work on the +file+ given. It + # will attempt to transform the image into one defined by +target_geometry+ + # which is a "WxH"-style string. +format+ will be inferred from the +file+ + # 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 + super + geometry = options[:geometry] + @file = file + @crop = geometry[-1,1] == '#' + @target_geometry = Geometry.parse geometry + @current_geometry = Geometry.from_file @file + @convert_options = options[:convert_options] + @whiny = options[:whiny].nil? ? true : options[:whiny] + @format = options[:format] + + @current_format = File.extname(@file.path) + @basename = File.basename(@file.path, @current_format) + end + + # Returns true if the +target_geometry+ is meant to crop. + def crop? + @crop + end + + # Returns true if the image is meant to make use of additional convert options. + def convert_options? + not @convert_options.blank? + end + + # Performs the conversion of the +file+ into a thumbnail. Returns the Tempfile + # that contains the new image. + def make + src = @file + dst = Tempfile.new([@basename, @format].compact.join(".")) + dst.binmode + + command = <<-end_command + "#{ File.expand_path(src.path) }[0]" + #{ transformation_command } + "#{ File.expand_path(dst.path) }" + end_command + + begin + success = Paperclip.run("convert", command.gsub(/\s+/, " ")) + rescue PaperclipCommandLineError + raise PaperclipError, "There was an error processing the thumbnail for #{@basename}" if @whiny + end + + dst + end + + # Returns the command ImageMagick's +convert+ needs to transform the image + # into the thumbnail. + def transformation_command + scale, crop = @current_geometry.transformation_to(@target_geometry, crop?) + trans = "-resize \"#{scale}\"" + trans << " -crop \"#{crop}\" +repage" if crop + trans << " #{convert_options}" if convert_options? + trans + end + end +end diff --git a/vendor/plugins/paperclip/lib/paperclip/upfile.rb b/vendor/plugins/paperclip/lib/paperclip/upfile.rb new file mode 100644 index 0000000..f0c8a44 --- /dev/null +++ b/vendor/plugins/paperclip/lib/paperclip/upfile.rb @@ -0,0 +1,48 @@ +module Paperclip + # The Upfile module is a convenience module for adding uploaded-file-type methods + # to the +File+ class. Useful for testing. + # user.avatar = File.new("test/test_avatar.jpg") + module Upfile + + # 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"jpe?g" 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 "csv", "xml", "css", "js" then "text/#{type}" + else "application/x-#{type}" + end + end + + # Returns the file's normal name. + def original_filename + File.basename(self.path) + end + + # Returns the size of the file. + def size + File.size(self) + end + end +end + +if defined? StringIO + class StringIO + attr_accessor :original_filename, :content_type + def original_filename + @original_filename ||= "stringio.txt" + end + def content_type + @content_type ||= "text/plain" + end + end +end + +class File #:nodoc: + include Paperclip::Upfile +end + diff --git a/vendor/plugins/paperclip/paperclip.gemspec b/vendor/plugins/paperclip/paperclip.gemspec new file mode 100644 index 0000000..12ce4c2 --- /dev/null +++ b/vendor/plugins/paperclip/paperclip.gemspec @@ -0,0 +1,40 @@ +# -*- encoding: utf-8 -*- + +Gem::Specification.new do |s| + s.name = %q{paperclip} + s.version = "2.2.8" + + s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version= + s.authors = ["Jon Yurek"] + s.date = %q{2009-04-02} + s.email = %q{jyurek@thoughtbot.com} + s.extra_rdoc_files = ["README.rdoc"] + s.files = ["README.rdoc", "LICENSE", "Rakefile", "init.rb", "generators/paperclip", "generators/paperclip/paperclip_generator.rb", "generators/paperclip/templates", "generators/paperclip/templates/paperclip_migration.rb.erb", "generators/paperclip/USAGE", "lib/paperclip", "lib/paperclip/attachment.rb", "lib/paperclip/callback_compatability.rb", "lib/paperclip/geometry.rb", "lib/paperclip/iostream.rb", "lib/paperclip/matchers", "lib/paperclip/matchers/have_attached_file_matcher.rb", "lib/paperclip/matchers/validate_attachment_content_type_matcher.rb", "lib/paperclip/matchers/validate_attachment_presence_matcher.rb", "lib/paperclip/matchers/validate_attachment_size_matcher.rb", "lib/paperclip/matchers.rb", "lib/paperclip/processor.rb", "lib/paperclip/storage.rb", "lib/paperclip/thumbnail.rb", "lib/paperclip/upfile.rb", "lib/paperclip.rb", "tasks/paperclip_tasks.rake", "test/attachment_test.rb", "test/database.yml", "test/debug.log", "test/fixtures", "test/fixtures/12k.png", "test/fixtures/50x50.png", "test/fixtures/5k.png", "test/fixtures/bad.png", "test/fixtures/s3.yml", "test/fixtures/text.txt", "test/fixtures/twopage.pdf", "test/geometry_test.rb", "test/helper.rb", "test/integration_test.rb", "test/iostream_test.rb", "test/matchers", "test/matchers/have_attached_file_matcher_test.rb", "test/matchers/validate_attachment_content_type_matcher_test.rb", "test/matchers/validate_attachment_presence_matcher_test.rb", "test/matchers/validate_attachment_size_matcher_test.rb", "test/paperclip_test.rb", "test/processor_test.rb", "test/s3.yml", "test/storage_test.rb", "test/thumbnail_test.rb", "test/tmp", "test/tmp/storage.txt", "shoulda_macros/paperclip.rb"] + s.has_rdoc = true + s.homepage = %q{http://www.thoughtbot.com/projects/paperclip} + s.rdoc_options = ["--line-numbers", "--inline-source"] + s.require_paths = ["lib"] + s.requirements = ["ImageMagick"] + s.rubyforge_project = %q{paperclip} + s.rubygems_version = %q{1.3.1} + s.summary = %q{File attachments as attributes for ActiveRecord} + + if s.respond_to? :specification_version then + current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION + s.specification_version = 2 + + if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then + s.add_runtime_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + s.add_development_dependency(%q, [">= 0"]) + else + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + end + else + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + s.add_dependency(%q, [">= 0"]) + end +end diff --git a/vendor/plugins/paperclip/shoulda_macros/paperclip.rb b/vendor/plugins/paperclip/shoulda_macros/paperclip.rb new file mode 100644 index 0000000..d690357 --- /dev/null +++ b/vendor/plugins/paperclip/shoulda_macros/paperclip.rb @@ -0,0 +1,68 @@ +require 'paperclip/matchers' + +module Paperclip + # =Paperclip Shoulda Macros + # + # These macros are intended for use with shoulda, and will be included into + # your tests automatically. All of the macros use the standard shoulda + # assumption that the name of the test is based on the name of the model + # you're testing (that is, UserTest is the test for the User model), and + # will load that class for testing purposes. + module Shoulda + include Matchers + # This will test whether you have defined your attachment correctly by + # checking for all the required fields exist after the definition of the + # attachment. + def should_have_attached_file name + klass = self.name.gsub(/Test$/, '').constantize + matcher = have_attached_file name + should matcher.description do + assert_accepts(matcher, klass) + end + end + + # Tests for validations on the presence of the attachment. + def should_validate_attachment_presence name + klass = self.name.gsub(/Test$/, '').constantize + matcher = validate_attachment_presence name + should matcher.description do + assert_accepts(matcher, klass) + end + end + + # Tests that you have content_type validations specified. There are two + # options, :valid and :invalid. Both accept an array of strings. The + # strings should be a list of content types which will pass and fail + # validation, respectively. + def should_validate_attachment_content_type name, options = {} + klass = self.name.gsub(/Test$/, '').constantize + valid = [options[:valid]].flatten + invalid = [options[:invalid]].flatten + matcher = validate_attachment_content_type(name).allowing(valid).rejecting(invalid) + should matcher.description do + assert_accepts(matcher, klass) + end + end + + # Tests to ensure that you have file size validations turned on. You + # can pass the same options to this that you can to + # validate_attachment_file_size - :less_than, :greater_than, and :in. + # :less_than checks that a file is less than a certain size, :greater_than + # checks that a file is more than a certain size, and :in takes a Range or + # Array which specifies the lower and upper limits of the file size. + def should_validate_attachment_size name, options = {} + klass = self.name.gsub(/Test$/, '').constantize + min = options[:greater_than] || (options[:in] && options[:in].first) || 0 + max = options[:less_than] || (options[:in] && options[:in].last) || (1.0/0) + range = (min..max) + matcher = validate_attachment_size(name).in(range) + should matcher.description do + assert_accepts(matcher, klass) + end + end + end +end + +class Test::Unit::TestCase #:nodoc: + extend Paperclip::Shoulda +end diff --git a/vendor/plugins/paperclip/tasks/paperclip_tasks.rake b/vendor/plugins/paperclip/tasks/paperclip_tasks.rake new file mode 100644 index 0000000..23e4c11 --- /dev/null +++ b/vendor/plugins/paperclip/tasks/paperclip_tasks.rake @@ -0,0 +1,79 @@ +def obtain_class + class_name = ENV['CLASS'] || ENV['class'] + raise "Must specify CLASS" unless class_name + @klass = Object.const_get(class_name) +end + +def obtain_attachments + 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) + [ name ] + else + @klass.attachment_definitions.keys + end +end + +def for_all_attachments + klass = obtain_class + names = obtain_attachments + ids = klass.connection.select_values(klass.send(:construct_finder_sql, :select => 'id')) + + ids.each do |id| + instance = klass.find(id) + names.each do |name| + result = if instance.send("#{ name }?") + yield(instance, name) + else + true + end + print result ? "." : "x"; $stdout.flush + end + end + puts " Done." +end + +namespace :paperclip do + desc "Refreshes both metadata and thumbnails." + task :refresh => ["paperclip:refresh:metadata", "paperclip:refresh:thumbnails"] + + namespace :refresh do + desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)." + task :thumbnails => :environment do + errors = [] + for_all_attachments do |instance, name| + result = instance.send(name).reprocess! + errors << [instance.id, instance.errors] unless instance.errors.blank? + result + end + errors.each{|e| puts "#{e.first}: #{e.last.full_messages.inspect}" } + end + + desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)." + task :metadata => :environment do + for_all_attachments do |instance, name| + if file = instance.send(name).to_file + 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") + instance.save(false) + else + true + end + end + end + end + + desc "Cleans out invalid attachments. Useful after you've added new validations." + task :clean => :environment do + for_all_attachments do |instance, name| + instance.send(name).send(:validate) + if instance.send(name).valid? + true + else + instance.send("#{name}=", nil) + instance.save + end + end + end +end diff --git a/vendor/plugins/paperclip/test/.gitignore b/vendor/plugins/paperclip/test/.gitignore new file mode 100644 index 0000000..b14c548 --- /dev/null +++ b/vendor/plugins/paperclip/test/.gitignore @@ -0,0 +1 @@ +debug.log diff --git a/vendor/plugins/paperclip/test/attachment_test.rb b/vendor/plugins/paperclip/test/attachment_test.rb new file mode 100644 index 0000000..61ab2a2 --- /dev/null +++ b/vendor/plugins/paperclip/test/attachment_test.rb @@ -0,0 +1,742 @@ +require 'test/helper' + +class Dummy + # This is a dummy class +end + +class AttachmentTest < Test::Unit::TestCase + context "Attachment default_options" do + setup do + rebuild_model + @old_default_options = Paperclip::Attachment.default_options.dup + @new_default_options = @old_default_options.merge({ + :path => "argle/bargle", + :url => "fooferon", + :default_url => "not here.png" + }) + end + + teardown do + Paperclip::Attachment.default_options.merge! @old_default_options + end + + should "be overrideable" do + Paperclip::Attachment.default_options.merge!(@new_default_options) + @new_default_options.keys.each do |key| + assert_equal @new_default_options[key], + Paperclip::Attachment.default_options[key] + end + end + + context "without an Attachment" do + setup do + @dummy = Dummy.new + end + + should "return false when asked exists?" do + assert !@dummy.avatar.exists? + end + end + + context "on an Attachment" do + setup do + @dummy = Dummy.new + @attachment = @dummy.avatar + end + + Paperclip::Attachment.default_options.keys.each do |key| + should "be the default_options for #{key}" do + assert_equal @old_default_options[key], + @attachment.instance_variable_get("@#{key}"), + key + end + end + + context "when redefined" do + setup do + Paperclip::Attachment.default_options.merge!(@new_default_options) + @dummy = Dummy.new + @attachment = @dummy.avatar + end + + Paperclip::Attachment.default_options.keys.each do |key| + should "be the new default_options for #{key}" do + assert_equal @new_default_options[key], + @attachment.instance_variable_get("@#{key}"), + key + end + end + end + end + end + + context "An attachment with similarly named interpolations" do + setup do + rebuild_model :path => ":id.omg/:id-bbq/:idwhat/:id_partition.wtf" + @dummy = Dummy.new + @dummy.stubs(:id).returns(1024) + @file = File.new(File.join(File.dirname(__FILE__), + "fixtures", + "5k.png"), 'rb') + @dummy.avatar = @file + end + + teardown { @file.close } + + should "make sure that they are interpolated correctly" do + assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path + end + end + + context "An attachment with a :rails_env interpolation" do + setup do + @rails_env = "blah" + @id = 1024 + rebuild_model :path => ":rails_env/:id.png" + @dummy = Dummy.new + @dummy.stubs(:id).returns(@id) + @file = StringIO.new(".") + @dummy.avatar = @file + end + + should "return the proper path" do + temporary_rails_env(@rails_env) { + assert_equal "#{@rails_env}/#{@id}.png", @dummy.avatar.path + } + end + end + + context "An attachment with :convert_options" do + setup do + rebuild_model :styles => { + :thumb => "100x100", + :large => "400x400" + }, + :convert_options => { + :all => "-do_stuff", + :thumb => "-thumbnailize" + } + @dummy = Dummy.new + @dummy.avatar + end + + should "report the correct options when sent #extra_options_for(:thumb)" do + assert_equal "-thumbnailize -do_stuff", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect + end + + should "report the correct options when sent #extra_options_for(:large)" do + assert_equal "-do_stuff", @dummy.avatar.send(:extra_options_for, :large) + end + + before_should "call extra_options_for(:thumb/:large)" do + Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:thumb) + Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:large) + end + end + + context "An attachment with :convert_options that is a proc" do + setup do + rebuild_model :styles => { + :thumb => "100x100", + :large => "400x400" + }, + :convert_options => { + :all => lambda{|i| i.all }, + :thumb => lambda{|i| i.thumb } + } + Dummy.class_eval do + def all; "-all"; end + def thumb; "-thumb"; end + end + @dummy = Dummy.new + @dummy.avatar + end + + should "report the correct options when sent #extra_options_for(:thumb)" do + assert_equal "-thumb -all", @dummy.avatar.send(:extra_options_for, :thumb), @dummy.avatar.convert_options.inspect + end + + should "report the correct options when sent #extra_options_for(:large)" do + assert_equal "-all", @dummy.avatar.send(:extra_options_for, :large) + end + + before_should "call extra_options_for(:thumb/:large)" do + Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:thumb) + Paperclip::Attachment.any_instance.expects(:extra_options_for).with(:large) + end + end + + context "An attachment with :path that is a proc" do + setup do + rebuild_model :path => lambda{ |attachment| "path/#{attachment.instance.other}.:extension" } + + @file = File.new(File.join(File.dirname(__FILE__), + "fixtures", + "5k.png"), 'rb') + @dummyA = Dummy.new(:other => 'a') + @dummyA.avatar = @file + @dummyB = Dummy.new(:other => 'b') + @dummyB.avatar = @file + end + + teardown { @file.close } + + should "return correct path" do + assert_equal "path/a.png", @dummyA.avatar.path + assert_equal "path/b.png", @dummyB.avatar.path + end + end + + context "An attachment with :styles that is a proc" do + setup do + rebuild_model :styles => lambda{ |attachment| {:thumb => "50x50#", :large => "400x400"} } + + @attachment = Dummy.new.avatar + end + + should "have the correct geometry" do + assert_equal "50x50#", @attachment.styles[:thumb][:geometry] + end + end + + context "An attachment with :url that is a proc" do + setup do + rebuild_model :url => lambda{ |attachment| "path/#{attachment.instance.other}.:extension" } + + @file = File.new(File.join(File.dirname(__FILE__), + "fixtures", + "5k.png"), 'rb') + @dummyA = Dummy.new(:other => 'a') + @dummyA.avatar = @file + @dummyB = Dummy.new(:other => 'b') + @dummyB.avatar = @file + end + + teardown { @file.close } + + should "return correct url" do + assert_equal "path/a.png", @dummyA.avatar.url(:original, false) + assert_equal "path/b.png", @dummyB.avatar.url(:original, false) + end + end + + geometry_specs = [ + [ lambda{|z| "50x50#" }, :png ], + lambda{|z| "50x50#" }, + { :geometry => lambda{|z| "50x50#" } } + ] + geometry_specs.each do |geometry_spec| + context "An attachment geometry like #{geometry_spec}" do + setup do + rebuild_model :styles => { :normal => geometry_spec } + @attachment = Dummy.new.avatar + end + + should "not run the procs immediately" do + assert_kind_of Proc, @attachment.styles[:normal][:geometry] + end + + context "when assigned" do + setup do + @file = StringIO.new(".") + @attachment.assign(@file) + end + + should "have the correct geometry" do + assert_equal "50x50#", @attachment.styles[:normal][:geometry] + end + end + end + end + + context "An attachment with both 'normal' and hash-style styles" do + setup do + rebuild_model :styles => { + :normal => ["50x50#", :png], + :hash => { :geometry => "50x50#", :format => :png } + } + @dummy = Dummy.new + @attachment = @dummy.avatar + end + + [:processors, :whiny, :convert_options, :geometry, :format].each do |field| + should "have the same #{field} field" do + assert_equal @attachment.styles[:normal][field], @attachment.styles[:hash][field] + end + end + end + + context "An attachment with :processors that is a proc" do + setup do + rebuild_model :styles => { :normal => '' }, :processors => lambda { |a| [ :test ] } + @attachment = Dummy.new.avatar + end + + should "not run the proc immediately" do + assert_kind_of Proc, @attachment.styles[:normal][:processors] + end + + context "when assigned" do + setup do + @attachment.assign(StringIO.new(".")) + end + + should "have the correct processors" do + assert_equal [ :test ], @attachment.styles[:normal][:processors] + end + end + end + + context "An attachment with erroring processor" do + setup do + rebuild_model :processor => [:thumbnail], :styles => { :small => '' }, :whiny_thumbnails => true + @dummy = Dummy.new + Paperclip::Thumbnail.expects(:make).raises(Paperclip::PaperclipError, "cannot be processed.") + @file = StringIO.new("...") + @file.stubs(:to_tempfile).returns(@file) + @dummy.avatar = @file + end + + should "correctly forward processing error message to the instance" do + @dummy.valid? + assert_contains @dummy.errors.full_messages, "Avatar cannot be processed." + end + end + + context "An attachment with multiple processors" do + setup do + class Paperclip::Test < Paperclip::Processor; end + @style_params = { :once => {:one => 1, :two => 2} } + rebuild_model :processors => [:thumbnail, :test], :styles => @style_params + @dummy = Dummy.new + @file = StringIO.new("...") + @file.stubs(:to_tempfile).returns(@file) + Paperclip::Test.stubs(:make).returns(@file) + Paperclip::Thumbnail.stubs(:make).returns(@file) + end + + context "when assigned" do + setup { @dummy.avatar = @file } + + before_should "call #make on all specified processors" do + expected_params = @style_params[:once].merge({:processors => [:thumbnail, :test], :whiny => nil, :convert_options => ""}) + Paperclip::Thumbnail.expects(:make).with(@file, expected_params, @dummy.avatar).returns(@file) + Paperclip::Test.expects(:make).with(@file, expected_params, @dummy.avatar).returns(@file) + end + + before_should "call #make with attachment passed as third argument" do + expected_params = @style_params[:once].merge({:processors => [:thumbnail, :test], :whiny => nil, :convert_options => ""}) + Paperclip::Test.expects(:make).with(@file, expected_params, @dummy.avatar).returns(@file) + end + end + end + + context "An attachment with no processors defined" do + setup do + rebuild_model :processors => [], :styles => {:something => 1} + @dummy = Dummy.new + @file = StringIO.new("...") + end + should "raise when assigned to" do + assert_raises(RuntimeError){ @dummy.avatar = @file } + end + end + + context "Assigning an attachment with post_process hooks" do + setup do + rebuild_model :styles => { :something => "100x100#" } + Dummy.class_eval do + before_avatar_post_process :do_before_avatar + after_avatar_post_process :do_after_avatar + before_post_process :do_before_all + after_post_process :do_after_all + def do_before_avatar; end + def do_after_avatar; end + def do_before_all; end + def do_after_all; end + end + @file = StringIO.new(".") + @file.stubs(:to_tempfile).returns(@file) + @dummy = Dummy.new + Paperclip::Thumbnail.stubs(:make).returns(@file) + @attachment = @dummy.avatar + end + + should "call the defined callbacks when assigned" do + @dummy.expects(:do_before_avatar).with() + @dummy.expects(:do_after_avatar).with() + @dummy.expects(:do_before_all).with() + @dummy.expects(:do_after_all).with() + Paperclip::Thumbnail.expects(:make).returns(@file) + @dummy.avatar = @file + end + + should "not cancel the processing if a before_post_process returns nil" do + @dummy.expects(:do_before_avatar).with().returns(nil) + @dummy.expects(:do_after_avatar).with() + @dummy.expects(:do_before_all).with().returns(nil) + @dummy.expects(:do_after_all).with() + Paperclip::Thumbnail.expects(:make).returns(@file) + @dummy.avatar = @file + end + + should "cancel the processing if a before_post_process returns false" do + @dummy.expects(:do_before_avatar).never + @dummy.expects(:do_after_avatar).never + @dummy.expects(:do_before_all).with().returns(false) + @dummy.expects(:do_after_all).never + Paperclip::Thumbnail.expects(:make).never + @dummy.avatar = @file + end + + should "cancel the processing if a before_avatar_post_process returns false" do + @dummy.expects(:do_before_avatar).with().returns(false) + @dummy.expects(:do_after_avatar).never + @dummy.expects(:do_before_all).with().returns(true) + @dummy.expects(:do_after_all).never + Paperclip::Thumbnail.expects(:make).never + @dummy.avatar = @file + end + end + + context "Assigning an attachment" do + setup do + rebuild_model :styles => { :something => "100x100#" } + @file = StringIO.new(".") + @file.expects(:original_filename).returns("5k.png\n\n") + @file.expects(:content_type).returns("image/png\n\n") + @file.stubs(:to_tempfile).returns(@file) + @dummy = Dummy.new + Paperclip::Thumbnail.expects(:make).returns(@file) + @dummy.expects(:run_callbacks).with(:before_avatar_post_process, {:original => @file}) + @dummy.expects(:run_callbacks).with(:before_post_process, {:original => @file}) + @dummy.expects(:run_callbacks).with(:after_avatar_post_process, {:original => @file, :something => @file}) + @dummy.expects(:run_callbacks).with(:after_post_process, {:original => @file, :something => @file}) + @attachment = @dummy.avatar + @dummy.avatar = @file + end + + should "strip whitespace from original_filename field" do + assert_equal "5k.png", @dummy.avatar.original_filename + end + + should "strip whitespace from content_type field" do + assert_equal "image/png", @dummy.avatar.instance.avatar_content_type + end + end + + context "Attachment with strange letters" do + setup do + rebuild_model + + @not_file = mock + @tempfile = mock + @not_file.stubs(:nil?).returns(false) + @not_file.expects(:size).returns(10) + @tempfile.expects(:size).returns(10) + @not_file.expects(:to_tempfile).returns(@tempfile) + @not_file.expects(:original_filename).returns("sheep_say_bæ.png\r\n") + @not_file.expects(:content_type).returns("image/png\r\n") + + @dummy = Dummy.new + @attachment = @dummy.avatar + @attachment.expects(:valid_assignment?).with(@not_file).returns(true) + @attachment.expects(:queue_existing_for_delete) + @attachment.expects(:post_process) + @attachment.expects(:valid?).returns(true) + @attachment.expects(:validate) + @dummy.avatar = @not_file + end + + should "remove strange letters and replace with underscore (_)" do + assert_equal "sheep_say_b_.png", @dummy.avatar.original_filename + end + + end + + context "An attachment" do + setup do + Paperclip::Attachment.default_options.merge!({ + :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension" + }) + FileUtils.rm_rf("tmp") + rebuild_model + @instance = Dummy.new + @attachment = Paperclip::Attachment.new(:avatar, @instance) + @file = File.new(File.join(File.dirname(__FILE__), + "fixtures", + "5k.png"), 'rb') + end + + teardown { @file.close } + + should "raise if there are not the correct columns when you try to assign" do + @other_attachment = Paperclip::Attachment.new(:not_here, @instance) + assert_raises(Paperclip::PaperclipError) do + @other_attachment.assign(@file) + end + end + + should "return its default_url when no file assigned" do + assert @attachment.to_file.nil? + assert_equal "/avatars/original/missing.png", @attachment.url + assert_equal "/avatars/blah/missing.png", @attachment.url(:blah) + end + + should "return nil as path when no file assigned" do + assert @attachment.to_file.nil? + assert_equal nil, @attachment.path + assert_equal nil, @attachment.path(:blah) + end + + context "with a file assigned in the database" do + setup do + @attachment.stubs(:instance_read).with(:file_name).returns("5k.png") + @attachment.stubs(:instance_read).with(:content_type).returns("image/png") + @attachment.stubs(:instance_read).with(:file_size).returns(12345) + now = Time.now + Time.stubs(:now).returns(now) + @attachment.stubs(:instance_read).with(:updated_at).returns(Time.now) + end + + should "return a correct url even if the file does not exist" do + assert_nil @attachment.to_file + assert_match %r{^/system/avatars/#{@instance.id}/blah/5k\.png}, @attachment.url(:blah) + end + + should "make sure the updated_at mtime is in the url if it is defined" do + assert_match %r{#{Time.now.to_i}$}, @attachment.url(:blah) + end + + should "make sure the updated_at mtime is NOT in the url if false is passed to the url method" do + assert_no_match %r{#{Time.now.to_i}$}, @attachment.url(:blah, false) + end + + context "with the updated_at field removed" do + setup do + @attachment.stubs(:instance_read).with(:updated_at).returns(nil) + end + + should "only return the url without the updated_at when sent #url" do + assert_match "/avatars/#{@instance.id}/blah/5k.png", @attachment.url(:blah) + end + end + + should "return the proper path when filename has a single .'s" do + assert_equal "./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.png", @attachment.path + end + + should "return the proper path when filename has multiple .'s" do + @attachment.stubs(:instance_read).with(:file_name).returns("5k.old.png") + assert_equal "./test/../tmp/avatars/dummies/original/#{@instance.id}/5k.old.png", @attachment.path + end + + context "when expecting three styles" do + setup do + styles = {:styles => { :large => ["400x400", :png], + :medium => ["100x100", :gif], + :small => ["32x32#", :jpg]}} + @attachment = Paperclip::Attachment.new(:avatar, + @instance, + styles) + end + + context "and assigned a file" do + setup do + now = Time.now + Time.stubs(:now).returns(now) + @attachment.assign(@file) + end + + should "be dirty" do + assert @attachment.dirty? + end + + context "and saved" do + setup do + @attachment.save + end + + should "return the real url" do + file = @attachment.to_file + assert file + assert_match %r{^/system/avatars/#{@instance.id}/original/5k\.png}, @attachment.url + assert_match %r{^/system/avatars/#{@instance.id}/small/5k\.jpg}, @attachment.url(:small) + file.close + end + + should "commit the files to disk" do + [:large, :medium, :small].each do |style| + io = @attachment.to_io(style) + assert File.exists?(io) + assert ! io.is_a?(::Tempfile) + io.close + end + end + + should "save the files as the right formats and sizes" do + [[:large, 400, 61, "PNG"], + [:medium, 100, 15, "GIF"], + [:small, 32, 32, "JPEG"]].each do |style| + cmd = %Q[identify -format "%w %h %b %m" "#{@attachment.path(style.first)}"] + out = `#{cmd}` + width, height, size, format = out.split(" ") + assert_equal style[1].to_s, width.to_s + assert_equal style[2].to_s, height.to_s + assert_equal style[3].to_s, format.to_s + end + end + + should "still have its #file attribute not be nil" do + assert ! (file = @attachment.to_file).nil? + file.close + end + + context "and trying to delete" do + setup do + @existing_names = @attachment.styles.keys.collect do |style| + @attachment.path(style) + end + end + + should "delete the files after assigning nil" do + @attachment.expects(:instance_write).with(:file_name, nil) + @attachment.expects(:instance_write).with(:content_type, nil) + @attachment.expects(:instance_write).with(:file_size, nil) + @attachment.expects(:instance_write).with(:updated_at, nil) + @attachment.assign nil + @attachment.save + @existing_names.each{|f| assert ! File.exists?(f) } + end + + should "delete the files when you call #clear and #save" do + @attachment.expects(:instance_write).with(:file_name, nil) + @attachment.expects(:instance_write).with(:content_type, nil) + @attachment.expects(:instance_write).with(:file_size, nil) + @attachment.expects(:instance_write).with(:updated_at, nil) + @attachment.clear + @attachment.save + @existing_names.each{|f| assert ! File.exists?(f) } + end + + should "delete the files when you call #delete" do + @attachment.expects(:instance_write).with(:file_name, nil) + @attachment.expects(:instance_write).with(:content_type, nil) + @attachment.expects(:instance_write).with(:file_size, nil) + @attachment.expects(:instance_write).with(:updated_at, nil) + @attachment.destroy + @existing_names.each{|f| assert ! File.exists?(f) } + end + end + end + end + end + + end + + context "when trying a nonexistant storage type" do + setup do + rebuild_model :storage => :not_here + end + + should "not be able to find the module" do + assert_raise(NameError){ Dummy.new.avatar } + end + end + end + + context "An attachment with only a avatar_file_name column" do + setup do + ActiveRecord::Base.connection.create_table :dummies, :force => true do |table| + table.column :avatar_file_name, :string + end + rebuild_class + @dummy = Dummy.new + @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb') + end + + teardown { @file.close } + + should "not error when assigned an attachment" do + assert_nothing_raised { @dummy.avatar = @file } + end + + should "return the time when sent #avatar_updated_at" do + now = Time.now + Time.stubs(:now).returns(now) + @dummy.avatar = @file + assert now, @dummy.avatar.updated_at + end + + should "return nil when reloaded and sent #avatar_updated_at" do + @dummy.save + @dummy.reload + assert_nil @dummy.avatar.updated_at + end + + should "return the right value when sent #avatar_file_size" do + @dummy.avatar = @file + assert_equal @file.size, @dummy.avatar.size + end + + context "and avatar_updated_at column" do + setup do + ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp + rebuild_class + @dummy = Dummy.new + end + + should "not error when assigned an attachment" do + assert_nothing_raised { @dummy.avatar = @file } + end + + should "return the right value when sent #avatar_updated_at" do + now = Time.now + Time.stubs(:now).returns(now) + @dummy.avatar = @file + assert_equal now.to_i, @dummy.avatar.updated_at + end + end + + context "and avatar_content_type column" do + setup do + ActiveRecord::Base.connection.add_column :dummies, :avatar_content_type, :string + rebuild_class + @dummy = Dummy.new + end + + should "not error when assigned an attachment" do + assert_nothing_raised { @dummy.avatar = @file } + end + + should "return the right value when sent #avatar_content_type" do + @dummy.avatar = @file + assert_equal "image/png", @dummy.avatar.content_type + end + end + + context "and avatar_file_size column" do + setup do + ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :integer + rebuild_class + @dummy = Dummy.new + end + + should "not error when assigned an attachment" do + assert_nothing_raised { @dummy.avatar = @file } + end + + should "return the right value when sent #avatar_file_size" do + @dummy.avatar = @file + assert_equal @file.size, @dummy.avatar.size + end + + should "return the right value when saved, reloaded, and sent #avatar_file_size" do + @dummy.avatar = @file + @dummy.save + @dummy = Dummy.find(@dummy.id) + assert_equal @file.size, @dummy.avatar.size + end + end + end +end diff --git a/vendor/plugins/paperclip/test/fixtures/12k.png b/vendor/plugins/paperclip/test/fixtures/12k.png new file mode 100644 index 0000000..f819d45 Binary files /dev/null and b/vendor/plugins/paperclip/test/fixtures/12k.png differ diff --git a/vendor/plugins/paperclip/test/fixtures/50x50.png b/vendor/plugins/paperclip/test/fixtures/50x50.png new file mode 100644 index 0000000..63f5646 Binary files /dev/null and b/vendor/plugins/paperclip/test/fixtures/50x50.png differ diff --git a/vendor/plugins/paperclip/test/fixtures/5k.png b/vendor/plugins/paperclip/test/fixtures/5k.png new file mode 100644 index 0000000..75d9f04 Binary files /dev/null and b/vendor/plugins/paperclip/test/fixtures/5k.png differ diff --git a/vendor/plugins/paperclip/test/fixtures/bad.png b/vendor/plugins/paperclip/test/fixtures/bad.png new file mode 100644 index 0000000..7ba4f07 --- /dev/null +++ b/vendor/plugins/paperclip/test/fixtures/bad.png @@ -0,0 +1 @@ +This is not an image. diff --git a/vendor/plugins/paperclip/test/fixtures/text.txt b/vendor/plugins/paperclip/test/fixtures/text.txt new file mode 100644 index 0000000..e69de29 diff --git a/vendor/plugins/paperclip/test/fixtures/twopage.pdf b/vendor/plugins/paperclip/test/fixtures/twopage.pdf new file mode 100644 index 0000000..0c34a51 Binary files /dev/null and b/vendor/plugins/paperclip/test/fixtures/twopage.pdf differ diff --git a/vendor/plugins/paperclip/test/geometry_test.rb b/vendor/plugins/paperclip/test/geometry_test.rb new file mode 100644 index 0000000..134372d --- /dev/null +++ b/vendor/plugins/paperclip/test/geometry_test.rb @@ -0,0 +1,168 @@ +require 'test/helper' + +class GeometryTest < Test::Unit::TestCase + context "Paperclip::Geometry" do + should "correctly report its given dimensions" do + assert @geo = Paperclip::Geometry.new(1024, 768) + assert_equal 1024, @geo.width + assert_equal 768, @geo.height + end + + should "set height to 0 if height dimension is missing" do + assert @geo = Paperclip::Geometry.new(1024) + assert_equal 1024, @geo.width + assert_equal 0, @geo.height + end + + should "set width to 0 if width dimension is missing" do + assert @geo = Paperclip::Geometry.new(nil, 768) + assert_equal 0, @geo.width + assert_equal 768, @geo.height + end + + should "be generated from a WxH-formatted string" do + assert @geo = Paperclip::Geometry.parse("800x600") + assert_equal 800, @geo.width + assert_equal 600, @geo.height + end + + should "be generated from a xH-formatted string" do + assert @geo = Paperclip::Geometry.parse("x600") + assert_equal 0, @geo.width + assert_equal 600, @geo.height + end + + should "be generated from a Wx-formatted string" do + assert @geo = Paperclip::Geometry.parse("800x") + assert_equal 800, @geo.width + assert_equal 0, @geo.height + end + + should "be generated from a W-formatted string" do + assert @geo = Paperclip::Geometry.parse("800") + assert_equal 800, @geo.width + assert_equal 0, @geo.height + end + + should "ensure the modifier is nil if not present" do + assert @geo = Paperclip::Geometry.parse("123x456") + assert_nil @geo.modifier + end + + ['>', '<', '#', '@', '%', '^', '!', nil].each do |mod| + should "ensure the modifier #{mod.inspect} is preserved" do + assert @geo = Paperclip::Geometry.parse("123x456#{mod}") + assert_equal mod, @geo.modifier + assert_equal "123x456#{mod}", @geo.to_s + end + end + + ['>', '<', '#', '@', '%', '^', '!', nil].each do |mod| + should "ensure the modifier #{mod.inspect} is preserved with no height" do + assert @geo = Paperclip::Geometry.parse("123x#{mod}") + assert_equal mod, @geo.modifier + assert_equal "123#{mod}", @geo.to_s + end + end + + should "make sure the modifier gets passed during transformation_to" do + assert @src = Paperclip::Geometry.parse("123x456") + assert @dst = Paperclip::Geometry.parse("123x456>") + assert_equal "123x456>", @src.transformation_to(@dst).to_s + end + + should "generate correct ImageMagick formatting string for W-formatted string" do + assert @geo = Paperclip::Geometry.parse("800") + assert_equal "800", @geo.to_s + end + + should "generate correct ImageMagick formatting string for Wx-formatted string" do + assert @geo = Paperclip::Geometry.parse("800x") + assert_equal "800", @geo.to_s + end + + should "generate correct ImageMagick formatting string for xH-formatted string" do + assert @geo = Paperclip::Geometry.parse("x600") + assert_equal "x600", @geo.to_s + end + + should "generate correct ImageMagick formatting string for WxH-formatted string" do + assert @geo = Paperclip::Geometry.parse("800x600") + assert_equal "800x600", @geo.to_s + end + + should "be generated from a file" do + file = File.join(File.dirname(__FILE__), "fixtures", "5k.png") + file = File.new(file, 'rb') + assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) } + assert @geo.height > 0 + assert @geo.width > 0 + end + + should "be generated from a file path" do + file = File.join(File.dirname(__FILE__), "fixtures", "5k.png") + assert_nothing_raised{ @geo = Paperclip::Geometry.from_file(file) } + assert @geo.height > 0 + assert @geo.width > 0 + end + + should "not generate from a bad file" do + file = "/home/This File Does Not Exist.omg" + assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) } + 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| + context "performing calculations on a #{args[0]} viewport" do + setup do + @geo = Paperclip::Geometry.new(args[1], args[2]) + end + + should "#{args[3] ? "" : "not"} be vertical" do + assert_equal args[3], @geo.vertical? + end + + should "#{args[4] ? "" : "not"} be horizontal" do + assert_equal args[4], @geo.horizontal? + end + + should "#{args[5] ? "" : "not"} be square" do + assert_equal args[5], @geo.square? + end + + should "report that #{args[6]} is the larger dimension" do + assert_equal args[6], @geo.larger + end + + should "report that #{args[7]} is the smaller dimension" do + assert_equal args[7], @geo.smaller + end + + should "have an aspect ratio of #{args[8]}" do + assert_in_delta args[8], @geo.aspect, 0.0001 + end + end + end + + [[ [1000, 100], [64, 64], "x64", "64x64+288+0" ], + [ [100, 1000], [50, 950], "x950", "50x950+22+0" ], + [ [100, 1000], [50, 25], "50x", "50x25+0+237" ]]. each do |args| + context "of #{args[0].inspect} and given a Geometry #{args[1].inspect} and sent transform_to" do + setup do + @geo = Paperclip::Geometry.new(*args[0]) + @dst = Paperclip::Geometry.new(*args[1]) + @scale, @crop = @geo.transformation_to @dst, true + end + + should "be able to return the correct scaling transformation geometry #{args[2]}" do + assert_equal args[2], @scale + end + + should "be able to return the correct crop transformation geometry #{args[3]}" do + assert_equal args[3], @crop + end + end + end + end +end diff --git a/vendor/plugins/paperclip/test/helper.rb b/vendor/plugins/paperclip/test/helper.rb new file mode 100644 index 0000000..3e7e6a1 --- /dev/null +++ b/vendor/plugins/paperclip/test/helper.rb @@ -0,0 +1,82 @@ +require 'rubygems' +require 'test/unit' +gem 'thoughtbot-shoulda', ">= 2.9.0" +require 'shoulda' +require 'mocha' +require 'tempfile' + +gem 'sqlite3-ruby' + +require 'active_record' +require 'active_support' +begin + require 'ruby-debug' +rescue LoadError + puts "ruby-debug not loaded" +end + +ROOT = File.join(File.dirname(__FILE__), '..') +RAILS_ROOT = ROOT + +$LOAD_PATH << File.join(ROOT, 'lib') +$LOAD_PATH << File.join(ROOT, 'lib', 'paperclip') + +require File.join(ROOT, 'lib', 'paperclip.rb') + +require 'shoulda_macros/paperclip' + +ENV['RAILS_ENV'] ||= 'test' + +FIXTURES_DIR = File.join(File.dirname(__FILE__), "fixtures") +config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml')) +ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log") +ActiveRecord::Base.establish_connection(config['test']) + +def reset_class class_name + ActiveRecord::Base.send(:include, Paperclip) + Object.send(:remove_const, class_name) rescue nil + klass = Object.const_set(class_name, Class.new(ActiveRecord::Base)) + klass.class_eval{ include Paperclip } + klass +end + +def reset_table table_name, &block + block ||= lambda{ true } + ActiveRecord::Base.connection.create_table :dummies, {:force => true}, &block +end + +def modify_table table_name, &block + ActiveRecord::Base.connection.change_table :dummies, &block +end + +def rebuild_model options = {} + ActiveRecord::Base.connection.create_table :dummies, :force => true do |table| + table.column :other, :string + table.column :avatar_file_name, :string + table.column :avatar_content_type, :string + table.column :avatar_file_size, :integer + table.column :avatar_updated_at, :datetime + end + rebuild_class options +end + +def rebuild_class options = {} + ActiveRecord::Base.send(:include, Paperclip) + Object.send(:remove_const, "Dummy") rescue nil + Object.const_set("Dummy", Class.new(ActiveRecord::Base)) + Dummy.class_eval do + include Paperclip + has_attached_file :avatar, options + end +end + +def temporary_rails_env(new_env) + old_env = defined?(RAILS_ENV) ? RAILS_ENV : nil + silence_warnings do + Object.const_set("RAILS_ENV", new_env) + end + yield + silence_warnings do + Object.const_set("RAILS_ENV", old_env) + end +end diff --git a/vendor/plugins/paperclip/test/integration_test.rb b/vendor/plugins/paperclip/test/integration_test.rb new file mode 100644 index 0000000..f7014ac --- /dev/null +++ b/vendor/plugins/paperclip/test/integration_test.rb @@ -0,0 +1,481 @@ +require 'test/helper' + +class IntegrationTest < Test::Unit::TestCase + context "Many models at once" do + setup do + rebuild_model + @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') + 300.times do |i| + Dummy.create! :avatar => @file + end + end + + should "not exceed the open file limit" do + assert_nothing_raised do + dummies = Dummy.find(:all) + dummies.each { |dummy| dummy.avatar } + end + end + end + + context "An attachment" do + setup do + rebuild_model :styles => { :thumb => "50x50#" } + @dummy = Dummy.new + @file = File.new(File.join(File.dirname(__FILE__), + "fixtures", + "5k.png"), 'rb') + @dummy.avatar = @file + assert @dummy.save + end + + teardown { @file.close } + + should "create its thumbnails properly" do + assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:thumb)}"` + end + + context "redefining its attachment styles" do + setup do + Dummy.class_eval do + has_attached_file :avatar, :styles => { :thumb => "150x25#" } + has_attached_file :avatar, :styles => { :thumb => "150x25#", :dynamic => lambda { |a| '50x50#' } } + end + @d2 = Dummy.find(@dummy.id) + @d2.avatar.reprocess! + @d2.save + end + + should "create its thumbnails properly" do + assert_match /\b150x25\b/, `identify "#{@dummy.avatar.path(:thumb)}"` + assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:dynamic)}"` + end + end + end + + context "A model that modifies its original" do + setup do + rebuild_model :styles => { :original => "2x2#" } + @dummy = Dummy.new + @file = File.new(File.join(File.dirname(__FILE__), + "fixtures", + "5k.png"), 'rb') + @dummy.avatar = @file + end + + should "report the file size of the processed file and not the original" do + assert_not_equal @file.size, @dummy.avatar.size + end + + teardown { @file.close } + end + + context "A model with attachments scoped under an id" do + setup do + rebuild_model :styles => { :large => "100x100", + :medium => "50x50" }, + :path => ":rails_root/tmp/:id/:attachments/:style.:extension" + @dummy = Dummy.new + @file = File.new(File.join(File.dirname(__FILE__), + "fixtures", + "5k.png"), 'rb') + @dummy.avatar = @file + end + + teardown { @file.close } + + context "when saved" do + setup do + @dummy.save + @saved_path = @dummy.avatar.path(:large) + end + + should "have a large file in the right place" do + assert File.exists?(@dummy.avatar.path(:large)) + end + + context "and deleted" do + setup do + @dummy.avatar.clear + @dummy.save + end + + should "not have a large file in the right place anymore" do + assert ! File.exists?(@saved_path) + end + + should "not have its next two parent directories" do + assert ! File.exists?(File.dirname(@saved_path)) + assert ! File.exists?(File.dirname(File.dirname(@saved_path))) + end + + before_should "not die if an unexpected SystemCallError happens" do + FileUtils.stubs(:rmdir).raises(Errno::EPIPE) + end + end + end + end + + context "A model with no attachment validation" 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 false when asked about whiny_thumbnails" do + assert ! Dummy.attachment_definitions[:avatar][:whiny_thumbnails] + end + + context "when validates_attachment_thumbnails is called" do + setup do + Dummy.validates_attachment_thumbnails :avatar + end + + should "have its definition return true when asked about whiny_thumbnails" do + assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails] + end + end + + context "redefined to have attachment validations" do + setup do + rebuild_model :styles => { :large => "300x300>", + :medium => "100x100", + :thumb => ["32x32#", :gif] }, + :whiny_thumbnails => true, + :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 true when asked about whiny_thumbnails" do + assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails] + end + end + end + + context "A model with no convert_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 convert_options" do + assert ! Dummy.attachment_definitions[:avatar][:convert_options] + end + + context "redefined to have convert_options setting" do + setup do + rebuild_model :styles => { :large => "300x300>", + :medium => "100x100", + :thumb => ["32x32#", :gif] }, + :convert_options => "-strip -depth 8", + :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 convert_options value when asked about convert_options" do + assert_equal "-strip -depth 8", Dummy.attachment_definitions[:avatar][:convert_options] + end + end + end + + context "A model with a filesystem attachment" do + setup do + rebuild_model :styles => { :large => "300x300>", + :medium => "100x100", + :thumb => ["32x32#", :gif] }, + :whiny_thumbnails => true, + :default_style => :medium, + :url => "/:attachment/:class/:style/:id/:basename.:extension", + :path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension" + @dummy = Dummy.new + @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') + @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb') + + assert @dummy.avatar = @file + assert @dummy.valid? + assert @dummy.save + end + + should "write and delete its files" do + [["434x66", :original], + ["300x46", :large], + ["100x15", :medium], + ["32x32", :thumb]].each do |geo, style| + cmd = %Q[identify -format "%wx%h" "#{@dummy.avatar.path(style)}"] + assert_equal geo, `#{cmd}`.chomp, cmd + end + + saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.path(s) } + + @d2 = Dummy.find(@dummy.id) + assert_equal "100x15", `identify -format "%wx%h" "#{@d2.avatar.path}"`.chomp + assert_equal "434x66", `identify -format "%wx%h" "#{@d2.avatar.path(:original)}"`.chomp + assert_equal "300x46", `identify -format "%wx%h" "#{@d2.avatar.path(:large)}"`.chomp + assert_equal "100x15", `identify -format "%wx%h" "#{@d2.avatar.path(:medium)}"`.chomp + assert_equal "32x32", `identify -format "%wx%h" "#{@d2.avatar.path(:thumb)}"`.chomp + + @dummy.avatar = "not a valid file but not nil" + assert_equal File.basename(@file.path), @dummy.avatar_file_name + assert @dummy.valid? + assert @dummy.save + + saved_paths.each do |p| + assert File.exists?(p) + end + + @dummy.avatar.clear + assert_nil @dummy.avatar_file_name + assert @dummy.valid? + assert @dummy.save + + saved_paths.each do |p| + assert ! File.exists?(p) + end + + @d2 = Dummy.find(@dummy.id) + assert_nil @d2.avatar_file_name + end + + should "work exactly the same when new as when reloaded" do + @d2 = Dummy.find(@dummy.id) + + assert_equal @dummy.avatar_file_name, @d2.avatar_file_name + [:thumb, :medium, :large, :original].each do |style| + assert_equal @dummy.avatar.path(style), @d2.avatar.path(style) + end + + saved_paths = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.path(s) } + + @d2.avatar.clear + assert @d2.save + + saved_paths.each do |p| + assert ! File.exists?(p) + end + end + + should "know the difference between good files, bad files, and not files" do + expected = @dummy.avatar.to_file + @dummy.avatar = "not a file" + assert @dummy.valid? + assert_equal expected.path, @dummy.avatar.path + expected.close + + @dummy.avatar = @bad_file + assert ! @dummy.valid? + end + + should "know the difference between good files, bad files, and not files when validating" do + Dummy.validates_attachment_presence :avatar + @d2 = Dummy.find(@dummy.id) + @d2.avatar = @file + assert @d2.valid?, @d2.errors.full_messages.inspect + @d2.avatar = @bad_file + assert ! @d2.valid? + end + + should "be able to reload without saving and not have the file disappear" do + @dummy.avatar = @file + assert @dummy.save + @dummy.avatar.clear + assert_nil @dummy.avatar_file_name + @dummy.reload + assert_equal "5k.png", @dummy.avatar_file_name + end + + context "that is assigned its file from another Paperclip attachment" do + setup do + @dummy2 = Dummy.new + @file2 = File.new(File.join(FIXTURES_DIR, "12k.png"), 'rb') + assert @dummy2.avatar = @file2 + @dummy2.save + end + + should "work when assigned a file" do + assert_not_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`, + `identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"` + + assert @dummy.avatar = @dummy2.avatar + @dummy.save + assert_equal `identify -format "%wx%h" "#{@dummy.avatar.path(:original)}"`, + `identify -format "%wx%h" "#{@dummy2.avatar.path(:original)}"` + end + end + + end + + context "A model with an attachments association and a Paperclip attachment" do + setup do + Dummy.class_eval do + has_many :attachments, :class_name => 'Dummy' + end + + @dummy = Dummy.new + @dummy.avatar = File.new(File.join(File.dirname(__FILE__), + "fixtures", + "5k.png"), 'rb') + end + + should "should not error when saving" do + assert_nothing_raised do + @dummy.save! + end + end + end + + if ENV['S3_TEST_BUCKET'] + def s3_files_for attachment + [:thumb, :medium, :large, :original].inject({}) do |files, style| + data = `curl "#{attachment.url(style)}" 2>/dev/null`.chomp + t = Tempfile.new("paperclip-test") + t.binmode + t.write(data) + t.rewind + files[style] = t + files + end + end + + def s3_headers_for attachment, style + `curl --head "#{attachment.url(style)}" 2>/dev/null`.split("\n").inject({}) do |h,head| + split_head = head.chomp.split(/\s*:\s*/, 2) + h[split_head.first.downcase] = split_head.last unless split_head.empty? + h + end + end + + context "A model with an S3 attachment" do + setup do + rebuild_model :styles => { :large => "300x300>", + :medium => "100x100", + :thumb => ["32x32#", :gif] }, + :storage => :s3, + :whiny_thumbnails => true, + # :s3_options => {:logger => Logger.new(StringIO.new)}, + :s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")), + :default_style => :medium, + :bucket => ENV['S3_TEST_BUCKET'], + :path => ":class/:attachment/:id/:style/:basename.:extension" + @dummy = Dummy.new + @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') + @bad_file = File.new(File.join(FIXTURES_DIR, "bad.png"), 'rb') + + assert @dummy.avatar = @file + assert @dummy.valid? + assert @dummy.save + + @files_on_s3 = s3_files_for @dummy.avatar + end + + should "write and delete its files" do + [["434x66", :original], + ["300x46", :large], + ["100x15", :medium], + ["32x32", :thumb]].each do |geo, style| + cmd = %Q[identify -format "%wx%h" "#{@files_on_s3[style].path}"] + assert_equal geo, `#{cmd}`.chomp, cmd + end + + @d2 = Dummy.find(@dummy.id) + @d2_files = s3_files_for @d2.avatar + [["434x66", :original], + ["300x46", :large], + ["100x15", :medium], + ["32x32", :thumb]].each do |geo, style| + cmd = %Q[identify -format "%wx%h" "#{@d2_files[style].path}"] + assert_equal geo, `#{cmd}`.chomp, cmd + end + + @dummy.avatar = "not a valid file but not nil" + assert_equal File.basename(@file.path), @dummy.avatar_file_name + assert @dummy.valid? + assert @dummy.save + + saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) } + + saved_keys.each do |key| + assert key.exists? + end + + @dummy.avatar.clear + assert_nil @dummy.avatar_file_name + assert @dummy.valid? + assert @dummy.save + + saved_keys.each do |key| + assert ! key.exists? + end + + @d2 = Dummy.find(@dummy.id) + assert_nil @d2.avatar_file_name + end + + should "work exactly the same when new as when reloaded" do + @d2 = Dummy.find(@dummy.id) + + assert_equal @dummy.avatar_file_name, @d2.avatar_file_name + [:thumb, :medium, :large, :original].each do |style| + assert_equal @dummy.avatar.to_file(style).to_s, @d2.avatar.to_file(style).to_s + end + + saved_keys = [:thumb, :medium, :large, :original].collect{|s| @dummy.avatar.to_file(s) } + + @d2.avatar.clear + assert @d2.save + + saved_keys.each do |key| + assert ! key.exists? + end + end + + should "know the difference between good files, bad files, not files, and nil" do + expected = @dummy.avatar.to_file + @dummy.avatar = "not a file" + assert @dummy.valid? + assert_equal expected.full_name, @dummy.avatar.to_file.full_name + + @dummy.avatar = @bad_file + assert ! @dummy.valid? + @dummy.avatar = nil + assert @dummy.valid? + + Dummy.validates_attachment_presence :avatar + @d2 = Dummy.find(@dummy.id) + @d2.avatar = @file + assert @d2.valid? + @d2.avatar = @bad_file + assert ! @d2.valid? + @d2.avatar = nil + assert ! @d2.valid? + end + + should "be able to reload without saving and not have the file disappear" do + @dummy.avatar = @file + assert @dummy.save + @dummy.avatar = nil + assert_nil @dummy.avatar_file_name + @dummy.reload + assert_equal "5k.png", @dummy.avatar_file_name + end + + should "have the right content type" do + headers = s3_headers_for(@dummy.avatar, :original) + p headers + assert_equal 'image/png', headers['content-type'] + end + end + end +end + diff --git a/vendor/plugins/paperclip/test/iostream_test.rb b/vendor/plugins/paperclip/test/iostream_test.rb new file mode 100644 index 0000000..97030b5 --- /dev/null +++ b/vendor/plugins/paperclip/test/iostream_test.rb @@ -0,0 +1,71 @@ +require 'test/helper' + +class IOStreamTest < Test::Unit::TestCase + context "IOStream" do + should "be included in IO, File, Tempfile, and StringIO" do + [IO, File, Tempfile, StringIO].each do |klass| + assert klass.included_modules.include?(IOStream), "Not in #{klass}" + end + end + end + + context "A file" do + setup do + @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb') + end + + teardown { @file.close } + + context "that is sent #stream_to" do + + context "and given a String" do + setup do + FileUtils.mkdir_p(File.join(ROOT, 'tmp')) + assert @result = @file.stream_to(File.join(ROOT, 'tmp', 'iostream.string.test')) + end + + should "return a File" do + assert @result.is_a?(File) + end + + should "contain the same data as the original file" do + @file.rewind; @result.rewind + assert_equal @file.read, @result.read + end + end + + context "and given a Tempfile" do + setup do + tempfile = Tempfile.new('iostream.test') + tempfile.binmode + assert @result = @file.stream_to(tempfile) + end + + should "return a Tempfile" do + assert @result.is_a?(Tempfile) + end + + should "contain the same data as the original file" do + @file.rewind; @result.rewind + assert_equal @file.read, @result.read + end + end + + end + + context "that is sent #to_tempfile" do + setup do + assert @tempfile = @file.to_tempfile + end + + should "convert it to a Tempfile" do + assert @tempfile.is_a?(Tempfile) + end + + should "have the Tempfile contain the same data as the file" do + @file.rewind; @tempfile.rewind + assert_equal @file.read, @tempfile.read + end + end + end +end diff --git a/vendor/plugins/paperclip/test/matchers/have_attached_file_matcher_test.rb b/vendor/plugins/paperclip/test/matchers/have_attached_file_matcher_test.rb new file mode 100644 index 0000000..b29ec37 --- /dev/null +++ b/vendor/plugins/paperclip/test/matchers/have_attached_file_matcher_test.rb @@ -0,0 +1,21 @@ +require 'test/helper' + +class HaveAttachedFileMatcherTest < Test::Unit::TestCase + context "have_attached_file" do + setup do + @dummy_class = reset_class "Dummy" + reset_table "dummies" + @matcher = self.class.have_attached_file(:avatar) + end + + should "reject a class with no attachment" do + assert_rejects @matcher, @dummy_class + end + + should "accept a class with an attachment" do + modify_table("dummies"){|d| d.string :avatar_file_name } + @dummy_class.has_attached_file :avatar + assert_accepts @matcher, @dummy_class + end + end +end diff --git a/vendor/plugins/paperclip/test/matchers/validate_attachment_content_type_matcher_test.rb b/vendor/plugins/paperclip/test/matchers/validate_attachment_content_type_matcher_test.rb new file mode 100644 index 0000000..241eb5f --- /dev/null +++ b/vendor/plugins/paperclip/test/matchers/validate_attachment_content_type_matcher_test.rb @@ -0,0 +1,30 @@ +require 'test/helper' + +class ValidateAttachmentContentTypeMatcherTest < Test::Unit::TestCase + context "validate_attachment_content_type" do + setup do + reset_table("dummies") do |d| + d.string :avatar_file_name + end + @dummy_class = reset_class "Dummy" + @dummy_class.has_attached_file :avatar + @matcher = self.class.validate_attachment_content_type(:avatar). + allowing(%w(image/png image/jpeg)). + rejecting(%w(audio/mp3 application/octet-stream)) + end + + should "reject a class with no validation" do + assert_rejects @matcher, @dummy_class + end + + should "reject a class with a validation that doesn't match" do + @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{audio/.*} + assert_rejects @matcher, @dummy_class + end + + should "accept a class with a validation" do + @dummy_class.validates_attachment_content_type :avatar, :content_type => %r{image/.*} + assert_accepts @matcher, @dummy_class + end + end +end diff --git a/vendor/plugins/paperclip/test/matchers/validate_attachment_presence_matcher_test.rb b/vendor/plugins/paperclip/test/matchers/validate_attachment_presence_matcher_test.rb new file mode 100644 index 0000000..860a760 --- /dev/null +++ b/vendor/plugins/paperclip/test/matchers/validate_attachment_presence_matcher_test.rb @@ -0,0 +1,21 @@ +require 'test/helper' + +class ValidateAttachmentPresenceMatcherTest < Test::Unit::TestCase + context "validate_attachment_presence" do + setup do + reset_table("dummies"){|d| d.string :avatar_file_name } + @dummy_class = reset_class "Dummy" + @dummy_class.has_attached_file :avatar + @matcher = self.class.validate_attachment_presence(:avatar) + end + + should "reject a class with no validation" do + assert_rejects @matcher, @dummy_class + end + + should "accept a class with a validation" do + @dummy_class.validates_attachment_presence :avatar + assert_accepts @matcher, @dummy_class + end + end +end diff --git a/vendor/plugins/paperclip/test/matchers/validate_attachment_size_matcher_test.rb b/vendor/plugins/paperclip/test/matchers/validate_attachment_size_matcher_test.rb new file mode 100644 index 0000000..7e4c9b4 --- /dev/null +++ b/vendor/plugins/paperclip/test/matchers/validate_attachment_size_matcher_test.rb @@ -0,0 +1,50 @@ +require 'test/helper' + +class ValidateAttachmentSizeMatcherTest < Test::Unit::TestCase + context "validate_attachment_size" do + setup do + reset_table("dummies") do |d| + d.string :avatar_file_name + end + @dummy_class = reset_class "Dummy" + @dummy_class.has_attached_file :avatar + end + + context "of limited size" do + setup{ @matcher = self.class.validate_attachment_size(:avatar).in(256..1024) } + + should "reject a class with no validation" do + assert_rejects @matcher, @dummy_class + end + + should "reject a class with a validation that's too high" do + @dummy_class.validates_attachment_size :avatar, :in => 256..2048 + assert_rejects @matcher, @dummy_class + end + + should "reject a class with a validation that's too low" do + @dummy_class.validates_attachment_size :avatar, :in => 0..1024 + assert_rejects @matcher, @dummy_class + end + + should "accept a class with a validation that matches" do + @dummy_class.validates_attachment_size :avatar, :in => 256..1024 + assert_accepts @matcher, @dummy_class + end + end + + context "validates_attachment_size with infinite range" do + setup{ @matcher = self.class.validate_attachment_size(:avatar) } + + should "accept a class with an upper limit" do + @dummy_class.validates_attachment_size :avatar, :less_than => 1 + assert_accepts @matcher, @dummy_class + end + + should "accept a class with no upper limit" do + @dummy_class.validates_attachment_size :avatar, :greater_than => 1 + assert_accepts @matcher, @dummy_class + end + end + end +end diff --git a/vendor/plugins/paperclip/test/paperclip_test.rb b/vendor/plugins/paperclip/test/paperclip_test.rb new file mode 100644 index 0000000..8365649 --- /dev/null +++ b/vendor/plugins/paperclip/test/paperclip_test.rb @@ -0,0 +1,233 @@ +require 'test/helper' + +class PaperclipTest < Test::Unit::TestCase + [:image_magick_path, :convert_path].each do |path| + context "Calling Paperclip.run with an #{path} specified" do + setup do + Paperclip.options[:image_magick_path] = nil + Paperclip.options[:convert_path] = nil + Paperclip.options[path] = "/usr/bin" + end + + should "execute the right command" do + Paperclip.expects(:path_for_command).with("convert").returns("/usr/bin/convert") + Paperclip.expects(:bit_bucket).returns("/dev/null") + Paperclip.expects(:"`").with("/usr/bin/convert one.jpg two.jpg 2>/dev/null") + Paperclip.run("convert", "one.jpg two.jpg") + end + end + end + + context "Calling Paperclip.run with no path specified" do + setup do + Paperclip.options[:image_magick_path] = nil + Paperclip.options[:convert_path] = nil + end + + should "execute the right command" do + Paperclip.expects(:path_for_command).with("convert").returns("convert") + Paperclip.expects(:bit_bucket).returns("/dev/null") + Paperclip.expects(:"`").with("convert one.jpg two.jpg 2>/dev/null") + Paperclip.run("convert", "one.jpg two.jpg") + 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 + + should "return a class when sent #processor and the name of a class under Paperclip" do + assert_equal ::Paperclip::Thumbnail, Paperclip.processor(:thumbnail) + end + + context "Paperclip.bit_bucket" do + context "on systems without /dev/null" do + setup do + File.expects(:exists?).with("/dev/null").returns(false) + end + + should "return 'NUL'" do + assert_equal "NUL", Paperclip.bit_bucket + end + end + + context "on systems with /dev/null" do + setup do + File.expects(:exists?).with("/dev/null").returns(true) + end + + should "return '/dev/null'" do + assert_equal "/dev/null", Paperclip.bit_bucket + end + end + end + + context "An ActiveRecord model with an 'avatar' attachment" do + setup do + rebuild_model :path => "tmp/:class/omg/:style.:extension" + @file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb') + end + + teardown { @file.close } + + should "not error when trying to also create a 'blah' attachment" do + assert_nothing_raised do + Dummy.class_eval do + has_attached_file :blah + end + end + end + + context "that is attr_protected" do + setup do + Dummy.class_eval do + attr_protected :avatar + end + @dummy = Dummy.new + end + + should "not assign the avatar on mass-set" do + @dummy.attributes = { :other => "I'm set!", + :avatar => @file } + + assert_equal "I'm set!", @dummy.other + assert ! @dummy.avatar? + end + + should "still allow assigment on normal set" do + @dummy.other = "I'm set!" + @dummy.avatar = @file + + assert_equal "I'm set!", @dummy.other + assert @dummy.avatar? + end + end + + context "with a subclass" do + setup do + class ::SubDummy < Dummy; end + end + + should "be able to use the attachment from the subclass" do + assert_nothing_raised do + @subdummy = SubDummy.create(:avatar => @file) + end + end + + should "be able to see the attachment definition from the subclass's class" do + assert_equal "tmp/:class/omg/:style.:extension", SubDummy.attachment_definitions[:avatar][:path] + end + + teardown do + Object.send(:remove_const, "SubDummy") rescue nil + end + end + + should "have an #avatar method" do + assert Dummy.new.respond_to?(:avatar) + end + + should "have an #avatar= method" do + assert Dummy.new.respond_to?(:avatar=) + end + + context "that is valid" do + setup do + @dummy = Dummy.new + @dummy.avatar = @file + end + + should "be valid" do + assert @dummy.valid? + end + + context "then has a validation added that makes it invalid" do + setup do + assert @dummy.save + Dummy.class_eval do + validates_attachment_content_type :avatar, :content_type => ["text/plain"] + end + @dummy2 = Dummy.find(@dummy.id) + end + + should "be invalid when reloaded" do + assert ! @dummy2.valid?, @dummy2.errors.inspect + end + + should "be able to call #valid? twice without having duplicate errors" do + @dummy2.avatar.valid? + first_errors = @dummy2.avatar.errors + @dummy2.avatar.valid? + assert_equal first_errors, @dummy2.avatar.errors + end + end + end + + def self.should_validate validation, options, valid_file, invalid_file + context "with #{validation} validation and #{options.inspect} options" do + setup do + Dummy.send(:"validates_attachment_#{validation}", :avatar, options) + @dummy = Dummy.new + end + context "and assigning nil" do + setup do + @dummy.avatar = nil + @dummy.valid? + end + if validation == :presence + should "have an error on the attachment" do + assert @dummy.errors.on(:avatar) + end + else + should "not have an error on the attachment" do + assert_nil @dummy.errors.on(:avatar) + end + end + end + context "and assigned a valid file" do + setup do + @dummy.avatar = valid_file + @dummy.valid? + end + should "not have an error when assigned a valid file" do + assert ! @dummy.avatar.errors.key?(validation) + end + should "not have an error on the attachment" do + assert_nil @dummy.errors.on(:avatar) + end + end + context "and assigned an invalid file" do + setup do + @dummy.avatar = invalid_file + @dummy.valid? + end + should "have an error when assigned a valid file" do + assert_not_nil @dummy.avatar.errors[validation] + end + should "have an error on the attachment" do + assert @dummy.errors.on(:avatar) + end + end + end + end + + [[:presence, {}, "5k.png", nil], + [:size, {:in => 1..10240}, nil, "12k.png"], + [:size, {:less_than => 10240}, "5k.png", "12k.png"], + [:size, {:greater_than => 8096}, "12k.png", "5k.png"], + [:content_type, {:content_type => "image/png"}, "5k.png", "text.txt"], + [:content_type, {:content_type => "text/plain"}, "text.txt", "5k.png"], + [:content_type, {:content_type => %r{image/.*}}, "5k.png", "text.txt"]].each do |args| + validation, options, valid_file, invalid_file = args + valid_file &&= File.open(File.join(FIXTURES_DIR, valid_file), "rb") + invalid_file &&= File.open(File.join(FIXTURES_DIR, invalid_file), "rb") + + should_validate validation, options, valid_file, invalid_file + end + + end +end diff --git a/vendor/plugins/paperclip/test/processor_test.rb b/vendor/plugins/paperclip/test/processor_test.rb new file mode 100644 index 0000000..a05f0a9 --- /dev/null +++ b/vendor/plugins/paperclip/test/processor_test.rb @@ -0,0 +1,10 @@ +require 'test/helper' + +class ProcessorTest < Test::Unit::TestCase + should "instantiate and call #make when sent #make to the class" do + processor = mock + processor.expects(:make).with() + Paperclip::Processor.expects(:new).with(:one, :two, :three).returns(processor) + Paperclip::Processor.make(:one, :two, :three) + end +end diff --git a/vendor/plugins/paperclip/test/storage_test.rb b/vendor/plugins/paperclip/test/storage_test.rb new file mode 100644 index 0000000..e2ed8a4 --- /dev/null +++ b/vendor/plugins/paperclip/test/storage_test.rb @@ -0,0 +1,277 @@ +require 'test/helper' + +class StorageTest < Test::Unit::TestCase + context "Parsing S3 credentials" do + setup do + rebuild_model :storage => :s3, + :bucket => "testing", + :s3_credentials => {:not => :important} + + @dummy = Dummy.new + @avatar = @dummy.avatar + + @current_env = ENV['RAILS_ENV'] + end + + teardown do + ENV['RAILS_ENV'] = @current_env + end + + should "get the correct credentials when RAILS_ENV is production" do + ENV['RAILS_ENV'] = 'production' + assert_equal({:key => "12345"}, + @avatar.parse_credentials('production' => {:key => '12345'}, + :development => {:key => "54321"})) + end + + should "get the correct credentials when RAILS_ENV is development" do + ENV['RAILS_ENV'] = 'development' + assert_equal({:key => "54321"}, + @avatar.parse_credentials('production' => {:key => '12345'}, + :development => {:key => "54321"})) + end + + should "return the argument if the key does not exist" do + ENV['RAILS_ENV'] = "not really an env" + assert_equal({:test => "12345"}, @avatar.parse_credentials(:test => "12345")) + end + end + + context "" do + setup do + rebuild_model :storage => :s3, + :s3_credentials => {}, + :bucket => "bucket", + :path => ":attachment/:basename.:extension", + :url => ":s3_path_url" + @dummy = Dummy.new + @dummy.avatar = StringIO.new(".") + end + + should "return a url based on an S3 path" do + assert_match %r{^http://s3.amazonaws.com/bucket/avatars/stringio.txt}, @dummy.avatar.url + end + end + context "" do + setup do + rebuild_model :storage => :s3, + :s3_credentials => {}, + :bucket => "bucket", + :path => ":attachment/:basename.:extension", + :url => ":s3_domain_url" + @dummy = Dummy.new + @dummy.avatar = StringIO.new(".") + end + + should "return a url based on an S3 subdomain" do + assert_match %r{^http://bucket.s3.amazonaws.com/avatars/stringio.txt}, @dummy.avatar.url + end + end + context "" do + setup do + rebuild_model :storage => :s3, + :s3_credentials => { + :production => { :bucket => "prod_bucket" }, + :development => { :bucket => "dev_bucket" } + }, + :s3_host_alias => "something.something.com", + :path => ":attachment/:basename.:extension", + :url => ":s3_alias_url" + @dummy = Dummy.new + @dummy.avatar = StringIO.new(".") + end + + should "return a url based on the host_alias" do + assert_match %r{^http://something.something.com/avatars/stringio.txt}, @dummy.avatar.url + end + end + + context "Parsing S3 credentials with a bucket in them" do + setup do + rebuild_model :storage => :s3, + :s3_credentials => { + :production => { :bucket => "prod_bucket" }, + :development => { :bucket => "dev_bucket" } + } + @dummy = Dummy.new + end + + should "get the right bucket in production", :before => lambda{ ENV.expects(:[]).returns('production') } do + assert_equal "prod_bucket", @dummy.avatar.bucket_name + end + + should "get the right bucket in development", :before => lambda{ ENV.expects(:[]).returns('development') } do + assert_equal "dev_bucket", @dummy.avatar.bucket_name + end + end + + context "An attachment with S3 storage" do + setup do + rebuild_model :storage => :s3, + :bucket => "testing", + :path => ":attachment/:style/:basename.:extension", + :s3_credentials => { + 'access_key_id' => "12345", + 'secret_access_key' => "54321" + } + end + + should "be extended by the S3 module" do + assert Dummy.new.avatar.is_a?(Paperclip::Storage::S3) + end + + should "not be extended by the Filesystem module" do + assert ! Dummy.new.avatar.is_a?(Paperclip::Storage::Filesystem) + 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 { @file.close } + + should "not get a bucket to get a URL" do + @dummy.avatar.expects(:s3).never + @dummy.avatar.expects(:s3_bucket).never + assert_match %r{^http://s3\.amazonaws\.com/testing/avatars/original/5k\.png}, @dummy.avatar.url + end + + context "and saved" do + setup do + @s3_mock = stub + @bucket_mock = stub + RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock) + @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock) + @key_mock = stub + @bucket_mock.expects(:key).returns(@key_mock) + @key_mock.expects(:data=) + @key_mock.expects(:put).with(nil, 'public-read', 'Content-type' => 'image/png') + @dummy.save + end + + should "succeed" do + assert true + end + end + + context "and remove" do + setup do + @s3_mock = stub + @bucket_mock = stub + RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock) + @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock) + @key_mock = stub + @bucket_mock.expects(:key).at_least(2).returns(@key_mock) + @key_mock.expects(:delete) + @dummy.destroy_attached_files + end + + should "succeed" do + assert true + end + end + end + end + + context "An attachment with S3 storage and bucket defined as a Proc" do + setup do + rebuild_model :storage => :s3, + :bucket => lambda { |attachment| "bucket_#{attachment.instance.other}" }, + :s3_credentials => {:not => :important} + end + + should "get the right bucket name" do + assert "bucket_a", Dummy.new(:other => 'a').avatar.bucket_name + assert "bucket_b", Dummy.new(:other => 'b').avatar.bucket_name + end + end + + context "An attachment with S3 storage and specific s3 headers set" do + setup do + rebuild_model :storage => :s3, + :bucket => "testing", + :path => ":attachment/:style/:basename.:extension", + :s3_credentials => { + 'access_key_id' => "12345", + 'secret_access_key' => "54321" + }, + :s3_headers => {'Cache-Control' => 'max-age=31557600'} + 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 { @file.close } + + context "and saved" do + setup do + @s3_mock = stub + @bucket_mock = stub + RightAws::S3.expects(:new).with("12345", "54321", {}).returns(@s3_mock) + @s3_mock.expects(:bucket).with("testing", true, "public-read").returns(@bucket_mock) + @key_mock = stub + @bucket_mock.expects(:key).returns(@key_mock) + @key_mock.expects(:data=) + @key_mock.expects(:put).with(nil, + 'public-read', + 'Content-type' => 'image/png', + 'Cache-Control' => 'max-age=31557600') + @dummy.save + end + + should "succeed" do + assert true + end + end + end + end + + unless ENV["S3_TEST_BUCKET"].blank? + 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 { @file.close } + + should "still return a Tempfile when sent #to_io" do + assert_equal Tempfile, @dummy.avatar.to_io.class + end + + context "and saved" do + setup do + @dummy.save + end + + should "be on S3" do + assert true + end + end + end + end + end +end diff --git a/vendor/plugins/paperclip/test/thumbnail_test.rb b/vendor/plugins/paperclip/test/thumbnail_test.rb new file mode 100644 index 0000000..624e7fa --- /dev/null +++ b/vendor/plugins/paperclip/test/thumbnail_test.rb @@ -0,0 +1,177 @@ +require 'test/helper' + +class ThumbnailTest < Test::Unit::TestCase + + context "A Paperclip Tempfile" do + setup do + @tempfile = Paperclip::Tempfile.new("file.jpg") + end + + should "have its path contain a real extension" do + assert_equal ".jpg", File.extname(@tempfile.path) + end + + should "be a real Tempfile" do + assert @tempfile.is_a?(::Tempfile) + end + end + + context "Another Paperclip Tempfile" do + setup do + @tempfile = Paperclip::Tempfile.new("file") + end + + should "not have an extension if not given one" do + assert_equal "", File.extname(@tempfile.path) + end + + should "still be a real Tempfile" do + assert @tempfile.is_a?(::Tempfile) + end + end + + context "An image" do + setup do + @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "5k.png"), 'rb') + end + + teardown { @file.close } + + [["600x600>", "434x66"], + ["400x400>", "400x61"], + ["32x32<", "434x66"] + ].each do |args| + context "being thumbnailed with a geometry of #{args[0]}" do + setup do + @thumb = Paperclip::Thumbnail.new(@file, :geometry => args[0]) + end + + should "start with dimensions of 434x66" do + cmd = %Q[identify -format "%wx%h" "#{@file.path}"] + assert_equal "434x66", `#{cmd}`.chomp + end + + should "report the correct target geometry" do + assert_equal args[0], @thumb.target_geometry.to_s + end + + context "when made" do + setup do + @thumb_result = @thumb.make + end + + should "be the size we expect it to be" do + cmd = %Q[identify -format "%wx%h" "#{@thumb_result.path}"] + assert_equal args[1], `#{cmd}`.chomp + end + end + end + end + + context "being thumbnailed at 100x50 with cropping" do + setup do + @thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x50#") + 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 + end + + should "report its correct format" do + assert_nil @thumb.format + end + + should "have whiny turned on by default" do + assert @thumb.whiny + end + + should "have convert_options set to nil by default" do + assert_equal nil, @thumb.convert_options + end + + should "send the right command to convert when sent #make" do + Paperclip.expects(:"`").with do |arg| + arg.match %r{convert\s+"#{File.expand_path(@thumb.file.path)}\[0\]"\s+-resize\s+\"x50\"\s+-crop\s+\"100x50\+114\+0\"\s+\+repage\s+".*?"} + end + @thumb.make + end + + should "create the thumbnail when sent #make" do + dst = @thumb.make + assert_match /100x50/, `identify "#{dst.path}"` + end + end + + context "being thumbnailed with convert options set" do + setup do + @thumb = Paperclip::Thumbnail.new(@file, + :geometry => "100x50#", + :convert_options => "-strip -depth 8") + end + + should "have convert_options value set" do + assert_equal "-strip -depth 8", @thumb.convert_options + end + + should "send the right command to convert when sent #make" do + Paperclip.expects(:"`").with do |arg| + arg.match %r{convert\s+"#{File.expand_path(@thumb.file.path)}\[0\]"\s+-resize\s+"x50"\s+-crop\s+"100x50\+114\+0"\s+\+repage\s+-strip\s+-depth\s+8\s+".*?"} + end + @thumb.make + end + + should "create the thumbnail when sent #make" do + dst = @thumb.make + assert_match /100x50/, `identify "#{dst.path}"` + end + + context "redefined to have bad convert_options setting" do + setup do + @thumb = Paperclip::Thumbnail.new(@file, + :geometry => "100x50#", + :convert_options => "-this-aint-no-option") + end + + should "error when trying to create the thumbnail" do + assert_raises(Paperclip::PaperclipError) do + @thumb.make + end + end + end + end + end + + context "A multipage PDF" do + setup do + @file = File.new(File.join(File.dirname(__FILE__), "fixtures", "twopage.pdf"), 'rb') + end + + teardown { @file.close } + + should "start with two pages with dimensions 612x792" do + cmd = %Q[identify -format "%wx%h" "#{@file.path}"] + assert_equal "612x792"*2, `#{cmd}`.chomp + end + + context "being thumbnailed at 100x100 with cropping" do + setup do + @thumb = Paperclip::Thumbnail.new(@file, :geometry => "100x100#", :format => :png) + end + + should "report its correct current and target geometries" do + assert_equal "100x100#", @thumb.target_geometry.to_s + assert_equal "612x792", @thumb.current_geometry.to_s + end + + should "report its correct format" do + assert_equal :png, @thumb.format + end + + should "create the thumbnail when sent #make" do + dst = @thumb.make + assert_match /100x100/, `identify "#{dst.path}"` + end + end + end +end -- cgit v1.3