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
ec3ffdd1
Commit
ec3ffdd1
authored
Jan 18, 2013
by
Jon Yurek
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Cache pluralizations in interpolations for speed
parent
0d4bf12d
Hide whitespace changes
Inline
Side-by-side
Showing
4 changed files
with
60 additions
and
2 deletions
+60
-2
lib/paperclip.rb
+1
-0
lib/paperclip/interpolations.rb
+6
-2
lib/paperclip/interpolations/plural_cache.rb
+17
-0
test/plural_cache_test.rb
+36
-0
No files found.
lib/paperclip.rb
View file @
ec3ffdd1
...
...
@@ -35,6 +35,7 @@ require 'paperclip/geometry'
require
'paperclip/processor'
require
'paperclip/tempfile'
require
'paperclip/thumbnail'
require
'paperclip/interpolations/plural_cache'
require
'paperclip/interpolations'
require
'paperclip/tempfile_factory'
require
'paperclip/style'
...
...
lib/paperclip/interpolations.rb
View file @
ec3ffdd1
...
...
@@ -36,6 +36,10 @@ module Paperclip
end
end
def
self
.
plural_cache
@plural_cache
||=
PluralCache
.
new
end
# Returns the filename, the same way as ":basename.:extension" would.
def
filename
attachment
,
style_name
[
basename
(
attachment
,
style_name
),
extension
(
attachment
,
style_name
)
].
reject
(
&
:blank?
).
join
(
"."
)
...
...
@@ -81,7 +85,7 @@ module Paperclip
# all class names. Calling #class will return the expected class.
def
class
attachment
=
nil
,
style_name
=
nil
return
super
()
if
attachment
.
nil?
&&
style_name
.
nil?
attachment
.
instance
.
class
.
to_s
.
underscore
.
pluralize
plural_cache
.
underscore_and_pluralize
(
attachment
.
instance
.
class
.
to_s
)
end
# Returns the basename of the file. e.g. "file" for "file.jpg"
...
...
@@ -169,7 +173,7 @@ module Paperclip
# Returns the pluralized form of the attachment name. e.g.
# "avatars" for an attachment of :avatar
def
attachment
attachment
,
style_name
attachment
.
name
.
to_s
.
downcase
.
pluralize
plural_cache
.
pluralize
(
attachment
.
name
.
to_s
.
downcase
)
end
# Returns the style, or the default style if nil is supplied.
...
...
lib/paperclip/interpolations/plural_cache.rb
0 → 100644
View file @
ec3ffdd1
module
Paperclip
module
Interpolations
class
PluralCache
def
initialize
@cache
=
{}
end
def
pluralize
(
word
)
@cache
[
word
]
||=
word
.
pluralize
end
def
underscore_and_pluralize
(
word
)
@cache
[
word
]
||=
word
.
underscore
.
pluralize
end
end
end
end
test/plural_cache_test.rb
0 → 100644
View file @
ec3ffdd1
require
'./test/helper'
class
PluralCacheTest
<
Test
::
Unit
::
TestCase
should
'cache pluralizations'
do
cache
=
Paperclip
::
Interpolations
::
PluralCache
.
new
word
=
"box"
word
.
expects
(
:pluralize
).
returns
(
"boxes"
).
once
cache
.
pluralize
(
word
)
cache
.
pluralize
(
word
)
end
should
'cache pluralizations and underscores'
do
cache
=
Paperclip
::
Interpolations
::
PluralCache
.
new
word
=
"BigBox"
word
.
expects
(
:pluralize
).
returns
(
word
).
once
word
.
expects
(
:underscore
).
returns
(
word
).
once
cache
.
underscore_and_pluralize
(
word
)
cache
.
underscore_and_pluralize
(
word
)
end
should
'pluralize words'
do
cache
=
Paperclip
::
Interpolations
::
PluralCache
.
new
word
=
"box"
assert_equal
"boxes"
,
cache
.
pluralize
(
word
)
end
should
'pluralize and underscore words'
do
cache
=
Paperclip
::
Interpolations
::
PluralCache
.
new
word
=
"BigBox"
assert_equal
"big_boxes"
,
cache
.
underscore_and_pluralize
(
word
)
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