Commit 3eb7844c by Jon Yurek

Added :url and :timestamp interpolations.

parent 7661c317
......@@ -139,6 +139,9 @@ module Paperclip
class NotIdentifiedByImageMagickError < PaperclipError #:nodoc:
end
class InfiniteInterpolationError < PaperclipError #:nodoc:
end
module ClassMethods
# +has_attached_file+ gives the class it is called on an attribute that maps to a file. This
# is typically a file stored somewhere on the filesystem and has been uploaded by a user.
......
......@@ -22,6 +22,15 @@ module Paperclip
end
end
def url attachment, style
raise InfiniteInterpolationError if attachment.options[:url].include?(":url")
attachment.url(style)
end
def timestamp attachment, style
attachment.instance_read(:updated_at).to_s
end
def rails_root attachment, style
RAILS_ROOT
end
......
......@@ -76,6 +76,26 @@ class InterpolationsTest < Test::Unit::TestCase
assert_equal :default_style, Paperclip::Interpolations.style(attachment, nil)
end
should "reinterpolate :url" do
attachment = mock
attachment.expects(:options).returns({:url => ":id"})
attachment.expects(:url).with(:style).returns("1234")
assert_equal "1234", Paperclip::Interpolations.url(attachment, :style)
end
should "raise if infinite loop detcted reinterpolatin :url" do
attachment = mock
attachment.expects(:options).returns({:url => ":url"})
assert_raises(Paperclip::InfiniteInterpolationError){ Paperclip::Interpolations.url(attachment, :style) }
end
should "return the timestamp" do
now = Time.now
attachment = mock
attachment.expects(:instance_read).with(:updated_at).returns(now)
assert_equal now.to_s, Paperclip::Interpolations.timestamp(attachment, :style)
end
should "call all expected interpolations with the given arguments" do
Paperclip::Interpolations.expects(:id).with(:attachment, :style).returns(1234)
Paperclip::Interpolations.expects(:attachment).with(:attachment, :style).returns("attachments")
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment