mongoid_userstamp 0.3.2 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/CHANGELOG.md +15 -0
- data/README.md +104 -38
- data/lib/mongoid/userstamp.rb +41 -52
- data/lib/mongoid/userstamp/config/gem_config.rb +38 -0
- data/lib/mongoid/userstamp/config/model_config.rb +27 -0
- data/lib/mongoid/userstamp/config/user_config.rb +17 -0
- data/lib/mongoid/userstamp/mixins/model.rb +41 -0
- data/lib/mongoid/userstamp/mixins/user.rb +46 -0
- data/lib/mongoid/userstamp/railtie.rb +29 -11
- data/lib/mongoid/userstamp/version.rb +2 -1
- data/lib/mongoid_userstamp.rb +6 -2
- data/mongoid_userstamp.gemspec +3 -2
- data/spec/spec_helper.rb +5 -6
- data/spec/support/admin.rb +7 -0
- data/spec/support/book.rb +3 -1
- data/spec/support/post.rb +11 -0
- data/spec/unit/gem_config_spec.rb +46 -0
- data/spec/unit/model_config_spec.rb +31 -0
- data/spec/unit/model_spec.rb +157 -0
- data/spec/unit/user_config_spec.rb +23 -0
- data/spec/unit/user_spec.rb +95 -0
- data/spec/unit/userstamp_spec.rb +120 -0
- metadata +41 -9
- data/lib/mongoid/userstamp/config.rb +0 -30
- data/lib/mongoid/userstamp/user.rb +0 -37
- data/spec/mongoid/userstamp_spec.rb +0 -163
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjBmOTVmOWY3YzY2NzE4NGM3MjgwM2U5ODE1MGIzZmU2YTA1ZWM3Ng==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
M2NmYjY1MjIyNDk5ZjI2YmY3YWJlMzEyNjgyOGJjMzE4NDcyZTI3OA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OGVjMWVjYzZlMDNkMTgzOTQ4MDc5Y2I1YmI3MGRiYmNlMTYyY2Y2YzNiNzcy
|
10
|
+
MjlhZWZjMDk1ZWVkZTI5ZWNlZmE4NGU4ZDZjM2I1YjhhOGNjZGJjMDVmMTNj
|
11
|
+
YzY5ZmJhZGZkZTBjYjdkZDEzZmJhOTU3MjgzZDNlNWYwY2RiY2E=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
N2U4YzlmZDNkZmJhYmM4NGFiZTZhNTVhYWQzMWNiZmJiYWJhMDk4OGFkMTMz
|
14
|
+
ZTE3MTBlNjIxZTViNzYzZjM0M2Y4M2I2YjY3MzE5OTliYjkyYmZmM2M2MGIz
|
15
|
+
NmNhYmNiN2FjMTNiN2QwOWU0YzY0MzZkY2NmYTEzYjk2NzZkNmI=
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,20 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## [0.4.0](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.4.0) - 2014-02-24
|
4
|
+
|
5
|
+
* Improvement
|
6
|
+
* BREAKING: Change userstamp keys to use Mongoid relations. This will change the underlying database field names, and as such will require a migration.
|
7
|
+
* BREAKING: Do not include Mongoid::Userstamp in "User" class by default.
|
8
|
+
* Mongoid::Userstamp config initializer is now optional.
|
9
|
+
* Add support for multiple user classes.
|
10
|
+
* Add class-level config override capability for both users and userstamped model classes.
|
11
|
+
* Add automatic support for `RequestStore` gem as drop-in replacement for `Thread.current`
|
12
|
+
* Refactor
|
13
|
+
* DEPRECATION: `created_column` config is now `created_name`
|
14
|
+
* DEPRECATION: `created_updated` config is now `created_updated`
|
15
|
+
* DEPRECATION: `user_model` config is no longer used. Instead, include Mongoid::Userstamp::User in your user model.
|
16
|
+
* Substantial refactoring of all classes and test cases. Among other changes, all access to `Thread.current` variables is now done in the Mongoid::Userstamp module singleton.
|
17
|
+
|
3
18
|
## [0.3.2](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.3.2) - 2014-01-12
|
4
19
|
|
5
20
|
* Fix bad gem release
|
data/README.md
CHANGED
@@ -1,11 +1,11 @@
|
|
1
|
-
#
|
1
|
+
# Mongoid::Userstamp [![Build Status](https://secure.travis-ci.org/tbpro/mongoid_userstamp.png)](https://travis-ci.org/tbpro/mongoid_userstamp) [![Code Climate](https://codeclimate.com/github/tbpro/mongoid_userstamp.png)](https://codeclimate.com/github/tbpro/mongoid_userstamp)
|
2
2
|
|
3
|
-
|
3
|
+
Mongoid::Userstamp adds stamp columns for created by and updated by
|
4
4
|
information within Rails applications using Mongoid ORM.
|
5
5
|
|
6
6
|
## Version Support
|
7
7
|
|
8
|
-
|
8
|
+
Mongoid::Userstamp is tested on the following versions:
|
9
9
|
|
10
10
|
* Ruby 1.9.3 and 2.0.0
|
11
11
|
* Rails 3
|
@@ -19,59 +19,124 @@ MongoidUserstamp is tested on the following versions:
|
|
19
19
|
|
20
20
|
## Usage
|
21
21
|
|
22
|
+
Mongoid::Userstamp does the following:
|
23
|
+
* Defines Mongoid `belongs_to` relations to the user class for `created_by` and `updated_by` on each class where `Mongoid::Userstamp` is included
|
24
|
+
* Automatically tracks the current user via a `before_filter` (see Rails Integration below)
|
25
|
+
* Sets the `created_by` and `updated_by` values in `before_save` and `before_update` callbacks respectively on the target models.
|
26
|
+
* Adds methods to the user class to check for the current user.
|
27
|
+
|
22
28
|
```ruby
|
23
|
-
# Default config
|
29
|
+
# Default config (optional unless you want to customize the values)
|
24
30
|
Mongoid::Userstamp.config do |c|
|
25
|
-
|
26
|
-
# Default config values
|
27
|
-
|
28
31
|
c.user_reader = :current_user
|
29
|
-
c.
|
30
|
-
|
31
|
-
c.created_column = :created_by
|
32
|
-
c.created_accessor = :creator
|
33
|
-
|
34
|
-
c.updated_column = :updated_by
|
35
|
-
c.updated_accessor = :updater
|
36
|
-
|
37
|
-
# Optional config values
|
38
|
-
|
39
|
-
# c.created_alias = :c
|
40
|
-
# c.updated_alias = :u
|
32
|
+
c.created_name = :created_by
|
33
|
+
c.updated_name = :updated_by
|
41
34
|
end
|
42
35
|
|
43
|
-
# Example model
|
44
|
-
class
|
36
|
+
# Example model class
|
37
|
+
class Product
|
45
38
|
include Mongoid::Document
|
46
39
|
include Mongoid::Userstamp
|
40
|
+
|
41
|
+
# optional class-level config override
|
42
|
+
# mongoid_userstamp user_model: 'MyUser',
|
43
|
+
# created_name: :creator,
|
44
|
+
# updated_name: :updater,
|
47
45
|
end
|
48
46
|
|
47
|
+
# Example user class (you can skip this step you have a single user class is named "User")
|
48
|
+
class MyUser
|
49
|
+
include Mongoid::Document
|
50
|
+
include Mongoid::Userstamp::User
|
51
|
+
|
52
|
+
# optional class-level config override
|
53
|
+
# mongoid_userstamp_user reader: :current_my_user
|
54
|
+
end
|
55
|
+
|
49
56
|
# Create instance
|
50
|
-
p =
|
57
|
+
p = Product.create
|
51
58
|
|
52
|
-
# Updater ObjectID
|
53
|
-
p.
|
59
|
+
# Creator ObjectID | Updater ObjectID
|
60
|
+
p.created_by_id | p.updated_by_id
|
54
61
|
# => BSON::ObjectId('4f7c719f476da850ba000039')
|
55
62
|
|
56
|
-
# Updater instance
|
57
|
-
p.
|
63
|
+
# Creator instance | Updater instance
|
64
|
+
p.created_by | p.updated_by
|
58
65
|
# => <User _id: 4f7c719f476da850ba000039>
|
59
66
|
|
60
|
-
# Set updater manually (usually not required)
|
61
|
-
p.
|
62
|
-
|
67
|
+
# Set creator/updater manually (usually not required)
|
68
|
+
p.created_by = MyUser.where(name: 'Will Smith')
|
69
|
+
p.updated_by = MyUser.where(name: 'DJ Jazzy Jeff')
|
70
|
+
```
|
63
71
|
|
64
|
-
# Creator ObjectID or nil
|
65
|
-
p.created_by
|
66
|
-
# => BSON::ObjectId('4f7c719f476da850ba000039')
|
67
72
|
|
68
|
-
|
69
|
-
|
70
|
-
|
73
|
+
## Rails Integration
|
74
|
+
|
75
|
+
Popular Rails authentication frameworks such as Devise and Sorcery make a `current_user` method available in
|
76
|
+
your Controllers. Mongoid::Userstamp will automatically use this to set its user reference in a `before_filter`
|
77
|
+
on each request. (You can set an alternative method name via the `user_reader` config.)
|
78
|
+
|
79
|
+
*Gotcha:* If you have special controller actions which change/switch the current user to a new user, you will
|
80
|
+
need to set `User.current = new_user` after the switch occurs.
|
81
|
+
|
82
|
+
|
83
|
+
## Thread Safety
|
84
|
+
|
85
|
+
Mongoid::Userstamp stores all-related user variables in `Thread.current`. If the
|
86
|
+
[RequestStore](https://github.com/steveklabnik/request_store) gem is installed, Mongoid::Userstamp
|
87
|
+
will automatically store variables in the `RequestStore.store` instead. RequestStore is recommended
|
88
|
+
for threaded web servers like Thin or Puma.
|
89
|
+
|
90
|
+
|
91
|
+
## Advanced Usage: Multiple User Classes
|
92
|
+
|
93
|
+
Most Rails apps use a single user model. However, Mongoid::Userstamp supports using multiple user models
|
94
|
+
at once, and will track a separate current_user for each class.
|
95
|
+
|
96
|
+
Please note that each model may subscribe to only one user type for its userstamps, set via the
|
97
|
+
`:user_model` option.
|
98
|
+
|
99
|
+
```ruby
|
100
|
+
class Admin
|
101
|
+
include Mongoid::Document
|
102
|
+
include Mongoid::Userstamp::User
|
103
|
+
|
104
|
+
mongoid_userstamp_user reader: :current_admin
|
105
|
+
end
|
106
|
+
|
107
|
+
class Customer
|
108
|
+
include Mongoid::Document
|
109
|
+
include Mongoid::Userstamp::User
|
110
|
+
|
111
|
+
mongoid_userstamp_user reader: :current_customer
|
112
|
+
end
|
113
|
+
|
114
|
+
class Album
|
115
|
+
include Mongoid::Document
|
116
|
+
include Mongoid::Userstamp
|
117
|
+
|
118
|
+
mongoid_userstamp user_model: 'Customer'
|
119
|
+
end
|
120
|
+
|
121
|
+
class Label
|
122
|
+
include Mongoid::Document
|
123
|
+
include Mongoid::Userstamp
|
124
|
+
|
125
|
+
mongoid_userstamp user_model: 'Admin'
|
126
|
+
end
|
127
|
+
|
128
|
+
# Set current user for each type
|
129
|
+
Admin.current = Admin.where(name: 'Biz Markie')
|
130
|
+
Customer.current = Customer.where(name: 'Sir Mix-A-Lot')
|
131
|
+
|
132
|
+
# In your Controller action
|
133
|
+
album = Album.new('Baby Got Back Single')
|
134
|
+
album.save!
|
135
|
+
album.created_by.name #=> 'Sir Mix-A-Lot'
|
71
136
|
|
72
|
-
|
73
|
-
|
74
|
-
|
137
|
+
label = Label.new('Cold Chillin Records')
|
138
|
+
label.save!
|
139
|
+
label.created_by.name #=> 'Biz Markie'
|
75
140
|
```
|
76
141
|
|
77
142
|
## Contributing
|
@@ -84,6 +149,7 @@ Please use Ruby 1.9.3 hash syntax, as Mongoid 3 requires Ruby >= 1.9.3
|
|
84
149
|
|
85
150
|
* [Thomas Boerger](http://www.tbpro.de)
|
86
151
|
* [John Shields](https://github.com/johnnyshields)
|
152
|
+
* [Bharat Gupta](https://github.com/Bharat311)
|
87
153
|
|
88
154
|
## Copyright
|
89
155
|
|
data/lib/mongoid/userstamp.rb
CHANGED
@@ -1,88 +1,77 @@
|
|
1
1
|
# -*- encoding : utf-8 -*-
|
2
2
|
|
3
3
|
module Mongoid
|
4
|
+
|
4
5
|
module Userstamp
|
5
6
|
extend ActiveSupport::Concern
|
6
7
|
|
7
8
|
included do
|
8
|
-
|
9
|
-
|
9
|
+
Mongoid::Userstamp.add_model_class(self)
|
10
|
+
end
|
10
11
|
|
11
|
-
|
12
|
-
before_create :set_creator
|
12
|
+
module ClassMethods
|
13
13
|
|
14
|
-
|
15
|
-
|
14
|
+
def mongoid_userstamp(opts = {})
|
15
|
+
mongoid_userstamp_config(opts)
|
16
|
+
self.send(:include, Mongoid::Userstamp::Model) unless self.included_modules.include?(Mongoid::Userstamp::Model)
|
16
17
|
end
|
17
18
|
|
18
|
-
|
19
|
-
Userstamp.
|
19
|
+
def mongoid_userstamp_config(opts = {})
|
20
|
+
@mongoid_userstamp_config ||= Mongoid::Userstamp::ModelConfig.new(opts)
|
20
21
|
end
|
22
|
+
end
|
21
23
|
|
22
|
-
|
23
|
-
self.send("#{Userstamp.config.updated_column}=", Userstamp.extract_bson_id(user))
|
24
|
-
end
|
24
|
+
class << self
|
25
25
|
|
26
|
-
|
27
|
-
|
26
|
+
def config(&block)
|
27
|
+
@config ||= Mongoid::Userstamp::GemConfig.new(&block)
|
28
28
|
end
|
29
29
|
|
30
|
-
|
30
|
+
# @deprecated
|
31
|
+
def configure(&block)
|
32
|
+
warn 'Mongoid::Userstamp.configure is deprecated. Please use Mongoid::Userstamp.config instead'
|
33
|
+
config(&block)
|
34
|
+
end
|
31
35
|
|
32
|
-
def
|
33
|
-
|
34
|
-
|
36
|
+
def current_user(user_class = nil)
|
37
|
+
user_class ||= user_classes.first
|
38
|
+
store[userstamp_key(user_class)]
|
35
39
|
end
|
36
40
|
|
37
|
-
def
|
38
|
-
|
39
|
-
self.send("#{Userstamp.config.created_accessor}=", Userstamp.current_user)
|
41
|
+
def current_user=(value)
|
42
|
+
set_current_user(value)
|
40
43
|
end
|
41
|
-
end
|
42
44
|
|
43
|
-
|
44
|
-
def
|
45
|
-
|
46
|
-
|
47
|
-
else
|
48
|
-
@@config ||= Userstamp::Config.new
|
49
|
-
end
|
45
|
+
# It is better to provide the user class, in case the value is nil.
|
46
|
+
def set_current_user(value, user_class = nil)
|
47
|
+
user_class ||= value ? value.class : user_classes.first
|
48
|
+
store[userstamp_key(user_class)] = value
|
50
49
|
end
|
51
50
|
|
52
|
-
|
53
|
-
|
54
|
-
warn 'Mongoid::Userstamp.configure is deprecated. Please use Mongoid::Userstamp.config instead'
|
55
|
-
config(block)
|
51
|
+
def model_classes
|
52
|
+
(@model_classes || []).map{|c| c.is_a?(Class) ? c : c.to_s.classify.constantize }
|
56
53
|
end
|
57
54
|
|
58
|
-
def
|
59
|
-
|
55
|
+
def add_model_class(model)
|
56
|
+
@model_classes ||= []
|
57
|
+
@model_classes << model
|
60
58
|
end
|
61
59
|
|
62
|
-
def
|
63
|
-
|
60
|
+
def user_classes
|
61
|
+
(@user_classes || []).map{|c| c.is_a?(Class) ? c : c.to_s.classify.constantize }
|
64
62
|
end
|
65
63
|
|
66
|
-
def
|
67
|
-
|
64
|
+
def add_user_class(user)
|
65
|
+
@user_classes ||= []
|
66
|
+
@user_classes << user
|
68
67
|
end
|
69
68
|
|
70
|
-
def
|
71
|
-
|
72
|
-
value.try(:_id)
|
73
|
-
elsif value.present?
|
74
|
-
::Moped::BSON::ObjectId.from_string(value.to_s)
|
75
|
-
else
|
76
|
-
nil
|
77
|
-
end
|
69
|
+
def userstamp_key(model)
|
70
|
+
"mongoid_userstamp/#{model.to_s.underscore}".to_sym
|
78
71
|
end
|
79
72
|
|
80
|
-
def
|
81
|
-
|
82
|
-
user_id ? Userstamp.config.user_model.unscoped.find(user_id) : nil
|
83
|
-
rescue Mongoid::Errors::DocumentNotFound => e
|
84
|
-
nil
|
85
|
-
end
|
73
|
+
def store
|
74
|
+
defined?(RequestStore) ? RequestStore.store : Thread.current
|
86
75
|
end
|
87
76
|
end
|
88
77
|
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module Userstamp
|
5
|
+
|
6
|
+
class GemConfig
|
7
|
+
|
8
|
+
attr_accessor :created_name
|
9
|
+
attr_accessor :updated_name
|
10
|
+
attr_accessor :user_reader
|
11
|
+
|
12
|
+
def initialize(&block)
|
13
|
+
@created_name = :created_by
|
14
|
+
@updated_name = :updated_by
|
15
|
+
@user_reader = :current_user
|
16
|
+
|
17
|
+
instance_eval(&block) if block_given?
|
18
|
+
end
|
19
|
+
|
20
|
+
# @deprecated
|
21
|
+
def user_model=(value)
|
22
|
+
warn 'Mongoid::Userstamp `user_model` config is removed as of v0.4.0. If using a model named other than `User`, please include `Mongoid::Userstamp::User` in your user model instead.'
|
23
|
+
end
|
24
|
+
|
25
|
+
# @deprecated
|
26
|
+
def created_column=(value)
|
27
|
+
warn 'Mongoid::Userstamp `created_column` is deprecated as of v0.4.0. Please use `created_name` instead.'
|
28
|
+
@created_name = value
|
29
|
+
end
|
30
|
+
|
31
|
+
# @deprecated
|
32
|
+
def updated_column=(value)
|
33
|
+
warn 'Mongoid::Userstamp `created_column` is deprecated as of v0.4.0. Please use `created_name` instead.'
|
34
|
+
@updated_name = value
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module Userstamp
|
5
|
+
|
6
|
+
class ModelConfig
|
7
|
+
|
8
|
+
def initialize(opts = {})
|
9
|
+
@user_model = opts.delete(:user_model)
|
10
|
+
@created_name = opts.delete(:created_name)
|
11
|
+
@updated_name = opts.delete(:updated_name)
|
12
|
+
end
|
13
|
+
|
14
|
+
def user_model
|
15
|
+
@user_model || Mongoid::Userstamp.user_classes.first
|
16
|
+
end
|
17
|
+
|
18
|
+
def created_name
|
19
|
+
@created_name || Mongoid::Userstamp.config.created_name
|
20
|
+
end
|
21
|
+
|
22
|
+
def updated_name
|
23
|
+
@updated_name || Mongoid::Userstamp.config.updated_name
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module Userstamp
|
5
|
+
|
6
|
+
class UserConfig
|
7
|
+
|
8
|
+
def initialize(opts = {})
|
9
|
+
@reader = opts.delete(:reader)
|
10
|
+
end
|
11
|
+
|
12
|
+
def reader
|
13
|
+
@reader || Mongoid::Userstamp.config.user_reader
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,41 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
module Mongoid
|
4
|
+
module Userstamp
|
5
|
+
|
6
|
+
module Model
|
7
|
+
|
8
|
+
extend ActiveSupport::Concern
|
9
|
+
|
10
|
+
included do
|
11
|
+
|
12
|
+
belongs_to mongoid_userstamp_config.created_name, class_name: mongoid_userstamp_config.user_model, inverse_of: nil
|
13
|
+
belongs_to mongoid_userstamp_config.updated_name, class_name: mongoid_userstamp_config.user_model, inverse_of: nil
|
14
|
+
|
15
|
+
before_create :set_created_by
|
16
|
+
before_save :set_updated_by
|
17
|
+
|
18
|
+
protected
|
19
|
+
|
20
|
+
def set_created_by
|
21
|
+
current_user = Mongoid::Userstamp.current_user(self.class.mongoid_userstamp_config.user_model)
|
22
|
+
return if current_user.blank? || self.send(self.class.mongoid_userstamp_config.created_name)
|
23
|
+
self.send("#{self.class.mongoid_userstamp_config.created_name}=", current_user)
|
24
|
+
end
|
25
|
+
|
26
|
+
def set_updated_by
|
27
|
+
current_user = Mongoid::Userstamp.current_user(self.class.mongoid_userstamp_config.user_model)
|
28
|
+
return if current_user.blank?
|
29
|
+
self.send("#{self.class.mongoid_userstamp_config.updated_name}=", current_user)
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
module ClassMethods
|
34
|
+
|
35
|
+
def current_user
|
36
|
+
Mongoid::Userstamp.current_user(mongoid_userstamp_config.user_model)
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|