Automatically userstamps create and update operations if the table has columns named created_by and/or updated_by. The Blamer gem attempts to mirror the simplicity of ActiveRecord's timestamp module.
User.current_user to return the current user object. Basic setup involves assigning to
User.current_user in a controller before filter:
class User < ActiveRecord::Base cattr_accessor :current_user end class ApplicationController < ActionController::Base before_action :set_userstamp def set_userstamp User.current_user = User.find(session[:user_id]) end end
If you don't want to use
User.current_user you can override this behavior by writing your own
userstamp_object method in ActiveRecord::Base or any of your models. For example, to use
def userstamp_object Person.current end
A default/fallback userstamp_object can be set if you never want to set the userstamps to nil be defining a
default_userstamp_object method in your model:
def default_userstamp_object Person.find_by(name: 'Admin') end
If you don't like created_by/updated_by you can customize the column names:
# Globally in environment.rb ActiveRecord::Base.created_userstamp_column = :creator_id ActiveRecord::Base.updated_userstamp_column = :updator_id # In a model definition class Subscription self.created_userstamp_column = :creator_id self.updated_userstamp_column = :updater_id end
Automatic userstamping can be turned off globally by setting:
ActiveRecord::Base.record_userstamps = false
Blamer adds a
userstamps migration helper which will add the created_by and updated_by columns (or your custom column names) to your table:
create_table :widgets do |t| t.string :name t.timestamps t.userstamps end
If you need thread safety make sure that your implementation of
User.current_user (or your custom user object) is thread safe. Here's one way this can be accomplished:
class User < ActiveRecord::Base def self.current_user Thread.current[:current_user] end def self.current_user=(user) Thread.current[:current_user] = user end end
Blamer is tested to be compatible with Rails 2.x, 3.x, 4.x, and 5.x. See version specific Installation instructions below.
You must use the correct version of blamer for the version of Rails you are running:
Rails 4 and Rails 5
Add to your Gemfile
gem 'blamer', github: 'infused/blamer'
Add to your Gemfile
gem 'blamer', '~> 3.0.0'
gem 'blamer', :github => 'infused/blamer', :branch => '3_stable'
Add a line to your environment.rb
config.gem 'blamer', '~> 3.0.0'
Copyright (c) 2008-2018 Keith Morrison <firstname.lastname@example.org>
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.