Commit 0c4f5d47 by jyurek

Moved methods around for easier scripting in rake tasks.

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@210 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent 6043576c
...@@ -81,7 +81,7 @@ module Thoughtbot #:nodoc: ...@@ -81,7 +81,7 @@ module Thoughtbot #:nodoc:
# of nonzero size. # of nonzero size.
# * destroy_attachment(complain = false): Flags the attachment and all thumbnails for deletion. Sets # * destroy_attachment(complain = false): Flags the attachment and all thumbnails for deletion. Sets
# the +attachment_file_name+ column and +attachment_content_type+ column to +nil+. Set +complain+ # the +attachment_file_name+ column and +attachment_content_type+ column to +nil+. Set +complain+
# to true to override the +whiny_deletes+ option. Note, this does not actually delete the attachment. # to true to override the +whiny_deletes+ option. NOTE: this does not actually delete the attachment.
# You must still call +save+ on the model to actually delete the file and commit the change to the # You must still call +save+ on the model to actually delete the file and commit the change to the
# database. # database.
# #
...@@ -164,6 +164,8 @@ module Thoughtbot #:nodoc: ...@@ -164,6 +164,8 @@ module Thoughtbot #:nodoc:
def has_attached_file *attachment_names def has_attached_file *attachment_names
options = attachment_names.last.is_a?(Hash) ? attachment_names.pop : {} options = attachment_names.last.is_a?(Hash) ? attachment_names.pop : {}
options = DEFAULT_ATTACHMENT_OPTIONS.merge(options) options = DEFAULT_ATTACHMENT_OPTIONS.merge(options)
@attachment_names ||= []
@attachment_names += attachment_names
include InstanceMethods include InstanceMethods
attachments ||= {} attachments ||= {}
...@@ -188,13 +190,7 @@ module Thoughtbot #:nodoc: ...@@ -188,13 +190,7 @@ module Thoughtbot #:nodoc:
write_attribute(:"#{attr}_content_type", attachments[attr][:content_type]) write_attribute(:"#{attr}_content_type", attachments[attr][:content_type])
if attachments[attr][:attachment_type] == :image if attachments[attr][:attachment_type] == :image
attachments[attr][:thumbnails].each do |style, geometry| send("process_#{attr}_thumbnails")
begin
attachments[attr][:files][style] = make_thumbnail(attachments[attr], attachments[attr][:files][:original], geometry)
rescue PaperclipError => e
attachments[attr][:errors] << "thumbnail '#{style}' could not be created."
end
end
end end
uploaded_file uploaded_file
...@@ -234,6 +230,10 @@ module Thoughtbot #:nodoc: ...@@ -234,6 +230,10 @@ module Thoughtbot #:nodoc:
end end
end end
define_method "process_#{attr}_thumbnails" do
make_thumbnails attachments[attr]
end
define_method "destroy_#{attr}" do |*args| define_method "destroy_#{attr}" do |*args|
complain = args.first || false complain = args.first || false
if attachments[attr].keys.any? if attachments[attr].keys.any?
...@@ -272,6 +272,10 @@ module Thoughtbot #:nodoc: ...@@ -272,6 +272,10 @@ module Thoughtbot #:nodoc:
end end
end end
def attachments
@attachment_names
end
# Adds errors if the attachments you specify are either missing or had errors on them. # Adds errors if the attachments you specify are either missing or had errors on them.
# Essentially, acts like validates_presence_of for attachments. # Essentially, acts like validates_presence_of for attachments.
def validates_attached_file *attachment_names def validates_attached_file *attachment_names
...@@ -359,6 +363,16 @@ module Thoughtbot #:nodoc: ...@@ -359,6 +363,16 @@ module Thoughtbot #:nodoc:
end end
end end
def make_thumbnails attachment
attachment[:thumbnails].each do |style, geometry|
begin
attachment[:files][style] = make_thumbnail(attachment, attachment[:files][:original], geometry)
rescue PaperclipError => e
attachment[:errors] << "thumbnail '#{style}' could not be created."
end
end
end
def make_thumbnail attachment, orig_io, geometry def make_thumbnail attachment, orig_io, geometry
operator = geometry[-1,1] operator = geometry[-1,1]
begin begin
......
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 do # task :refresh do
# klass = obtain_class
# attachment = ENV['ATTACHMENT']
# attachments = attachment.blank? ? klass.attachments : [attachment]
# instances = klass.find(:all)
# puts "Regenerating thumbnails for #{instances.length} instances:"
# instances.each do |instance|
# attachments.each do |attach|
# instance.send("process_#{attach}_thumbnails")
# end # end
# # print instance.save ? "." : "x"; $stdout.flush
# end
# end
# desc "Cleans out unused attachments for the given CLASS (and optional ATTACHMENT)" # desc "Cleans out unused attachments for the given CLASS (and optional ATTACHMENT)"
# task :clean do # task :clean do
# end # end
......
...@@ -12,6 +12,10 @@ class PaperclipImagesTest < Test::Unit::TestCase ...@@ -12,6 +12,10 @@ class PaperclipImagesTest < Test::Unit::TestCase
assert @foo.image = @file assert @foo.image = @file
end end
def test_should_supply_all_attachment_names
assert_equal %w( image ), Foo.attachments.map{|a| a.to_s }.sort
end
def test_should_validate_before_save def test_should_validate_before_save
assert @foo.image_valid? assert @foo.image_valid?
assert @foo.valid? assert @foo.valid?
......
...@@ -12,6 +12,10 @@ class PaperclipNonStandardTest < Test::Unit::TestCase ...@@ -12,6 +12,10 @@ class PaperclipNonStandardTest < Test::Unit::TestCase
assert @ns.avatar = @avatar assert @ns.avatar = @avatar
end end
def test_should_supply_all_attachment_names
assert_equal %w( avatar resume ), NonStandard.attachments.map{|a| a.to_s }.sort
end
def test_should_validate_before_save def test_should_validate_before_save
assert @ns.avatar_valid? assert @ns.avatar_valid?
assert @ns.valid? assert @ns.valid?
......
...@@ -10,6 +10,10 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -10,6 +10,10 @@ class PaperclipTest < Test::Unit::TestCase
assert @bar.document = @file assert @bar.document = @file
end end
def test_should_supply_all_attachment_names
assert_equal %w( document ), Bar.attachments.map{|a| a.to_s }.sort
end
def test_should_validate_before_save def test_should_validate_before_save
assert @bar.document_valid? assert @bar.document_valid?
assert @bar.valid? assert @bar.valid?
......
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