Commit 35b912ac by Michał Szajbe Committed by Jon Yurek

Added support for :name and :url options being defined as a Proc.

Possible application:
Providing backward compatibility for existing attachments when changing naming conventions for new attachments.
parent 044f00ec
......@@ -28,7 +28,9 @@ module Paperclip
options = self.class.default_options.merge(options)
@url = options[:url]
@url = @url.call(self) if @url.is_a?(Proc)
@path = options[:path]
@path = @path.call(self) if @path.is_a?(Proc)
@styles = options[:styles]
@default_url = options[:default_url]
@validations = options[:validations]
......
......@@ -196,6 +196,48 @@ class AttachmentTest < Test::Unit::TestCase
end
end
context "An attachment with :path that is a proc" do
setup do
rebuild_model :path => lambda{ |attachment| "path/#{attachment.instance.other}.:extension" }
@file = File.new(File.join(File.dirname(__FILE__),
"fixtures",
"5k.png"), 'rb')
@dummyA = Dummy.new(:other => 'a')
@dummyA.avatar = @file
@dummyB = Dummy.new(:other => 'b')
@dummyB.avatar = @file
end
teardown { @file.close }
should "return correct path" do
assert_equal "path/a.png", @dummyA.avatar.path
assert_equal "path/b.png", @dummyB.avatar.path
end
end
context "An attachment with :url that is a proc" do
setup do
rebuild_model :url => lambda{ |attachment| "path/#{attachment.instance.other}.:extension" }
@file = File.new(File.join(File.dirname(__FILE__),
"fixtures",
"5k.png"), 'rb')
@dummyA = Dummy.new(:other => 'a')
@dummyA.avatar = @file
@dummyB = Dummy.new(:other => 'b')
@dummyB.avatar = @file
end
teardown { @file.close }
should "return correct url" do
assert_equal "path/a.png", @dummyA.avatar.url(:original, false)
assert_equal "path/b.png", @dummyB.avatar.url(:original, false)
end
end
geometry_specs = [
[ lambda{|z| "50x50#" }, :png ],
lambda{|z| "50x50#" },
......
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