Permalink
Browse files

Merge pull request #9 from grantovich/acg-fix-extensions

Fix breakage with file extensions not 3 or 4 chars
  • Loading branch information...
2 parents 4054cb6 + 30c02c2 commit 396717a7aaaab15224a3ef3d678188676d898c25 @janko-m committed Nov 21, 2012
@@ -57,10 +57,14 @@ def path(style)
end
def path_for_url(style)
- result = instance.instance_exec(style, &file_path)
- result += extension if result !~ /\.\w{3,4}$/
+ path = instance.instance_exec(style, &file_path)
style_suffix = (style != default_style ? "_#{style}" : "")
- result.sub(extension, "#{style_suffix}#{extension}")
+
+ if original_extension && path =~ /#{original_extension}$/
+ path.sub(original_extension, "#{style_suffix}#{original_extension}")
+ else
+ path + style_suffix + original_extension.to_s
+ end
end
def copy_to_local_file(style, destination_path)
@@ -71,8 +75,8 @@ def copy_to_local_file(style, destination_path)
private
- def extension
- original_filename[/\.\w{3,4}$/]
+ def original_extension
+ original_filename[/\.[^.]+$/]
end
def user_id
View
@@ -0,0 +1 @@
+This is a test file.
View
@@ -0,0 +1 @@
+// This is a test file.
@@ -0,0 +1 @@
+## This is a test file.
@@ -27,7 +27,7 @@ def change
Paperclip.options[:log] = false
end
- def uploaded_file(filename, content_type)
+ def uploaded_file(filename, content_type = "text/plain")
Rack::Test::UploadedFile.new("#{RSPEC_DIR}/files/#{filename}", content_type)
end
@@ -168,6 +168,58 @@ def set_options(options)
end
end
+ describe "#path_for_url" do
+ def set_options(options)
+ stub_const("User", Class.new(ActiveRecord::Base) do
+ has_attached_file :avatar,
+ storage: :dropbox,
+ dropbox_credentials: CREDENTIALS_FILE,
+ dropbox_options: options
+ end)
+ end
+
+ it "handles files with varying extension lengths" do
+ set_options({})
+ ["test_file", "test_file.c", "test_file.markdown"].each do |filename|
+ user = User.new(avatar: uploaded_file(filename))
+ user.avatar.path_for_url(user.avatar.default_style).should == filename
+ end
+ end
+
+ it "appends the style name, preserving the extension if one exists" do
+ set_options({})
+
+ user = User.new(avatar: uploaded_file("test_file.c"))
+ user.avatar.path_for_url(:dummy).should == "test_file_dummy.c"
+
+ user = User.new(avatar: uploaded_file("test_file"))
+ user.avatar.path_for_url(:dummy).should == "test_file_dummy"
+ end
+
+ it "uses the result of the path proc if one is provided" do
+ set_options(path: proc { |style| "avatars/#{style}/filename" })
+ user = User.new(avatar: uploaded_file("test_file"))
+ user.avatar.path_for_url(:dummy).should == "avatars/dummy/filename_dummy"
+ end
+
+ it "executes the path proc in the context of the model instance" do
+ set_options(path: proc { |style| "#{self.class.name}" })
+ user = User.new(avatar: uploaded_file("test_file"))
+ user.avatar.path_for_url(user.avatar.default_style).should == "User"
+ end
+
+ it "makes the path unique per model if unique_filename is set" do
+ set_options(unique_filename: true)
+ user1 = User.new(avatar: uploaded_file("test_file"))
+ user2 = User.new(avatar: uploaded_file("test_file"))
+ user1.id = 1
+ user2.id = 2
+
+ user1.avatar.path_for_url(:dummy).
+ should_not == user2.avatar.path_for_url(:dummy)
+ end
+ end
+
describe "CUD" do
before(:each) do
stub_const("User", Class.new(ActiveRecord::Base) do

0 comments on commit 396717a

Please sign in to comment.