- Notifications
You must be signed in to change notification settings - Fork0
🕐 Douglas adds stamps to any ActiveRecord model
License
fteem/douglas
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Status: Rough edges are to be expected.
Douglas is a Ruby gem that adds the ability to easily addcreated_by
andupdated_by
attributes to any ActiveRecord model.It allows easy user-stamping for any Active Record model.
Add this line to your application's Gemfile:
gem'douglas'
And then execute:
$ bundle
When added in a Rails application, Douglas adds a generator which enables the usersto easily generate migrations which need to be stamped.
To generate these migrations, you can use Douglas'table
generator:
rails generate douglas:table NAME
This will generate a migration:
create db/migrate/20170322225126_add_created_by_and_updated_by_to_<model-name>.rb
The contents of this migration will be:
classAddCreatedByAndUpdatedByTo<model-name> <ActiveRecord::Migration[5.0]defchangeadd_column:<model-name>,:created_by,:integer,null:falseadd_column:<model-name>,:updated_by,:integer,null:falseadd_index:<model-name>,:created_byadd_index:<model-name>,:updated_byendend
In yourApplicationController
add abefore_action
:
classApplicationControllerbefore_action:set_douglas_the_stamperend
This will enableDouglas
to know the current user that peroforms the action (create/update) on an model object.
In the model (that you ran the generate task for), add this:
classUser <ApplicationRecordhas_stampsend
This will populat thecreated_by
andupdated_by
attributes accordintly. Underthe hood this injects callbacks that are executed before create and update.
Since the{created,updated}_by
columns cannot benull
it means that theDouglas.the_stamper
has to always be set to a value. This is normally done via the controllerbefore_filter
method, but in console you have to use theDouglas.with_stamper
method. This method expects two arguments, an identifier of the user that does the action and a block. This means that the user will be logged as the person that has done the actions within the block.
For example:
>> Douglas.with_stamper(123) do?> p = Post.first>> p.body = 'Lorem ipsum dolor sit amet''>> p.save>> end
ThePost
will be updated, and theupdated_by
attribute will be set to the first passed argument -123
.
- Add migrations generator (#1)
- Find way to hijack
current_user
and store inRequestStore
- Find way to plug-in
Douglas
in model lifecycle - Improve README quality
- Add tests
- Make logged attribute (currently
id
) to be configurable
After checking out the repo, runbin/setup
to install dependencies. Then, runrake spec
to run the tests. You can also runbin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, runbundle exec rake install
. To release a new version, update the version number inversion.rb
, and then runbundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the.gem
file torubygems.org.
Bug reports and pull requests are welcome on GitHub athttps://github.com/[USERNAME]/douglas. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to theContributor Covenant code of conduct.
Named afterDouglas "Doug"Stamper,President Frank Underwood's White House Chief of Staff and former director of strategy.
The gem is available as open source under the terms of theMIT License.