Commit 4adcbeda by Lucas Caton Committed by Tute Costa

Update AttachmentSizeValidator

Drops Rails 3 branches.

[fixes #2101]
parent db5998b7
...@@ -71,13 +71,7 @@ module Paperclip ...@@ -71,13 +71,7 @@ module Paperclip
end end
def human_size(size) def human_size(size)
if defined?(ActiveSupport::NumberHelper) # Rails 4.0+
ActiveSupport::NumberHelper.number_to_human_size(size) ActiveSupport::NumberHelper.number_to_human_size(size)
else
storage_units_format = I18n.translate(:'number.human.storage_units.format', :locale => options[:locale], :raise => true)
unit = I18n.translate(:'number.human.storage_units.units.byte', :locale => options[:locale], :count => size.to_i, :raise => true)
storage_units_format.gsub(/%n/, size.to_i.to_s).gsub(/%u/, unit).html_safe
end
end end
def min_value_in_human_size(record) def min_value_in_human_size(record)
......
...@@ -12,14 +12,6 @@ describe Paperclip::Validators::AttachmentSizeValidator do ...@@ -12,14 +12,6 @@ describe Paperclip::Validators::AttachmentSizeValidator do
)) ))
end end
def self.storage_units
if defined?(ActiveSupport::NumberHelper) # Rails 4.0+
{ 5120 => '5 KB', 10240 => '10 KB' }
else
{ 5120 => '5120 Bytes', 10240 => '10240 Bytes' }
end
end
def self.should_allow_attachment_file_size(size) def self.should_allow_attachment_file_size(size)
context "when the attachment size is #{size}" do context "when the attachment size is #{size}" do
it "adds error to dummy object" do it "adds error to dummy object" do
...@@ -158,8 +150,10 @@ describe Paperclip::Validators::AttachmentSizeValidator do ...@@ -158,8 +150,10 @@ describe Paperclip::Validators::AttachmentSizeValidator do
message: "is invalid. (Between %{min} and %{max} please.)" message: "is invalid. (Between %{min} and %{max} please.)"
end end
should_not_allow_attachment_file_size 11.kilobytes, should_not_allow_attachment_file_size(
message: "is invalid. (Between #{storage_units[5120]} and #{storage_units[10240]} please.)" 11.kilobytes,
message: "is invalid. (Between 5 KB and 10 KB please.)"
)
end end
context "given :less_than and :greater_than" do context "given :less_than and :greater_than" do
...@@ -169,8 +163,10 @@ describe Paperclip::Validators::AttachmentSizeValidator do ...@@ -169,8 +163,10 @@ describe Paperclip::Validators::AttachmentSizeValidator do
message: "is invalid. (Between %{min} and %{max} please.)" message: "is invalid. (Between %{min} and %{max} please.)"
end end
should_not_allow_attachment_file_size 11.kilobytes, should_not_allow_attachment_file_size(
message: "is invalid. (Between #{storage_units[5120]} and #{storage_units[10240]} please.)" 11.kilobytes,
message: "is invalid. (Between 5 KB and 10 KB please.)"
)
end end
end end
...@@ -181,10 +177,15 @@ describe Paperclip::Validators::AttachmentSizeValidator do ...@@ -181,10 +177,15 @@ describe Paperclip::Validators::AttachmentSizeValidator do
less_than: 10.kilobytes less_than: 10.kilobytes
end end
should_not_allow_attachment_file_size 11.kilobytes, should_not_allow_attachment_file_size(
message: "must be less than #{storage_units[10240]}" 11.kilobytes,
should_not_allow_attachment_file_size 4.kilobytes, message: "must be less than 10 KB"
message: "must be greater than #{storage_units[5120]}" )
should_not_allow_attachment_file_size(
4.kilobytes,
message: "must be greater than 5 KB"
)
end end
context "given a size range" do context "given a size range" do
...@@ -192,10 +193,15 @@ describe Paperclip::Validators::AttachmentSizeValidator do ...@@ -192,10 +193,15 @@ describe Paperclip::Validators::AttachmentSizeValidator do
build_validator in: (5.kilobytes..10.kilobytes) build_validator in: (5.kilobytes..10.kilobytes)
end end
should_not_allow_attachment_file_size 11.kilobytes, should_not_allow_attachment_file_size(
message: "must be in between #{storage_units[5120]} and #{storage_units[10240]}" 11.kilobytes,
should_not_allow_attachment_file_size 4.kilobytes, message: "must be in between 5 KB and 10 KB"
message: "must be in between #{storage_units[5120]} and #{storage_units[10240]}" )
should_not_allow_attachment_file_size(
4.kilobytes,
message: "must be in between 5 KB and 10 KB"
)
end end
end end
......
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