Permalink
Browse files

Updated to match up to Paperclip 2.1.2

  • Loading branch information...
1 parent 9817479 commit 5e6136bc6e76393eaa1f0fe65fbbf7cc102cb213 @krobertson krobertson committed May 15, 2008
Showing with 55 additions and 6 deletions.
  1. +1 −1 lib/dm-paperclip.rb
  2. +16 −0 lib/dm-paperclip/attachment.rb
  3. +6 −5 tasks/paperclip_tasks.rake
  4. +32 −0 test/test_integration.rb
View
2 lib/dm-paperclip.rb
@@ -35,7 +35,7 @@
require File.join(File.dirname(__FILE__), 'dm-paperclip', 'attachment')
module Paperclip
- VERSION = "2.1.0"
+ VERSION = "2.1.2"
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
View
16 lib/dm-paperclip/attachment.rb
@@ -153,6 +153,22 @@ def self.interpolations
}
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")
+ 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)
+ end
+
def self.underscore(camel_cased_word)
camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
View
11 tasks/paperclip_tasks.rake
@@ -6,8 +6,8 @@ end
def obtain_attachments
name = ENV['ATTACHMENT'] || ENV['attachment']
- raise "Class #{@klass.name} has no attachments specified" unless @klass.respond_to?(:attachment_names)
- if !name.blank? && @klass.attachment_names.include?(name)
+ 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
@@ -19,12 +19,13 @@ namespace :paperclip do
task :refresh => :environment do
klass = obtain_class
names = obtain_attachments
-
+ instances = klass.all
+
puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:"
- klass.all.each do |instance|
+ instances.each do |instance|
names.each do |name|
result = if instance.send("#{ name }?")
- instance.send(name).send("post_process")
+ instance.send(name).reprocess!
instance.send(name).save
else
true
View
32 test/test_integration.rb
@@ -18,6 +18,38 @@ class IntegrationTest < Test::Unit::TestCase
end
end
+ context "An attachment" do
+ setup do
+ rebuild_model :styles => { :thumb => "50x50#" }
+ @dummy = Dummy.new
+ @dummy.id = 1
+ @file = File.new(File.join(File.dirname(__FILE__),
+ "fixtures",
+ "5k.png"))
+ @dummy.avatar = @file
+ assert @dummy.save
+ end
+
+ 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#" }
+ end
+ @d2 = Dummy[@dummy.id]
+ @d2.avatar.reprocess!
+ @d2.save
+ end
+
+ should "create its thumbnails properly" do
+ assert_match /\b150x25\b/, `identify '#{@dummy.avatar.path(:thumb)}'`
+ end
+ end
+ end
+
context "A model with no attachment validation" do
setup do
rebuild_model :styles => { :large => "300x300>",

0 comments on commit 5e6136b

Please sign in to comment.