Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
paperclip
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
ikcrm_common
paperclip
Commits
e38bdc12
Commit
e38bdc12
authored
Mar 29, 2014
by
Lucas Caton
Committed by
Jon Yurek
May 16, 2014
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
AttachmentSizeValidator#human_size method works now with all supported Rails versions
parent
35373dac
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
21 additions
and
7 deletions
+21
-7
lib/paperclip/validators/attachment_size_validator.rb
+7
-1
spec/paperclip/validators/attachment_size_validator_spec.rb
+14
-6
No files found.
lib/paperclip/validators/attachment_size_validator.rb
View file @
e38bdc12
...
@@ -71,7 +71,13 @@ module Paperclip
...
@@ -71,7 +71,13 @@ module Paperclip
end
end
def
human_size
(
size
)
def
human_size
(
size
)
ActiveSupport
::
NumberHelper
.
number_to_human_size
(
size
)
if
defined?
(
ActiveRecord
::
NumberHelper
)
# Rails 4.0+
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
)
...
...
spec/paperclip/validators/attachment_size_validator_spec.rb
View file @
e38bdc12
...
@@ -12,6 +12,14 @@ describe Paperclip::Validators::AttachmentSizeValidator do
...
@@ -12,6 +12,14 @@ describe Paperclip::Validators::AttachmentSizeValidator do
))
))
end
end
def
self
.
storage_units
if
defined?
(
ActiveRecord
::
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
...
@@ -151,7 +159,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
...
@@ -151,7 +159,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
end
end
should_not_allow_attachment_file_size
11
.
kilobytes
,
should_not_allow_attachment_file_size
11
.
kilobytes
,
message:
"is invalid. (Between
5 KB and 10 KB
please.)"
message:
"is invalid. (Between
#{
storage_units
[
5120
]
}
and
#{
storage_units
[
10240
]
}
please.)"
end
end
context
"given :less_than and :greater_than"
do
context
"given :less_than and :greater_than"
do
...
@@ -162,7 +170,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
...
@@ -162,7 +170,7 @@ describe Paperclip::Validators::AttachmentSizeValidator do
end
end
should_not_allow_attachment_file_size
11
.
kilobytes
,
should_not_allow_attachment_file_size
11
.
kilobytes
,
message:
"is invalid. (Between
5 KB and 10 KB
please.)"
message:
"is invalid. (Between
#{
storage_units
[
5120
]
}
and
#{
storage_units
[
10240
]
}
please.)"
end
end
end
end
...
@@ -174,9 +182,9 @@ describe Paperclip::Validators::AttachmentSizeValidator do
...
@@ -174,9 +182,9 @@ describe Paperclip::Validators::AttachmentSizeValidator do
end
end
should_not_allow_attachment_file_size
11
.
kilobytes
,
should_not_allow_attachment_file_size
11
.
kilobytes
,
message:
"must be less than
10 KB
"
message:
"must be less than
#{
storage_units
[
10240
]
}
"
should_not_allow_attachment_file_size
4
.
kilobytes
,
should_not_allow_attachment_file_size
4
.
kilobytes
,
message:
"must be greater than
5 KB
"
message:
"must be greater than
#{
storage_units
[
5120
]
}
"
end
end
context
"given a size range"
do
context
"given a size range"
do
...
@@ -185,9 +193,9 @@ describe Paperclip::Validators::AttachmentSizeValidator do
...
@@ -185,9 +193,9 @@ describe Paperclip::Validators::AttachmentSizeValidator do
end
end
should_not_allow_attachment_file_size
11
.
kilobytes
,
should_not_allow_attachment_file_size
11
.
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
,
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
]
}
"
end
end
end
end
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment