Module: ActiveRecordAuditable::Audited

Defined in:
lib/active_record_auditable/audited.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.__cached_audit_table_namesObject



2
3
4
5
6
7
8
9
10
# File 'lib/active_record_auditable/audited.rb', line 2

def self.__cached_audit_table_names
  @__cached_audit_table_names ||= begin
    result = {}
    ActiveRecordAuditable::Audit.connection.tables.filter { |table_name| table_name.ends_with?("_audits") }.each do |table_name|
      result[table_name] = true
    end
    result
  end
end

.__dedicated_audit_class(base, table_name) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/active_record_auditable/audited.rb', line 82

def self.__dedicated_audit_class(base, table_name)
  if base.const_defined?("Audit")
    base.const_get("Audit")
  else
    audit_class = Class.new(ActiveRecordAuditable::BaseAudit)
    audit_class.class_eval do
      self.table_name = table_name

      belongs_to base.model_name.param_key.to_sym, optional: true
      belongs_to :auditable, class_name: base.name, foreign_key: :"#{base.model_name.param_key}_id", optional: true

      def self.base_model
        reflections["auditable"].klass
      end

      def auditable_type
        self.class.reflections["auditable"].class_name
      end
    end

    base.const_set("Audit", audit_class)

    ActiveRecordAuditable::AuditAction.has_many(audit_class.model_name.plural.to_sym, class_name: audit_class.name)
    ActiveRecordAuditable::AuditAuditableType.has_many(audit_class.model_name.plural.to_sym, class_name: audit_class.name)

    audit_class
  end
end

.__dedicated_table_exists?(base, dedicated_table_name) ⇒ Boolean

Returns:

  • (Boolean)


76
77
78
79
80
# File 'lib/active_record_auditable/audited.rb', line 76

def self.__dedicated_table_exists?(base, dedicated_table_name)
  ActiveRecordAuditable::Audited.__cached_audit_table_names.key?(dedicated_table_name)
rescue ActiveRecord::StatementInvalid
  false
end

.included(base) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/active_record_auditable/audited.rb', line 12

def self.included(base)
  dedicated_table_name = "#{base.table_name.singularize}_audits"
  dedicated_table_exists = __dedicated_table_exists?(base, dedicated_table_name)

  if dedicated_table_exists
    table_name = dedicated_table_name
    audit_class = __dedicated_audit_class(base, table_name)
    audit_class_name = "#{base.name}::Audit"
    inverse_of = base.model_name.param_key.to_sym
    as = inverse_of
  else
    table_name = "audits"
    audit_class = ActiveRecordAuditable::Audit
    audit_class_name = "ActiveRecordAuditable::Audit"
    inverse_of = :auditable
    as = :auditable
  end

  base.has_one :create_audit, # rubocop:disable Rails/HasManyOrHasOneDependent
    -> { joins(:audit_action).where(audit_actions: {action: "create"}) },
    as: as,
    class_name: audit_class_name,
    inverse_of: inverse_of
  base.has_many :audits, # rubocop:disable Rails/HasManyOrHasOneDependent
    as: as,
    class_name: audit_class_name,
    inverse_of: inverse_of

  base.has_one :create_audit, # rubocop:disable Rails/HasManyOrHasOneDependent
    -> { joins(:audit_action).where(audit_actions: {action: "create"}) },
    class_name: audit_class_name,
    inverse_of: inverse_of
  base.has_many :audits, # rubocop:disable Rails/HasManyOrHasOneDependent
    class_name: audit_class_name,
    inverse_of: inverse_of

  base.after_create do
    create_audit!(action: :create)
  end

  base.after_update do
    create_audit!(action: :update)
  end

  base.after_destroy do
    create_audit!(action: :destroy, audited_changes: attributes)
  end

  base.scope :without_audit, lambda { |action|
    audit_class = base.reflections["audits"].klass
    audit_foreign_key = base.reflections["audits"].foreign_key

    audit_query = audit_class
      .select(1)
      .joins(:audit_action)
      .where(audit_actions: {action:})
      .where("#{audit_class.table_name}.#{audit_foreign_key} = #{base.table_name}.#{base.primary_key}")
      .limit(1)

    audit_query = audit_query.where(auditable_type: base.model_name.name) unless dedicated_table_exists
    where("NOT EXISTS (#{audit_query.to_sql})")
  }
end

Instance Method Details

#audit_monitorObject



111
112
113
# File 'lib/active_record_auditable/audited.rb', line 111

def audit_monitor
  @@audit_monitor ||= Monitor.new # rubocop:disable Style/ClassVars
end

#create_audit!(action:, audited_changes: saved_changes_for_audit, **args) ⇒ Object



115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/active_record_auditable/audited.rb', line 115

def create_audit!(action:, audited_changes: saved_changes_for_audit, **args)
  audit_data = {
    audit_action: find_or_create_auditable_action(action),
    audited_changes:
  }

  audit_class = self.class.reflections["audits"].klass

  if audit_class == ActiveRecordAuditable::Audit
    audit_data[:audit_auditable_type_id] = find_or_create_auditable_type.id
    audit_data[:auditable_id] = id
    audit_data[:auditable_type] = self.model_name.name
  else
    audit_data[:"#{self.class.model_name.param_key}_id"] = id
  end

  audit_class.create!(audit_data.merge(args))
end

#find_or_create_auditable_action(action) ⇒ Object



134
135
136
137
138
# File 'lib/active_record_auditable/audited.rb', line 134

def find_or_create_auditable_action(action)
  audit_monitor.synchronize do
    return ActiveRecordAuditable::AuditAction.find_or_create_by!(action:)
  end
end

#find_or_create_auditable_typeObject



140
141
142
143
144
# File 'lib/active_record_auditable/audited.rb', line 140

def find_or_create_auditable_type
  audit_monitor.synchronize do
    return ActiveRecordAuditable::AuditAuditableType.find_or_create_by!(name: self.class.name)
  end
end

#saved_changes_for_auditObject



146
147
148
149
150
151
152
# File 'lib/active_record_auditable/audited.rb', line 146

def saved_changes_for_audit
  saved_changes_for_audit = {}
  saved_changes.each do |attribute_name, from_and_to|
    saved_changes_for_audit[attribute_name] = from_and_to[1]
  end
  saved_changes_for_audit
end