Module: Paperclip::Interpolations

Extended by:
Interpolations
Included in:
Interpolations
Defined in:
lib/paperclip/interpolations.rb

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.

Constant Summary collapse

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.

"#{__FILE__.gsub(%r{^\./}, "")}:#{__LINE__ + 3}"

Class Method Summary collapse

Instance Method Summary collapse

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

.allObject

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_name) ⇒ Object

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



105
106
107
# File 'lib/paperclip/interpolations.rb', line 105

def attachment attachment, style_name
  attachment.name.to_s.downcase.pluralize
end

#basename(attachment, style_name) ⇒ Object

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



75
76
77
# File 'lib/paperclip/interpolations.rb', line 75

def basename attachment, style_name
  attachment.original_filename.gsub(/#{File.extname(attachment.original_filename)}$/, "")
end

#class(attachment = nil, style_name = 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.



69
70
71
72
# File 'lib/paperclip/interpolations.rb', line 69

def class attachment = nil, style_name = nil
  return super() if attachment.nil? && style_name.nil?
  attachment.instance.class.to_s.underscore.pluralize
end

#extension(attachment, style_name) ⇒ 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.



82
83
84
85
# File 'lib/paperclip/interpolations.rb', line 82

def extension attachment, style_name
  ((style = attachment.styles[style_name]) && style[:format]) ||
    File.extname(attachment.original_filename).gsub(/^\.+/, "")
end

#filename(attachment, style_name) ⇒ Object

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



37
38
39
# File 'lib/paperclip/interpolations.rb', line 37

def filename attachment, style_name
  "#{basename(attachment, style_name)}.#{extension(attachment, style_name)}"
end

#fingerprint(attachment, style_name) ⇒ Object

Returns the fingerprint of the instance.



93
94
95
# File 'lib/paperclip/interpolations.rb', line 93

def fingerprint attachment, style_name
  attachment.fingerprint
end

#id(attachment, style_name) ⇒ Object

Returns the id of the instance.



88
89
90
# File 'lib/paperclip/interpolations.rb', line 88

def id attachment, style_name
  attachment.instance.id
end

#id_partition(attachment, style_name) ⇒ Object

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



99
100
101
# File 'lib/paperclip/interpolations.rb', line 99

def id_partition attachment, style_name
  ("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
end

#rails_env(attachment, style_name) ⇒ Object

Returns the Rails.env constant.



61
62
63
# File 'lib/paperclip/interpolations.rb', line 61

def rails_env attachment, style_name
  Rails.env
end

#rails_root(attachment, style_name) ⇒ Object

Returns the Rails.root constant.



56
57
58
# File 'lib/paperclip/interpolations.rb', line 56

def rails_root attachment, style_name
  Rails.root
end

#style(attachment, style_name) ⇒ Object

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



110
111
112
# File 'lib/paperclip/interpolations.rb', line 110

def style attachment, style_name
  style_name || attachment.default_style
end

#timestamp(attachment, style_name) ⇒ Object

Returns the timestamp as defined by the <attachment>_updated_at field



51
52
53
# File 'lib/paperclip/interpolations.rb', line 51

def timestamp attachment, style_name
  attachment.instance_read(:updated_at).to_s
end

#url(attachment, style_name) ⇒ Object



45
46
47
48
# File 'lib/paperclip/interpolations.rb', line 45

def url attachment, style_name
  raise InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) }
  attachment.url(style_name, false)
end