Project

blamer

0.03
No release in over 3 years
Low commit activity in last 3 years
Automatically userstamps create and update operations if the table has created_by and/or updated_by columns
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Runtime

 Project Readme

Blamer

Version Build Status Code Quality Total Downloads License

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.

Blamer expects 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 Person.current instead:

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 = :updater_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

Thread Safety

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

Compatability

Blamer is tested to be compatible with Rails 5.x, 6.x, and 7.x. See version specific Installation instructions below.

Installation

You must use the correct version of blamer for the version of Rails you are running:

Rails 5+

Add to your Gemfile

gem 'blamer'

Or

gem 'blamer', github: 'infused/blamer'

Credits

Thanks to DeLynn Berry delynn@gmail.com for writing the original Userstamp plugin (http://github.com/delynn/userstamp/), which was the inspiration for this plugin.

License

Copyright (c) 2008-2022 Keith Morrison <keithm@infused.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.