Mongoid::Userstamps
Adds stamps relations to mongoid models.
Version Support
5.x, 6.x and 7.x
Installation
Add this line to your application's Gemfile:
gem 'mongoid-simple-userstamps'
And then execute:
$ bundle
Or install it yourself as:
$ gem install mongoid-simple-userstamps
Usage
- Include
Mongoid::Userstamps::Model
in your models you want user stamps. - Define current user throught
Mongoid::Userstamps::User.current
.
class User
include Mongoid::Document
end
class Post
include Mongoid::Document
include Mongoid::Userstamps::Model
field :title
end
user = User.create
Mongoid::Userstamps::User.current = user
post = Post.create
post.created_by # <User _id: 1 >
post.updated_by # nil
post.update(title: 'title')
post.updated_by # <User _id: 1 >
# set manually
other_user = User.create
post.update(updated_by: other_user)
post.updated_by # <User _id: 2 >
# skip userstamps
post.skip_userstamps.update(title: 'new_title')
post.updated_by # <User _id: 2 >
Preservation of Manually-set Values
-
The
creator
is only set during the creation of new models (before_create
callback). Mongoid::Userstamps will not overwrite thecreator
field if it already contains a value (i.e. was manually set.) -
The
updater
is set each time the model is updated (before_update
callback). Mongoid::Userstamps will not overwrite theupdater
field if it been modified since the last save, as per Mongoid's built-in "dirty tracking" feature.When the
updater
is the same and we need to preserve, we need to useskip_userstamps
method.
Rails
- Define current user in your
application_controller.rb
class ApplicationController < ActionController::Base
before_action :authenticate!
before_action :define_userstamps_current
protected
def define_userstamps_current
Mongoid::Userstamps::User.current = current_user
end
end
Warden
- Define current user in warden callback
Warden::Manager.after_set_user do |user, auth, opts|
Mongoid::Userstamps::User.current = record
end
Contributing
- Fork it ( http://github.com//mongoid-simple-userstamps/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request