Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Correctly set style when interpolating :hash

  • Loading branch information...
commit e4a387416e899b012160fd7349a90121583b11f6 1 parent 6a10b35
@jmileham authored
View
4 lib/paperclip/attachment.rb
@@ -212,10 +212,10 @@ def time_zone
# Returns a unique hash suitable for obfuscating the URL of an otherwise
# publicly viewable attachment.
- def hash
+ def hash(style_name = default_style)
raise ArgumentError, "Unable to generate hash without :hash_secret" unless @hash_secret
require 'openssl' unless defined?(OpenSSL)
- data = interpolate(@hash_data)
+ data = interpolate(@hash_data, style_name)
OpenSSL::HMAC.hexdigest(OpenSSL::Digest.const_get(@hash_digest).new, @hash_secret, data)
end
View
4 lib/paperclip/interpolations.rb
@@ -49,7 +49,7 @@ def url attachment, style_name
# 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
+ # 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.
def timestamp attachment, style_name
@@ -107,7 +107,7 @@ def fingerprint attachment, style_name
# Returns a the attachment hash. See Paperclip::Attachment#hash for
# more details.
def hash attachment, style_name
- attachment.hash
+ attachment.hash(style_name)
end
# Returns the id of the instance in a split path form. e.g. returns
View
27 test/attachment_test.rb
@@ -96,7 +96,7 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "1024.omg/1024-bbq/1024what/000/001/024.wtf", @dummy.avatar.path
end
end
-
+
context "An attachment with :timestamp interpolations" do
setup do
@file = StringIO.new("...")
@@ -117,7 +117,7 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal @dummy.avatar_updated_at.in_time_zone(@zone_default).to_s, @dummy.avatar.path
end
end
-
+
context "using per-thread time zone" do
setup do
rebuild_model :path => ":timestamp", :use_default_time_zone => false
@@ -130,12 +130,12 @@ class AttachmentTest < Test::Unit::TestCase
end
end
end
-
+
context "An attachment with :hash interpolations" do
setup do
@file = StringIO.new("...")
end
-
+
should "raise if no secret is provided" do
@attachment = attachment :path => ":hash"
@attachment.assign @file
@@ -153,19 +153,24 @@ class AttachmentTest < Test::Unit::TestCase
@attachment.instance.id = 1234
@attachment.assign @file
end
-
+
should "interpolate the hash data" do
- @attachment.expects(:interpolate).with(@attachment.options[:hash_data]).returns("interpolated_stuff")
+ @attachment.expects(:interpolate).with(@attachment.options[:hash_data],anything).returns("interpolated_stuff")
@attachment.hash
end
-
+
should "result in the correct interpolation" do
assert_equal "fake_models/avatars/1234/original/1234567890", @attachment.send(:interpolate,@attachment.options[:hash_data])
end
-
+
should "result in a correct hash" do
assert_equal "d22b617d1bf10016aa7d046d16427ae203f39fce", @attachment.path
end
+
+ should "generate a hash digest with the correct style" do
+ OpenSSL::HMAC.expects(:hexdigest).with(anything, anything, "fake_models/avatars/1234/medium/1234567890")
+ @attachment.path("medium")
+ end
end
end
@@ -558,7 +563,7 @@ def do_after_all; end
assert_equal "sheep_say_bæ.png", @dummy.avatar.original_filename
end
end
-
+
context "Attachment with uppercase extension and a default style" do
setup do
@old_defaults = Paperclip::Attachment.default_options.dup
@@ -571,7 +576,7 @@ def do_after_all; end
@instance.stubs(:id).returns 123
@file = File.new(File.join(File.dirname(__FILE__), "fixtures", "uppercase.PNG"), 'rb')
-
+
styles = {:styles => { :large => ["400x400", :jpg],
:medium => ["100x100", :jpg],
:small => ["32x32#", :jpg]},
@@ -584,7 +589,7 @@ def do_after_all; end
@attachment.assign(@file)
@attachment.save
end
-
+
teardown do
@file.close
Paperclip::Attachment.default_options.merge!(@old_defaults)
Please sign in to comment.
Something went wrong with that request. Please try again.