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:
# of nonzero size.
# * 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+
# 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
# database.
#
......@@ -164,6 +164,8 @@ module Thoughtbot #:nodoc:
def has_attached_file *attachment_names
options = attachment_names.last.is_a?(Hash) ? attachment_names.pop : {}
options = DEFAULT_ATTACHMENT_OPTIONS.merge(options)
@attachment_names ||= []
@attachment_names += attachment_names
include InstanceMethods
attachments ||= {}
......@@ -188,13 +190,7 @@ module Thoughtbot #:nodoc:
write_attribute(:"#{attr}_content_type", attachments[attr][:content_type])
if attachments[attr][:attachment_type] == :image
attachments[attr][:thumbnails].each do |style, geometry|
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
send("process_#{attr}_thumbnails")
end
uploaded_file
......@@ -234,6 +230,10 @@ module Thoughtbot #:nodoc:
end
end
define_method "process_#{attr}_thumbnails" do
make_thumbnails attachments[attr]
end
define_method "destroy_#{attr}" do |*args|
complain = args.first || false
if attachments[attr].keys.any?
......@@ -272,6 +272,10 @@ module Thoughtbot #:nodoc:
end
end
def attachments
@attachment_names
end
# Adds errors if the attachments you specify are either missing or had errors on them.
# Essentially, acts like validates_presence_of for attachments.
def validates_attached_file *attachment_names
......@@ -359,6 +363,16 @@ module Thoughtbot #:nodoc:
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
operator = geometry[-1,1]
begin
......
namespace :paperclip do
# desc "Regenerates thumbnails for a given CLASS (and optional ATTACHMENT)"
# 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
# print instance.save ? "." : "x"; $stdout.flush
# end
# end
#
# desc "Cleans out unused attachments for the given CLASS (and optional ATTACHMENT)"
# task :clean do
# end
......
......@@ -11,6 +11,10 @@ class PaperclipImagesTest < Test::Unit::TestCase
assert @document = File.new(File.join(File.dirname(__FILE__), 'fixtures', 'test_document.doc'))
assert @foo.image = @file
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
assert @foo.image_valid?
......
......@@ -11,6 +11,10 @@ class PaperclipNonStandardTest < Test::Unit::TestCase
assert @ns.resume = @resume
assert @ns.avatar = @avatar
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
assert @ns.avatar_valid?
......
......@@ -9,6 +9,10 @@ class PaperclipTest < Test::Unit::TestCase
assert @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', 'test_document.doc'))
assert @bar.document = @file
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
assert @bar.document_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