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

Commit

Permalink
has_attached_file respects :whiny_thumbnails. Added validates_attachm…
Browse files Browse the repository at this point in the history
…ent_thumbnails which does the same thing.
  • Loading branch information
Jon Yurek committed May 7, 2008
1 parent 7dd35f5 commit d177c8f
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 1 deletion.
5 changes: 5 additions & 0 deletions lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,11 @@ def validates_attachment_size name, options = {}
end
end

# Adds errors if thumbnail creation fails. The same as specifying :whiny_thumbnails => true.
def validates_attachment_thumbnails name, options = {}
attachment_definitions[name][:whiny_thumbnails] = true
end

# Places ActiveRecord-style validations on the presence of a file.
def validates_attachment_presence name, options = {}
attachment_definitions[name][:validations] << lambda do |attachment, instance|
Expand Down
1 change: 1 addition & 0 deletions lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ def initialize name, instance, options = {}
@validations = options[:validations]
@default_style = options[:default_style]
@storage = options[:storage]
@whiny_thumbnails = options[:whiny_thumbnails]
@options = options
@queued_for_delete = []
@queued_for_write = {}
Expand Down
2 changes: 1 addition & 1 deletion test/test_geometry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ class GeometryTest < Test::Unit::TestCase

should "not generate from a bad file" do
file = "/home/This File Does Not Exist.omg"
assert_raise(Paperclip::NotIdentifiedByImageMagick){ @geo = Paperclip::Geometry.from_file(file) }
assert_raise(Paperclip::NotIdentifiedByImageMagickError){ @geo = Paperclip::Geometry.from_file(file) }
end

[['vertical', 900, 1440, true, false, false, 1440, 900, 0.625],
Expand Down
46 changes: 46 additions & 0 deletions test/test_integration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,55 @@ class IntegrationTest < Test::Unit::TestCase
end
end
end

context "A model with no attachment validation" do
setup do
rebuild_model :styles => { :large => "300x300>",
:medium => "100x100",
:thumb => ["32x32#", :gif] },
:default_style => :medium,
:url => "/:attachment/:class/:style/:id/:basename.:extension",
:path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
@dummy = Dummy.new
end

should "have its definition return false when asked about whiny_thumbnails" do
assert ! Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
end

context "when validates_attachment_thumbnails is called" do
setup do
Dummy.validates_attachment_thumbnails :avatar
end

should "have its definition return true when asked about whiny_thumbnails" do
assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
end
end

context "redefined to have attachment validations" do
setup do
rebuild_model :styles => { :large => "300x300>",
:medium => "100x100",
:thumb => ["32x32#", :gif] },
:whiny_thumbnails => true,
:default_style => :medium,
:url => "/:attachment/:class/:style/:id/:basename.:extension",
:path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
end

should "have its definition return true when asked about whiny_thumbnails" do
assert_equal true, Dummy.attachment_definitions[:avatar][:whiny_thumbnails]
end
end
end

context "A model with a filesystem attachment" do
setup do
rebuild_model :styles => { :large => "300x300>",
:medium => "100x100",
:thumb => ["32x32#", :gif] },
:whiny_thumbnails => true,
:default_style => :medium,
:url => "/:attachment/:class/:style/:id/:basename.:extension",
:path => ":rails_root/tmp/:attachment/:class/:style/:id/:basename.:extension"
Expand Down Expand Up @@ -103,7 +146,9 @@ class IntegrationTest < Test::Unit::TestCase
assert ! @dummy.valid?
@dummy.avatar = nil
assert @dummy.valid?
end

should "know the difference between good files, bad files, not files, and nil when validating" do
Dummy.validates_attachment_presence :avatar
@d2 = Dummy.find(@dummy.id)
@d2.avatar = @file
Expand Down Expand Up @@ -142,6 +187,7 @@ def s3_files_for attachment
:medium => "100x100",
:thumb => ["32x32#", :gif] },
:storage => :s3,
:whiny_thumbnails => true,
# :s3_options => {:logger => Logger.new(StringIO.new)},
:s3_credentials => File.new(File.join(File.dirname(__FILE__), "s3.yml")),
:default_style => :medium,
Expand Down

0 comments on commit d177c8f

Please sign in to comment.