Project

has_logs

0.0
No commit activity in last 3 years
No release in over 3 years
Logging your ActiveRecord model, and supply useful methods.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 2.1
~> 1.15
~> 0.10
~> 10.0
~> 3.0
~> 1.2
~> 3.5
~> 1.0

Runtime

 Project Readme

HasLogs

Build Status

Logging your ActiveRecord model, and supply useful methods.

Installation

Add this line to your application's Gemfile:

gem 'has_logs'

And then execute:

$ bundle

Or install it yourself as:

$ gem install has_logs

Basic Example

First Migration

class CreateAllTables < ActiveRecord::Migration
  def self.up
    create_table(:articles) do |t|
      t.timestamps
    end

    create_table(:article_logs) do |t|
      t.integer :article_id, null: false
      t.string :title, null: false
      t.text :content
      t.boolean :public, null: false, default: true
      t.datetime :created_at
    end
    add_index :article_logs,  :article_id
    add_index :article_logs, [:article_id, :created_at], unique: true
  end
end

Model Class

class Article < ActiveRecord::Base
  has_logs
end

class ArticleLog < ActiveRecord::Base
  act_as_log
end

Usage Examples

article = Article.create(title: 'test1', content: 'demo1', public: false)
article.attributes =   { title: 'test2', content: 'demo2', public: false } ; article.save!
article.attributes =   { title: 'test3', content: 'demo3', public: true  } ; article.save!

article.title
=> 'test3'

article.logs
=> [#<ArticleLog id: 1, article_id: 1, title: "test1", content: "demo1", created_at: "2015-01-04 03:36:51">,
 #<ArticleLog id: 2, article_id: 1, title: "test2", content: "demo2", created_at: "2015-01-04 03:36:51">,
 #<ArticleLog id: 3, article_id: 1, title: "test3", content: "demo3", created_at: "2015-01-04 03:36:51">]

article.oldest_log
=> #<ArticleLog id: 1, article_id: 1, title: "test1", content: "demo1", created_at: "2015-01-04 03:36:51">

article.latest_log
=> #<ArticleLog id: 3, article_id: 1, title: "test3", content: "demo3", created_at: "2015-01-04 03:36:51">

article.oldest_log.next
=> #<ArticleLog id: 2, article_id: 1, title: "test2", content: "demo2", created_at: "2015-01-04 03:36:51">

article.latest_log.prev
=> #<ArticleLog id: 2, article_id: 1, title: "test2", content: "demo2", created_at: "2015-01-04 03:36:51">

Originator Methods

Methods Describe
logs get the log list
latest_log get the latest log
oldest_log get the oldest log

Log Methods

Methods Describe
originator get the originator model
next get a next log
prev get a prev log

Other Naming Example

Migration

class CreateAllTables < ActiveRecord::Migration
  def self.up
    create_table(:users) do |t|
      t.timestamps
    end

    create_table(:user_histories) do |t|
      t.integer :user_id, null: false
      t.string :name, null: false
      t.datetime :created_at
    end
    add_index :user_logs,  :user_id
    add_index :user_logs, [:user_id, :created_at], unique: true
  end
end

Model Class

class User < ActiveRecord::Base
  has_logs_as 'UserHistory'
end

class UserHistory < ActiveRecord::Base
  act_as_log_of 'User'
end

Duplicate Type Example

Migration

class CreateAllTables < ActiveRecord::Migration
  def self.up
    create_table(:tasks) do |t|
      t.string :name, null: false
      t.timestamps
    end
    create_table(:task_logs) do |t|
      t.integer :task_id, null: false
      t.string :name, null: false
      t.datetime :created_at
    end
    add_index :task_logs,  :task_id
    add_index :task_logs, [:task_id, :created_at], unique: true
  end
end

Model Class

class Task < ActiveRecord::Base
  has_logs have_type: :duplicate
end
class TaskLog < ActiveRecord::Base
  act_as_log
end

Usage Examples

task  = Task.create(name: 'name1')
task.attributes = { name: 'name2' } ; task.save!
task.attributes = { name: 'name3' } ; task.save!

Task.all
=>
+----+-------+
| id | name  |
+----+-------+
|  1 | name3 |
+----+-------+

TaskLog.all
=>
+----+---------+-------+
| id | task_id | name  |
+----+---------+-------+
|  1 |       1 | name1 |
|  2 |       1 | name2 |
|  3 |       1 | name3 |
+----+---------+-------+

Mutual Type Example

Migration

class CreateAllTables < ActiveRecord::Migration
  def self.up
    create_table(:posts) do |t|
      t.string :name, null: false
      t.timestamps
    end
    create_table(:post_logs) do |t|
      t.integer :post_id, null: false
      t.string :name, null: false
      t.datetime :created_at
    end
    add_index :post_logs,  :post_id
    add_index :post_logs, [:post_id, :created_at], unique: true
  end
end

Model Class

class Post < ActiveRecord::Base
  has_logs have_type: :mutual
end
class PostLog < ActiveRecord::Base
  act_as_log
end

Usage Examples

post  = Post.create(name: 'name1')
post.attributes = { name: 'name2' } ; post.save!
post.attributes = { name: 'name3' } ; post.save!

Post.all
=>
+----+-------+
| id | name  |
+----+-------+
|  1 | name3 |
+----+-------+

PostLog.all
=>
+----+---------+-------+
| id | post_id | name  |
+----+---------+-------+
|  1 |       1 | name1 |
|  2 |       1 | name2 |
+----+---------+-------+

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/isuke/has_logs. 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.