Permalink
Browse files

made most of the tests pass, callbacks still need refactor

  • Loading branch information...
1 parent b7975ad commit 1604941a98c5abcd5841a041afc602c2a88565d7 mihael committed Dec 19, 2010
@@ -183,9 +183,9 @@ def self.extended(base)
base.after_save :after_process_attachment
base.after_destroy :destroy_file
base.after_validation :process_attachment
- if defined?(::ActiveSupport::Callbacks)
- base.define_callbacks :after_resize, :before_thumbnail_saved
- end
+ #if defined?(::ActiveSupport::Callbacks)
+ # base.define_callbacks :after_resize, :after_attachment_saved, :before_thumbnail_saved
+ #end
end
unless defined?(::ActiveSupport::Callbacks)
@@ -201,6 +201,19 @@ def after_resize(&block)
write_inheritable_array(:after_resize, [block])
end
+ # Callback after an attachment has been saved either to the file system or the DB.
+ # Only called if the file has been changed, not necessarily if the record is updated.
+ #
+ # class Foo < ActiveRecord::Base
+ # acts_as_attachment
+ # after_attachment_saved do |record|
+ # ...
+ # end
+ # end
+ def after_attachment_saved(&block)
+ write_inheritable_array(:after_attachment_saved, [block])
+ end
+
# Callback before a thumbnail is saved. Use this to pass any necessary extra attributes that may be required.
#
# class Foo < ActiveRecord::Base
@@ -336,7 +349,7 @@ def uploaded_data=(file_data)
file_data.rewind
set_temp_data file_data.read
else
- self.temp_paths.unshift file_data.tempfile.path
+ file_data.respond_to?(:tempfile) ? self.temp_paths.unshift( file_data.tempfile.path ) : self.temp_paths.unshift( file_data.path )
end
end
@@ -445,6 +458,8 @@ def after_process_attachment
save_to_storage
@temp_paths.clear
@saved_attachment = nil
+ #callback :after_attachment_saved
+ callback_with_args :after_attachment_saved, nil
end
end
@@ -459,7 +474,9 @@ def resize_image_or_thumbnail!(img)
if defined?(Rails) && Rails::VERSION::MAJOR >= 3
def callback_with_args(method, arg = self)
- send(method, arg) if respond_to?(method)
+ if respond_to?(method)
+ send(method, arg)
+ end
end
# Yanked from ActiveRecord::Callbacks, modified so I can pass args to the callbacks besides self.
# Only accept blocks, however
@@ -53,7 +53,7 @@ def test_should_delete_old_file_when_updating(klass = FileAttachment)
assert_not_created do
use_temp_file 'files/rails.png' do |file|
attachment.filename = 'rails2.png'
- attachment.temp_paths.unshift File.join(fixture_path, file)
+ attachment.temp_paths.unshift File.join(FIXTURE_PATH, file)
attachment.save!
assert File.exists?(attachment.full_filename), "#{attachment.full_filename} does not exist"
assert !File.exists?(old_filename), "#{old_filename} still exists"
@@ -55,7 +55,7 @@ def test_should_overwrite_old_contents_when_updating
assert_not_created do # no new db_file records
use_temp_file 'files/rails.png' do |file|
attachment.filename = 'rails2.png'
- attachment.temp_paths.unshift File.join(fixture_path, file)
+ attachment.temp_paths.unshift File.join(FIXTURE_PATH, file)
attachment.save!
end
end
View
@@ -1,9 +1,9 @@
sqlite:
:adapter: sqlite
- :dbfile: attachment_fu_plugin.sqlite.db
+ :database: attachment_fu_plugin.sqlite.db
sqlite3:
:adapter: sqlite3
- :dbfile: attachment_fu_plugin.sqlite3.db
+ :database: attachment_fu_plugin.sqlite3.db
postgresql:
:adapter: postgresql
:username: postgres
@@ -3,7 +3,7 @@ class Attachment < ActiveRecord::Base
cattr_accessor :saves
has_attachment :processor => :rmagick
validates_as_attachment
- after_attachment_saved do |record|
+ after_save do |record|
self.saves += 1
end
end
@@ -34,8 +34,8 @@ class ImageOrPdfAttachment < Attachment
class ImageWithThumbsAttachment < Attachment
has_attachment :thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
- after_resize do |record, img|
- # record.aspect_ratio = img.columns.to_f / img.rows.to_f
+ def after_resize(img)
+ self.aspect_ratio = img.columns.to_f / img.rows.to_f
end
end
@@ -84,8 +84,8 @@ class ImageFileAttachment < FileAttachment
class ImageWithThumbsFileAttachment < FileAttachment
has_attachment :path_prefix => 'vendor/plugins/attachment_fu/test/files',
:thumbnails => { :thumb => [50, 50], :geometry => 'x50' }, :resize_to => [55,55]
- after_resize do |record, img|
- # record.aspect_ratio = img.columns.to_f / img.rows.to_f
+ def after_resize(img)
+ self.aspect_ratio = img.columns.to_f / img.rows.to_f
end
end
@@ -1,5 +1,4 @@
require File.expand_path(File.join(File.dirname(__FILE__), '..', 'test_helper'))
-
class RmagickTest < Test::Unit::TestCase
attachment_model Attachment
@@ -181,7 +180,7 @@ def test_should_remove_old_thumbnail_files_when_updating(klass = ImageWithThumbs
assert_not_created do
use_temp_file "files/rails.png" do |file|
attachment.filename = 'rails2.png'
- attachment.temp_paths.unshift File.join(fixture_path, file)
+ attachment.temp_paths.unshift File.join(FIXTURE_PATH, file)
attachment.save
new_filenames = [attachment.reload.full_filename] + attachment.thumbnails.collect { |t| t.reload.full_filename }
new_filenames.each { |f| assert File.exists?(f), "#{f} does not exist" }
@@ -224,7 +223,7 @@ def test_should_overwrite_old_thumbnail_records_when_updating(klass = ImageWithT
# #temp_path calls #full_filename, which is not getting mixed into the attachment. Maybe we don't need to
# set temp_path at all?
#
- # attachment.temp_paths.unshift File.join(fixture_path, file)
+ # attachment.temp_paths.unshift File.join(FIXTURE_PATH, file)
attachment.save!
end
end
View
@@ -5,8 +5,6 @@
require 'test/unit'
require File.expand_path(File.join(ENV['RAILS_ROOT'], 'config/environment.rb'))
-require 'active_record/fixtures'
-require 'action_controller/test_process'
config = YAML::load(IO.read(File.dirname(__FILE__) + '/database.yml'))
ActiveRecord::Base.logger = Logger.new(File.dirname(__FILE__) + "/debug.log")
@@ -35,16 +33,16 @@
load(File.dirname(__FILE__) + "/schema.rb")
-Test::Unit::TestCase.fixture_path = File.dirname(__FILE__) + "/fixtures"
-$LOAD_PATH.unshift(Test::Unit::TestCase.fixture_path)
+FIXTURE_PATH = File.dirname(__FILE__) + "/fixtures"
+$LOAD_PATH.unshift(FIXTURE_PATH)
class Test::Unit::TestCase #:nodoc:
- include ActionController::TestProcess
+ include ActionDispatch::TestProcess
def create_fixtures(*table_names)
if block_given?
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names) { yield }
+ Fixtures.create_fixtures(FIXTURE_PATH, table_names) { yield }
else
- Fixtures.create_fixtures(Test::Unit::TestCase.fixture_path, table_names)
+ Fixtures.create_fixtures(FIXTURE_PATH, table_names)
end
end
@@ -58,8 +56,8 @@ def teardown
FileUtils.rm_rf File.join(File.dirname(__FILE__), 'files')
end
- self.use_transactional_fixtures = true
- self.use_instantiated_fixtures = false
+ #self.use_transactional_fixtures = true
+ #self.use_instantiated_fixtures = false
def self.attachment_model(klass = nil)
@attachment_model = klass if klass
@@ -97,11 +95,13 @@ def upload_merb_file(options = {})
def use_temp_file(fixture_filename)
temp_path = File.join('/tmp', File.basename(fixture_filename))
- FileUtils.mkdir_p File.join(fixture_path, 'tmp')
- FileUtils.cp File.join(fixture_path, fixture_filename), File.join(fixture_path, temp_path)
- yield temp_path
+ temp_dir = File.join(FIXTURE_PATH, 'tmp')
+ use_file = File.join(FIXTURE_PATH, temp_path)
+ FileUtils.mkdir_p temp_dir
+ FileUtils.cp File.join(FIXTURE_PATH, fixture_filename), use_file
+ yield use_file
ensure
- FileUtils.rm_rf File.join(fixture_path, 'tmp')
+ FileUtils.rm_rf temp_dir
end
def assert_created(num = 1)
@@ -115,6 +115,10 @@ def assert_created(num = 1)
end
end
end
+
+ def assert_valid(record)
+ assert record.valid?, record.errors.full_messages.join("\n")
+ end
def assert_not_created
assert_created(0) { yield }
@@ -4,52 +4,52 @@ class ValidationTest < Test::Unit::TestCase
def test_should_invalidate_big_files
@attachment = SmallAttachment.new
assert !@attachment.valid?
- assert @attachment.errors.on(:size)
+ assert @attachment.errors[:size]
@attachment.size = 2000
assert !@attachment.valid?
- assert @attachment.errors.on(:size), @attachment.errors.full_messages.to_sentence
+ assert @attachment.errors[:size], @attachment.errors.full_messages.to_sentence
@attachment.size = 1000
assert !@attachment.valid?
- assert_nil @attachment.errors.on(:size)
+ assert @attachment.errors[:size].empty?
end
def test_should_invalidate_small_files
@attachment = BigAttachment.new
assert !@attachment.valid?
- assert @attachment.errors.on(:size)
+ assert @attachment.errors[:size]
@attachment.size = 2000
assert !@attachment.valid?
- assert @attachment.errors.on(:size), @attachment.errors.full_messages.to_sentence
+ assert @attachment.errors[:size], @attachment.errors.full_messages.to_sentence
@attachment.size = 1.megabyte
assert !@attachment.valid?
- assert_nil @attachment.errors.on(:size)
+ assert @attachment.errors[:size].empty?
end
def test_should_validate_content_type
@attachment = PdfAttachment.new
assert !@attachment.valid?
- assert @attachment.errors.on(:content_type)
+ assert @attachment.errors[:content_type]
@attachment.content_type = 'foo'
assert !@attachment.valid?
- assert @attachment.errors.on(:content_type)
+ assert @attachment.errors[:content_type]
@attachment.content_type = 'pdf'
assert !@attachment.valid?
- assert_nil @attachment.errors.on(:content_type)
+ assert @attachment.errors[:content_type].empty?
end
def test_should_require_filename
@attachment = Attachment.new
assert !@attachment.valid?
- assert @attachment.errors.on(:filename)
+ assert @attachment.errors[:filename]
@attachment.filename = 'foo'
assert !@attachment.valid?
- assert_nil @attachment.errors.on(:filename)
+ assert @attachment.errors[:filename].empty?
end
end

0 comments on commit 1604941

Please sign in to comment.