Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bug] running validations callback so as to prevent post_processing on invalid attachments #16

Merged
merged 3 commits into from
Dec 30, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
[bug] executing the validation callback before post_process is done s…
…o that if validations fail, post_process won't be called
sbhawsingka committed Dec 26, 2019
commit 740f14756b91359338f4a57049bb054e8a0d1115
14 changes: 10 additions & 4 deletions lib/paperclip/attachment.rb
Original file line number Diff line number Diff line change
@@ -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 check_validity? || !@options[:check_validity_before_processing]
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])
2 changes: 1 addition & 1 deletion lib/paperclip/has_attached_file.rb
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion lib/paperclip/validators.rb
Original file line number Diff line number Diff line change
@@ -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
7 changes: 5 additions & 2 deletions spec/paperclip/attachment_spec.rb
Original file line number Diff line number Diff line change
@@ -63,6 +63,7 @@
Dummy.create!(avatar: File.new(fixture_file("50x50.png"), "rb"))

dummy = Dummy.first
allow(dummy.avatar).to receive(:check_validity?).and_return(true)
dummy.avatar.reprocess!(:small)

expect(dummy.avatar.path(:small)).to exist
@@ -868,7 +869,8 @@ def do_after_all; end
expect(@dummy).to receive(:do_after_avatar).never
expect(@dummy).to receive(:do_before_all).and_return(false)
expect(@dummy).to receive(:do_after_all)
expect(Paperclip::Thumbnail).not_to receive(:make)
expect(@dummy).to receive(:check_validity?).and_return(false)
@dummy.check_validity?
@dummy.avatar = @file
end

@@ -877,7 +879,8 @@ def do_after_all; end
expect(@dummy).to receive(:do_after_avatar)
expect(@dummy).to receive(:do_before_all).and_return(true)
expect(@dummy).to receive(:do_after_all)
expect(Paperclip::Thumbnail).not_to receive(:make)
expect(@dummy).to receive(:check_validity?).and_return(false)
@dummy.check_validity?
@dummy.avatar = @file
end
end