From 8d262e30572534c54aad71ad73ffa83ca4e8d9f9 Mon Sep 17 00:00:00 2001 From: Jon Yurek Date: Sun, 26 Jun 2011 21:53:11 -0400 Subject: [PATCH] Add a #to_param interpolation --- lib/paperclip/interpolations.rb | 5 +++++ test/integration_test.rb | 20 ++++++++++++++++---- test/interpolations_test.rb | 7 +++++++ 3 files changed, 28 insertions(+), 4 deletions(-) diff --git a/lib/paperclip/interpolations.rb b/lib/paperclip/interpolations.rb index 863fb3598..e189432d4 100644 --- a/lib/paperclip/interpolations.rb +++ b/lib/paperclip/interpolations.rb @@ -99,6 +99,11 @@ def id attachment, style_name attachment.instance.id end + # Returns the #to_param of the instance. + def param attachment, style_name + attachment.instance.to_param + end + # Returns the fingerprint of the instance. def fingerprint attachment, style_name attachment.fingerprint diff --git a/test/integration_test.rb b/test/integration_test.rb index 308260a7f..e940f5159 100644 --- a/test/integration_test.rb +++ b/test/integration_test.rb @@ -34,20 +34,20 @@ class IntegrationTest < Test::Unit::TestCase should "create its thumbnails properly" do assert_match /\b50x50\b/, `identify "#{@dummy.avatar.path(:thumb)}"` end - + context 'reprocessing with unreadable original' do setup { File.chmod(0000, @dummy.avatar.path) } - + should "not raise an error" do assert_nothing_raised do @dummy.avatar.reprocess! end end - + should "return false" do assert ! @dummy.avatar.reprocess! end - + teardown { File.chmod(0644, @dummy.avatar.path) } end @@ -467,6 +467,18 @@ def s3_headers_for attachment, style @files_on_s3 = s3_files_for @dummy.avatar end + context 'assigning itself to a new model' do + setup do + @d2 = Dummy.new + @d2.avatar = @dummy.avatar + @d2.save + end + + should "have the same name as the old file" do + assert_equal @d2.avatar.original_filename, @dummy.avatar.original_filename + end + end + should "have the same contents as the original" do @file.rewind assert_equal @file.read, @files_on_s3[:original].read diff --git a/test/interpolations_test.rb b/test/interpolations_test.rb index 0731c3a05..89c079510 100644 --- a/test/interpolations_test.rb +++ b/test/interpolations_test.rb @@ -50,6 +50,13 @@ class InterpolationsTest < Test::Unit::TestCase assert_equal "png", Paperclip::Interpolations.extension(attachment, :style) end + should "return the #to_param of the attachment" do + attachment = mock + attachment.expects(:to_param).returns("23-awesome") + attachment.expects(:instance).returns(attachment) + assert_equal "23-awesome", Paperclip::Interpolations.param(attachment, :style) + end + should "return the id of the attachment" do attachment = mock attachment.expects(:id).returns(23)