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,19 +14,15 @@ def obtain_attachments ...@@ -14,19 +14,15 @@ def obtain_attachments
end end
end end
namespace :paperclip do def for_all_attachments
desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
task :refresh => :environment do
klass = obtain_class klass = obtain_class
names = obtain_attachments names = obtain_attachments
instances = klass.find(:all) instances = klass.find(:all)
puts "Regenerating thumbnails for #{instances.length} instances of #{klass.name}:"
instances.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).reprocess! yield(instance, name)
instance.send(name).save
else else
true true
end end
...@@ -34,5 +30,26 @@ namespace :paperclip do ...@@ -34,5 +30,26 @@ namespace :paperclip do
end end
end end
puts " Done." puts " Done."
end
namespace :paperclip do
desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
task :refresh => :environment do
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
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