Skip to content
This repository has been archived by the owner on Jul 13, 2023. It is now read-only.

Commit

Permalink
Removed validation code left over from switching to more AR-ish valid…
Browse files Browse the repository at this point in the history
…ations
  • Loading branch information
Jon Yurek committed Dec 31, 2009
1 parent 91b30cd commit 51637fe
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 107 deletions.
2 changes: 1 addition & 1 deletion lib/paperclip.rb
Expand Up @@ -240,7 +240,7 @@ def has_attached_file name, options = {}

validates_each(name) do |record, attr, value|
attachment = record.attachment_for(name)
attachment.send(:flush_errors) unless attachment.valid?
attachment.send(:flush_errors)
end
end

Expand Down
77 changes: 6 additions & 71 deletions lib/paperclip/attachment.rb
Expand Up @@ -14,7 +14,6 @@ def self.default_options
:convert_options => {},
:default_url => "/:attachment/:style/missing.png",
:default_style => :original,
:validations => [],
:storage => :filesystem,
:whiny => Paperclip.options[:whiny] || Paperclip.options[:whiny_thumbnails]
}
Expand All @@ -38,7 +37,6 @@ def initialize name, instance, options = {}
@styles = options[:styles]
@normalized_styles = nil
@default_url = options[:default_url]
@validations = options[:validations]
@default_style = options[:default_style]
@storage = options[:storage]
@whiny = options[:whiny_thumbnails] || options[:whiny]
Expand All @@ -48,7 +46,6 @@ def initialize name, instance, options = {}
@queued_for_delete = []
@queued_for_write = {}
@errors = {}
@validation_errors = nil
@dirty = false

initialize_storage
Expand All @@ -69,13 +66,11 @@ def processors
end

# 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
# #save of its host. In addition to form uploads, you can also assign
# another Paperclip attachment:
# 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
ensure_required_accessors!

Expand All @@ -99,13 +94,12 @@ def assign uploaded_file

@dirty = true

post_process if valid?
post_process

# Reset the file size if the original file was reprocessed.
instance_write(:file_size, @queued_for_write[:original].size.to_i)
ensure
uploaded_file.close if close_uploaded_file
validate
end

# Returns the public URL of the attachment, with a given style. Note that
Expand Down Expand Up @@ -133,12 +127,6 @@ def to_s style_name = nil
url(style_name)
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.
def errors
@errors
Expand All @@ -152,15 +140,10 @@ def dirty?
# 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.
def save
if valid?
flush_deletes
flush_writes
@dirty = false
true
else
flush_errors
false
end
flush_deletes
flush_writes
@dirty = false
true
end

# Clears out the attachment. Has the same effect as previously assigning
Expand All @@ -169,7 +152,6 @@ def save
def clear
queue_existing_for_delete
@errors = {}
@validation_errors = nil
end

# Destroys the attachment. Has the same effect as previously assigning
Expand Down Expand Up @@ -282,53 +264,6 @@ def valid_assignment? file #:nodoc:
file.nil? || (file.respond_to?(:original_filename) && file.respond_to?(:content_type))
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:
@storage_module = Paperclip::Storage.const_get(@storage.to_s.capitalize)
self.extend(@storage_module)
Expand Down
14 changes: 0 additions & 14 deletions test/attachment_test.rb
Expand Up @@ -14,18 +14,6 @@ class AttachmentTest < Test::Unit::TestCase
assert_equal "#{RAILS_ROOT}/public/fake_models/1234/fake", @attachment.path
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
setup do
rebuild_model
Expand Down Expand Up @@ -474,8 +462,6 @@ def do_after_all; end
@attachment.expects(:valid_assignment?).with(@not_file).returns(true)
@attachment.expects(:queue_existing_for_delete)
@attachment.expects(:post_process)
@attachment.expects(:valid?).returns(true)
@attachment.expects(:validate)
@dummy.avatar = @not_file
end

Expand Down
21 changes: 0 additions & 21 deletions test/paperclip_test.rb
Expand Up @@ -185,27 +185,6 @@ class ::SubDummy < Dummy; end
should "be valid" do
assert @dummy.valid?
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

context "a validation with an if guard clause" do
Expand Down

1 comment on commit 51637fe

@sikachu
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So it seems like rake paperclip:clean breaks since this commit, as we're calling attachment.validate inside the rake task. Fixing this and also adding an integration test to cover it.

Please sign in to comment.