Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Fixed issue with size validations and the removal of size column requ…

…irements.
  • Loading branch information...
commit 0f4be8ac8904c7212b399fc0546321d2804da21d 1 parent c72812f
@jyurek jyurek authored
View
2  lib/paperclip.rb
@@ -181,7 +181,7 @@ def validates_attachment_size name, options = {}
message = options[:message] || "file size must be between :min and :max bytes."
attachment_definitions[name][:validations][:size] = lambda do |attachment, instance|
- if attachment.file? && !range.include?(attachment.instance_read(:file_size).to_i)
+ if attachment.file? && !range.include?(attachment.size.to_i)
message.gsub(/:min/, min.to_s).gsub(/:max/, max.to_s)
end
end
View
4 lib/paperclip/attachment.rb
@@ -152,6 +152,10 @@ def original_filename
instance_read(:file_name)
end
+ def size
+ instance_read(:file_size) || (@queued_for_write[:original] && @queued_for_write[:original].size)
+ end
+
def content_type
instance_read(:content_type)
end
View
28 test/attachment_test.rb
@@ -387,6 +387,11 @@ class AttachmentTest < Test::Unit::TestCase
assert_nil @dummy.avatar.updated_at
end
+ should "return the right value when sent #avatar_file_size" do
+ @dummy.avatar = @file
+ assert_equal @file.size, @dummy.avatar.size
+ end
+
context "and avatar_updated_at column" do
setup do
ActiveRecord::Base.connection.add_column :dummies, :avatar_updated_at, :timestamp
@@ -423,5 +428,28 @@ class AttachmentTest < Test::Unit::TestCase
end
end
+ context "and avatar_file_size column" do
+ setup do
+ ActiveRecord::Base.connection.add_column :dummies, :avatar_file_size, :integer
+ rebuild_class
+ @dummy = Dummy.new
+ end
+
+ should "not error when assigned an attachment" do
+ assert_nothing_raised { @dummy.avatar = @file }
+ end
+
+ should "return the right value when sent #avatar_file_size" do
+ @dummy.avatar = @file
+ assert_equal @file.size, @dummy.avatar.size
+ end
+
+ should "return the right value when saved, reloaded, and sent #avatar_file_size" do
+ @dummy.avatar = @file
+ @dummy.save
+ @dummy = Dummy.find(@dummy.id)
+ assert_equal @file.size, @dummy.avatar.size
+ end
+ end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.