Commit c9834ef9 by Jon Yurek

Added new task for cleaning out invalid attachments.

parent 13895667
...@@ -203,6 +203,7 @@ module Paperclip ...@@ -203,6 +203,7 @@ module Paperclip
@styles.each do |name, args| @styles.each do |name, args|
begin begin
dimensions, format = args dimensions, format = args
dimensions = dimensions.call(instance) if dimensions.respond_to? :call
@queued_for_write[name] = Thumbnail.make(@queued_for_write[:original], @queued_for_write[name] = Thumbnail.make(@queued_for_write[:original],
dimensions, dimensions,
format, format,
......
...@@ -14,25 +14,42 @@ def obtain_attachments ...@@ -14,25 +14,42 @@ def obtain_attachments
end end
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 namespace :paperclip do
desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)" desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
task :refresh => :environment do task :refresh => :environment do
klass = obtain_class for_all_attachments do |instance, name|
names = obtain_attachments instance.send(name).reprocess!
instances = klass.find(:all) instance.send(name).save
end
puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:" end
instances.each do |instance|
names.each do |name| desc "Cleans out invalid attachments. Useful after you've added new validations."
result = if instance.send("#{ name }?") task :clean => :environment do
instance.send(name).reprocess! for_all_attachments do |instance, name|
instance.send(name).save if instance.valid?
else true
true else
end instance.send("#{name}=", nil)
print result ? "." : "x"; $stdout.flush instance.save
end end
end end
puts " Done."
end end
end end
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment