Giter Site home page Giter Site logo

blamer's Introduction

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 Thread.current[:current_user] to return the current user. Storing the current_user in Thread.current ensures that this gem is threadsafe (e.g., compatible with threaded webservers like Puma). Basic setup involves assigning to Thread.current[:current_user] in a controller before filter:

class ApplicationController < ActionController::Base
  before_action :set_userstamp

  def set_userstamp
    Thread.current[:current_user] = User.find(session[:user_id])
  end
end

If you don't want to use Thread.current[: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:

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

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 2.x, 3.x, 4.x, and 5.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 4 and Rails 5

Add to your Gemfile

gem 'blamer'

Or

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

Rails 3.x

Add to your Gemfile

gem 'blamer', '~> 3.0.0'

Or

gem 'blamer', :github => 'infused/blamer', :branch => '3_stable'

Rails 2.x

Add a line to your environment.rb

config.gem 'blamer', '~> 3.0.0'

Credits

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

License

Copyright (c) 2008-2018 Keith Morrison <[email protected]>

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.

blamer's People

Contributors

infused avatar ronughoshal avatar knzai avatar audy avatar martinemde avatar indirect avatar

Watchers

Karsten Temme avatar  avatar Mikela avatar James Cloos avatar Cait Ryan avatar

Forkers

ronughoshal

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.