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

Commit

Permalink
Fixed issue with size validations and the removal of size column requ…
Browse files Browse the repository at this point in the history
…irements.
  • Loading branch information
Jon Yurek committed Dec 10, 2008
1 parent c72812f commit 0f4be8a
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/paperclip.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 4 additions & 0 deletions lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 28 additions & 0 deletions test/attachment_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

0 comments on commit 0f4be8a

Please sign in to comment.