Permalink
Browse files

last-minute cleanups

  • Loading branch information...
1 parent 865d8f1 commit 8853ca72ccaa2de5e65650e1e169c03f0c14a701 @mike-burns mike-burns committed with jyurek Jul 27, 2012
View
@@ -535,60 +535,24 @@ end
Deployment
----------
-Paperclip is aware of new attachment styles you have added in previous deploys. The only thing you should do after each deployment is to call
-`rake paperclip:refresh:missing_styles`. It will store current attachment styles in `RAILS_ROOT/public/system/paperclip_attachments.yml`
-by default. You can change it by:
+When you add a new style you can write a migration to ease the
+deployment. Do this using the `add_style` and `remove_style` migration
+schema helpers.
```ruby
-Paperclip.registered_attachments_styles_path = '/tmp/config/paperclip_attachments.yml'
-```
-
-Here is an example for Capistrano:
-
-```ruby
-namespace :deploy do
- desc "build missing paperclip styles"
- task :build_missing_paperclip_styles, :roles => :app do
- run "cd #{release_path}; RAILS_ENV=production bundle exec rake paperclip:refresh:missing_styles"
+class AddThumbnailsToUser < ActiveRecord::Migration
+ def up
+ add_style :users, :avatar, thumbnail: '24x24#'
end
-end
-
-after("deploy:update_code", "deploy:build_missing_paperclip_styles")
-```
-
-Now you don't have to remember to refresh thumbnails in production every time you add a new style.
-Unfortunately it does not work with dynamic styles - it just ignores them.
-If you already have a working app and don't want `rake paperclip:refresh:missing_styles` to refresh old pictures, you need to tell
-Paperclip about existing styles. Simply create a `paperclip_attachments.yml` file by hand. For example:
-
-```ruby
-class User < ActiveRecord::Base
- has_attached_file :avatar, :styles => {:thumb => 'x100', :croppable => '600x600>', :big => '1000x1000>'}
-end
-
-class Book < ActiveRecord::Base
- has_attached_file :cover, :styles => {:small => 'x100', :large => '1000x1000>'}
- has_attached_file :sample, :styles => {:thumb => 'x100'}
+ def down
+ remove_style :users, :avatar, :thumbnail
+ end
end
```
-Then in `RAILS_ROOT/public/system/paperclip_attachments.yml`:
-
-```yml
----
-:User:
- :avatar:
- - :thumb
- - :croppable
- - :big
-:Book:
- :cover:
- - :small
- - :large
- :sample:
- - :thumb
-```
+The above migration will process just the `thumbnail` style when going
+up, and will remove the `thumbnail` files when going down.
Testing
-------
@@ -1,63 +0,0 @@
-Feature: Rake tasks
-
- Background:
- Given I generate a new rails application
- And I run a rails generator to generate a "User" scaffold with "name:string"
- And I run a paperclip generator to add a paperclip "attachment" to the "User" model
- And I run a migration
- And I add this snippet to the User model:
- """
- attr_accessible :name, :attachment
- has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
- """
-
- Scenario: Paperclip refresh thumbnails task
- When I modify my attachment definition to:
- """
- has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
- :styles => { :medium => "200x200#" }
- """
- And I upload the fixture "5k.png"
- Then the attachment "medium/5k.png" should have a dimension of 200x200
- When I modify my attachment definition to:
- """
- has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
- :styles => { :medium => "100x100#" }
- """
- When I successfully run `bundle exec rake paperclip:refresh:thumbnails CLASS=User --trace`
- Then the attachment "original/5k.png" should exist
- And the attachment "medium/5k.png" should have a dimension of 100x100
-
- Scenario: Paperclip refresh metadata task
- When I upload the fixture "5k.png"
- And I swap the attachment "original/5k.png" with the fixture "12k.png"
- And I successfully run `bundle exec rake paperclip:refresh:metadata CLASS=User --trace`
- Then the attachment should have the same content type as the fixture "12k.png"
- And the attachment should have the same file size as the fixture "12k.png"
-
- Scenario: Paperclip refresh missing styles task
- When I upload the fixture "5k.png"
- Then the attachment file "original/5k.png" should exist
- And the attachment file "medium/5k.png" should not exist
- When I modify my attachment definition to:
- """
- has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename",
- :styles => { :medium => "200x200#" }
- """
- When I successfully run `bundle exec rake paperclip:refresh:missing_styles --trace`
- Then the attachment file "original/5k.png" should exist
- And the attachment file "medium/5k.png" should exist
-
- Scenario: Paperclip clean task
- When I upload the fixture "5k.png"
- And I upload the fixture "12k.png"
- Then the attachment file "original/5k.png" should exist
- And the attachment file "original/12k.png" should exist
- When I modify my attachment definition to:
- """
- has_attached_file :attachment, :path => ":rails_root/public/system/:attachment/:style/:filename"
- validates_attachment_size :attachment, :less_than => 10.kilobytes
- """
- And I successfully run `bundle exec rake paperclip:clean CLASS=User --trace`
- Then the attachment file "original/5k.png" should exist
- But the attachment file "original/12k.png" should not exist
@@ -32,10 +32,6 @@ def self.default_options
}
end
- def self.reset
- @default_options = nil
- end
-
attr_reader :name, :instance, :default_style, :convert_options, :queued_for_write, :whiny,
:options, :interpolator, :source_file_options, :whiny
attr_accessor :post_processing
@@ -14,8 +14,6 @@ class Railtie < Rails::Railtie
Paperclip::Attachment.default_options.merge!(app.config.paperclip_defaults)
end
end
-
- rake_tasks { load "tasks/paperclip.rake" }
end
class Railtie
@@ -1,94 +0,0 @@
-module Paperclip
- module Task
- def self.obtain_class
- class_name = ENV['CLASS'] || ENV['class']
- raise "Must specify CLASS" unless class_name
- class_name
- end
-
- def self.obtain_attachments(klass)
- klass = Paperclip.class_for(klass.to_s)
- name = ENV['ATTACHMENT'] || ENV['attachment']
- raise "Class #{klass.name} has no attachments specified" unless klass.respond_to?(:attachment_definitions)
- if !name.blank? && klass.attachment_definitions.keys.map(&:to_s).include?(name.to_s)
- [ name ]
- else
- klass.attachment_definitions.keys
- end
- end
- end
-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 and STYLES splitted by comma)."
- task :thumbnails => :environment do
- klass = Paperclip::Task.obtain_class
- names = Paperclip::Task.obtain_attachments(klass)
- styles = (ENV['STYLES'] || ENV['styles'] || '').split(',').map(&:to_sym)
- names.each do |name|
- Paperclip.each_instance_with_attachment(klass, name) do |instance|
- instance.send(name).reprocess!(*styles)
- unless instance.errors.blank?
- puts "errors while processing #{klass} ID #{instance.id}:"
- puts " " + instance.errors.full_messages.join("\n ") + "\n"
- end
- end
- end
- end
-
- desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)."
- task :metadata => :environment do
- klass = Paperclip::Task.obtain_class
- names = Paperclip::Task.obtain_attachments(klass)
- names.each do |name|
- Paperclip.each_instance_with_attachment(klass, name) do |instance|
- if file = Paperclip.io_adapters.for(instance.send(name))
- instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
- instance.send("#{name}_content_type=", file.content_type.to_s.strip)
- instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
- instance.save(:validate => false)
- else
- true
- end
- end
- end
- end
-
- desc "Regenerates missing thumbnail styles for all classes using Paperclip."
- task :missing_styles => :environment do
- # Force loading all model classes to never miss any has_attached_file declaration:
- Dir[Rails.root + 'app/models/**/*.rb'].each { |path| load path }
- Paperclip.missing_attachments_styles.each do |klass, attachment_definitions|
- attachment_definitions.each do |attachment_name, missing_styles|
- puts "Regenerating #{klass} -> #{attachment_name} -> #{missing_styles.inspect}"
- ENV['CLASS'] = klass.to_s
- ENV['ATTACHMENT'] = attachment_name.to_s
- ENV['STYLES'] = missing_styles.join(',')
- Rake::Task['paperclip:refresh:thumbnails'].execute
- end
- end
- Paperclip.save_current_attachments_styles!
- end
- end
-
- desc "Cleans out invalid attachments. Useful after you've added new validations."
- task :clean => :environment do
- klass = Paperclip::Task.obtain_class
- names = Paperclip::Task.obtain_attachments(klass)
- names.each do |name|
- Paperclip.each_instance_with_attachment(klass, name) do |instance|
- unless instance.valid?
- attributes = %w(file_size file_name content_type).map{ |suffix| "#{name}_#{suffix}".to_sym }
- if attributes.any?{ |attribute| instance.errors[attribute].present? }
- instance.send("#{name}=", nil)
- instance.save(:validate => false)
- end
- end
- end
- end
- end
-end
@@ -6,9 +6,11 @@ class StyleAdderTest < Test::Unit::TestCase
register_recording_processor
Dummy = rebuild_model styles: { thumbnail: '24x24' }, processors: [:recording]
+ file = File.new(fixture_file("50x50.png"), 'rb')
dummy = Dummy.new
- dummy.avatar = File.new(fixture_file("50x50.png"), 'rb')
+ dummy.avatar = file
dummy.save
+ file.close
Dummy.class_eval do
has_attached_file :avatar, styles: { thumbnail: '24x24', large: '124x124' }, processors: [:recording]
@@ -6,9 +6,11 @@ class StyleRemoverTest < Test::Unit::TestCase
register_recording_processor
Dummy = rebuild_model styles: { large: '24x24' }, processors: [:recording]
+ file = File.new(fixture_file("50x50.png"), 'rb')
dummy = Dummy.new
- dummy.avatar = File.new(fixture_file("50x50.png"), 'rb')
+ dummy.avatar = file
dummy.save
+ file.close
large_path = dummy.avatar.path(:large)
original_path = dummy.avatar.path(:original)

0 comments on commit 8853ca7

Please sign in to comment.