Snapcher is an ORM extension that logs changes to specific columns to your model.
When a change is made to a specific column, the difference between before and after the change is obtained and saved.
To make it easier for analysts, save the table name, column name, and data before and after changes as separate columns.
The name of this gem comes from one of Hideo Kojima's game works, "Snatcher".
It was the first of his game works to introduce cinematic direction, and "Snapcher" is also the first of my gem works.
- 3.1
- 3.2
- 7.0
- 7.1
Snapcher is currently ActiveRecord-only.
Add the gem to your Gemfile:
gem "snapcher"
Then, from your Rails app directory, create the scannings
table:
$ rails generate snapcher:install
$ rails db:migrate
Simply call scanning
on your models.
Use column_name:
to select the column you want to log.
class User < ActiveRecord::Base
scanning column_name: "name"
end
By default, whenever a user is created, updated or destroyed, a new scanning is created.
user = User.create!(name: "Gillian Seed")
user.scannings.count # => 1
user.update!(name: "Mika Slayton")
user.scannings.count # => 2
user.destroy
user.scannings.count # => 3
Scanning contain information regarding what action was taken on the model and what changes were made.
user.update!(name: "Mika Slayton")
snapcher = user.scannings.last
snapcher.action # => "update"
snapcher.before_params # => "Gillian Seed"
snapcher.after_params # => "Mika Slayton"
If the "Snatcher" column you want to capture is not user_id, you can specify this.
class User < ActiveRecord::Base
scanning column_name: "name", snatch_user: "id"
end