Commit 51637fe9 by Jon Yurek

Removed validation code left over from switching to more AR-ish validations

parent 91b30cd2
...@@ -240,7 +240,7 @@ module Paperclip ...@@ -240,7 +240,7 @@ module Paperclip
validates_each(name) do |record, attr, value| validates_each(name) do |record, attr, value|
attachment = record.attachment_for(name) attachment = record.attachment_for(name)
attachment.send(:flush_errors) unless attachment.valid? attachment.send(:flush_errors)
end end
end end
......
...@@ -14,7 +14,6 @@ module Paperclip ...@@ -14,7 +14,6 @@ module Paperclip
:convert_options => {}, :convert_options => {},
:default_url => "/:attachment/:style/missing.png", :default_url => "/:attachment/:style/missing.png",
:default_style => :original, :default_style => :original,
:validations => [],
:storage => :filesystem, :storage => :filesystem,
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails] :whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
} }
...@@ -38,7 +37,6 @@ module Paperclip ...@@ -38,7 +37,6 @@ module Paperclip
@styles = options[:styles] @styles = options[:styles]
@normalized_styles = nil @normalized_styles = nil
@default_url = options[:default_url] @default_url = options[:default_url]
@validations = options[:validations]
@default_style = options[:default_style] @default_style = options[:default_style]
@storage = options[:storage] @storage = options[:storage]
@whiny = options[:whiny_thumbnails] || options[:whiny] @whiny = options[:whiny_thumbnails] || options[:whiny]
...@@ -48,7 +46,6 @@ module Paperclip ...@@ -48,7 +46,6 @@ module Paperclip
@queued_for_delete = [] @queued_for_delete = []
@queued_for_write = {} @queued_for_write = {}
@errors = {} @errors = {}
@validation_errors = nil
@dirty = false @dirty = false
initialize_storage initialize_storage
...@@ -69,13 +66,11 @@ module Paperclip ...@@ -69,13 +66,11 @@ module Paperclip
end end
# What gets called when you call instance.attachment = File. It clears # What gets called when you call instance.attachment = File. It clears
# errors, assigns attributes, processes the file, and runs validations. It # errors, assigns attributes, and processes the file. It
# also queues up the previous file for deletion, to be flushed away on # also queues up the previous file for deletion, to be flushed away on
# #save of its host. In addition to form uploads, you can also assign # #save of its host. In addition to form uploads, you can also assign
# another Paperclip attachment: # another Paperclip attachment:
# new_user.avatar = old_user.avatar # new_user.avatar = old_user.avatar
# If the file that is assigned is not valid, the processing (i.e.
# thumbnailing, etc) will NOT be run.
def assign uploaded_file def assign uploaded_file
ensure_required_accessors! ensure_required_accessors!
...@@ -99,13 +94,12 @@ module Paperclip ...@@ -99,13 +94,12 @@ module Paperclip
@dirty = true @dirty = true
post_process if valid? post_process
# Reset the file size if the original file was reprocessed. # Reset the file size if the original file was reprocessed.
instance_write(:file_size, @queued_for_write[:original].size.to_i) instance_write(:file_size, @queued_for_write[:original].size.to_i)
ensure ensure
uploaded_file.close if close_uploaded_file uploaded_file.close if close_uploaded_file
validate
end end
# Returns the public URL of the attachment, with a given style. Note that # Returns the public URL of the attachment, with a given style. Note that
...@@ -133,12 +127,6 @@ module Paperclip ...@@ -133,12 +127,6 @@ module Paperclip
url(style_name) url(style_name)
end end
# Returns true if there are no errors on this attachment.
def valid?
validate
errors.empty?
end
# Returns an array containing the errors on this attachment. # Returns an array containing the errors on this attachment.
def errors def errors
@errors @errors
...@@ -152,15 +140,10 @@ module Paperclip ...@@ -152,15 +140,10 @@ module Paperclip
# Saves the file, if there are no errors. If there are, it flushes them to # Saves the file, if there are no errors. If there are, it flushes them to
# the instance's errors and returns false, cancelling the save. # the instance's errors and returns false, cancelling the save.
def save def save
if valid? flush_deletes
flush_deletes flush_writes
flush_writes @dirty = false
@dirty = false true
true
else
flush_errors
false
end
end end
# Clears out the attachment. Has the same effect as previously assigning # Clears out the attachment. Has the same effect as previously assigning
...@@ -169,7 +152,6 @@ module Paperclip ...@@ -169,7 +152,6 @@ module Paperclip
def clear def clear
queue_existing_for_delete queue_existing_for_delete
@errors = {} @errors = {}
@validation_errors = nil
end end
# Destroys the attachment. Has the same effect as previously assigning # Destroys the attachment. Has the same effect as previously assigning
...@@ -282,53 +264,6 @@ module Paperclip ...@@ -282,53 +264,6 @@ module Paperclip
file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type)) file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type))
end end
def validate #:nodoc:
unless @validation_errors
@validation_errors = @validations.inject({}) do |errors, validation|
name, options = validation
errors[name] = send(:"validate_#{name}", options) if allow_validation?(options)
errors
end
@validation_errors.reject!{|k,v| v == nil }
@errors.merge!(@validation_errors)
end
@validation_errors
end
def allow_validation? options #:nodoc:
(options[:if].nil? || check_guard(options[:if])) && (options[:unless].nil? || !check_guard(options[:unless]))
end
def check_guard guard #:nodoc:
if guard.respond_to? :call
guard.call(instance)
elsif ! guard.blank?
instance.send(guard.to_s)
end
end
def validate_size options #:nodoc:
if file? && !options[:range].include?(size.to_i)
options[:message].gsub(/:min/, options[:min].to_s).gsub(/:max/, options[:max].to_s)
end
end
def validate_presence options #:nodoc:
options[:message] unless file?
end
def validate_content_type options #:nodoc:
valid_types = [options[:content_type]].flatten
unless original_filename.blank?
unless valid_types.blank?
content_type = instance_read(:content_type)
unless valid_types.any?{|t| content_type.nil? || t === content_type }
options[:message] || "is not one of the allowed file types."
end
end
end
end
def initialize_storage #:nodoc: def initialize_storage #:nodoc:
@storage_module = Paperclip::Storage.const_get(@storage.to_s.capitalize) @storage_module = Paperclip::Storage.const_get(@storage.to_s.capitalize)
self.extend(@storage_module) self.extend(@storage_module)
......
...@@ -14,18 +14,6 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -14,18 +14,6 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "#{RAILS_ROOT}/public/fake_models/1234/fake", @attachment.path assert_equal "#{RAILS_ROOT}/public/fake_models/1234/fake", @attachment.path
end end
should "call a proc sent to check_guard" do
@dummy = Dummy.new
@dummy.expects(:one).returns(:one)
assert_equal :one, @dummy.avatar.send(:check_guard, lambda{|x| x.one })
end
should "call a method name sent to check_guard" do
@dummy = Dummy.new
@dummy.expects(:one).returns(:one)
assert_equal :one, @dummy.avatar.send(:check_guard, :one)
end
context "Attachment default_options" do context "Attachment default_options" do
setup do setup do
rebuild_model rebuild_model
...@@ -474,8 +462,6 @@ class AttachmentTest < Test::Unit::TestCase ...@@ -474,8 +462,6 @@ class AttachmentTest < Test::Unit::TestCase
@attachment.expects(:valid_assignment?).with(@not_file).returns(true) @attachment.expects(:valid_assignment?).with(@not_file).returns(true)
@attachment.expects(:queue_existing_for_delete) @attachment.expects(:queue_existing_for_delete)
@attachment.expects(:post_process) @attachment.expects(:post_process)
@attachment.expects(:valid?).returns(true)
@attachment.expects(:validate)
@dummy.avatar = @not_file @dummy.avatar = @not_file
end end
......
...@@ -185,27 +185,6 @@ class PaperclipTest < Test::Unit::TestCase ...@@ -185,27 +185,6 @@ class PaperclipTest < Test::Unit::TestCase
should "be valid" do should "be valid" do
assert @dummy.valid? assert @dummy.valid?
end end
context "then has a validation added that makes it invalid" do
setup do
assert @dummy.save
Dummy.class_eval do
validates_attachment_content_type :avatar, :content_type => ["text/plain"]
end
@dummy2 = Dummy.find(@dummy.id)
end
should "be invalid when reloaded" do
assert ! @dummy2.valid?, @dummy2.errors.inspect
end
should "be able to call #valid? twice without having duplicate errors" do
@dummy2.avatar.valid?
first_errors = @dummy2.avatar.errors
@dummy2.avatar.valid?
assert_equal first_errors, @dummy2.avatar.errors
end
end
end end
context "a validation with an if guard clause" do context "a validation with an if guard clause" do
......
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