Skip to content

Commit

Permalink
Updated to match up to Paperclip 2.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
krobertson committed May 15, 2008
1 parent 9817479 commit 5e6136b
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 6 deletions.
2 changes: 1 addition & 1 deletion lib/dm-paperclip.rb
Expand Up @@ -35,7 +35,7 @@
require File.join(File.dirname(__FILE__), 'dm-paperclip', 'attachment') require File.join(File.dirname(__FILE__), 'dm-paperclip', 'attachment')


module Paperclip module Paperclip
VERSION = "2.1.0" VERSION = "2.1.2"
class << self class << self
# Provides configurability to Paperclip. There are a number of options available, such as: # 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 # * whiny_thumbnails: Will raise an error if Paperclip cannot process thumbnails of
Expand Down
16 changes: 16 additions & 0 deletions lib/dm-paperclip/attachment.rb
Expand Up @@ -153,6 +153,22 @@ def self.interpolations
} }
end 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) def self.underscore(camel_cased_word)
camel_cased_word.to_s.gsub(/::/, '/'). camel_cased_word.to_s.gsub(/::/, '/').
gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2'). gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
Expand Down
11 changes: 6 additions & 5 deletions tasks/paperclip_tasks.rake
Expand Up @@ -6,8 +6,8 @@ end


def obtain_attachments def obtain_attachments
name = ENV['ATTACHMENT'] || ENV['attachment'] name = ENV['ATTACHMENT'] || ENV['attachment']
raise "Class #{@klass.name} has no attachments specified" unless @klass.respond_to?(:attachment_names) raise "Class #{@klass.name} has no attachments specified" unless @klass.respond_to?(:attachment_definitions)
if !name.blank? && @klass.attachment_names.include?(name) if !name.blank? && @klass.attachment_definitions.keys.include?(name)
[ name ] [ name ]
else else
@klass.attachment_definitions.keys @klass.attachment_definitions.keys
Expand All @@ -19,12 +19,13 @@ namespace :paperclip do
task :refresh => :environment do task :refresh => :environment do
klass = obtain_class klass = obtain_class
names = obtain_attachments names = obtain_attachments

instances = klass.all

puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:" puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:"
klass.all.each do |instance| instances.each do |instance|
names.each do |name| names.each do |name|
result = if instance.send("#{ name }?") result = if instance.send("#{ name }?")
instance.send(name).send("post_process") instance.send(name).reprocess!
instance.send(name).save instance.send(name).save
else else
true true
Expand Down
32 changes: 32 additions & 0 deletions test/test_integration.rb
Expand Up @@ -18,6 +18,38 @@ class IntegrationTest < Test::Unit::TestCase
end end
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 context "A model with no attachment validation" do
setup do setup do
rebuild_model :styles => { :large => "300x300>", rebuild_model :styles => { :large => "300x300>",
Expand Down

0 comments on commit 5e6136b

Please sign in to comment.