Commit 2226c020 by Matthew Mongeau Committed by Jon Yurek

Refactor the looping of attachments in the rake task

parent 6cbd3d50
...@@ -8,3 +8,4 @@ gem "sqlite3-ruby", "~>1.3.0" ...@@ -8,3 +8,4 @@ gem "sqlite3-ruby", "~>1.3.0"
gem "shoulda" gem "shoulda"
gem "mocha" gem "mocha"
gem "aws-s3", {:require=>"aws/s3"} gem "aws-s3", {:require=>"aws/s3"}
gem "appraisal"
\ No newline at end of file
...@@ -11,6 +11,9 @@ GEM ...@@ -11,6 +11,9 @@ GEM
activeresource (2.3.10) activeresource (2.3.10)
activesupport (= 2.3.10) activesupport (= 2.3.10)
activesupport (2.3.10) activesupport (2.3.10)
appraisal (0.1)
bundler
rake
aws-s3 (0.6.2) aws-s3 (0.6.2)
builder builder
mime-types mime-types
...@@ -43,6 +46,7 @@ PLATFORMS ...@@ -43,6 +46,7 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
appraisal
aws-s3 aws-s3
mocha mocha
rails (~> 2.3.0) rails (~> 2.3.0)
......
...@@ -2,9 +2,10 @@ ...@@ -2,9 +2,10 @@
source "http://rubygems.org" source "http://rubygems.org"
gem "ruby-debug" gem "ruby-debug"
gem "rails", "~>3.0.0" gem "rails", ">=3.0.3"
gem "rake" gem "rake"
gem "sqlite3-ruby", "~>1.3.0" gem "sqlite3-ruby", "~>1.3.0"
gem "shoulda" gem "shoulda"
gem "mocha" gem "mocha"
gem "aws-s3", {:require=>"aws/s3"} gem "aws-s3", {:require=>"aws/s3"}
gem "appraisal"
\ No newline at end of file
...@@ -28,6 +28,9 @@ GEM ...@@ -28,6 +28,9 @@ GEM
activemodel (= 3.0.3) activemodel (= 3.0.3)
activesupport (= 3.0.3) activesupport (= 3.0.3)
activesupport (3.0.3) activesupport (3.0.3)
appraisal (0.1)
bundler
rake
arel (2.0.4) arel (2.0.4)
aws-s3 (0.6.2) aws-s3 (0.6.2)
builder builder
...@@ -84,9 +87,10 @@ PLATFORMS ...@@ -84,9 +87,10 @@ PLATFORMS
ruby ruby
DEPENDENCIES DEPENDENCIES
appraisal
aws-s3 aws-s3
mocha mocha
rails (~> 3.0.0) rails (>= 3.0.3)
rake rake
ruby-debug ruby-debug
shoulda shoulda
......
...@@ -113,6 +113,12 @@ module Paperclip ...@@ -113,6 +113,12 @@ module Paperclip
processor processor
end end
def each_instance_with_attachment(klass, name)
Object.const_get(klass).all.each do |instance|
yield(instance) if instance.send(:"#{name}?")
end
end
# Log a paperclip-specific line. Uses ActiveRecord::Base.logger # Log a paperclip-specific line. Uses ActiveRecord::Base.logger
# by default. Set Paperclip.options[:log] to false to turn off. # by default. Set Paperclip.options[:log] to false to turn off.
def log message def log message
......
def obtain_class def obtain_class
class_name = ENV['CLASS'] || ENV['class'] class_name = ENV['CLASS'] || ENV['class']
raise "Must specify CLASS" unless class_name raise "Must specify CLASS" unless class_name
@klass = Object.const_get(class_name)
end end
def obtain_attachments def obtain_attachments
...@@ -14,25 +13,6 @@ def obtain_attachments ...@@ -14,25 +13,6 @@ def obtain_attachments
end end
end end
def for_all_attachments
klass = obtain_class
names = obtain_attachments
ids = klass.connection.select_values(klass.send(:construct_finder_sql, :select => 'id'))
ids.each do |id|
instance = klass.find(id)
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 "Refreshes both metadata and thumbnails." desc "Refreshes both metadata and thumbnails."
task :refresh => ["paperclip:refresh:metadata", "paperclip:refresh:thumbnails"] task :refresh => ["paperclip:refresh:metadata", "paperclip:refresh:thumbnails"]
...@@ -41,17 +21,23 @@ namespace :paperclip do ...@@ -41,17 +21,23 @@ namespace :paperclip do
desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)." desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)."
task :thumbnails => :environment do task :thumbnails => :environment do
errors = [] errors = []
for_all_attachments do |instance, name| names = obtain_attachments
klass = obtain_class
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
result = instance.send(name).reprocess! result = instance.send(name).reprocess!
errors << [instance.id, instance.errors] unless instance.errors.blank? errors << [instance.id, instance.errors] unless instance.errors.blank?
result end
end end
errors.each{|e| puts "#{e.first}: #{e.last.full_messages.inspect}" } errors.each{|e| puts "#{e.first}: #{e.last.full_messages.inspect}" }
end end
desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)." desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)."
task :metadata => :environment do task :metadata => :environment do
for_all_attachments do |instance, name| names = obtain_attachments
klass = obtain_class
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
if file = instance.send(name).to_file if file = instance.send(name).to_file
instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip) instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
instance.send("#{name}_content_type=", file.content_type.strip) instance.send("#{name}_content_type=", file.content_type.strip)
...@@ -63,10 +49,14 @@ namespace :paperclip do ...@@ -63,10 +49,14 @@ namespace :paperclip do
end end
end end
end end
end
desc "Cleans out invalid attachments. Useful after you've added new validations." desc "Cleans out invalid attachments. Useful after you've added new validations."
task :clean => :environment do task :clean => :environment do
for_all_attachments do |instance, name| names = obtain_attachments
klass = obtain_class
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
instance.send(name).send(:validate) instance.send(name).send(:validate)
if instance.send(name).valid? if instance.send(name).valid?
true true
...@@ -76,4 +66,5 @@ namespace :paperclip do ...@@ -76,4 +66,5 @@ namespace :paperclip do
end end
end end
end end
end
end end
...@@ -43,6 +43,23 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -43,6 +43,23 @@ class PaperclipTest < Test::Unit::TestCase
end end
end end
context "Paperclip.each_instance_with_attachment" do
setup do
@file = File.new(File.join(FIXTURES_DIR, "5k.png"), 'rb')
d1 = Dummy.create(:avatar => @file)
d2 = Dummy.create
d3 = Dummy.create(:avatar => @file)
@expected = [d1, d3]
end
should "yield every instance of a model that has an attachment" do
actual = []
Paperclip.each_instance_with_attachment("Dummy", "avatar") do |instance|
actual << instance
end
assert_same_elements @expected, actual
end
end
should "raise when sent #processor and the name of a class that exists but isn't a subclass of Processor" do should "raise when sent #processor and the name of a class that exists but isn't a subclass of Processor" do
assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) } assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) }
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