audit_log_vk 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +7 -0
- data/.rspec +3 -0
- data/.rubocop.yml +13 -0
- data/CHANGELOG.md +5 -0
- data/CODE_OF_CONDUCT.md +84 -0
- data/LICENSE.txt +21 -0
- data/README.md +121 -0
- data/Rakefile +12 -0
- data/audit_log.gemspec +38 -0
- data/lib/audit_log/config.rb +24 -0
- data/lib/audit_log/context.rb +38 -0
- data/lib/audit_log/entry.rb +20 -0
- data/lib/audit_log/helpers.rb +19 -0
- data/lib/audit_log/model.rb +82 -0
- data/lib/audit_log/version.rb +5 -0
- data/lib/audit_log.rb +12 -0
- data/lib/generators/audit_log/install_generator.rb +21 -0
- data/lib/generators/audit_log/templates/initializer.rb +10 -0
- data/lib/generators/audit_log/templates/migration.rb +17 -0
- data/sig/audit_log.rbs +4 -0
- metadata +80 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: df1d9e69ffc1af41bc77961e10bc4b7914f5db29dd283186d0b6dd19e346ff47
|
4
|
+
data.tar.gz: 2ca0bb840d2427ff623275d0bd4c37293080ef4fb04efaeafdabb57c359a784b
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 36e27ef44a58edc2e2f334d943eb4f411f9948fad4e0168df77db0a5ee879f0eca003094d60898e5c9df96ffcaa82276d2139da2e93619007ecd3d74eeeb1b75
|
7
|
+
data.tar.gz: df0979b95450867bd5ed279f4809fdab9d7c613c1d7a78005773bad8a37d902280b0572f6bde7e36c4f8c9e2d0d58bed07a588442b93e6697490610a197e1eea
|
data/.rspec
ADDED
data/.rubocop.yml
ADDED
data/CHANGELOG.md
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
We as members, contributors, and leaders pledge to make participation in our community a harassment-free experience for everyone, regardless of age, body size, visible or invisible disability, ethnicity, sex characteristics, gender identity and expression, level of experience, education, socio-economic status, nationality, personal appearance, race, religion, or sexual identity and orientation.
|
6
|
+
|
7
|
+
We pledge to act and interact in ways that contribute to an open, welcoming, diverse, inclusive, and healthy community.
|
8
|
+
|
9
|
+
## Our Standards
|
10
|
+
|
11
|
+
Examples of behavior that contributes to a positive environment for our community include:
|
12
|
+
|
13
|
+
* Demonstrating empathy and kindness toward other people
|
14
|
+
* Being respectful of differing opinions, viewpoints, and experiences
|
15
|
+
* Giving and gracefully accepting constructive feedback
|
16
|
+
* Accepting responsibility and apologizing to those affected by our mistakes, and learning from the experience
|
17
|
+
* Focusing on what is best not just for us as individuals, but for the overall community
|
18
|
+
|
19
|
+
Examples of unacceptable behavior include:
|
20
|
+
|
21
|
+
* The use of sexualized language or imagery, and sexual attention or
|
22
|
+
advances of any kind
|
23
|
+
* Trolling, insulting or derogatory comments, and personal or political attacks
|
24
|
+
* Public or private harassment
|
25
|
+
* Publishing others' private information, such as a physical or email
|
26
|
+
address, without their explicit permission
|
27
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
28
|
+
professional setting
|
29
|
+
|
30
|
+
## Enforcement Responsibilities
|
31
|
+
|
32
|
+
Community leaders are responsible for clarifying and enforcing our standards of acceptable behavior and will take appropriate and fair corrective action in response to any behavior that they deem inappropriate, threatening, offensive, or harmful.
|
33
|
+
|
34
|
+
Community leaders have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, and will communicate reasons for moderation decisions when appropriate.
|
35
|
+
|
36
|
+
## Scope
|
37
|
+
|
38
|
+
This Code of Conduct applies within all community spaces, and also applies when an individual is officially representing the community in public spaces. Examples of representing our community include using an official e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event.
|
39
|
+
|
40
|
+
## Enforcement
|
41
|
+
|
42
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported to the community leaders responsible for enforcement at araujo. All complaints will be reviewed and investigated promptly and fairly.
|
43
|
+
|
44
|
+
All community leaders are obligated to respect the privacy and security of the reporter of any incident.
|
45
|
+
|
46
|
+
## Enforcement Guidelines
|
47
|
+
|
48
|
+
Community leaders will follow these Community Impact Guidelines in determining the consequences for any action they deem in violation of this Code of Conduct:
|
49
|
+
|
50
|
+
### 1. Correction
|
51
|
+
|
52
|
+
**Community Impact**: Use of inappropriate language or other behavior deemed unprofessional or unwelcome in the community.
|
53
|
+
|
54
|
+
**Consequence**: A private, written warning from community leaders, providing clarity around the nature of the violation and an explanation of why the behavior was inappropriate. A public apology may be requested.
|
55
|
+
|
56
|
+
### 2. Warning
|
57
|
+
|
58
|
+
**Community Impact**: A violation through a single incident or series of actions.
|
59
|
+
|
60
|
+
**Consequence**: A warning with consequences for continued behavior. No interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, for a specified period of time. This includes avoiding interactions in community spaces as well as external channels like social media. Violating these terms may lead to a temporary or permanent ban.
|
61
|
+
|
62
|
+
### 3. Temporary Ban
|
63
|
+
|
64
|
+
**Community Impact**: A serious violation of community standards, including sustained inappropriate behavior.
|
65
|
+
|
66
|
+
**Consequence**: A temporary ban from any sort of interaction or public communication with the community for a specified period of time. No public or private interaction with the people involved, including unsolicited interaction with those enforcing the Code of Conduct, is allowed during this period. Violating these terms may lead to a permanent ban.
|
67
|
+
|
68
|
+
### 4. Permanent Ban
|
69
|
+
|
70
|
+
**Community Impact**: Demonstrating a pattern of violation of community standards, including sustained inappropriate behavior, harassment of an individual, or aggression toward or disparagement of classes of individuals.
|
71
|
+
|
72
|
+
**Consequence**: A permanent ban from any sort of public interaction within the community.
|
73
|
+
|
74
|
+
## Attribution
|
75
|
+
|
76
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 2.0,
|
77
|
+
available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
|
78
|
+
|
79
|
+
Community Impact Guidelines were inspired by [Mozilla's code of conduct enforcement ladder](https://github.com/mozilla/diversity).
|
80
|
+
|
81
|
+
[homepage]: https://www.contributor-covenant.org
|
82
|
+
|
83
|
+
For answers to common questions about this code of conduct, see the FAQ at
|
84
|
+
https://www.contributor-covenant.org/faq. Translations are available at https://www.contributor-covenant.org/translations.
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2025 viktor
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,121 @@
|
|
1
|
+
# AuditLog
|
2
|
+
|
3
|
+
A lightweight, Rails-friendly audit logging gem to track model changes (`create`, `update`, `destroy`) with contextual metadata like `actor` and `reason`. Perfect for admin panels, SaaS apps, and audit trails.
|
4
|
+
|
5
|
+
---
|
6
|
+
|
7
|
+
## ✨ Features
|
8
|
+
|
9
|
+
- Track `create`, `update`, and `destroy` events on any model
|
10
|
+
- Store changes in an `audit_log_entries` table
|
11
|
+
- Associate logs with the user (or any actor) who made the change
|
12
|
+
- Optional `reason` for change
|
13
|
+
- Configurable `only:` and `except:` fields
|
14
|
+
- Easy to install and integrate
|
15
|
+
|
16
|
+
---
|
17
|
+
|
18
|
+
## 🔧 Installation
|
19
|
+
|
20
|
+
Add this line to your application's Gemfile:
|
21
|
+
|
22
|
+
```ruby
|
23
|
+
gem "audit_log", path: "path/to/your/local/gem"
|
24
|
+
# or gem "audit_log" if published
|
25
|
+
```
|
26
|
+
Then run:
|
27
|
+
```ruby
|
28
|
+
bundle install
|
29
|
+
```
|
30
|
+
Install the initializer and migration:
|
31
|
+
```ruby
|
32
|
+
bin/rails generate audit_log:install
|
33
|
+
bin/rails db:migrate
|
34
|
+
```
|
35
|
+
|
36
|
+
## 🚀 Usage
|
37
|
+
|
38
|
+
### 1. Include the concern in your model:
|
39
|
+
|
40
|
+
```ruby
|
41
|
+
class Post < ApplicationRecord
|
42
|
+
include AuditLog::Model
|
43
|
+
|
44
|
+
audited
|
45
|
+
end
|
46
|
+
```
|
47
|
+
You can optionally limit or exclude tracked attributes:
|
48
|
+
```ruby
|
49
|
+
audited only: [:title, :status] # Only track these fields
|
50
|
+
audited except: [:updated_at, :synced] # Track everything but these
|
51
|
+
```
|
52
|
+
|
53
|
+
### 2. Set the actor (who made the change):
|
54
|
+
In your controller (e.g. ApplicationController):
|
55
|
+
```ruby
|
56
|
+
before_action do
|
57
|
+
AuditLog::Context.actor = current_user
|
58
|
+
end
|
59
|
+
```
|
60
|
+
You can configure the method name (current_user) in the initializer.
|
61
|
+
|
62
|
+
### 3. Optionally set a reason:
|
63
|
+
You can set a reason for changes anywhere in your code:
|
64
|
+
```ruby
|
65
|
+
AuditLog::Context.with(reason: "bulk import") do
|
66
|
+
post.update!(status: "archived")
|
67
|
+
end
|
68
|
+
```
|
69
|
+
|
70
|
+
## ⚙️ Configuration
|
71
|
+
Generated initializer at config/initializers/audit_log.rb:
|
72
|
+
```ruby
|
73
|
+
AuditLog.configure do |config|
|
74
|
+
config.actor_method = :current_user
|
75
|
+
config.ignored_attributes = ["updated_at"]
|
76
|
+
end
|
77
|
+
```
|
78
|
+
|
79
|
+
## 🧪 Testing in your app
|
80
|
+
Use AuditLog::Entry to inspect audit logs:
|
81
|
+
```ruby
|
82
|
+
AuditLog::Entry.for_model(post).order(created_at: :desc)
|
83
|
+
```
|
84
|
+
|
85
|
+
## 📦 Entry Table Schema
|
86
|
+
The install generator includes a migration that creates:
|
87
|
+
```ruby
|
88
|
+
create_table :audit_log_entries do |t|
|
89
|
+
t.string :auditable_type
|
90
|
+
t.bigint :auditable_id
|
91
|
+
t.string :action
|
92
|
+
t.json :changed_data
|
93
|
+
t.string :reason
|
94
|
+
t.string :actor_type
|
95
|
+
t.bigint :actor_id
|
96
|
+
t.timestamps
|
97
|
+
end
|
98
|
+
```
|
99
|
+
|
100
|
+
## ✅ Development
|
101
|
+
This gem uses rspec and an in-memory SQLite DB to test audit hooks.
|
102
|
+
|
103
|
+
Run specs with:
|
104
|
+
```ruby
|
105
|
+
bundle exec rspec
|
106
|
+
```
|
107
|
+
|
108
|
+
|
109
|
+
## Contributing
|
110
|
+
|
111
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/audit_log. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/audit_log/blob/master/CODE_OF_CONDUCT.md).
|
112
|
+
|
113
|
+
## 📄 License
|
114
|
+
|
115
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
116
|
+
|
117
|
+
## 🙌 Credits
|
118
|
+
|
119
|
+
Created with by Viktor Araújo.
|
120
|
+
|
121
|
+
Inspired by open-source audit trail gems like PaperTrail, Audited, and others — but lighter and more simple.
|
data/Rakefile
ADDED
data/audit_log.gemspec
ADDED
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative "lib/audit_log/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "audit_log_vk"
|
7
|
+
spec.version = AuditLog::VERSION
|
8
|
+
spec.authors = ["viktor"]
|
9
|
+
spec.email = ["araujo"]
|
10
|
+
|
11
|
+
spec.summary = "Track changes to models with audit logging."
|
12
|
+
spec.description = "AuditLog is a lightweight gem for adding create/update/delete auditing to your Rails models. Tracks changes, actor, and optional reasons."
|
13
|
+
spec.homepage = "https://github.com/vkaraujo/audit_log"
|
14
|
+
spec.license = "MIT"
|
15
|
+
spec.required_ruby_version = ">= 2.6.0"
|
16
|
+
|
17
|
+
spec.metadata["source_code_uri"] = spec.homepage
|
18
|
+
spec.metadata["changelog_uri"] = "#{spec.homepage}/blob/main/CHANGELOG.md"
|
19
|
+
spec.metadata["homepage_uri"] = "https://github.com/vkaraujo/audit_log"
|
20
|
+
|
21
|
+
# Specify which files should be added to the gem when it is released.
|
22
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
23
|
+
spec.files = Dir.chdir(__dir__) do
|
24
|
+
`git ls-files -z`.split("\x0").reject do |f|
|
25
|
+
(File.expand_path(f) == __FILE__) ||
|
26
|
+
f.start_with?(*%w[bin/ test/ spec/ features/ .git .circleci appveyor Gemfile])
|
27
|
+
end
|
28
|
+
end
|
29
|
+
spec.bindir = "exe"
|
30
|
+
spec.executables = spec.files.grep(%r{\Aexe/}) { |f| File.basename(f) }
|
31
|
+
spec.require_paths = ["lib"]
|
32
|
+
|
33
|
+
# Uncomment to register a new dependency of your gem
|
34
|
+
spec.add_dependency "rails", ">= 6.0"
|
35
|
+
|
36
|
+
# For more information and examples about making a new gem, check out our
|
37
|
+
# guide at: https://bundler.io/guides/creating_gem.html
|
38
|
+
end
|
@@ -0,0 +1,24 @@
|
|
1
|
+
# lib/audit_log/config.rb
|
2
|
+
module AuditLog
|
3
|
+
class << self
|
4
|
+
attr_accessor :configuration
|
5
|
+
|
6
|
+
def configure
|
7
|
+
self.configuration ||= Configuration.new
|
8
|
+
yield(configuration)
|
9
|
+
end
|
10
|
+
|
11
|
+
def reset_configuration!
|
12
|
+
self.configuration = Configuration.new
|
13
|
+
end
|
14
|
+
end
|
15
|
+
|
16
|
+
class Configuration
|
17
|
+
attr_accessor :actor_method, :ignored_attributes
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
@actor_method = :current_user
|
21
|
+
@ignored_attributes = ["updated_at"]
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# lib/audit_log/context.rb
|
2
|
+
module AuditLog
|
3
|
+
class Context
|
4
|
+
THREAD_KEY = :audit_log_context
|
5
|
+
|
6
|
+
class << self
|
7
|
+
# Set the current actor (e.g. current_user)
|
8
|
+
def actor=(value)
|
9
|
+
context[:actor] = value
|
10
|
+
end
|
11
|
+
|
12
|
+
def actor
|
13
|
+
context[:actor]
|
14
|
+
end
|
15
|
+
|
16
|
+
# Set a reason for this audit action
|
17
|
+
def reason=(value)
|
18
|
+
context[:reason] = value
|
19
|
+
end
|
20
|
+
|
21
|
+
def reason
|
22
|
+
context[:reason]
|
23
|
+
end
|
24
|
+
|
25
|
+
# Clears the stored context after each request/job
|
26
|
+
def reset!
|
27
|
+
Thread.current[THREAD_KEY] = {}
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
# Lazily initializes the thread-local context hash
|
33
|
+
def context
|
34
|
+
Thread.current[THREAD_KEY] ||= {}
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,20 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require "active_record"
|
3
|
+
|
4
|
+
module AuditLog
|
5
|
+
class Entry < ActiveRecord::Base
|
6
|
+
self.table_name = "audit_log_entries"
|
7
|
+
|
8
|
+
belongs_to :auditable, polymorphic: true
|
9
|
+
belongs_to :actor, polymorphic: true, optional: true
|
10
|
+
|
11
|
+
# serialize :changed_data, coder: JSON
|
12
|
+
|
13
|
+
validates :action, presence: true
|
14
|
+
validates :auditable_type, :auditable_id, presence: true
|
15
|
+
|
16
|
+
scope :for_model, ->(model) do
|
17
|
+
where(auditable_type: model.class.name, auditable_id: model.id)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# lib/audit_log/helpers.rb
|
2
|
+
module AuditLog
|
3
|
+
module Helpers
|
4
|
+
# Executes a block with the given audit context
|
5
|
+
#
|
6
|
+
# Example:
|
7
|
+
# AuditLog.with_context(actor: current_user, reason: "Batch update") do
|
8
|
+
# user.update!(name: "New")
|
9
|
+
# end
|
10
|
+
#
|
11
|
+
def self.with_context(actor: nil, reason: nil)
|
12
|
+
AuditLog::Context.actor = actor
|
13
|
+
AuditLog::Context.reason = reason
|
14
|
+
yield
|
15
|
+
ensure
|
16
|
+
AuditLog::Context.reset!
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# lib/audit_log/model.rb
|
2
|
+
require "active_support/concern"
|
3
|
+
|
4
|
+
module AuditLog
|
5
|
+
module Model
|
6
|
+
extend ActiveSupport::Concern
|
7
|
+
|
8
|
+
included do
|
9
|
+
# Stores configuration options passed to the `audited` macro
|
10
|
+
class_attribute :audit_log_options
|
11
|
+
end
|
12
|
+
|
13
|
+
class_methods do
|
14
|
+
# Call this in any model to enable audit logging
|
15
|
+
# Example: `audited only: [:name], except: [:updated_at]`
|
16
|
+
def audited(only: nil, except: [])
|
17
|
+
self.audit_log_options = {
|
18
|
+
only: Array(only).map(&:to_s),
|
19
|
+
except: Array(except).map(&:to_s)
|
20
|
+
}
|
21
|
+
|
22
|
+
# Includes the callback hooks that handle logging
|
23
|
+
include AuditLog::Model::Hooks
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
module Hooks
|
28
|
+
extend ActiveSupport::Concern
|
29
|
+
|
30
|
+
included do
|
31
|
+
# Add model lifecycle callbacks for auditing
|
32
|
+
after_create :log_audit_create
|
33
|
+
after_update :log_audit_update
|
34
|
+
before_destroy :log_audit_destroy
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
# Called after a record is created
|
40
|
+
# Logs all initial attributes
|
41
|
+
def log_audit_create
|
42
|
+
create_audit_entry("create", saved_changes.except(*ignored_audit_attrs))
|
43
|
+
end
|
44
|
+
|
45
|
+
# Called before a record is updated
|
46
|
+
# Skips logging if there are no meaningful changes
|
47
|
+
def log_audit_update
|
48
|
+
return if saved_changes.except(*ignored_audit_attrs).empty?
|
49
|
+
|
50
|
+
create_audit_entry("update", saved_changes.except(*ignored_audit_attrs))
|
51
|
+
end
|
52
|
+
|
53
|
+
# Called before a record is destroyed
|
54
|
+
# Logs the full record state before deletion
|
55
|
+
def log_audit_destroy
|
56
|
+
create_audit_entry("destroy", attributes)
|
57
|
+
end
|
58
|
+
|
59
|
+
# Actually creates the audit log entry
|
60
|
+
def create_audit_entry(action, changed_data)
|
61
|
+
AuditLog::Entry.create!(
|
62
|
+
auditable: self,
|
63
|
+
action: action,
|
64
|
+
actor: AuditLog::Context.actor,
|
65
|
+
reason: AuditLog::Context.reason,
|
66
|
+
changed_data: changed_data
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
# Builds the list of attributes to ignore when logging
|
71
|
+
def ignored_audit_attrs
|
72
|
+
if audit_log_options[:only].any?
|
73
|
+
# Only track the specified attributes; ignore all others
|
74
|
+
attribute_names - audit_log_options[:only]
|
75
|
+
else
|
76
|
+
# Ignore what's in `except:` + global ignored config
|
77
|
+
Array(audit_log_options[:except]) + AuditLog.configuration.ignored_attributes
|
78
|
+
end
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
data/lib/audit_log.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
require_relative "audit_log/version"
|
3
|
+
require_relative "audit_log/config"
|
4
|
+
require_relative "audit_log/context"
|
5
|
+
require_relative "audit_log/entry"
|
6
|
+
require_relative "audit_log/model"
|
7
|
+
require_relative "audit_log/helpers"
|
8
|
+
|
9
|
+
module AuditLog
|
10
|
+
class Error < StandardError; end
|
11
|
+
# Your code goes here...
|
12
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require "rails/generators"
|
4
|
+
|
5
|
+
module AuditLog
|
6
|
+
module Generators
|
7
|
+
class InstallGenerator < Rails::Generators::Base
|
8
|
+
source_root File.expand_path("templates", __dir__)
|
9
|
+
|
10
|
+
def copy_initializer
|
11
|
+
template "initializer.rb", "config/initializers/audit_log.rb"
|
12
|
+
end
|
13
|
+
|
14
|
+
def copy_migration
|
15
|
+
timestamp = Time.now.utc.strftime("%Y%m%d%H%M%S")
|
16
|
+
migration_name = "create_audit_log_entries"
|
17
|
+
copy_file "migration.rb", "db/migrate/#{timestamp}_#{migration_name}.rb"
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# Configure AuditLog defaults
|
4
|
+
AuditLog.configure do |config|
|
5
|
+
# Method used to get the actor (e.g. :current_user or :current_admin)
|
6
|
+
config.actor_method = :current_user
|
7
|
+
|
8
|
+
# Attributes to ignore when tracking changes
|
9
|
+
config.ignored_attributes = ["updated_at"]
|
10
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
class CreateAuditLogEntries < ActiveRecord::Migration[6.0]
|
2
|
+
def change
|
3
|
+
create_table :audit_log_entries do |t|
|
4
|
+
t.string :auditable_type
|
5
|
+
t.bigint :auditable_id
|
6
|
+
t.string :action
|
7
|
+
t.json :changed_data
|
8
|
+
t.string :reason
|
9
|
+
t.string :actor_type
|
10
|
+
t.bigint :actor_id
|
11
|
+
t.timestamps
|
12
|
+
end
|
13
|
+
|
14
|
+
add_index :audit_log_entries, [:auditable_type, :auditable_id]
|
15
|
+
add_index :audit_log_entries, [:actor_type, :actor_id]
|
16
|
+
end
|
17
|
+
end
|
data/sig/audit_log.rbs
ADDED
metadata
ADDED
@@ -0,0 +1,80 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: audit_log_vk
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- viktor
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2025-03-31 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rails
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '6.0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '6.0'
|
27
|
+
description: AuditLog is a lightweight gem for adding create/update/delete auditing
|
28
|
+
to your Rails models. Tracks changes, actor, and optional reasons.
|
29
|
+
email:
|
30
|
+
- araujo
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- ".rspec"
|
36
|
+
- ".rubocop.yml"
|
37
|
+
- CHANGELOG.md
|
38
|
+
- CODE_OF_CONDUCT.md
|
39
|
+
- LICENSE.txt
|
40
|
+
- README.md
|
41
|
+
- Rakefile
|
42
|
+
- audit_log.gemspec
|
43
|
+
- lib/audit_log.rb
|
44
|
+
- lib/audit_log/config.rb
|
45
|
+
- lib/audit_log/context.rb
|
46
|
+
- lib/audit_log/entry.rb
|
47
|
+
- lib/audit_log/helpers.rb
|
48
|
+
- lib/audit_log/model.rb
|
49
|
+
- lib/audit_log/version.rb
|
50
|
+
- lib/generators/audit_log/install_generator.rb
|
51
|
+
- lib/generators/audit_log/templates/initializer.rb
|
52
|
+
- lib/generators/audit_log/templates/migration.rb
|
53
|
+
- sig/audit_log.rbs
|
54
|
+
homepage: https://github.com/vkaraujo/audit_log
|
55
|
+
licenses:
|
56
|
+
- MIT
|
57
|
+
metadata:
|
58
|
+
source_code_uri: https://github.com/vkaraujo/audit_log
|
59
|
+
changelog_uri: https://github.com/vkaraujo/audit_log/blob/main/CHANGELOG.md
|
60
|
+
homepage_uri: https://github.com/vkaraujo/audit_log
|
61
|
+
post_install_message:
|
62
|
+
rdoc_options: []
|
63
|
+
require_paths:
|
64
|
+
- lib
|
65
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - ">="
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: 2.6.0
|
70
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - ">="
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '0'
|
75
|
+
requirements: []
|
76
|
+
rubygems_version: 3.4.19
|
77
|
+
signing_key:
|
78
|
+
specification_version: 4
|
79
|
+
summary: Track changes to models with audit logging.
|
80
|
+
test_files: []
|