Commit 2226c020 by Matthew Mongeau Committed by Jon Yurek

Refactor the looping of attachments in the rake task

parent 6cbd3d50
...@@ -7,4 +7,5 @@ gem "rake" ...@@ -7,4 +7,5 @@ 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"}
\ No newline at end of file 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"}
\ No newline at end of file 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,24 +21,31 @@ namespace :paperclip do ...@@ -41,24 +21,31 @@ 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
result = instance.send(name).reprocess! klass = obtain_class
errors << [instance.id, instance.errors] unless instance.errors.blank? names.each do |name|
result Paperclip.each_instance_with_attachment(klass, name) do |instance|
result = instance.send(name).reprocess!
errors << [instance.id, instance.errors] unless instance.errors.blank?
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
if file = instance.send(name).to_file klass = obtain_class
instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip) names.each do |name|
instance.send("#{name}_content_type=", file.content_type.strip) Paperclip.each_instance_with_attachment(klass, name) do |instance|
instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size") if file = instance.send(name).to_file
instance.save(false) instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
else instance.send("#{name}_content_type=", file.content_type.strip)
true instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
instance.save(false)
else
true
end
end end
end end
end end
...@@ -66,13 +53,17 @@ namespace :paperclip do ...@@ -66,13 +53,17 @@ namespace :paperclip do
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
instance.send(name).send(:validate) klass = obtain_class
if instance.send(name).valid? names.each do |name|
true Paperclip.each_instance_with_attachment(klass, name) do |instance|
else instance.send(name).send(:validate)
instance.send("#{name}=", nil) if instance.send(name).valid?
instance.save true
else
instance.send("#{name}=", nil)
instance.save
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