Commit bcba1518 by jyurek

Added some tests to check for no record id and no file_name.

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@174 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent 4aa096ed
...@@ -34,7 +34,7 @@ module Thoughtbot #:nodoc: ...@@ -34,7 +34,7 @@ module Thoughtbot #:nodoc:
DEFAULT_ATTACHMENT_OPTIONS = { DEFAULT_ATTACHMENT_OPTIONS = {
:path_prefix => ":rails_root/public", :path_prefix => ":rails_root/public",
:url_prefix => "", :url_prefix => "",
:path => ":class/:id/:style_:name", :path => ":attachment/:id/:style_:name",
:attachment_type => :image, :attachment_type => :image,
:thumbnails => {}, :thumbnails => {},
:delete_on_destroy => true :delete_on_destroy => true
...@@ -215,13 +215,16 @@ module Thoughtbot #:nodoc: ...@@ -215,13 +215,16 @@ module Thoughtbot #:nodoc:
module InstanceMethods #:nodoc: module InstanceMethods #:nodoc:
private private
def interpolate attachment, prefix_type, style def interpolate attachment, prefix_type, style
file_name = read_attribute("#{attachment[:name]}_file_name")
return "" unless file_name && self.id
returning "#{attachment[prefix_type]}/#{attachment[:path]}" do |prefix| returning "#{attachment[prefix_type]}/#{attachment[:path]}" do |prefix|
prefix.gsub!(/:rails_root/, RAILS_ROOT) prefix.gsub!(/:rails_root/, RAILS_ROOT)
prefix.gsub!(/:id/, self.id.to_s) if self.id prefix.gsub!(/:id/, self.id.to_s) if self.id
prefix.gsub!(/:class/, self.class.to_s.underscore.pluralize) prefix.gsub!(/:class/, self.class.to_s.underscore.pluralize)
prefix.gsub!(/:style/, style.to_s) prefix.gsub!(/:style/, style.to_s)
prefix.gsub!(/:attachment/, attachment[:name].to_s.pluralize) prefix.gsub!(/:attachment/, attachment[:name].to_s.pluralize)
prefix.gsub!(/:name/, attachment[:filename]) prefix.gsub!(/:name/, file_name)
end end
end end
...@@ -230,7 +233,7 @@ module Thoughtbot #:nodoc: ...@@ -230,7 +233,7 @@ module Thoughtbot #:nodoc:
return nil unless file return nil unless file
prefix = interpolate attachment, :path_prefix, style prefix = interpolate attachment, :path_prefix, style
File.join( prefix.split("/").reject(&:blank?) ) File.join( prefix.split("/") )
end end
def url_for attachment, style = :original def url_for attachment, style = :original
......
...@@ -34,6 +34,21 @@ class PaperclipImagesTest < Test::Unit::TestCase ...@@ -34,6 +34,21 @@ class PaperclipImagesTest < Test::Unit::TestCase
assert @foo.valid? assert @foo.valid?
end end
def test_should_ensure_that_file_are_accessible_after_reload
assert @foo.save
assert @foo.image_valid?
assert @foo.valid?
@foo2 = Foo.find @foo.id
assert @foo.image_valid?
assert File.exists?( @foo.image_file_name(:original) ), @foo.image_file_name(:original)
assert File.exists?( @foo.image_file_name(:medium) ), @foo.image_file_name(:medium)
assert File.exists?( @foo.image_file_name(:thumb) ), @foo.image_file_name(:thumb)
out = `identify '#{@foo.image_file_name(:original)}'`; assert out.match("405x375"); assert $?.exitstatus == 0
out = `identify '#{@foo.image_file_name(:medium)}'`; assert out.match("300x278"); assert $?.exitstatus == 0
out = `identify '#{@foo.image_file_name(:thumb)}'`; assert out.match("100x93"); assert $?.exitstatus == 0
end
def test_should_delete_all_thumbnails_on_destroy def test_should_delete_all_thumbnails_on_destroy
assert @foo.save assert @foo.save
names = [:original, :medium, :thumb].map{|style| @foo.image_file_name(style) } names = [:original, :medium, :thumb].map{|style| @foo.image_file_name(style) }
...@@ -41,4 +56,36 @@ class PaperclipImagesTest < Test::Unit::TestCase ...@@ -41,4 +56,36 @@ class PaperclipImagesTest < Test::Unit::TestCase
names.each {|path| assert !File.exists?( path ), path } names.each {|path| assert !File.exists?( path ), path }
end end
def test_should_ensure_file_names_and_urls_are_empty_if_no_file_set
assert @foo.save
assert @foo.image_valid?
mappings = [:original, :medium, :thumb].map do |style|
assert @foo.image_file_name(style)
assert @foo.image_url(style)
[style, @foo.image_file_name(style), @foo.image_url(style)]
end
assert @foo.destroy_image
mappings.each do |style, file, url|
assert_not_equal file, @foo.image_file_name(style)
assert_equal "", @foo.image_file_name(style)
assert_not_equal url, @foo.image_url(style)
assert_equal "", @foo.image_url(style)
end
assert @foo2 = Foo.find(@foo.id)
mappings.each do |style, file, url|
assert_not_equal file, @foo2.image_file_name(style)
assert_equal "", @foo2.image_file_name(style)
assert_not_equal url, @foo2.image_url(style)
assert_equal "", @foo2.image_url(style)
end
assert @foo3 = Foo.new
mappings.each do |style, file, url|
assert_equal "", @foo3.image_file_name(style), @foo3["image_file_name"]
assert_equal "", @foo3.image_url(style)
end
end
end end
\ No newline at end of file
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