Commit 439879e3 by jyurek

Validations, changed up the deletion process, and added URI saving.

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@188 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent 5adeb849
......@@ -25,6 +25,7 @@ end
class Bar < ActiveRecord::Base
has_attached_file :document, :attachment_type => :document,
:path_prefix => "./repository"
validates_attached_file :document
end
class NonStandard < ActiveRecord::Base
......
require 'test/unit'
require 'uri'
require File.dirname(__FILE__) + "/test_helper.rb"
require File.dirname(__FILE__) + "/../init.rb"
require File.join(File.dirname(__FILE__), "models.rb")
class PaperclipImagesTest < Test::Unit::TestCase
def setup
assert @foo = Foo.new
assert @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', 'test_image.jpg'))
assert @foo = Foo.new
assert @file = File.new(File.join(File.dirname(__FILE__), 'fixtures', 'test_image.jpg'))
assert @document = File.new(File.join(File.dirname(__FILE__), 'fixtures', 'test_document.doc'))
assert @foo.image = @file
end
......@@ -66,6 +68,7 @@ class PaperclipImagesTest < Test::Unit::TestCase
end
assert @foo.destroy_image
assert @foo.save
mappings.each do |style, file, url|
assert_not_equal file, @foo.image_file_name(style)
assert_equal "", @foo.image_file_name(style)
......@@ -88,4 +91,44 @@ class PaperclipImagesTest < Test::Unit::TestCase
end
end
def test_should_save_image_from_uri
uri = URI.parse("http://thoughtbot.com/assets/11/thoughtbot.jpg")
@foo.image = uri
@foo.save
@foo.image_valid?
assert File.exists?( @foo.image_file_name(:original) ), @foo.image_file_name(:original)
assert File.exists?( @foo.image_file_name(:medium) ), @foo.image_file_name(:medium)
assert File.exists?( @foo.image_file_name(:thumb) ), @foo.image_file_name(:thumb)
out = `identify '#{@foo.image_file_name(:original)}'`; assert out.match("350x100"); assert $?.exitstatus == 0
out = `identify '#{@foo.image_file_name(:medium)}'`; assert out.match("300x86"); assert $?.exitstatus == 0
out = `identify '#{@foo.image_file_name(:thumb)}'`; assert out.match("100x29"); assert $?.exitstatus == 0
end
def test_should_put_errors_on_object_if_convert_does_not_exist
old_path = Thoughtbot::Paperclip.options[:image_magick_path]
Thoughtbot::Paperclip.options[:image_magick_path] = "/does/not/exist"
assert_nothing_raised{ @foo.image = @file }
assert !@foo.save
assert !@foo.valid?
assert @foo.errors.length > 0
assert @foo.errors.on(:image)
[@foo.errors.on(:image)].flatten.each do |err|
assert_match /could not/, err, err
end
ensure
Thoughtbot::Paperclip.options[:image_magick_path] = old_path
end
def test_should_put_errors_on_object_if_convert_fails
assert_nothing_raised{ @foo.image = @document }
assert !@foo.save
assert !@foo.valid?
assert @foo.errors.length > 0
assert @foo.errors.on(:image)
[@foo.errors.on(:image)].flatten.each do |err|
assert_match /could not/, err, err
end
end
end
\ No newline at end of file
......@@ -19,7 +19,7 @@ class PaperclipNonStandardTest < Test::Unit::TestCase
def test_should_save_the_created_file_to_the_final_asset_directory
assert @ns.save
assert File.exists?( @ns.resume_file_name )
assert File.exists?( @ns.resume_file_name ), @ns.resume_file_name
assert File.exists?( @ns.avatar_file_name(:original) )
assert File.exists?( @ns.avatar_file_name(:bigger) )
assert File.exists?( @ns.avatar_file_name(:cropped) )
......
......@@ -39,5 +39,15 @@ class PaperclipTest < Test::Unit::TestCase
assert @bar.destroy
assert !File.exists?( document_file_name ), document_file_name
end
def test_should_put_on_errors_if_no_file_exists
assert @bar.save
@bar.document = nil
assert !@bar.document_valid?
assert !@bar.save
assert @bar.errors.length > 0
assert @bar.errors.on(:document)
assert_match /requires a valid/, @bar.errors.on(:document), @bar.errors.on(:document)
end
end
\ No newline at end of file
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