GithubHelp home page GithubHelp logo

logbook's Introduction

LogBook

Ruby on Rails, Automatically keeping record of all Model Changes

Installation

Add this line to your application's Gemfile:

gem "log_book"

As the Model should be loaded after ActiveReccord has established the connection you have to add this:

# config/application.rb
config.after_initialize do
  require "log_book/event"
end

Create the table

rails generate log_book:migration
rake db:migrate

ActsOnTaggableOn dependency

rails generate acts_as_taggable_on:migration
# rake acts_as_taggable_on_engine:install:migrations # for version '~> 4.0' or superior
rake db:migrate

Usage

In any point:

LogBook.event(<who executes the action>, <over which object>, <text>, <list of tags>)

For example:

LogBook.event(current_user, item, "Item canceled", [:purchase, :canceled])

ActiveRecord integration

class MyModel < ActiveRecord::Base
  log_book
end

MyModel.create!   # => LogBook created
my_model.save!    # => LogBook created
my_model.destroy! # => LogBook created

If you want to include who executes the action use the special attribute log_book_historian:

my_model.log_book_historian = current_user
my_model.save!

If you want to mute a model change:

my_model.log_book_mute = true
my_model.save! # No LogBook::Event will be generated

If you want to mute LogBook globally:

LogBook.mute = true
my_model.save! # No LogBook::Event will be generated

If you want to ignore some fields from the changes Event:

class MyModel < ActiveRecord::Base
  log_book :ignore => [:my_counter]
end

my_model.update_atttibtes!(:my_counter => 9) # No LogBook::Event will be generated

If you want LogBook::Events to be destroyed on Model destroy:

class MyModel < ActiveRecord::Base
  log_book :dependent => :destroy
end

In other case the LogBook::Events will remain after Model destroyed.

Rails Integration

Check this example project to see how LogBook is integrated:

Or this commit in a more modern Rails project:

TODO

Use block configuration instead of model.log_book_historian do something like:

LogBook.conf(:log_book_historian => user) do
  model.save!
end

Sate of the art

Beta version but already used in production environments

In case your IDs are strings

Change the migration with this one in your project:

class CreateLogBookEventsUuidsSupport < ActiveRecord::Migration[4.2]
  def self.up
    create_table :log_book_events do |t|
      t.string :historian_id
      t.string :historian_type
      t.string :historizable_id
      t.string :historizable_type
      t.text :differences, :limit => 16777215 # mediumtext

      t.timestamps :null => false
    end

    add_index :log_book_events, [:historizable_id, :historizable_type, :created_at], :name => "index_log_book_events_on_historizable_and_created_at"
    add_index :log_book_events, [:created_at]
  end

  def self.down
    drop_table :log_book_events
  end
end

Development

Running test

rake test

Release new version

Update: lib/log_book/version.rb

git tag -a v[version] -m "Changelog..."
git push origin v[version]
gem build
gem push log_book-[version].gem

logbook's People

Contributors

fguillen avatar mistersourcerer avatar sebkkom avatar

Stargazers

 avatar  avatar

Watchers

 avatar

logbook's Issues

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.