Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

🕐 Douglas adds stamps to any ActiveRecord model

License

NotificationsYou must be signed in to change notification settings

fteem/douglas

Repository files navigation

Active Development

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.

Installation

Add this line to your application's Gemfile:

gem'douglas'

And then execute:

$ bundle

Usage

Built-in generator

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

Plug it into a controller

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.

Use it in models

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.

Use it in console

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.

Progress

  • Add migrations generator (#1)
  • Find way to hijackcurrent_user and store inRequestStore
  • Find way to plug-inDouglas in model lifecycle
  • Improve README quality
  • Add tests
  • Make logged attribute (currentlyid) to be configurable

Development

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.

Contributing

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.

Why Douglas?

Named afterDouglas "Doug"Stamper,President Frank Underwood's White House Chief of Staff and former director of strategy.

License

The gem is available as open source under the terms of theMIT License.

About

🕐 Douglas adds stamps to any ActiveRecord model

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

[8]ページ先頭

©2009-2025 Movatter.jp