LogBook
LogBook is a gem that tracks changes on your records. Created for the purpuse of auditing and showing activity log. For comparison with paper___trail and audited see
Supported ORMs
Currently only supports ActiveRecord.
Instalation
Add to your Gemfile:
gem 'rails_log_book'
Then run:
rails generate log_book:install
rake db:migrate
Usage
Add to models you want to keep track of:
class User < ActiveRecord::Base
include LogBook::Recorder
has_log_book_records
end
Add to controlers and actions you want the tracker to be active:
class UsersController < ApplicationController
include LogBook::ControllerRecord
end
By default, whenever a user record is created, updated or deleted in any actions of users_controller a new log_book record will be created.
ActiveRecord Options
fields
class User < ActiveRecord::Base
include LogBook::Recorder
# all fields
# has_log_book_records
# Only fields
# has_log_book_records only: [:email, :name]
# Ignored fields
# has_log_book_records except: [:password]
# Default ignored fields
# primary_key (id), LogBook.config.ignored_attributes (:created_at, :updated_at)
end
callbacks
class User < ActiveRecord::Base
include LogBook::Recorder
# all events
# has_log_book_records
# Only record on create and destroy (not update)
# has_log_book_records on: [:create, :destroy]
end
parent
Define who is a parent of this object. Will be recorded in a parent
polymorphic columns
class User < ActiveRecord::Base
include LogBook::Recorder
belongs_to :company
# Parent is Company and will be recorded with each user change
# has_log_book_records parent: :company
end
meta
Arbitrary column. This is a jsonb field which can have all kinds of information. Useful when you want to cache fields at the exact point of record creation
class User < ActiveRecord::Base
include LogBook::Recorder
# runs `log_book_meta(record)` method to assign to `:meta` field
# has_log_book_records meta: true
# runs `meta_method` method to assing to `:meta` field
# has_log_book_records meta: :meta_method
# runs passed proc to assing to `:meta` field
# has_log_book_records meta: -> { { slug: email.split('@').first } }
end
squash
Enables/disables suashing on this model
class User < ActiveRecord::Base
include LogBook::Recorder
# Enables squashing (defaults to false)
# has_log_book_records squash: true
ActionController options
current_author
Defines what method is run when looking for the author for recording
class Admin::UsersController < ActionController::Base
inlcude LogBook::ControllerRecord
# defaults to `current_user`
def current_author
current_admin
end
Configuration
# config/initializers/log_book.rb
LogBook.configure do |config|
config.records_table_name = 'records'
config.ignored_attributes = [:updated_at, :created_at]
config. = :current_user
config.record_squashing = false
config.recording_enabled = false
config.skip_if_empty_actions = [:update]
end
Additional methods
LogBook.with_recording {} #=> Enables recording within block
LogBook.without_recording {} #=> Disables recording within block
LogBook.record_as() {} #=> Records as a different author within block
LogBook.with_record_squashing {} #=> Squashes records within block
LogBook.enable_recording #=> Enables recording from this point
LogBook.disable_recording #=> Disables recording from this point
LogBook.record_squashing_enabled #=> Enables record squashing from this point
LogBook.recording_enabled #=> Returns true if recording is enabled
LogBook.recording_enabled=(val) #=> Enables/Disables recording
LogBook.squash_records #=> Squash records with current :request_uuid
Squashing
The idea of squashing came when we needed to show an activity page where all changes made in the single request as one change.
Each request has its own unique request_uuid
and it is recorded with each record. If squashing is enabled, after_action
method with squashing is called.
All records with the same request_uuid
are "squashed" into one record.
INSERT INTO "users" ("name", "email") VALUES ("test", "[email protected]")
Development
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Contributing
Bug reports and pull requests are welcome on GitHub at https://github.com/infinum/log_book. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the Contributor Covenant code of conduct.
License
The gem is available as open source under the terms of the MIT License.