Module Paperclip::Interpolations
In: lib/paperclip/interpolations.rb

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.

Methods

[]   []=   all   attachment   basename   class   extension   filename   fingerprint   id   id_partition   interpolate   rails_env   rails_root   style   timestamp   url  

Constants

RIGHT_HERE = "#{__FILE__.gsub(%r{^\./}, "")}:#{__LINE__ + 3}"   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

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

[Source]

    # File lib/paperclip/interpolations.rb, line 17
17:     def self.[] name
18:       method(name)
19:     end

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

[Source]

    # File lib/paperclip/interpolations.rb, line 11
11:     def self.[]= name, block
12:       define_method(name, &block)
13:     end

Returns a sorted list of all interpolations.

[Source]

    # File lib/paperclip/interpolations.rb, line 22
22:     def self.all
23:       self.instance_methods(false).sort
24:     end

Perform the actual interpolation. Takes the pattern to interpolate and the arguments to pass, which are the attachment and style name.

[Source]

    # File lib/paperclip/interpolations.rb, line 28
28:     def self.interpolate pattern, *args
29:       all.reverse.inject( pattern.dup ) do |result, tag|
30:         result.gsub(/:#{tag}/) do |match|
31:           send( tag, *args )
32:         end
33:       end
34:     end

Public Instance methods

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

[Source]

     # File lib/paperclip/interpolations.rb, line 105
105:     def attachment attachment, style_name
106:       attachment.name.to_s.downcase.pluralize
107:     end

Returns the basename of the file. e.g. "file" for "file.jpg"

[Source]

    # File lib/paperclip/interpolations.rb, line 75
75:     def basename attachment, style_name
76:       attachment.original_filename.gsub(/#{File.extname(attachment.original_filename)}$/, "")
77:     end

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.

[Source]

    # File lib/paperclip/interpolations.rb, line 69
69:     def class attachment = nil, style_name = nil
70:       return super() if attachment.nil? && style_name.nil?
71:       attachment.instance.class.to_s.underscore.pluralize
72:     end

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.

[Source]

    # File lib/paperclip/interpolations.rb, line 82
82:     def extension attachment, style_name
83:       ((style = attachment.styles[style_name]) && style[:format]) ||
84:         File.extname(attachment.original_filename).gsub(/^\.+/, "")
85:     end

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

[Source]

    # File lib/paperclip/interpolations.rb, line 37
37:     def filename attachment, style_name
38:       "#{basename(attachment, style_name)}.#{extension(attachment, style_name)}"
39:     end

Returns the fingerprint of the instance.

[Source]

    # File lib/paperclip/interpolations.rb, line 93
93:     def fingerprint attachment, style_name
94:       attachment.fingerprint
95:     end

Returns the id of the instance.

[Source]

    # File lib/paperclip/interpolations.rb, line 88
88:     def id attachment, style_name
89:       attachment.instance.id
90:     end

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

[Source]

     # File lib/paperclip/interpolations.rb, line 99
 99:     def id_partition attachment, style_name
100:       ("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
101:     end

Returns the Rails.env constant.

[Source]

    # File lib/paperclip/interpolations.rb, line 61
61:     def rails_env attachment, style_name
62:       Rails.env
63:     end

Returns the Rails.root constant.

[Source]

    # File lib/paperclip/interpolations.rb, line 56
56:     def rails_root attachment, style_name
57:       Rails.root
58:     end

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

[Source]

     # File lib/paperclip/interpolations.rb, line 110
110:     def style attachment, style_name
111:       style_name || attachment.default_style
112:     end

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

[Source]

    # File lib/paperclip/interpolations.rb, line 51
51:     def timestamp attachment, style_name
52:       attachment.instance_read(:updated_at).to_s
53:     end

[Source]

    # File lib/paperclip/interpolations.rb, line 45
45:     def url attachment, style_name
46:       raise InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) }
47:       attachment.url(style_name, false)
48:     end

[Validate]