From 9a628a5f8662bdcbb38eaace40d587780cb9b278 Mon Sep 17 00:00:00 2001 From: gmhawash Date: Tue, 25 Jun 2024 04:52:06 +0300 Subject: [PATCH] dynamic max_audits to be proc or symbol --- lib/audited/auditor.rb | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lib/audited/auditor.rb b/lib/audited/auditor.rb index e2f1388e..c9c867ae 100644 --- a/lib/audited/auditor.rb +++ b/lib/audited/auditor.rb @@ -384,13 +384,25 @@ def comment_required_state? end def combine_audits_if_needed - max_audits = audited_options[:max_audits] + max_audits = evaluate_max_audits + if max_audits && (extra_count = audits.count - max_audits) > 0 audits_to_combine = audits.limit(extra_count + 1) combine_audits(audits_to_combine) end end + def evaluate_max_audits + max_audits = case (option = audited_options[:max_audits]) + when Proc then option.call + when Symbol then send(option) + else + option + end + + Integer(max_audits).abs if max_audits + end + def require_comment if auditing_enabled && audit_comment.blank? errors.add(:audit_comment, :blank) @@ -502,8 +514,7 @@ def normalize_audited_options audited_options[:on] = ([:create, :update, :touch, :destroy] - Audited.ignored_default_callbacks) if audited_options[:on].empty? audited_options[:only] = Array.wrap(audited_options[:only]).map(&:to_s) audited_options[:except] = Array.wrap(audited_options[:except]).map(&:to_s) - max_audits = audited_options[:max_audits] || Audited.max_audits - audited_options[:max_audits] = Integer(max_audits).abs if max_audits + audited_options[:max_audits] ||= Audited.max_audits end def calculate_non_audited_columns