Files

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.

Public Class Methods

[](name) click to toggle source

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

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

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

# File lib/dm-paperclip/interpolations.rb, line 11
def self.[]= name, block
  define_method(name, &block)
end
all() click to toggle source

Returns a sorted list of all interpolations.

# File lib/dm-paperclip/interpolations.rb, line 22
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.

# File lib/dm-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

Public Instance Methods

attachment(attachment, style) click to toggle source

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

# File lib/dm-paperclip/interpolations.rb, line 114
def attachment attachment, style
  attachment.name.to_s.downcase.pluralize
end
basename(attachment, style) click to toggle source

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

# File lib/dm-paperclip/interpolations.rb, line 89
def basename attachment, style
  attachment.original_filename.gsub(/#{File.extname(attachment.original_filename)}$/, "")
end
class(attachment, style) click to toggle source

Returns the snake cased, pluralized version of the class name. e.g. "users" for the User class.

# File lib/dm-paperclip/interpolations.rb, line 84
def class attachment, style
  attachment.instance.class.to_s.snake_case.pluralize
end
extension(attachment, style) 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/dm-paperclip/interpolations.rb, line 96
def extension attachment, style
  ((style = attachment.styles[style]) && style[:format]) ||
    File.extname(attachment.original_filename).gsub(/^\.+/, "")
end
filename(attachment, style) click to toggle source

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

# File lib/dm-paperclip/interpolations.rb, line 37
def filename attachment, style
  "#{basename(attachment, style)}.#{extension(attachment, style)}"
end
id(attachment, style) click to toggle source

Returns the id of the instance.

# File lib/dm-paperclip/interpolations.rb, line 102
def id attachment, style
  attachment.instance.id
end
id_partition(attachment, style) 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/dm-paperclip/interpolations.rb, line 108
def id_partition attachment, style
  ("%09d" % attachment.instance.id).scan(/\d{3}/).join("/")
end
merb_env(attachment, style) click to toggle source
# File lib/dm-paperclip/interpolations.rb, line 78
def merb_env attachment, style
  Object.const_defined?('Merb') ? Merb.env : nil
end
merb_root(attachment, style) click to toggle source
# File lib/dm-paperclip/interpolations.rb, line 74
def merb_root attachment, style
  Object.const_defined?('Merb') ? Merb.root : nil
end
rails_env(attachment, style) click to toggle source

Returns the RAILS_ENV constant.

# File lib/dm-paperclip/interpolations.rb, line 70
def rails_env attachment, style
  Object.const_defined?('RAILS_ENV') ? RAILS_ENV : nil
end
rails_root(attachment, style) click to toggle source

Returns the RAILS_ROOT constant.

# File lib/dm-paperclip/interpolations.rb, line 65
def rails_root attachment, style
  Object.const_defined?('RAILS_ROOT') ? RAILS_ROOT : nil
end
style(attachment, style) click to toggle source

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

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

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

# File lib/dm-paperclip/interpolations.rb, line 50
def timestamp attachment, style
  attachment.instance_read(:updated_at).to_s
end
url(attachment, style) click to toggle source

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/dm-paperclip/interpolations.rb, line 44
def url attachment, style
  raise InfiniteInterpolationError if attachment.options[:url].include?(":url")
  attachment.url(style, false)
end
web_root(attachment, style) click to toggle source
# File lib/dm-paperclip/interpolations.rb, line 54
def web_root attachment, style
  if Object.const_defined?('Merb')
    merb_root(attachment, style)
  elsif Object.const_defined("RAILS_ROOT")
    rails_root(attachment, style)
  else
    ""
  end
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.