mongoid-userstamps 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f50851182f67227d7ce48d89b444cc6d0404cef2
4
+ data.tar.gz: 951bff1d14fce51b98ff36e2203d0e7cc42f23ed
5
+ SHA512:
6
+ metadata.gz: 623715ef2edd2124ebb2b2886c7c8e82256a211157c851dec91b11ad6054faaadc069e33f47c4a12156d206bad213a7b470c0f7b191fcb88e7a87ff1a98f168e
7
+ data.tar.gz: 5c3ed62b39cd302a306956971407a8948883825fae1d963741083f75c6b16a4cfa40c88fbde5f196ba5f78c9387837611af537712762858747820b861c96f85b
data/.gitignore ADDED
@@ -0,0 +1,13 @@
1
+ .DS_Store
2
+
3
+ /.idea
4
+ /.yardoc
5
+ /.bundle
6
+
7
+ /pkg
8
+ /doc
9
+
10
+ /.ruby-version
11
+ /.rbenv-version
12
+ /*.gem
13
+ /Gemfile.lock
data/CHANGELOG.md ADDED
@@ -0,0 +1,66 @@
1
+ # Changelog
2
+
3
+ ## [0.4.1](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.4.1) - 2014-08-21
4
+
5
+ * Improvement
6
+ * Do not overwrite the `updater` if has been manually set (i.e. is in a dirty state.)
7
+ * Add specs for Rails 4/Mongoid 4
8
+
9
+ * Documentation
10
+ * Document `User.do_as` feature.
11
+
12
+
13
+ ## [0.4.0](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.4.0) - 2014-02-24
14
+
15
+ * Improvement
16
+ * BREAKING: Change userstamp keys to use Mongoid relations. This will change the underlying database field names, and as such will require a migration.
17
+ * BREAKING: Do not include Mongoid::Userstamp in "User" class by default.
18
+ * Mongoid::Userstamp config initializer is now optional.
19
+ * Add support for multiple user classes.
20
+ * Add class-level config override capability for both users and userstamped model classes.
21
+ * Add automatic support for `RequestStore` gem as drop-in replacement for `Thread.current`
22
+ * Refactor
23
+ * DEPRECATION: `created_column` config is now `created_name`
24
+ * DEPRECATION: `created_updated` config is now `created_updated`
25
+ * DEPRECATION: `user_model` config is no longer used. Instead, include Mongoid::Userstamp::User in your user model.
26
+ * 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.
27
+
28
+ ## [0.3.2](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.3.2) - 2014-01-12
29
+
30
+ * Fix bad gem release
31
+
32
+ ## [0.3.1](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.3.1) - 2014-01-11
33
+
34
+ * Improvement
35
+ * Remove autoload and replace with require in gem root lib file
36
+ * Add log warning for `#configure` deprecation
37
+
38
+ * Admin
39
+ * Remove Jeweler dependency, replace with gem-release gem
40
+ * Add Gemfile.lock to .gitignore (best practice for gems)
41
+ * Simplify version file
42
+
43
+ ## [0.3.0](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.3.0) - 2013-10-02
44
+
45
+ * Improvement
46
+ * BREAKING: Default value for `updated_accessor` was `:updator`, is now `:updater` (@johnnyshields)
47
+ * DEPRECATION: `#configure` is now an alias for `#config` and is deprecated (@johnnyshields)
48
+ * Replace `Mongoid::Userstamp.configuration` and `Mongoid::Userstamp.configure` with `#config` (@johnnyshields)
49
+ * Replace usages of `id` with `_id`, since there are many gems which override the behavior of `id` (@johnnyshields)
50
+ * Do not overwrite creator if it has already been set manually (@johnnyshields)
51
+ * Define setter methods for updated_accessor/created_accessor which assign the user id. Accepts both `Mongoid::Document` and `BSON::ObjectID` types (@johnnyshields)
52
+ * Allow pass-in of Mongoid field options for updated_column/created_column. Useful to set field aliases, default value, etc. (@johnnyshields)
53
+ * Expose current_user as `Mongoid::Userstamp.current_user` (@johnnyshields)
54
+ * Field type is now `::Moped::BSON::ObjectID` instead of Object (@johnnyshields)
55
+ * Query for creator/updater user using `unscoped` (@johnnyshields)
56
+ * Improve code readability (@johnnyshields)
57
+ * Improve test coverage, including adding tests for config (@johnnyshields)
58
+
59
+ * Bugfix
60
+ * Catch error if creator/updater has been deleted and return nil (@johnnyshields)
61
+
62
+ ## [0.2.1](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.2.1) - 2013-03-24
63
+
64
+ * Improvement
65
+ * Added some specs for test coverage (@johnnyshields)
66
+ * Removed dependecy for `Mongoid::Timestamps` (@johnnyshields)
data/Gemfile ADDED
@@ -0,0 +1,9 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gemspec
4
+
5
+ gem 'request_store', require: false
6
+
7
+ gem 'simplecov', :require => false
8
+ gem 'pry-nav', '~> 0.2.4'
9
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2012 Thomas Boerger Programmierung <http://www.tbpro.de>
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,185 @@
1
+ # Mongoid::Userstamps
2
+
3
+ Mongoid::Userstamps adds stamp fields for created by, updated by and optionally deleted by
4
+ information within a ruby application using Mongoid ORM.
5
+
6
+ This is a fork from [mongoid_userstamp](https://github.com/tbpro/mongoid_userstamp) but it
7
+ has been completely rewritten to provide with some new feature and improvements.
8
+
9
+ ## Install
10
+
11
+ ```ruby
12
+ gem 'mongoid-userstamps'
13
+ ```
14
+
15
+ ## Usage
16
+
17
+ Mongoid::Userstamps does the following:
18
+ * Defines Mongoid `belongs_to` relations to the user class for `created_by` and `updated_by` on each class where `Mongoid::Userstamps` is included
19
+ * Adds the possibility to define only `created_by` or `updated_by` by including `Mongoid::Userstamps::Created` or `Mongoid::Userstamps::Updated` respectively
20
+ * Automatically tracks the current user via a `before_filter` (see Rails Integration below)
21
+ * Sets the `created_by` and `updated_by` values in `before_create` and `before_save` callbacks respectively on the target models.
22
+ * Adds methods to the user class to check for the current user.
23
+ * Defines `belongs_to` relation to the user class for `deleted_by` when the class includes `Mongoid::Paranoia` or when `Mongoid::Userstamps::Deleted` is specifically included
24
+
25
+ ```ruby
26
+ # Default config (optional unless you want to customize the values)
27
+ Mongoid::Userstamps.config do |c|
28
+ c.user_reader = :current_user
29
+ c.created_name = :created_by
30
+ c.updated_name = :updated_by
31
+ c.deleted_name = :deleted_by
32
+ end
33
+
34
+ # Example model class
35
+ class Product
36
+ include Mongoid::Document
37
+ include Mongoid::Userstamps
38
+
39
+ # optional class-level config override
40
+ # mongoid_userstamp user_model: 'MyUser',
41
+ # created_name: :creator,
42
+ # updated_name: :updater,
43
+ end
44
+
45
+ # Example user class
46
+ class MyUser
47
+ include Mongoid::Document
48
+ include Mongoid::Userstamps::User
49
+
50
+ # optional class-level config override
51
+ # mongoid_userstamp_user reader: :current_my_user
52
+ end
53
+
54
+ # Create instance
55
+ p = Product.create
56
+
57
+ # Creator ObjectID | Updater ObjectID
58
+ p.created_by_id | p.updated_by_id
59
+ # => BSON::ObjectId('4f7c719f476da850ba000039')
60
+
61
+ # Creator instance | Updater instance
62
+ p.created_by | p.updated_by
63
+ # => <User _id: 4f7c719f476da850ba000039>
64
+
65
+ # Set creator/updater manually (usually not required)
66
+ p.created_by = MyUser.where(name: 'Will Smith')
67
+ p.updated_by = MyUser.where(name: 'DJ Jazzy Jeff')
68
+ ```
69
+
70
+
71
+ ## Preservation of Manually-set Values
72
+
73
+ Mongoid::Userstamps will not overwrite manually set values in the creator and updater fields. Specifically:
74
+
75
+ * The creator is only set during the creation of new models (`before_create` callback). Mongoid::Userstamps will not
76
+ overwrite the creator field if it already contains a value (i.e. was manually set.)
77
+ * The updater is set each time the model is saved (`before_save` callback), which includes the initial
78
+ creation. Mongoid::Userstamps will not overwrite the updater field if it been modified since the last save, as
79
+ per Mongoid's built-in "dirty tracking" feature.
80
+ * The same logic goes for the deleter field if `Mongoid::Userstamps::Deleted` is included.
81
+
82
+
83
+ ## Rails Integration
84
+
85
+ Popular Rails authentication frameworks such as Devise and Sorcery make a `current_user` method available in
86
+ your Controllers. Mongoid::Userstamps will automatically use this to set its user reference in a `before_filter`
87
+ on each request. (You can set an alternative method name via the `user_reader` config.)
88
+
89
+ *Gotcha:* If you have special controller actions which change/switch the current user to a new user, you will
90
+ need to set `User.current = new_user` after the switch occurs.
91
+
92
+
93
+ ## Thread Safety
94
+
95
+ Mongoid::Userstamps stores all-related user variables in `Thread.current`. If the
96
+ [RequestStore](https://github.com/steveklabnik/request_store) gem is installed, Mongoid::Userstamps
97
+ will automatically store variables in the `RequestStore.store` instead. RequestStore is recommended
98
+ for threaded web servers like Thin or Puma.
99
+
100
+
101
+ ## Advanced Usage: Scoped Execution
102
+
103
+ It is possible to execute a block of code within the context of a given user as follows:
104
+
105
+ ```ruby
106
+ User.current = staff
107
+ User.current #=> staff
108
+
109
+ User.sudo(admin) do
110
+ my_model.save!
111
+ User.current #=> admin
112
+ end
113
+
114
+ User.current #=> staff
115
+ ```
116
+
117
+
118
+ ## Advanced Usage: Multiple User Classes
119
+
120
+ Most Rails apps use a single user model. However, Mongoid::Userstamps supports using multiple user models
121
+ at once, and will track a separate current_user for each class.
122
+
123
+ Please note that each model may subscribe to only one user type for its userstamps, set via the
124
+ `:user_model` option.
125
+
126
+ ```ruby
127
+ class Admin
128
+ include Mongoid::Document
129
+ include Mongoid::Userstamps::User
130
+
131
+ mongoid_userstamp_user reader: :current_admin
132
+ end
133
+
134
+ class Customer
135
+ include Mongoid::Document
136
+ include Mongoid::Userstamps::User
137
+
138
+ mongoid_userstamp_user reader: :current_customer
139
+ end
140
+
141
+ class Album
142
+ include Mongoid::Document
143
+ include Mongoid::Userstamps
144
+
145
+ mongoid_userstamp user_model: 'Customer'
146
+ end
147
+
148
+ class Label
149
+ include Mongoid::Document
150
+ include Mongoid::Userstamps
151
+
152
+ mongoid_userstamp user_model: 'Admin'
153
+ end
154
+
155
+ # Set current user for each type
156
+ Admin.current = Admin.where(name: 'Biz Markie')
157
+ Customer.current = Customer.where(name: 'Sir Mix-A-Lot')
158
+
159
+ # In your Controller action
160
+ album = Album.new('Baby Got Back Single')
161
+ album.save!
162
+ album.created_by.name #=> 'Sir Mix-A-Lot'
163
+
164
+ label = Label.new('Cold Chillin Records')
165
+ label.save!
166
+ label.created_by.name #=> 'Biz Markie'
167
+ ```
168
+
169
+ ## Contributing
170
+
171
+ Fork -> Patch -> Test -> Push -> Pull Request
172
+
173
+ Please use Ruby 1.9.3 hash syntax, as Mongoid 3 requires Ruby >= 1.9.3
174
+
175
+ ## Authors
176
+
177
+ * [Geoffroy Planquart](https://github.com/Aethelflaed)
178
+ * [Thomas Boerger](http://www.tbpro.de)
179
+ * [John Shields](https://github.com/johnnyshields)
180
+ * [Bharat Gupta](https://github.com/Bharat311)
181
+
182
+ ## Copyright
183
+
184
+ Licensed under the MIT License (MIT). Refer to LICENSE for details.
185
+
data/Rakefile ADDED
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env rake
2
+ require 'bundler/gem_tasks'
3
+ require 'rake/testtask'
4
+
5
+ desc 'Default: run unit tests'
6
+ task :default => :test
7
+
8
+ desc 'Run tests'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
data/init.rb ADDED
@@ -0,0 +1,2 @@
1
+ # -*- encoding : utf-8 -*-
2
+ require 'mongoid_userstamp'
@@ -0,0 +1,43 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module Mongoid
4
+ module Userstamps
5
+ module Config
6
+ class Model
7
+ attr_accessor :created_name
8
+ attr_accessor :updated_name
9
+ attr_accessor :deleted_name
10
+ attr_accessor :user_model
11
+
12
+ def initialize(opts = {})
13
+ @created_name = opts[:created_name]
14
+ @updated_name = opts[:updated_name]
15
+ @user_model = opts[:user_model]
16
+ end
17
+
18
+ def created_name
19
+ @created_name || Mongoid::Userstamps::Config.created_name
20
+ end
21
+
22
+ def updated_name
23
+ @updated_name || Mongoid::Userstamps::Config.updated_name
24
+ end
25
+
26
+ def deleted_name
27
+ @deleted_name || Mongoid::Userstamps::Config.deleted_name
28
+ end
29
+
30
+ def user_model
31
+ @user_model || Mongoid::Userstamps::Config.user_classes.first.to_s
32
+ end
33
+ def user_model=(value)
34
+ @user_model = value.to_s if value
35
+ end
36
+ def user_model_defined?
37
+ !!@user_model
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module Mongoid
4
+ module Userstamps
5
+ module Config
6
+ class User
7
+ attr_accessor :reader
8
+
9
+ def initialize(opts = {})
10
+ @reader = opts[:reader]
11
+ end
12
+
13
+ def reader
14
+ @reader || Mongoid::Userstamps::Config.user_reader
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,79 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ require 'mongoid/userstamps/config/model'
4
+ require 'mongoid/userstamps/config/user'
5
+
6
+ module Mongoid
7
+ module Userstamps
8
+ module Config
9
+ class << self
10
+ attr_accessor :created_name
11
+ attr_accessor :updated_name
12
+ attr_accessor :deleted_name
13
+ attr_accessor :user_reader
14
+
15
+ def eval
16
+ instance_eval(&Proc.new)
17
+ end
18
+
19
+ def user_classes
20
+ @user_classes ||= []
21
+ end
22
+
23
+ def add_user_class(klass)
24
+ user_classes << klass
25
+ model_classes.each do |model|
26
+ config = model.userstamps_model
27
+ if !config.user_model_defined?
28
+ model.relations[config.created_name.to_s].try(:[]=, :class_name, config.user_model)
29
+ end
30
+ end
31
+ end
32
+
33
+ def model_classes
34
+ @model_classes ||= []
35
+ end
36
+
37
+ def add_model_class(klass)
38
+ model_classes << klass
39
+ end
40
+
41
+ def created_name
42
+ @created_name ||= :created_by
43
+ end
44
+
45
+ def updated_name
46
+ @updated_name ||= :updated_by
47
+ end
48
+
49
+ def deleted_name
50
+ @deleted_name ||= :deleted_by
51
+ end
52
+
53
+ def user_reader
54
+ @user_reader ||= :current_user
55
+ end
56
+
57
+ def current_user(user_class)
58
+ store(user_class)
59
+ end
60
+
61
+ def set_current_user(user_class, value)
62
+ store(user_class, value)
63
+ end
64
+
65
+ def store(model, value = false)
66
+ return if !model
67
+ key = "mongoid-userstamps/#{model.to_s.underscore}".to_sym
68
+ store = defined?(RequestStore) ? RequestStore.store : Thread.current
69
+ if value == false
70
+ store[key]
71
+ else
72
+ store[key] = value
73
+ end
74
+ end
75
+ end
76
+ end
77
+ end
78
+ end
79
+
@@ -0,0 +1,29 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module Mongoid
4
+ module Userstamps
5
+ module Created
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ if !self.included_modules.include?(Mongoid::Userstamps::Model)
10
+ include Mongoid::Userstamps::Model
11
+ end
12
+
13
+ belongs_to userstamps_model.created_name, {
14
+ class_name: userstamps_model.user_model,
15
+ inverse_of: nil
16
+ }
17
+
18
+ set_callback :create, :before, :set_created_by
19
+ end
20
+
21
+ def set_created_by
22
+ user = self.class.current_user
23
+ return if !user || self.public_send(userstamps_model.created_name)
24
+ self.public_send("#{userstamps_model.created_name}=", user)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,29 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module Mongoid
4
+ module Userstamps
5
+ module Deleted
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ if !self.included_modules.include?(Mongoid::Userstamps::Model)
10
+ include Mongoid::Userstamps::Model
11
+ end
12
+
13
+ belongs_to userstamps_model.deleted_name, {
14
+ class_name: userstamps_model.user_model,
15
+ inverse_of: nil
16
+ }
17
+
18
+ set_callback :destroy, :before, :set_deleted_by
19
+ end
20
+
21
+ def set_deleted_by
22
+ user = self.class.current_user
23
+ return if !user || self.public_send("#{userstamps_model.deleted_name}_id_changed?")
24
+ self.public_send("#{userstamps_model.deleted_name}=", user)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,26 @@
1
+ module Mongoid
2
+ module Userstamps
3
+ module Model
4
+ extend ActiveSupport::Concern
5
+
6
+ included do
7
+ Mongoid::Userstamps::Config.add_model_class(self)
8
+ end
9
+
10
+ def userstamps_model
11
+ self.class.userstamps_model
12
+ end
13
+
14
+ class_methods do
15
+ def userstamps_model(opts = {})
16
+ @userstamps_model ||= Mongoid::Userstamps::Config::Model.new(opts)
17
+ end
18
+
19
+ def current_user
20
+ Mongoid::Userstamps::Config.current_user(userstamps_model.user_model)
21
+ end
22
+ end
23
+ end
24
+ end
25
+ end
26
+
@@ -0,0 +1,20 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module Mongoid
4
+ module Userstamps
5
+ class Railtie < Rails::Railtie
6
+ # Set current_user from controller reader method
7
+ ActiveSupport.on_load :action_controller do
8
+ before_filter do |c|
9
+ Mongoid::Userstamps::Config.user_classes.each do |user_class|
10
+ begin
11
+ user_class.current = c.public_send(user_class.userstamps_user.reader)
12
+ rescue
13
+ end
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
20
+
@@ -0,0 +1,29 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module Mongoid
4
+ module Userstamps
5
+ module Updated
6
+ extend ActiveSupport::Concern
7
+
8
+ included do
9
+ if !self.included_modules.include?(Mongoid::Userstamps::Model)
10
+ include Mongoid::Userstamps::Model
11
+ end
12
+
13
+ belongs_to userstamps_model.updated_name, {
14
+ class_name: userstamps_model.user_model,
15
+ inverse_of: nil
16
+ }
17
+
18
+ set_callback :save, :before, :set_updated_by
19
+ end
20
+
21
+ def set_updated_by
22
+ user = self.class.current_user
23
+ return if !user || self.public_send("#{userstamps_model.updated_name}_id_changed?")
24
+ self.public_send("#{userstamps_model.updated_name}=", user)
25
+ end
26
+ end
27
+ end
28
+ end
29
+
@@ -0,0 +1,43 @@
1
+ # -*- encoding : utf-8 -*-
2
+ module Mongoid
3
+ module Userstamps
4
+ module User
5
+ extend ActiveSupport::Concern
6
+
7
+ included do
8
+ Mongoid::Userstamps::Config.add_user_class(self)
9
+ end
10
+
11
+ def current?
12
+ self.id == self.class.current.try(:id)
13
+ end
14
+
15
+ def userstamps_user
16
+ self.class.userstamps_user
17
+ end
18
+
19
+ class_methods do
20
+ def current
21
+ Mongoid::Userstamps::Config.current_user(self)
22
+ end
23
+
24
+ def current=(value)
25
+ Mongoid::Userstamps::Config.set_current_user(self, value)
26
+ end
27
+
28
+ def sudo(user)
29
+ old = self.current
30
+ self.current = user
31
+ yield
32
+ ensure
33
+ self.current = old
34
+ end
35
+
36
+ def userstamps_user(opts = {})
37
+ @userstamps_user ||= Mongoid::Userstamps::Config::User.new(opts)
38
+ end
39
+ end
40
+ end
41
+ end
42
+ end
43
+
@@ -0,0 +1,8 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module Mongoid
4
+ module Userstamps
5
+ VERSION = '1.0.0'
6
+ end
7
+ end
8
+