Skip to content

Commit

Permalink
[bug] running validations callback so as to prevent post_processing o…
Browse files Browse the repository at this point in the history
…n invalid attachments (#16)
  • Loading branch information
sbhawsingka authored and ssinghi committed Dec 30, 2019
1 parent 8420ed0 commit ac18a07
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 6 deletions.
14 changes: 10 additions & 4 deletions lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -491,14 +491,20 @@ def process_options(options_type, style) #:nodoc:

def post_process(*style_args) #:nodoc:
return if @queued_for_write[:original].nil?

instance.run_paperclip_callbacks(:post_process) do
instance.run_paperclip_callbacks(:"#{name}_post_process") do
post_process_styles(*style_args) if !@options[:check_validity_before_processing] || instance.errors.none?
if !@options[:check_validity_before_processing] || check_validity?
instance.run_paperclip_callbacks(:post_process) do
instance.run_paperclip_callbacks(:"#{name}_post_process") do
post_process_styles(*style_args)
end
end
end
end

def check_validity?
instance.run_paperclip_callbacks(:"#{name}_validate")
instance.errors.none?
end

def post_process_styles(*style_args) #:nodoc:
if styles.include?(:original) && process_style?(:original, style_args)
post_process_style(:original, styles[:original])
Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/has_attached_file.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ def add_active_record_callbacks
def add_paperclip_callbacks
@klass.send(
:define_paperclip_callbacks,
:post_process, :"#{@name}_post_process"
:post_process, :"#{@name}_post_process", :"#{@name}_validate"
)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/paperclip/validators.rb
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,7 @@ def validate_before_processing(validator_class, options)
def create_validating_before_filter(attribute, validator_class, options)
if_clause = options.delete(:if)
unless_clause = options.delete(:unless)
send(:"before_#{attribute}_post_process", if: if_clause, unless: unless_clause) do |*_args|
send(:"before_#{attribute}_validate", if: if_clause, unless: unless_clause) do |*_args|
validator_class.new(options.dup).validate(self)
end
end
Expand Down
25 changes: 25 additions & 0 deletions spec/paperclip/attachment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -880,6 +880,31 @@ def do_after_all; end
expect(Paperclip::Thumbnail).not_to receive(:make)
@dummy.avatar = @file
end

it "should not call process hooks if validation fails" do
Dummy.class_eval do
validates_attachment_content_type :avatar, content_type: 'image/jpeg'
end
expect(@dummy).not_to receive(:do_before_avatar)
expect(@dummy).not_to receive(:do_after_avatar)
expect(@dummy).not_to receive(:do_before_all)
expect(@dummy).not_to receive(:do_after_all)
expect(Paperclip::Thumbnail).not_to receive(:make)
@dummy.avatar = @file
end

it "should call process hooks if validation would fail but check validity flag is false" do
Dummy.class_eval do
validates_attachment_content_type :avatar, content_type: 'image/jpeg'
end
@dummy.avatar.options[:check_validity_before_processing] = false
expect(@dummy).to receive(:do_before_avatar)
expect(@dummy).to receive(:do_after_avatar)
expect(@dummy).to receive(:do_before_all)
expect(@dummy).to receive(:do_after_all)
expect(Paperclip::Thumbnail).to receive(:make).and_return(@file)
@dummy.avatar = @file
end
end

context "Assigning an attachment" do
Expand Down

0 comments on commit ac18a07

Please sign in to comment.