lib/audited/auditor.rb in audited-4.7.1 vs lib/audited/auditor.rb in audited-4.8.0

- old
+ new

@@ -53,11 +53,11 @@ extend Audited::Auditor::AuditedClassMethods include Audited::Auditor::AuditedInstanceMethods class_attribute :audit_associated_with, instance_writer: false class_attribute :audited_options, instance_writer: false - attr_accessor :version, :audit_comment + attr_accessor :audit_version, :audit_comment self.audited_options = options normalize_audited_options self.audit_associated_with = audited_options[:associated_with] @@ -88,10 +88,20 @@ has_many :associated_audits, as: :associated, class_name: Audited.audit_class.name end end module AuditedInstanceMethods + # Deprecate version attribute in favor of audit_version attribute – preparing for eventual removal. + def method_missing(method_name, *args, &block) + if method_name == :version + ActiveSupport::Deprecation.warn("`version` attribute has been changed to `audit_version`. This attribute will be removed.") + audit_version + else + super + end + end + # Temporarily turns off auditing while saving. def save_without_auditing without_auditing { save } end @@ -143,10 +153,18 @@ # List of attributes that are audited. def audited_attributes attributes.except(*non_audited_columns) end + # Returns a list combined of record audits and associated audits. + def own_and_associated_audits + Audited.audit_class.unscoped + .where('(auditable_type = :type AND auditable_id = :id) OR (associated_type = :type AND associated_id = :id)', + type: self.class.name, id: id) + .order(created_at: :desc) + end + # Combine multiple audits into one. def combine_audits(audits_to_combine) combine_target = audits_to_combine.last combine_target.audited_changes = audits_to_combine.pluck(:audited_changes).reduce(&:merge) combine_target.comment = "#{combine_target.comment}\nThis audit is the result of multiple audits being combined." @@ -208,12 +226,12 @@ end end def audits_to(version = nil) if version == :previous - version = if self.version - self.version - 1 + version = if self.audit_version + self.audit_version - 1 else previous = audits.descending.offset(1).first previous ? previous.version : 1 end end @@ -288,13 +306,12 @@ self.class.auditing_enabled end def run_conditional_check(condition, matching: true) return true if condition.blank? - return condition.call(self) == matching if condition.respond_to?(:call) - return send(condition) == matching if respond_to?(condition.to_sym) + return send(condition) == matching if respond_to?(condition.to_sym, true) true end def auditing_enabled=(val) @@ -353,10 +370,10 @@ def audit_as(user, &block) Audited.audit_class.as_user(user, &block) end def auditing_enabled - Audited.store.fetch("#{table_name}_auditing_enabled", true) + Audited.store.fetch("#{table_name}_auditing_enabled", true) && Audited.auditing_enabled end def auditing_enabled=(val) Audited.store["#{table_name}_auditing_enabled"] = val end