Commit cd7030a7 by jyurek

Only nil and valid files clobber the existing attachment.

git-svn-id: https://svn.thoughtbot.com/plugins/paperclip/trunk@431 7bbfaf0e-4d1d-0410-9690-a8bb5f8ef2aa
parent afc36559
......@@ -3,6 +3,17 @@ module Paperclip
# deletes when the model is destroyed, and processes the file upon assignment.
class Attachment
def self.defaults
@defaults ||= {
:url => "/:attachment/:id/:style/:basename.:extension",
:path => ":rails_root/public/:attachment/:id/:style/:basename.:extension",
:styles => {},
:default_url => "/:attachment/:style/missing.png",
:default_style => :original,
:validations => []
}
end
attr_reader :name, :instance, :file, :styles, :default_style
# Creates an Attachment object. +name+ is the name of the attachment, +instance+ is the
......@@ -11,6 +22,9 @@ module Paperclip
def initialize name, instance, options
@name = name
@instance = instance
# options = options.merge(self.class.defaults)
@url = options[:url] ||
"/:attachment/:id/:style/:basename.:extension"
@path = options[:path] ||
......@@ -34,11 +48,13 @@ module Paperclip
# assigns attributes, processes the file, and runs validations. It also queues up
# the previous file for deletion, to be flushed away on #save of its host.
def assign uploaded_file
return nil unless valid_file?(uploaded_file) || uploaded_file.nil?
queue_existing_for_delete
@errors = []
@validation_errors = nil
return nil unless valid_file?(uploaded_file)
return nil if uploaded_file.nil?
@file = uploaded_file.to_tempfile
@instance[:"#{@name}_file_name"] = uploaded_file.original_filename
......
......@@ -5,6 +5,49 @@ class Dummy
end
class AttachmentTest < Test::Unit::TestCase
# context "Attachment defaults" do
# setup do
# @old_defaults = Paperclip::Attachment.defaults
# @new_defaults = @old_defaults.merge({
# :path => "argle/bargle",
# :url => "fooferon",
# :default_url => "not here.png"
# })
# end
#
# should "be overrideable" do
# Paperclip::Attachment.defaults.merge!(@new_defaults)
# @new_defaults.keys.each do |key|
# assert_equal @new_defaults[key], Paperclip::Attachment.defaults[key]
# end
# end
#
# context "on an Attachment" do
# setup do
# @attachment = Dummy.new
# end
#
# should "be the defaults" do
# @old_defaults.keys.each do |key|
# assert_equal @old_defaults[key], @attachment.instance_variable_get("@#{key}"), key
# end
# end
#
# context "when redefined" do
# setup do
# Paperclip::Attachment.defaults.merge!(@new_defaults)
# @attachment = Dummy.new
# end
#
# should "be the new defaults" do
# @new_defaults.keys.each do |key|
# assert_equal @new_defaults[key], @attachment.instance_variable_get("@#{key}"), key
# end
# end
# end
# end
# end
context "An attachment" do
setup do
@default_options = {
......
......@@ -38,6 +38,15 @@ class PaperclipTest < Test::Unit::TestCase
assert_equal "100x15", `identify -format "%wx%h" #{@d2.avatar.to_io(:medium).path}`.chomp
assert_equal "32x32", `identify -format "%wx%h" #{@d2.avatar.to_io(:thumb).path}`.chomp
@dummy.avatar = "not a valid file but not nil"
assert_equal File.basename(@file.path), @dummy.avatar_file_name
assert @dummy.valid?
assert @dummy.save
saved_paths.each do |p|
assert File.exists?(p)
end
@dummy.avatar = nil
assert_nil @dummy.avatar_file_name
assert @dummy.valid?
......
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