Userstamper
Overview
Userstamp extends ActiveRecord::Base to add automatic updating of creator and updater attributes.
Two class methods (model_stamper and stampable) are implemented in this gem. The model_stamper
method is used in models that are responsible for creating or updating other objects.
Typically this would be the User model of your application. The stampable method is used in
models that are subject to being created or updated by stampers.
Gem was latest tested with Ruby 3.1.3 and Rails 7.0.3 In history the gem has been tested with Ruby 2.7.2, 2.7.3 and Rails 6.1.3.1.
Features
to result in a belongs_to relation which looks like:
belongs_to :creator, class_name: '::User', foreign_key: :created_bySaving before validation
This includes changes to perform model stamping before validation. This allows models to enforce the presence of stamp attributes:
validates :created_by, presence: true
validates :updated_by, presence: trueFurthermore, the creator attribute is set only if the value is blank allowing for a manual
override.
Usage
Assume that we are building a blog application, with User and Post objects. Add the following to the application's Gemfile:
gem 'userstamper'Define an initializer in your Rails application to configure the gem: config/initilialize/userstamper.rb:
Userstamper.configure do |config|
# config.default_stamper = 'User'
# config.creator_attribute = :creator_id
# config.updater_attribute = :updater_id
config.deleter_attribute = nil
endEnsure that each model has a set of columns for creators and updaters.
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
...
t.userstamps
end
end
end
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
...
t.userstamps
end
end
endIf you use protect_from_forgery, make sure the hooks are prepended:
class ApplicationController < ActionController::Base
protect_from_forgery with: :exception, prepend: true # with: anything will do, note `prepend: true`!
endDeclare the stamper on the User model:
class User < ActiveRecord::Base
model_stamper
endIf your stamper is called User, that's it; you're done.
Customisation
The association which is created on each of the creator_id and updater_id can
be customised. Also, the stamper used by each class can also be customised. For this purpose, the
ActiveRecord::Base.stampable method can be used:
class Post < ActiveRecord::Base
stampable
endThe stampable method allows you to customize the creator and updater associations.
It also allows you to specify the name of the stamper for the class being declared. Any additional
arguments are passed to the belongs_to declaration.
Upgrade
When upgradeing from activerecord_userstamp gem, please remove deleted attributes. Soft delete is not supported and not recommended. Soft delete is more pain in real life.
Tests
Run
$ bundle exec rspec
Forkception
This is a fork of:
- the activerecord-userstamp gem
- the magiclabs-userstamp gem
- which is a fork of Michael Grosser's [userstamp gem] (https://github.com/grosser/userstamp)
- which is a fork of the original userstamp plugin by delynn
In addition to these, some ideas are cherry picked from the following forks:
Authors
- DeLynn Berry: The original idea for this plugin came from the Rails Wiki article entitled Extending ActiveRecord
- Michael Grosser
- John Dell
- Chris Hilton
- Thomas von Deyen
- Joel Low
- Priit Tark
- Carsten Reinke