| 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.
Hash access of interpolations. Included only for compatability, and is not intended for normal use.
# 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.
# File lib/paperclip/interpolations.rb, line 11
11: def self.[]= name, block
12: define_method(name, &block)
13: end
Perform the actual interpolation. Takes the pattern to interpolate and the arguments to pass, which are the attachment and style name.
# 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
Returns the pluralized form of the attachment name. e.g. "avatars" for an attachment of :avatar
# File lib/paperclip/interpolations.rb, line 96
96: def attachment attachment, style
97: attachment.name.to_s.downcase.pluralize
98: 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.
# File lib/paperclip/interpolations.rb, line 78
78: def extension attachment, style
79: ((style = attachment.styles[style]) && style[:format]) ||
80: File.extname(attachment.original_filename).gsub(/^\.+/, "")
81: end
Returns the RAILS_ENV constant.
# File lib/paperclip/interpolations.rb, line 60
60: def rails_env attachment, style
61: RAILS_ENV
62: end
Returns the RAILS_ROOT constant.
# File lib/paperclip/interpolations.rb, line 55
55: def rails_root attachment, style
56: RAILS_ROOT
57: end
Returns the timestamp as defined by the <attachment>_updated_at field
# File lib/paperclip/interpolations.rb, line 50
50: def timestamp attachment, style
51: attachment.instance_read(:updated_at).to_s
52: end
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 lib/paperclip/interpolations.rb, line 44
44: def url attachment, style
45: raise InfiniteInterpolationError if attachment.options[:url].include?(":url")
46: attachment.url(style)
47: end