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
Show file tree
Hide file tree
Changes from all commits
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
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