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
51bb0f9f
Commit
51bb0f9f
authored
Mar 30, 2012
by
Prem Sichanugrist
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Adding Attachment#geometry to get file's dimension
Fixes #768
parent
7088f5b9
Show whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
43 additions
and
2 deletions
+43
-2
lib/paperclip/attachment.rb
+5
-0
lib/paperclip/geometry.rb
+5
-0
test/attachment_test.rb
+15
-0
test/geometry_test.rb
+18
-2
No files found.
lib/paperclip/attachment.rb
View file @
51bb0f9f
...
@@ -319,6 +319,11 @@ module Paperclip
...
@@ -319,6 +319,11 @@ module Paperclip
instance
.
send
(
getter
)
if
responds
||
attr
.
to_s
==
"file_name"
instance
.
send
(
getter
)
if
responds
||
attr
.
to_s
==
"file_name"
end
end
# Returns a geometry object which you can get the image's dimension.
def
geometry
(
style
=
:original
)
Geometry
.
from_file
(
path
(
style
))
end
private
private
def
path_option
def
path_option
...
...
lib/paperclip/geometry.rb
View file @
51bb0f9f
...
@@ -11,6 +11,11 @@ module Paperclip
...
@@ -11,6 +11,11 @@ module Paperclip
@modifier
=
modifier
@modifier
=
modifier
end
end
# Returns the equality based on height, width, and modifier
def
==
(
other
)
height
==
other
.
height
&&
width
==
other
.
width
&&
modifier
==
other
.
modifier
end
# Uses ImageMagick to determing the dimensions of a file, passed in as either a
# Uses ImageMagick to determing the dimensions of a file, passed in as either a
# File or path.
# File or path.
# NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be
# NOTE: (race cond) Do not reassign the 'file' variable inside this method as it is likely to be
...
...
test/attachment_test.rb
View file @
51bb0f9f
...
@@ -1158,4 +1158,19 @@ class AttachmentTest < Test::Unit::TestCase
...
@@ -1158,4 +1158,19 @@ class AttachmentTest < Test::Unit::TestCase
end
end
end
end
context
"attachment's geometry"
do
setup
do
rebuild_model
@dummy
=
Dummy
.
new
@file
=
File
.
new
(
File
.
join
(
File
.
dirname
(
__FILE__
),
"fixtures"
,
"5k.png"
),
'rb'
)
@dummy
.
avatar
=
@file
@dummy
.
save!
@attachment
=
@dummy
.
avatar
@geometry
=
Paperclip
::
Geometry
.
from_file
(
@file
.
path
)
end
should
"return a correct geometry object"
do
assert_equal
@geometry
,
@attachment
.
geometry
end
end
end
end
test/geometry_test.rb
View file @
51bb0f9f
require
'./test/helper'
require
'./test/helper'
class
GeometryTest
<
Test
::
Unit
::
TestCase
class
GeometryTest
<
Test
::
Unit
::
TestCase
context
"Paperclip::Geometry"
do
context
"equality check"
do
should
"return true when width, height, modifier are all the same"
do
assert_equal
Paperclip
::
Geometry
.
new
(
100
,
100
,
"foo"
),
Paperclip
::
Geometry
.
new
(
100
,
100
,
"foo"
)
end
should
"return false if width, height, modifier are not the same"
do
assert_not_equal
Paperclip
::
Geometry
.
new
(
100
,
100
,
"foo"
),
Paperclip
::
Geometry
.
new
(
100
,
200
,
"foo"
)
assert_not_equal
Paperclip
::
Geometry
.
new
(
100
,
100
,
"foo"
),
Paperclip
::
Geometry
.
new
(
200
,
100
,
"foo"
)
assert_not_equal
Paperclip
::
Geometry
.
new
(
100
,
100
,
"foo"
),
Paperclip
::
Geometry
.
new
(
100
,
100
,
"bar"
)
end
end
should
"correctly report its given dimensions"
do
should
"correctly report its given dimensions"
do
assert
@geo
=
Paperclip
::
Geometry
.
new
(
1024
,
768
)
assert
@geo
=
Paperclip
::
Geometry
.
new
(
1024
,
768
)
assert_equal
1024
,
@geo
.
width
assert_equal
1024
,
@geo
.
width
...
@@ -139,11 +154,13 @@ class GeometryTest < Test::Unit::TestCase
...
@@ -139,11 +154,13 @@ class GeometryTest < Test::Unit::TestCase
should
"let us know when a command isn't found versus a processing error"
do
should
"let us know when a command isn't found versus a processing error"
do
old_path
=
ENV
[
'PATH'
]
old_path
=
ENV
[
'PATH'
]
begin
begin
silence_stream
(
STDERR
)
do
ENV
[
'PATH'
]
=
''
ENV
[
'PATH'
]
=
''
assert_raises
(
Paperclip
::
Errors
::
CommandNotFoundError
)
do
assert_raises
(
Paperclip
::
Errors
::
CommandNotFoundError
)
do
file
=
File
.
join
(
File
.
dirname
(
__FILE__
),
"fixtures"
,
"5k.png"
)
file
=
File
.
join
(
File
.
dirname
(
__FILE__
),
"fixtures"
,
"5k.png"
)
@geo
=
Paperclip
::
Geometry
.
from_file
(
file
)
@geo
=
Paperclip
::
Geometry
.
from_file
(
file
)
end
end
end
ensure
ensure
ENV
[
'PATH'
]
=
old_path
ENV
[
'PATH'
]
=
old_path
end
end
...
@@ -202,5 +219,4 @@ class GeometryTest < Test::Unit::TestCase
...
@@ -202,5 +219,4 @@ class GeometryTest < Test::Unit::TestCase
end
end
end
end
end
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