Skip to content

Commit

Permalink
Respect the 'on' option when used with 'comment_required' option
Browse files Browse the repository at this point in the history
  • Loading branch information
jeffdill2 committed Mar 2, 2018
1 parent fe85b9a commit 421c8d3
Showing 1 changed file with 21 additions and 4 deletions.
25 changes: 21 additions & 4 deletions lib/audited/auditor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def audited(options = {})
self.audit_associated_with = audited_options[:associated_with]

if audited_options[:comment_required]
validates_presence_of :audit_comment, if: :auditing_enabled
validate :presence_of_audit_comment
before_destroy :require_comment
end

Expand Down Expand Up @@ -219,11 +219,28 @@ def write_audit(attrs)
run_callbacks(:audit) { audits.create(attrs) } if auditing_enabled
end

def presence_of_audit_comment
case
when !auditing_enabled
return true
when audit_comment.present?
return true
when audited_options[:on].present? && audited_options[:on].exclude?(:create) && self.new_record?
return true
when audited_options[:on].present? && audited_options[:on].exclude?(:update) && self.persisted?
return true
else
errors.add(:audit_comment, "can't be blank.")
end
end

def require_comment
if auditing_enabled && audit_comment.blank?
errors.add(:audit_comment, "Comment required before destruction")
return false if Rails.version.start_with?('4.')
throw :abort
unless audited_options[:on].present? && audited_options[:on].exclude?(:destroy)
errors.add(:audit_comment, "Comment required before destruction")
return false if Rails.version.start_with?('4.')
throw :abort
end
end
end

Expand Down

0 comments on commit 421c8d3

Please sign in to comment.