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

Commit

Permalink
Refactor the looping of attachments in the rake task
Browse files Browse the repository at this point in the history
  • Loading branch information
halogenandtoast authored and Jon Yurek committed Dec 14, 2010
1 parent 6cbd3d5 commit 2226c02
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 43 deletions.
3 changes: 2 additions & 1 deletion gemfiles/rails2.gemfile
Expand Up @@ -7,4 +7,5 @@ gem "rake"
gem "sqlite3-ruby", "~>1.3.0"
gem "shoulda"
gem "mocha"
gem "aws-s3", {:require=>"aws/s3"}
gem "aws-s3", {:require=>"aws/s3"}
gem "appraisal"
4 changes: 4 additions & 0 deletions gemfiles/rails2.gemfile.lock
Expand Up @@ -11,6 +11,9 @@ GEM
activeresource (2.3.10)
activesupport (= 2.3.10)
activesupport (2.3.10)
appraisal (0.1)
bundler
rake
aws-s3 (0.6.2)
builder
mime-types
Expand Down Expand Up @@ -43,6 +46,7 @@ PLATFORMS
ruby

DEPENDENCIES
appraisal
aws-s3
mocha
rails (~> 2.3.0)
Expand Down
5 changes: 3 additions & 2 deletions gemfiles/rails3.gemfile
Expand Up @@ -2,9 +2,10 @@

source "http://rubygems.org"
gem "ruby-debug"
gem "rails", "~>3.0.0"
gem "rails", ">=3.0.3"
gem "rake"
gem "sqlite3-ruby", "~>1.3.0"
gem "shoulda"
gem "mocha"
gem "aws-s3", {:require=>"aws/s3"}
gem "aws-s3", {:require=>"aws/s3"}
gem "appraisal"
6 changes: 5 additions & 1 deletion gemfiles/rails3.gemfile.lock
Expand Up @@ -28,6 +28,9 @@ GEM
activemodel (= 3.0.3)
activesupport (= 3.0.3)
activesupport (3.0.3)
appraisal (0.1)
bundler
rake
arel (2.0.4)
aws-s3 (0.6.2)
builder
Expand Down Expand Up @@ -84,9 +87,10 @@ PLATFORMS
ruby

DEPENDENCIES
appraisal
aws-s3
mocha
rails (~> 3.0.0)
rails (>= 3.0.3)
rake
ruby-debug
shoulda
Expand Down
6 changes: 6 additions & 0 deletions lib/paperclip.rb
Expand Up @@ -113,6 +113,12 @@ def processor name #:nodoc:
processor
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
# by default. Set Paperclip.options[:log] to false to turn off.
def log message
Expand Down
69 changes: 30 additions & 39 deletions lib/tasks/paperclip.rake
@@ -1,7 +1,6 @@
def obtain_class
class_name = ENV['CLASS'] || ENV['class']
raise "Must specify CLASS" unless class_name
@klass = Object.const_get(class_name)
end

def obtain_attachments
Expand All @@ -14,25 +13,6 @@ def obtain_attachments
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
desc "Refreshes both metadata and thumbnails."
task :refresh => ["paperclip:refresh:metadata", "paperclip:refresh:thumbnails"]
Expand All @@ -41,38 +21,49 @@ namespace :paperclip do
desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)."
task :thumbnails => :environment do
errors = []
for_all_attachments do |instance, name|
result = instance.send(name).reprocess!
errors << [instance.id, instance.errors] unless instance.errors.blank?
result
names = obtain_attachments

This comment has been minimized.

Copy link
@scudco

scudco Dec 15, 2010

This depends on @klass being defined as class constant. I cannot run any of the rake tasks because of the changes in this commit.

klass = obtain_class
names.each do |name|
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
errors.each{|e| puts "#{e.first}: #{e.last.full_messages.inspect}" }
end

desc "Regenerates content_type/size metadata for a given CLASS (and optional ATTACHMENT)."
task :metadata => :environment do
for_all_attachments do |instance, name|
if file = instance.send(name).to_file
instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
instance.send("#{name}_content_type=", file.content_type.strip)
instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
instance.save(false)
else
true
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
instance.send("#{name}_file_name=", instance.send("#{name}_file_name").strip)
instance.send("#{name}_content_type=", file.content_type.strip)
instance.send("#{name}_file_size=", file.size) if instance.respond_to?("#{name}_file_size")
instance.save(false)
else
true
end
end
end
end
end

desc "Cleans out invalid attachments. Useful after you've added new validations."
task :clean => :environment do
for_all_attachments do |instance, name|
instance.send(name).send(:validate)
if instance.send(name).valid?
true
else
instance.send("#{name}=", nil)
instance.save
names = obtain_attachments
klass = obtain_class
names.each do |name|
Paperclip.each_instance_with_attachment(klass, name) do |instance|
instance.send(name).send(:validate)
if instance.send(name).valid?
true
else
instance.send("#{name}=", nil)
instance.save
end
end
end
end
Expand Down
17 changes: 17 additions & 0 deletions test/paperclip_test.rb
Expand Up @@ -43,6 +43,23 @@ class PaperclipTest < Test::Unit::TestCase
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
assert_raises(Paperclip::PaperclipError){ Paperclip.processor(:attachment) }
end
Expand Down

0 comments on commit 2226c02

Please sign in to comment.