Module: Paperclip::Interpolations
Overview
This module contains all the methods that are available for interpolation in paths and urls. To add your own (or override an existing one), you can either open this module and define it, or call the Paperclip.interpolates method.
Class Method Summary collapse
-
.[](name) ⇒ Object
Hash access of interpolations.
-
.[]=(name, block) ⇒ Object
Hash assignment of interpolations.
-
.all ⇒ Object
Returns a sorted list of all interpolations.
-
.interpolate(pattern, *args) ⇒ Object
Perform the actual interpolation.
Instance Method Summary collapse
-
#attachment(attachment, style) ⇒ Object
Returns the pluralized form of the attachment name.
-
#basename(attachment, style) ⇒ Object
Returns the basename of the file.
-
#class(attachment = nil, style = nil) ⇒ Object
Returns the underscored, pluralized version of the class name.
-
#extension(attachment, style) ⇒ Object
Returns the extension of the file.
-
#filename(attachment, style) ⇒ Object
Returns the filename, the same way as “:basename.:extension” would.
-
#id(attachment, style) ⇒ Object
Returns the id of the instance.
-
#id_partition(attachment, style) ⇒ Object
Returns the id of the instance in a split path form.
-
#rails_env(attachment, style) ⇒ Object
Returns the RAILS_ENV constant.
-
#rails_root(attachment, style) ⇒ Object
Returns the RAILS_ROOT constant.
-
#style(attachment, style) ⇒ Object
Returns the style, or the default style if nil is supplied.
-
#timestamp(attachment, style) ⇒ Object
Returns the timestamp as defined by the <attachment>_updated_at field.
-
#url(attachment, style) ⇒ Object
Returns the interpolated URL.
Class Method Details
.[](name) ⇒ Object
Hash access of interpolations. Included only for compatability, and is not intended for normal use.
17 18 19 |
# File 'lib/paperclip/interpolations.rb', line 17 def self.[] name method(name) end |
.[]=(name, block) ⇒ Object
Hash assignment of interpolations. Included only for compatability, and is not intended for normal use.
11 12 13 |
# File 'lib/paperclip/interpolations.rb', line 11 def self.[]= name, block define_method(name, &block) end |
.all ⇒ Object
Returns a sorted list of all interpolations.
22 23 24 |
# File 'lib/paperclip/interpolations.rb', line 22 def self.all self.instance_methods(false).sort end |
.interpolate(pattern, *args) ⇒ Object
Perform the actual interpolation. Takes the pattern to interpolate and the arguments to pass, which are the attachment and style name.
28 29 30 31 32 33 34 |
# File 'lib/paperclip/interpolations.rb', line 28 def self.interpolate pattern, *args all.reverse.inject( pattern.dup ) do |result, tag| result.gsub(/:#{tag}/) do |match| send( tag, *args ) end end end |
Instance Method Details
#attachment(attachment, style) ⇒ Object
Returns the pluralized form of the attachment name. e.g. “avatars” for an attachment of :avatar
103 104 105 |
# File 'lib/paperclip/interpolations.rb', line 103 def , style .name.to_s.downcase.pluralize end |
#basename(attachment, style) ⇒ Object
Returns the basename of the file. e.g. “file” for “file.jpg”
78 79 80 |
# File 'lib/paperclip/interpolations.rb', line 78 def basename , style .original_filename.gsub(/#{File.extname(.original_filename)}$/, "") end |
#class(attachment = nil, style = nil) ⇒ Object
Returns the underscored, pluralized version of the class name. e.g. “users” for the User class. NOTE: The arguments need to be optional, because some tools fetch all class names. Calling #class will return the expected class.
72 73 74 75 |
# File 'lib/paperclip/interpolations.rb', line 72 def class = nil, style = nil return super() if .nil? && style.nil? .instance.class.to_s.underscore.pluralize end |
#extension(attachment, style) ⇒ Object
Returns the extension of the file. e.g. “jpg” for “file.jpg” If the style has a format defined, it will return the format instead of the actual extension.
85 86 87 88 |
# File 'lib/paperclip/interpolations.rb', line 85 def extension , style ((style = .styles[style]) && style[:format]) || File.extname(.original_filename).gsub(/^\.+/, "") end |
#filename(attachment, style) ⇒ Object
Returns the filename, the same way as “:basename.:extension” would.
37 38 39 40 41 42 43 |
# File 'lib/paperclip/interpolations.rb', line 37 def filename , style unless extension(, style).strip.empty? "#{basename(, style)}.#{extension(, style)}" else basename(, style) end end |
#id(attachment, style) ⇒ Object
Returns the id of the instance.
91 92 93 |
# File 'lib/paperclip/interpolations.rb', line 91 def id , style .instance.id end |
#id_partition(attachment, style) ⇒ Object
Returns the id of the instance in a split path form. e.g. returns 000/001/234 for an id of 1234.
97 98 99 |
# File 'lib/paperclip/interpolations.rb', line 97 def id_partition , style ("%09d" % .instance.id).scan(/\d{3}/).join("/") end |
#rails_env(attachment, style) ⇒ Object
Returns the RAILS_ENV constant.
64 65 66 |
# File 'lib/paperclip/interpolations.rb', line 64 def rails_env , style RAILS_ENV end |
#rails_root(attachment, style) ⇒ Object
Returns the RAILS_ROOT constant.
59 60 61 |
# File 'lib/paperclip/interpolations.rb', line 59 def rails_root , style RAILS_ROOT end |
#style(attachment, style) ⇒ Object
Returns the style, or the default style if nil is supplied.
108 109 110 |
# File 'lib/paperclip/interpolations.rb', line 108 def style , style style || .default_style end |
#timestamp(attachment, style) ⇒ Object
Returns the timestamp as defined by the <attachment>_updated_at field
54 55 56 |
# File 'lib/paperclip/interpolations.rb', line 54 def , style .instance_read(:updated_at).to_s end |
#url(attachment, style) ⇒ Object
Returns the interpolated URL. Will raise an error if the url itself contains “:url” to prevent infinite recursion. This interpolation is used in the default :path to ease default specifications.
48 49 50 51 |
# File 'lib/paperclip/interpolations.rb', line 48 def url , style raise InfiniteInterpolationError if .[:url].include?(":url") .url(style, false) end |