Module: Paperclip::Interpolations
- Extended by:
- Interpolations
- Included in:
- Interpolations
- Defined in:
- lib/dm-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
-
.[](name) ⇒ Object
Hash access of interpolations.
-
.[]=(name, block) ⇒ Object
Hash assignment of interpolations.
-
.all ⇒ Object
Returns a sorted list of all interpolations.
-
.interpolate(pattern, *args) ⇒ Object
Perform the actual interpolation.
Instance Method Summary collapse
-
#attachment(attachment, style_name) ⇒ Object
Returns the pluralized form of the attachment name.
-
#basename(attachment, style_name) ⇒ Object
Returns the basename of the file.
-
#class(attachment = nil, style_name = nil) ⇒ Object
Returns the underscored, pluralized version of the class name.
-
#extension(attachment, style_name) ⇒ Object
Returns the extension of the file.
-
#filename(attachment, style_name) ⇒ Object
Returns the filename, the same way as “:basename.:extension” would.
-
#fingerprint(attachment, style_name) ⇒ Object
Returns the fingerprint of the instance.
-
#hash(attachment, style_name) ⇒ Object
Returns a the attachment hash.
-
#id(attachment, style_name) ⇒ Object
Returns the id of the instance.
-
#id_partition(attachment, style_name) ⇒ Object
Returns the id of the instance in a split path form.
- #merb_env(attachment, style) ⇒ Object
- #merb_root(attachment, style) ⇒ Object
-
#rails_env(attachment, style_name) ⇒ Object
Returns the Rails.env constant.
-
#rails_root(attachment, style_name) ⇒ Object
Returns the Rails.root constant.
-
#style(attachment, style_name) ⇒ Object
Returns the style, or the default style if nil is supplied.
-
#timestamp(attachment, style_name) ⇒ Object
Returns the timestamp as defined by the <attachment>_updated_at field.
-
#updated_at(attachment, style_name) ⇒ Object
Returns an integer timestamp that is time zone-neutral, so that paths remain valid even if a server’s time zone changes.
- #url(attachment, style_name) ⇒ Object
-
#uuid(attachment, style_name) ⇒ Object
Returns the uuid of the instance.
- #web_root(attachment, style) ⇒ Object
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/dm-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/dm-paperclip/interpolations.rb', line 11 def self.[]= name, block define_method(name, &block) end |
.all ⇒ Object
Returns a sorted list of all interpolations.
22 23 24 |
# File 'lib/dm-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 35 |
# 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 |
Instance Method Details
#attachment(attachment, style_name) ⇒ Object
Returns the pluralized form of the attachment name. e.g. “avatars” for an attachment of :avatar
134 135 136 |
# File 'lib/dm-paperclip/interpolations.rb', line 134 def , style_name DataMapper::Inflector.pluralize(.name.to_s.downcase) end |
#basename(attachment, style_name) ⇒ Object
Returns the basename of the file. e.g. “file” for “file.jpg”
98 99 100 |
# File 'lib/dm-paperclip/interpolations.rb', line 98 def basename , style_name .original_filename.gsub(/#{File.extname(.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.
91 92 93 94 95 |
# File 'lib/dm-paperclip/interpolations.rb', line 91 def class = nil, style_name = nil return super() if .nil? && style_name.nil? name = DataMapper::Inflector.underscore(.instance.class.to_s) DataMapper::Inflector.pluralize(name) 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.
105 106 107 108 |
# File 'lib/dm-paperclip/interpolations.rb', line 105 def extension , style_name ((style = .styles[style_name]) && style[:format]) || File.extname(.original_filename).gsub(/^\.+/, "") end |
#filename(attachment, style_name) ⇒ Object
Returns the filename, the same way as “:basename.:extension” would.
38 39 40 |
# File 'lib/dm-paperclip/interpolations.rb', line 38 def filename , style_name "#{basename(, style_name)}.#{extension(, style_name)}" end |
#fingerprint(attachment, style_name) ⇒ Object
Returns the fingerprint of the instance.
116 117 118 |
# File 'lib/dm-paperclip/interpolations.rb', line 116 def fingerprint , style_name .fingerprint end |
#hash(attachment, style_name) ⇒ Object
Returns a the attachment hash. See Paperclip::Attachment#hash for more details.
122 123 124 |
# File 'lib/dm-paperclip/interpolations.rb', line 122 def hash , style_name .hash(style_name) end |
#id(attachment, style_name) ⇒ Object
Returns the id of the instance.
111 112 113 |
# File 'lib/dm-paperclip/interpolations.rb', line 111 def id , style_name .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.
128 129 130 |
# File 'lib/dm-paperclip/interpolations.rb', line 128 def id_partition , style_name ("%09d" % .instance.id).scan(/\d{3}/).join("/") end |
#merb_env(attachment, style) ⇒ Object
83 84 85 |
# File 'lib/dm-paperclip/interpolations.rb', line 83 def merb_env , style Object.const_defined?('Merb') ? Merb.env : nil end |
#merb_root(attachment, style) ⇒ Object
79 80 81 |
# File 'lib/dm-paperclip/interpolations.rb', line 79 def merb_root , style Object.const_defined?('Merb') ? Merb.root : nil end |
#rails_env(attachment, style_name) ⇒ Object
Returns the Rails.env constant.
75 76 77 |
# File 'lib/dm-paperclip/interpolations.rb', line 75 def rails_env , style_name Object.const_defined?('Rails') ? Rails.env : nil end |
#rails_root(attachment, style_name) ⇒ Object
Returns the Rails.root constant.
70 71 72 |
# File 'lib/dm-paperclip/interpolations.rb', line 70 def rails_root , style_name Object.const_defined?('Rails') ? Rails.root : nil end |
#style(attachment, style_name) ⇒ Object
Returns the style, or the default style if nil is supplied.
139 140 141 |
# File 'lib/dm-paperclip/interpolations.rb', line 139 def style , style_name style_name || .default_style end |
#timestamp(attachment, style_name) ⇒ Object
Returns the timestamp as defined by the <attachment>_updated_at field
52 53 54 |
# File 'lib/dm-paperclip/interpolations.rb', line 52 def , style_name .instance_read(:updated_at).to_s end |
#updated_at(attachment, style_name) ⇒ Object
Returns an integer timestamp that is time zone-neutral, so that paths remain valid even if a server’s time zone changes.
58 59 60 |
# File 'lib/dm-paperclip/interpolations.rb', line 58 def updated_at , style_name .updated_at end |
#url(attachment, style_name) ⇒ Object
46 47 48 49 |
# File 'lib/dm-paperclip/interpolations.rb', line 46 def url , style_name raise InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) } .url(style_name, false) end |
#uuid(attachment, style_name) ⇒ Object
Returns the uuid of the instance.
144 145 146 |
# File 'lib/dm-paperclip/interpolations.rb', line 144 def uuid , style_name .instance.uuid end |