Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Added new task for cleaning out invalid attachments.
Browse files Browse the repository at this point in the history
  • Loading branch information
Jon Yurek committed Jul 8, 2008
1 parent 1389566 commit c9834ef
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
1 change: 1 addition & 0 deletions lib/paperclip/attachment.rb
Expand Up @@ -203,6 +203,7 @@ def post_process #:nodoc:
@styles.each do |name, args|
begin
dimensions, format = args
dimensions = dimensions.call(instance) if dimensions.respond_to? :call
@queued_for_write[name] = Thumbnail.make(@queued_for_write[:original],
dimensions,
format,
Expand Down
47 changes: 32 additions & 15 deletions tasks/paperclip_tasks.rake
Expand Up @@ -14,25 +14,42 @@ def obtain_attachments
end
end

def for_all_attachments
klass = obtain_class
names = obtain_attachments
instances = klass.find(:all)

instances.each do |instance|
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 "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
task :refresh => :environment do
klass = obtain_class
names = obtain_attachments
instances = klass.find(:all)

puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:"
instances.each do |instance|
names.each do |name|
result = if instance.send("#{ name }?")
instance.send(name).reprocess!
instance.send(name).save
else
true
end
print result ? "." : "x"; $stdout.flush
for_all_attachments do |instance, name|
instance.send(name).reprocess!
instance.send(name).save
end
end

desc "Cleans out invalid attachments. Useful after you've added new validations."
task :clean => :environment do
for_all_attachments do |instance, name|
if instance.valid?
true
else
instance.send("#{name}=", nil)
instance.save
end
end
puts " Done."
end
end

0 comments on commit c9834ef

Please sign in to comment.