module Paperclip::Interpolations

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.

Constants

ID_PARTITION_LIMIT
RIGHT_HERE

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.

Public Class Methods

[](name) click to toggle source

Hash access of interpolations. Included only for compatibility, and is not intended for normal use.

# File lib/paperclip/interpolations.rb, line 19
def self.[] name
  method(name)
end
[]=(name, block) click to toggle source

Hash assignment of interpolations. Included only for compatibility, and is not intended for normal use.

# File lib/paperclip/interpolations.rb, line 12
def self.[]= name, block
  define_method(name, &block)
  @interpolators_cache = nil
end
all() click to toggle source

Returns a sorted list of all interpolations.

# File lib/paperclip/interpolations.rb, line 24
def self.all
  self.instance_methods(false).sort!
end
interpolate(pattern, *args) click to toggle source

Perform the actual interpolation. Takes the pattern to interpolate and the arguments to pass, which are the attachment and style name. You can pass a method name on your record as a symbol, which should turn an interpolation pattern for Paperclip to use.

# File lib/paperclip/interpolations.rb, line 32
def self.interpolate pattern, *args
  pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol
  result = pattern.dup
  interpolators_cache.each do |method, token|
    result.gsub!(token) { send(method, *args) } if result.include?(token)
  end
  result
end
interpolators_cache() click to toggle source
# File lib/paperclip/interpolations.rb, line 41
def self.interpolators_cache
  @interpolators_cache ||= all.reverse!.map! { |method| [method, ":#{method}"] }
end
plural_cache() click to toggle source
# File lib/paperclip/interpolations.rb, line 45
def self.plural_cache
  @plural_cache ||= PluralCache.new
end

Public Instance Methods

attachment(attachment, style_name) click to toggle source

Returns the pluralized form of the attachment name. e.g. “avatars” for an attachment of :avatar

# File lib/paperclip/interpolations.rb, line 193
def attachment attachment, style_name
  plural_cache.pluralize_symbol(attachment.name)
end
basename(attachment, style_name) click to toggle source

Returns the basename of the file. e.g. “file” for “file.jpg”

# File lib/paperclip/interpolations.rb, line 98
def basename attachment, style_name
  File.basename(attachment.original_filename, ".*".freeze)
end
class(attachment = nil, style_name = nil) click to toggle source

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.

Calls superclass method
# File lib/paperclip/interpolations.rb, line 92
def class attachment = nil, style_name = nil
  return super() if attachment.nil? && style_name.nil?
  plural_cache.underscore_and_pluralize_class(attachment.instance.class)
end
content_type_extension(attachment, style_name) click to toggle source

Returns an extension based on the content type. e.g. “jpeg” for “image/jpeg”. If the style has a specified format, it will override the content-type detection.

Each mime type generally has multiple extensions associated with it, so if the extension from the original filename is one of these extensions, that extension is used, otherwise, the first in the list is used.

# File lib/paperclip/interpolations.rb, line 125
def content_type_extension attachment, style_name
  mime_type = MIME::Types[attachment.content_type]
  extensions_for_mime_type = unless mime_type.empty?
    mime_type.first.extensions
  else
    []
  end

  original_extension = extension(attachment, style_name)
  style = attachment.styles[style_name.to_s.to_sym]
  if style && style[:format]
    style[:format].to_s
  elsif extensions_for_mime_type.include? original_extension
    original_extension
  elsif !extensions_for_mime_type.empty?
    extensions_for_mime_type.first
  else
    # It's possible, though unlikely, that the mime type is not in the
    # database, so just use the part after the '/' in the mime type as the
    # extension.
    %r{/([^/]*)\z}.match(attachment.content_type)[1]
  end
end
dotextension(attachment, style_name) click to toggle source

Returns the dot+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. If the extension is empty, no dot is added.

# File lib/paperclip/interpolations.rb, line 113
def dotextension attachment, style_name
  ext = extension(attachment, style_name)
  ext.empty? ? ext : ".#{ext}"
end
extension(attachment, style_name) click to toggle source

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.

# File lib/paperclip/interpolations.rb, line 105
def extension attachment, style_name
  ((style = attachment.styles[style_name.to_s.to_sym]) && style[:format]) ||
    File.extname(attachment.original_filename).sub(/\A\.+/, "".freeze)
end
filename(attachment, style_name) click to toggle source

Returns the filename, the same way as “:basename.:extension” would.

# File lib/paperclip/interpolations.rb, line 50
def filename attachment, style_name
  [ basename(attachment, style_name), extension(attachment, style_name) ].delete_if(&:empty?).join(".".freeze)
end
fingerprint(attachment, style_name) click to toggle source

Returns the fingerprint of the instance.

# File lib/paperclip/interpolations.rb, line 160
def fingerprint attachment, style_name
  attachment.fingerprint
end
hash(attachment=nil, style_name=nil) click to toggle source

Returns a the attachment hash. See Paperclip::Attachment#hash_key for more details.

Calls superclass method
# File lib/paperclip/interpolations.rb, line 166
def hash attachment=nil, style_name=nil
  if attachment && style_name
    attachment.hash_key(style_name)
  else
    super()
  end
end
id(attachment, style_name) click to toggle source

Returns the id of the instance.

# File lib/paperclip/interpolations.rb, line 150
def id attachment, style_name
  attachment.instance.id
end
id_partition(attachment, style_name) click to toggle source

Returns the id of the instance in a split path form. e.g. returns 000/001/234 for an id of 1234.

# File lib/paperclip/interpolations.rb, line 176
def id_partition attachment, style_name
  case id = attachment.instance.id
  when Integer
    if id < ID_PARTITION_LIMIT
      ("%09d".freeze % id).scan(/\d{3}/).join("/".freeze)
    else
      ("%012d".freeze % id).scan(/\d{3}/).join("/".freeze)
    end
  when String
    id.scan(/.{3}/).first(3).join("/".freeze)
  else
    nil
  end
end
param(attachment, style_name) click to toggle source

Returns the to_param of the instance.

# File lib/paperclip/interpolations.rb, line 155
def param attachment, style_name
  attachment.instance.to_param
end
rails_env(attachment, style_name) click to toggle source

Returns the Rails.env constant.

# File lib/paperclip/interpolations.rb, line 84
def rails_env attachment, style_name
  Rails.env
end
rails_root(attachment, style_name) click to toggle source

Returns the Rails.root constant.

# File lib/paperclip/interpolations.rb, line 79
def rails_root attachment, style_name
  Rails.root
end
style(attachment, style_name) click to toggle source

Returns the style, or the default style if nil is supplied.

# File lib/paperclip/interpolations.rb, line 198
def style attachment, style_name
  style_name || attachment.default_style
end
timestamp(attachment, style_name) click to toggle source

Returns the timestamp as defined by the <attachment>_updated_at field in the server default time zone unless :use_global_time_zone is set to false. Note that a Rails.config.time_zone change will still invalidate any path or URL that uses :timestamp. For a time_zone-agnostic timestamp, use updated_at.

# File lib/paperclip/interpolations.rb, line 68
def timestamp attachment, style_name
  attachment.instance_read(:updated_at).in_time_zone(attachment.time_zone).to_s
end
updated_at(attachment, style_name) click to toggle source

Returns an integer timestamp that is time zone-neutral, so that paths remain valid even if a server's time zone changes.

# File lib/paperclip/interpolations.rb, line 74
def updated_at attachment, style_name
  attachment.updated_at
end
url(attachment, style_name) click to toggle source
# File lib/paperclip/interpolations.rb, line 58
def url attachment, style_name
  raise Errors::InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) }
  attachment.url(style_name, :timestamp => false, :escape => false)
end