Module: Paperclip::Interpolations
- Extended by:
- Interpolations
- Included in:
- Interpolations
- Defined in:
- lib/paperclip/interpolations.rb,
lib/paperclip/interpolations/plural_cache.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.
Defined Under Namespace
Classes: PluralCache
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{\A\./}, "")}:#{__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.
- .plural_cache ⇒ Object
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.
-
#content_type_extension(attachment, style_name) ⇒ Object
Returns an extension based on the content type.
-
#dotextension(attachment, style_name) ⇒ Object
Returns the dot+extension of the file.
-
#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 = nil, style_name = nil) ⇒ 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.
-
#param(attachment, style_name) ⇒ Object
Returns the #to_param of the instance.
-
#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 in the server default time zone unless :use_global_time_zone is set to false.
-
#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
Class Method Details
.[](name) ⇒ Object
Hash access of interpolations. Included only for compatibility, 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 compatibility, 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 |
.all ⇒ Object
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. You can pass a method name on your record as a symbol, which should turn an interpolation pattern for Paperclip to use.
30 31 32 33 34 35 36 37 |
# File 'lib/paperclip/interpolations.rb', line 30 def self.interpolate pattern, *args pattern = args.first.instance.send(pattern) if pattern.kind_of? Symbol all.reverse.inject(pattern) do |result, tag| result.gsub(/:#{tag}/) do |match| send( tag, *args ) end end end |
.plural_cache ⇒ Object
39 40 41 |
# File 'lib/paperclip/interpolations.rb', line 39 def self.plural_cache @plural_cache ||= PluralCache.new 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
183 184 185 |
# File 'lib/paperclip/interpolations.rb', line 183 def , style_name plural_cache.pluralize(.name.to_s.downcase) end |
#basename(attachment, style_name) ⇒ Object
Returns the basename of the file. e.g. “file” for “file.jpg”
92 93 94 |
# File 'lib/paperclip/interpolations.rb', line 92 def basename , style_name .original_filename.gsub(/#{Regexp.escape(File.extname(.original_filename))}\Z/, "") 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.
86 87 88 89 |
# File 'lib/paperclip/interpolations.rb', line 86 def class = nil, style_name = nil return super() if .nil? && style_name.nil? plural_cache.underscore_and_pluralize(.instance.class.to_s) end |
#content_type_extension(attachment, style_name) ⇒ Object
Returns an extension based on the content type. e.g. “jpeg” for “image/jpeg”. If the style has a specified format, it will override the content-type detection.
Each mime type generally has multiple extensions associated with it, so if the extension from the original filename is one of these extensions, that extension is used, otherwise, the first in the list is used.
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/paperclip/interpolations.rb', line 119 def content_type_extension , style_name mime_type = MIME::Types[.content_type] extensions_for_mime_type = unless mime_type.empty? mime_type.first.extensions else [] end original_extension = extension(, style_name) style = .styles[style_name.to_s.to_sym] if style && style[:format] style[:format].to_s elsif extensions_for_mime_type.include? original_extension original_extension elsif !extensions_for_mime_type.empty? extensions_for_mime_type.first else # It's possible, though unlikely, that the mime type is not in the # database, so just use the part after the '/' in the mime type as the # extension. %r{/([^/]*)\Z}.match(.content_type)[1] end end |
#dotextension(attachment, style_name) ⇒ Object
Returns the dot+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. If the extension is empty, no dot is added.
107 108 109 110 |
# File 'lib/paperclip/interpolations.rb', line 107 def dotextension , style_name ext = extension(, style_name) ext.empty? ? "" : ".#{ext}" 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.
99 100 101 102 |
# File 'lib/paperclip/interpolations.rb', line 99 def extension , style_name ((style = .styles[style_name.to_s.to_sym]) && style[:format]) || File.extname(.original_filename).gsub(/\A\.+/, "") end |
#filename(attachment, style_name) ⇒ Object
Returns the filename, the same way as “:basename.:extension” would.
44 45 46 |
# File 'lib/paperclip/interpolations.rb', line 44 def filename , style_name [ basename(, style_name), extension(, style_name) ].reject(&:blank?).join(".") end |
#fingerprint(attachment, style_name) ⇒ Object
Returns the fingerprint of the instance.
154 155 156 |
# File 'lib/paperclip/interpolations.rb', line 154 def fingerprint , style_name .fingerprint end |
#hash(attachment = nil, style_name = nil) ⇒ Object
Returns a the attachment hash. See Paperclip::Attachment#hash_key for more details.
160 161 162 163 164 165 166 |
# File 'lib/paperclip/interpolations.rb', line 160 def hash =nil, style_name=nil if && style_name .hash_key(style_name) else super() end end |
#id(attachment, style_name) ⇒ Object
Returns the id of the instance.
144 145 146 |
# File 'lib/paperclip/interpolations.rb', line 144 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.
170 171 172 173 174 175 176 177 178 179 |
# File 'lib/paperclip/interpolations.rb', line 170 def id_partition , style_name case id = .instance.id when Integer ("%09d" % id).scan(/\d{3}/).join("/") when String ('%9.9s' % id).tr(" ", "0").scan(/.{3}/).join("/") else nil end end |
#param(attachment, style_name) ⇒ Object
Returns the #to_param of the instance.
149 150 151 |
# File 'lib/paperclip/interpolations.rb', line 149 def param , style_name .instance.to_param end |
#rails_env(attachment, style_name) ⇒ Object
Returns the Rails.env constant.
78 79 80 |
# File 'lib/paperclip/interpolations.rb', line 78 def rails_env , style_name Rails.env end |
#rails_root(attachment, style_name) ⇒ Object
Returns the Rails.root constant.
73 74 75 |
# File 'lib/paperclip/interpolations.rb', line 73 def rails_root , style_name Rails.root end |
#style(attachment, style_name) ⇒ Object
Returns the style, or the default style if nil is supplied.
188 189 190 |
# File 'lib/paperclip/interpolations.rb', line 188 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 in the server default time zone unless :use_global_time_zone is set to false. Note that a Rails.config.time_zone change will still invalidate any path or URL that uses :timestamp. For a time_zone-agnostic timestamp, use #updated_at.
62 63 64 |
# File 'lib/paperclip/interpolations.rb', line 62 def , style_name .instance_read(:updated_at).in_time_zone(.time_zone).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.
68 69 70 |
# File 'lib/paperclip/interpolations.rb', line 68 def updated_at , style_name .updated_at end |
#url(attachment, style_name) ⇒ Object
52 53 54 55 |
# File 'lib/paperclip/interpolations.rb', line 52 def url , style_name raise Errors::InfiniteInterpolationError if caller.any?{|b| b.index(RIGHT_HERE) } .url(style_name, :timestamp => false, :escape => false) end |