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
f22d78f3
Commit
f22d78f3
authored
May 06, 2009
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Moved interpolations out of attachment
parent
ea5f6ac0
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
138 additions
and
24 deletions
+138
-24
lib/paperclip.rb
+1
-0
lib/paperclip/attachment.rb
+4
-24
lib/paperclip/interpolations.rb
+54
-0
test/helper.rb
+1
-0
test/interpolations_test.rb
+78
-0
No files found.
lib/paperclip.rb
View file @
f22d78f3
...
@@ -32,6 +32,7 @@ require 'paperclip/geometry'
...
@@ -32,6 +32,7 @@ require 'paperclip/geometry'
require
'paperclip/processor'
require
'paperclip/processor'
require
'paperclip/thumbnail'
require
'paperclip/thumbnail'
require
'paperclip/storage'
require
'paperclip/storage'
require
'paperclip/interpolations'
require
'paperclip/attachment'
require
'paperclip/attachment'
if
defined?
RAILS_ROOT
if
defined?
RAILS_ROOT
Dir
.
glob
(
File
.
join
(
File
.
expand_path
(
RAILS_ROOT
),
"lib"
,
"paperclip_processors"
,
"*.rb"
)).
each
do
|
processor
|
Dir
.
glob
(
File
.
join
(
File
.
expand_path
(
RAILS_ROOT
),
"lib"
,
"paperclip_processors"
,
"*.rb"
)).
each
do
|
processor
|
...
...
lib/paperclip/attachment.rb
View file @
f22d78f3
...
@@ -198,26 +198,7 @@ module Paperclip
...
@@ -198,26 +198,7 @@ module Paperclip
# style as arguments. This hash can be added to with your own proc if
# style as arguments. This hash can be added to with your own proc if
# necessary.
# necessary.
def
self
.
interpolations
def
self
.
interpolations
@interpolations
||=
{
Paperclip
::
Interpolations
:rails_root
=>
lambda
{
|
attachment
,
style
|
RAILS_ROOT
},
:rails_env
=>
lambda
{
|
attachment
,
style
|
RAILS_ENV
},
:class
=>
lambda
do
|
attachment
,
style
|
attachment
.
instance
.
class
.
name
.
underscore
.
pluralize
end
,
:basename
=>
lambda
do
|
attachment
,
style
|
attachment
.
original_filename
.
gsub
(
/
#{
File
.
extname
(
attachment
.
original_filename
)
}
$/
,
""
)
end
,
:extension
=>
lambda
do
|
attachment
,
style
|
((
style
=
attachment
.
styles
[
style
])
&&
style
[
:format
])
||
File
.
extname
(
attachment
.
original_filename
).
gsub
(
/^\.+/
,
""
)
end
,
:id
=>
lambda
{
|
attachment
,
style
|
attachment
.
instance
.
id
},
:id_partition
=>
lambda
do
|
attachment
,
style
|
(
"%09d"
%
attachment
.
instance
.
id
).
scan
(
/\d{3}/
).
join
(
"/"
)
end
,
:attachment
=>
lambda
{
|
attachment
,
style
|
attachment
.
name
.
to_s
.
downcase
.
pluralize
},
:style
=>
lambda
{
|
attachment
,
style
|
style
||
attachment
.
default_style
},
}
end
end
# This method really shouldn't be called that often. It's expected use is
# This method really shouldn't be called that often. It's expected use is
...
@@ -373,11 +354,10 @@ module Paperclip
...
@@ -373,11 +354,10 @@ module Paperclip
end
end
def
interpolate
pattern
,
style
=
default_style
#:nodoc:
def
interpolate
pattern
,
style
=
default_style
#:nodoc:
interpolations
=
self
.
class
.
interpolations
.
sort
{
|
a
,
b
|
a
.
first
.
to_s
<=>
b
.
first
.
to_s
}
interpolations
=
Paperclip
::
Interpolations
.
all
interpolations
.
reverse
.
inject
(
pattern
.
dup
)
do
|
result
,
interpolation
|
interpolations
.
reverse
.
inject
(
pattern
.
dup
)
do
|
result
,
tag
|
tag
,
blk
=
interpolation
result
.
gsub
(
/:
#{
tag
}
/
)
do
|
match
|
result
.
gsub
(
/:
#{
tag
}
/
)
do
|
match
|
blk
.
call
(
self
,
style
)
Paperclip
::
Interpolations
.
send
(
tag
,
self
,
style
)
end
end
end
end
end
end
...
...
lib/paperclip/interpolations.rb
0 → 100644
View file @
f22d78f3
module
Paperclip
module
Interpolations
def
self
.
[]=
name
,
block
(
class
<<
self
;
self
;
end
).
class_eval
do
define_method
(
name
,
&
block
)
end
end
def
self
.
[]
name
method
(
name
)
end
def
self
.
all
(
singleton_methods
-
[
"[]="
,
"[]"
,
"all"
]).
sort
end
def
self
.
rails_root
attachment
,
style
RAILS_ROOT
end
def
self
.
rails_env
attachment
,
style
RAILS_ENV
end
def
self
.
class
attachment
,
style
attachment
.
instance
.
class
.
to_s
.
underscore
.
pluralize
end
def
self
.
basename
attachment
,
style
attachment
.
original_filename
.
gsub
(
/
#{
File
.
extname
(
attachment
.
original_filename
)
}
$/
,
""
)
end
def
self
.
extension
attachment
,
style
((
style
=
attachment
.
styles
[
style
])
&&
style
[
:format
])
||
File
.
extname
(
attachment
.
original_filename
).
gsub
(
/^\.+/
,
""
)
end
def
self
.
id
attachment
,
style
attachment
.
instance
.
id
end
def
self
.
id_partition
attachment
,
style
(
"%09d"
%
attachment
.
instance
.
id
).
scan
(
/\d{3}/
).
join
(
"/"
)
end
def
self
.
attachment
attachment
,
style
attachment
.
name
.
to_s
.
downcase
.
pluralize
end
def
self
.
style
attachment
,
style
style
||
attachment
.
default_style
end
end
end
test/helper.rb
View file @
f22d78f3
...
@@ -17,6 +17,7 @@ end
...
@@ -17,6 +17,7 @@ end
ROOT
=
File
.
join
(
File
.
dirname
(
__FILE__
),
'..'
)
ROOT
=
File
.
join
(
File
.
dirname
(
__FILE__
),
'..'
)
RAILS_ROOT
=
ROOT
RAILS_ROOT
=
ROOT
RAILS_ENV
=
"test"
$LOAD_PATH
<<
File
.
join
(
ROOT
,
'lib'
)
$LOAD_PATH
<<
File
.
join
(
ROOT
,
'lib'
)
$LOAD_PATH
<<
File
.
join
(
ROOT
,
'lib'
,
'paperclip'
)
$LOAD_PATH
<<
File
.
join
(
ROOT
,
'lib'
,
'paperclip'
)
...
...
test/interpolations_test.rb
0 → 100644
View file @
f22d78f3
require
'test/helper'
class
InterpolationsTest
<
Test
::
Unit
::
TestCase
should
"return all methods but the infrastructure when sent #all"
do
methods
=
Paperclip
::
Interpolations
.
all
assert
!
methods
.
include?
(
:[]
)
assert
!
methods
.
include?
(
:[]=
)
assert
!
methods
.
include?
(
:all
)
methods
.
each
do
|
m
|
assert
Paperclip
::
Interpolations
.
respond_to?
m
end
end
should
"return the RAILS_ROOT"
do
assert_equal
RAILS_ROOT
,
Paperclip
::
Interpolations
.
rails_root
(
:attachment
,
:style
)
end
should
"return the RAILS_ENV"
do
assert_equal
RAILS_ENV
,
Paperclip
::
Interpolations
.
rails_env
(
:attachment
,
:style
)
end
should
"return the class of the instance"
do
attachment
=
mock
attachment
.
expects
(
:instance
).
returns
(
attachment
)
attachment
.
expects
(
:class
).
returns
(
"Thing"
)
assert_equal
"things"
,
Paperclip
::
Interpolations
.
class
(
attachment
,
:style
)
end
should
"return the basename of the file"
do
attachment
=
mock
attachment
.
expects
(
:original_filename
).
returns
(
"one.jpg"
).
times
(
2
)
assert_equal
"one"
,
Paperclip
::
Interpolations
.
basename
(
attachment
,
:style
)
end
should
"return the extension of the file"
do
attachment
=
mock
attachment
.
expects
(
:original_filename
).
returns
(
"one.jpg"
)
attachment
.
expects
(
:styles
).
returns
({})
assert_equal
"jpg"
,
Paperclip
::
Interpolations
.
extension
(
attachment
,
:style
)
end
should
"return the extension of the file as the format is defined in the style"
do
attachment
=
mock
attachment
.
expects
(
:original_filename
).
never
attachment
.
expects
(
:styles
).
returns
({
:style
=>
{
:format
=>
"png"
}})
assert_equal
"png"
,
Paperclip
::
Interpolations
.
extension
(
attachment
,
:style
)
end
should
"return the id of the attachment"
do
attachment
=
mock
attachment
.
expects
(
:id
).
returns
(
23
)
attachment
.
expects
(
:instance
).
returns
(
attachment
)
assert_equal
23
,
Paperclip
::
Interpolations
.
id
(
attachment
,
:style
)
end
should
"return the partitioned id of the attachment"
do
attachment
=
mock
attachment
.
expects
(
:id
).
returns
(
23
)
attachment
.
expects
(
:instance
).
returns
(
attachment
)
assert_equal
"000/000/023"
,
Paperclip
::
Interpolations
.
id_partition
(
attachment
,
:style
)
end
should
"return the name of the attachment"
do
attachment
=
mock
attachment
.
expects
(
:name
).
returns
(
"file"
)
assert_equal
"files"
,
Paperclip
::
Interpolations
.
attachment
(
attachment
,
:style
)
end
should
"return the style"
do
assert_equal
:style
,
Paperclip
::
Interpolations
.
style
(
:attachment
,
:style
)
end
should
"return the default style"
do
attachment
=
mock
attachment
.
expects
(
:default_style
).
returns
(
:default_style
)
assert_equal
:default_style
,
Paperclip
::
Interpolations
.
style
(
attachment
,
nil
)
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