glebtv_mongoid_userstamp 0.4.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 +7 -0
- data/.gitignore +13 -0
- data/.ruby-gemset +1 -0
- data/.travis.yml +20 -0
- data/CHANGELOG.md +41 -0
- data/Gemfile +3 -0
- data/LICENSE +22 -0
- data/README.md +92 -0
- data/Rakefile +13 -0
- data/lib/glebtv_mongoid_userstamp.rb +2 -0
- data/lib/mongoid/userstamp/config.rb +30 -0
- data/lib/mongoid/userstamp/railtie.rb +22 -0
- data/lib/mongoid/userstamp/user.rb +37 -0
- data/lib/mongoid/userstamp/version.rb +6 -0
- data/lib/mongoid/userstamp.rb +51 -0
- data/lib/mongoid_userstamp.rb +6 -0
- data/mongoid_mongoid_userstamp.gemspec +27 -0
- data/spec/mongoid/userstamp_spec.rb +158 -0
- data/spec/spec_helper.rb +29 -0
- data/spec/support/book.rb +7 -0
- data/spec/support/user.rb +7 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5d873bed4646aeb401c5a38861a424b2006b8772
|
4
|
+
data.tar.gz: 79534179cd97ff59c8007a72f1fe519f6b1c2eda
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 797365acd8a2be727c354256545ca8edc2b8a60f65e86fd6672fa4904a39efc2cc6beed6fcf799106cef63fb5168db708cd91237311a0a0b737e5291684b55a1
|
7
|
+
data.tar.gz: 8ae7c9dec30f9a1eb145989af3469adc9f51bd26bfec9be34a5e5f2ff4a4fc9d2f2617514cb73bdb5d41e5f2665cac013740af46fc6484eb5b271702f3d06cee
|
data/.gitignore
ADDED
data/.ruby-gemset
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
mongoid_userstamp
|
data/.travis.yml
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
language: ruby
|
2
|
+
rvm:
|
3
|
+
- 2.1.0
|
4
|
+
- 2.0.0
|
5
|
+
- 1.9.3
|
6
|
+
- ruby-head
|
7
|
+
- jruby
|
8
|
+
- jruby-19mode
|
9
|
+
- rbx
|
10
|
+
- rbx-19mode
|
11
|
+
|
12
|
+
matrix:
|
13
|
+
allow_failures:
|
14
|
+
- rvm: ruby-head
|
15
|
+
- rvm: jruby
|
16
|
+
- rvm: jruby-19mode
|
17
|
+
- rvm: rbx
|
18
|
+
- rvm: rbx-19mode
|
19
|
+
|
20
|
+
services: mongodb
|
data/CHANGELOG.md
ADDED
@@ -0,0 +1,41 @@
|
|
1
|
+
# Changelog
|
2
|
+
|
3
|
+
## [0.3.2](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.3.2) - 2014-01-12
|
4
|
+
|
5
|
+
* Fix bad gem release
|
6
|
+
|
7
|
+
## [0.3.1](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.3.1) - 2014-01-11
|
8
|
+
|
9
|
+
* Improvement
|
10
|
+
* Remove autoload and replace with require in gem root lib file
|
11
|
+
* Add log warning for `#configure` deprecation
|
12
|
+
|
13
|
+
* Admin
|
14
|
+
* Remove Jeweler dependency, replace with gem-release gem
|
15
|
+
* Add Gemfile.lock to .gitignore (best practice for gems)
|
16
|
+
* Simplify version file
|
17
|
+
|
18
|
+
## [0.3.0](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.3.0) - 2013-10-02
|
19
|
+
|
20
|
+
* Improvement
|
21
|
+
* BREAKING: Default value for `updated_accessor` was `:updator`, is now `:updater` (@johnnyshields)
|
22
|
+
* DEPRECATION: `#configure` is now an alias for `#config` and is deprecated (@johnnyshields)
|
23
|
+
* Replace `Mongoid::Userstamp.configuration` and `Mongoid::Userstamp.configure` with `#config` (@johnnyshields)
|
24
|
+
* Replace usages of `id` with `_id`, since there are many gems which override the behavior of `id` (@johnnyshields)
|
25
|
+
* Do not overwrite creator if it has already been set manually (@johnnyshields)
|
26
|
+
* Define setter methods for updated_accessor/created_accessor which assign the user id. Accepts both `Mongoid::Document` and `BSON::ObjectID` types (@johnnyshields)
|
27
|
+
* Allow pass-in of Mongoid field options for updated_column/created_column. Useful to set field aliases, default value, etc. (@johnnyshields)
|
28
|
+
* Expose current_user as `Mongoid::Userstamp.current_user` (@johnnyshields)
|
29
|
+
* Field type is now `::Moped::BSON::ObjectID` instead of Object (@johnnyshields)
|
30
|
+
* Query for creator/updater user using `unscoped` (@johnnyshields)
|
31
|
+
* Improve code readability (@johnnyshields)
|
32
|
+
* Improve test coverage, including adding tests for config (@johnnyshields)
|
33
|
+
|
34
|
+
* Bugfix
|
35
|
+
* Catch error if creator/updater has been deleted and return nil (@johnnyshields)
|
36
|
+
|
37
|
+
## [0.2.1](https://github.com/tbpro/mongoid_userstamp/releases/tag/v0.2.1) - 2013-03-24
|
38
|
+
|
39
|
+
* Improvement
|
40
|
+
* Added some specs for test coverage (@johnnyshields)
|
41
|
+
* Removed dependecy for `Mongoid::Timestamps` (@johnnyshields)
|
data/Gemfile
ADDED
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,92 @@
|
|
1
|
+
# WARNING
|
2
|
+
|
3
|
+
This fork is INCOMPATIBLE with upstream due to different default field names
|
4
|
+
|
5
|
+
This fork uses actual mongoid belongs_to relations instead of low level work with BSON fields. Primary reason for doing
|
6
|
+
this is to support eager loading for creator/updater modifier
|
7
|
+
|
8
|
+
MongoidUserstamp adds stamp columns for created by and updated by
|
9
|
+
information within Rails applications using Mongoid ORM.
|
10
|
+
|
11
|
+
## Version Support
|
12
|
+
|
13
|
+
MongoidUserstamp is tested on the following versions:
|
14
|
+
|
15
|
+
* Ruby 1.9.3 and 2.0.0
|
16
|
+
* Rails 3
|
17
|
+
* Mongoid 3
|
18
|
+
|
19
|
+
## Install
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
gem 'mongoid_userstamp'
|
23
|
+
```
|
24
|
+
|
25
|
+
## Usage
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
# Default config
|
29
|
+
Mongoid::Userstamp.config do |c|
|
30
|
+
|
31
|
+
# Default config values
|
32
|
+
|
33
|
+
c.user_reader = :current_user
|
34
|
+
c.user_model = :user
|
35
|
+
|
36
|
+
c.creator_field = :creator
|
37
|
+
c.updater_field = :updater
|
38
|
+
|
39
|
+
end
|
40
|
+
|
41
|
+
# Example model
|
42
|
+
class Person
|
43
|
+
include Mongoid::Document
|
44
|
+
include Mongoid::Userstamp
|
45
|
+
end
|
46
|
+
|
47
|
+
# Create instance
|
48
|
+
p = Person.create
|
49
|
+
|
50
|
+
# Updater ObjectID or nil
|
51
|
+
p.updater_id
|
52
|
+
# => BSON::ObjectId('4f7c719f476da850ba000039')
|
53
|
+
|
54
|
+
# Updater instance or nil
|
55
|
+
p.updater
|
56
|
+
# => <User _id: 4f7c719f476da850ba000039>
|
57
|
+
|
58
|
+
# Set updater manually (usually not required)
|
59
|
+
p.updater = my_user # can be a Mongoid::Document or a BSON::ObjectID
|
60
|
+
# => sets updated_by to my_user's ObjectID
|
61
|
+
|
62
|
+
# Creator ObjectID or nil
|
63
|
+
p.creator_id
|
64
|
+
# => BSON::ObjectId('4f7c719f476da850ba000039')
|
65
|
+
|
66
|
+
# Creator instance or nil
|
67
|
+
p.creator
|
68
|
+
# => <User _id: 4f7c719f476da850ba000039>
|
69
|
+
|
70
|
+
# Set creator manually (usually not required)
|
71
|
+
p.creator = my_user # can be a Mongoid::Document or a BSON::ObjectID
|
72
|
+
# => sets created_by to my_user._id
|
73
|
+
```
|
74
|
+
|
75
|
+
## Contributing
|
76
|
+
|
77
|
+
Fork -> Patch -> Spec -> Push -> Pull Request
|
78
|
+
|
79
|
+
Please use Ruby 1.9.3 hash syntax, as Mongoid 3 requires Ruby >= 1.9.3
|
80
|
+
|
81
|
+
## Authors
|
82
|
+
|
83
|
+
* [Thomas Boerger](http://www.tbpro.de)
|
84
|
+
* [John Shields](https://github.com/johnnyshields)
|
85
|
+
* [GlebTV](https://github.com/glebtv)
|
86
|
+
|
87
|
+
## Copyright
|
88
|
+
|
89
|
+
Copyright (c) 2012-2013 Thomas Boerger Programmierung <http://www.tbpro.de>
|
90
|
+
Copyright (c) 2014 glebtv <http://rocketscience.pro>
|
91
|
+
|
92
|
+
Licensed under the MIT License (MIT). Refer to LICENSE for details.
|
data/Rakefile
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
$:.push File.expand_path("../lib", __FILE__)
|
2
|
+
|
3
|
+
require 'bundler'
|
4
|
+
Bundler::GemHelper.install_tasks
|
5
|
+
|
6
|
+
require 'rspec/core/rake_task'
|
7
|
+
|
8
|
+
task :default => :spec
|
9
|
+
|
10
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
11
|
+
spec.pattern = "./spec/**/*_spec.rb"
|
12
|
+
end
|
13
|
+
|
@@ -0,0 +1,30 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Userstamp
|
3
|
+
class Config
|
4
|
+
attr_writer :user_model
|
5
|
+
attr_accessor :user_reader
|
6
|
+
attr_accessor :creator_field
|
7
|
+
attr_accessor :updater_field
|
8
|
+
|
9
|
+
def initialize(&block)
|
10
|
+
reset!
|
11
|
+
instance_eval(&block) if block_given?
|
12
|
+
end
|
13
|
+
|
14
|
+
def reset!
|
15
|
+
@user_model = :user
|
16
|
+
@user_reader = :current_user
|
17
|
+
@creator_field = :creator
|
18
|
+
@updater_field = :updater
|
19
|
+
end
|
20
|
+
|
21
|
+
def user_model_name
|
22
|
+
@user_model.to_s.classify
|
23
|
+
end
|
24
|
+
|
25
|
+
def user_model
|
26
|
+
@user_model.to_s.classify.constantize
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Mongoid
|
3
|
+
module Userstamp
|
4
|
+
class Railtie < Rails::Railtie
|
5
|
+
ActiveSupport.on_load :action_controller do
|
6
|
+
before_filter do |c|
|
7
|
+
unless Mongoid::Userstamp.config.user_model.respond_to? :current
|
8
|
+
Mongoid::Userstamp.config.user_model.send(
|
9
|
+
:include,
|
10
|
+
Mongoid::Userstamp::User
|
11
|
+
)
|
12
|
+
end
|
13
|
+
|
14
|
+
begin
|
15
|
+
Mongoid::Userstamp.config.user_model.current = c.send(Mongoid::Userstamp.config.user_reader)
|
16
|
+
rescue
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Mongoid
|
3
|
+
module Userstamp
|
4
|
+
module User
|
5
|
+
extend ActiveSupport::Concern
|
6
|
+
|
7
|
+
included do
|
8
|
+
def current?
|
9
|
+
!Thread.current[:user].nil? && self._id == Thread.current[:user]._id
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
module ClassMethods
|
14
|
+
def current
|
15
|
+
Thread.current[:user]
|
16
|
+
end
|
17
|
+
|
18
|
+
def current=(value)
|
19
|
+
Thread.current[:user] = value
|
20
|
+
end
|
21
|
+
|
22
|
+
def do_as(user, &block)
|
23
|
+
old = self.current
|
24
|
+
|
25
|
+
begin
|
26
|
+
self.current = user
|
27
|
+
response = block.call unless block.nil?
|
28
|
+
ensure
|
29
|
+
self.current = old
|
30
|
+
end
|
31
|
+
|
32
|
+
response
|
33
|
+
end
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,51 @@
|
|
1
|
+
module Mongoid
|
2
|
+
module Userstamp
|
3
|
+
extend ActiveSupport::Concern
|
4
|
+
|
5
|
+
included do
|
6
|
+
belongs_to Userstamp.config.creator_field, class_name: Userstamp.config.user_model_name
|
7
|
+
belongs_to Userstamp.config.updater_field, class_name: Userstamp.config.user_model_name
|
8
|
+
|
9
|
+
before_save :set_updater
|
10
|
+
before_create :set_creator
|
11
|
+
|
12
|
+
protected
|
13
|
+
|
14
|
+
def set_updater
|
15
|
+
return if !Userstamp.has_current_user?
|
16
|
+
self.send("#{Userstamp.config.updater_field}=", Userstamp.current_user)
|
17
|
+
end
|
18
|
+
|
19
|
+
def set_creator
|
20
|
+
return if !Userstamp.has_current_user? || self.send(Userstamp.config.creator_field)
|
21
|
+
self.send("#{Userstamp.config.creator_field}=", Userstamp.current_user)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
class << self
|
26
|
+
def config(&block)
|
27
|
+
if block_given?
|
28
|
+
@@config = Userstamp::Config.new(&block)
|
29
|
+
else
|
30
|
+
@@config ||= Userstamp::Config.new
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
def has_current_user?
|
35
|
+
config.user_model.respond_to?(:current)
|
36
|
+
end
|
37
|
+
|
38
|
+
def current_user
|
39
|
+
config.user_model.try(:current)
|
40
|
+
end
|
41
|
+
|
42
|
+
def find_user(user_id)
|
43
|
+
begin
|
44
|
+
user_id ? Userstamp.config.user_model.unscoped.find(user_id) : nil
|
45
|
+
rescue Mongoid::Errors::DocumentNotFound => e
|
46
|
+
nil
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
@@ -0,0 +1,27 @@
|
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'mongoid/userstamp/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'glebtv_mongoid_userstamp'
|
6
|
+
s.version = Mongoid::Userstamp::VERSION
|
7
|
+
s.authors = ['GlebTv', 'Thomas Boerger', 'Johnny Shields']
|
8
|
+
s.homepage = 'https://github.com/glebtv/mongoid_userstamp'
|
9
|
+
s.license = 'MIT'
|
10
|
+
s.summary = 'Userstamp for Mongoid'
|
11
|
+
s.description = 'Userstamp for creator and updater columns using Mongoid'
|
12
|
+
s.email = 'glebtv@gmail.com'
|
13
|
+
|
14
|
+
s.files = `git ls-files`.split($/)
|
15
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
16
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
17
|
+
s.require_paths = ['lib']
|
18
|
+
|
19
|
+
s.post_install_message = File.read('UPGRADING') if File.exists?('UPGRADING')
|
20
|
+
|
21
|
+
s.add_runtime_dependency 'mongoid', '~> 4.0.0.beta1'
|
22
|
+
s.add_development_dependency 'rake'
|
23
|
+
s.add_development_dependency 'rspec'
|
24
|
+
s.add_development_dependency 'yard'
|
25
|
+
s.add_development_dependency 'gem-release'
|
26
|
+
end
|
27
|
+
|
@@ -0,0 +1,158 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Mongoid::Userstamp do
|
5
|
+
subject { Book.new(name: 'Crafting Rails Applications') }
|
6
|
+
let(:user_1) { User.create!(name: 'Charles Dikkens') }
|
7
|
+
let(:user_2) { User.create!(name: 'Edmund Wells') }
|
8
|
+
|
9
|
+
it { is_expected.to respond_to :creator_id }
|
10
|
+
it { is_expected.to respond_to :creator }
|
11
|
+
it { is_expected.to respond_to :updater_id }
|
12
|
+
it { is_expected.to respond_to :updater }
|
13
|
+
|
14
|
+
describe '#current_user' do
|
15
|
+
subject{ Mongoid::Userstamp.current_user }
|
16
|
+
|
17
|
+
context 'when current user is not set' do
|
18
|
+
before { User.current = nil }
|
19
|
+
it { is_expected.to be_nil }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when current user is set' do
|
23
|
+
before{ User.current = user_1 }
|
24
|
+
it { is_expected.to eq user_1 }
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context 'when created without a user' do
|
29
|
+
before do
|
30
|
+
User.current = nil
|
31
|
+
subject.save!
|
32
|
+
end
|
33
|
+
|
34
|
+
it { expect(subject.creator_id).to be_nil }
|
35
|
+
it { expect(subject.creator).to be_nil }
|
36
|
+
it { expect(subject.updater_id).to be_nil }
|
37
|
+
it { expect(subject.updater).to be_nil }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when creator is manually set' do
|
41
|
+
before{ User.current = user_1 }
|
42
|
+
|
43
|
+
context 'set by id' do
|
44
|
+
before do
|
45
|
+
subject.creator_id = user_2._id
|
46
|
+
subject.save!
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should not be overridden when saved' do
|
50
|
+
expect(subject.creator_id).to eq user_2.id
|
51
|
+
expect(subject.creator).to eq user_2
|
52
|
+
expect(subject.updater_id).to eq user_1.id
|
53
|
+
expect(subject.updater).to eq user_1
|
54
|
+
end
|
55
|
+
end
|
56
|
+
context 'set by model' do
|
57
|
+
before do
|
58
|
+
subject.creator = user_2
|
59
|
+
subject.save!
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should not be overridden when saved' do
|
63
|
+
expect(subject.creator_id).to eq user_2.id
|
64
|
+
expect(subject.creator).to eq user_2
|
65
|
+
expect(subject.updater_id).to eq user_1.id
|
66
|
+
expect(subject.updater).to eq user_1
|
67
|
+
end
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
context 'when created by a user' do
|
72
|
+
before do
|
73
|
+
User.current = user_1
|
74
|
+
subject.save!
|
75
|
+
end
|
76
|
+
|
77
|
+
it { expect(subject.creator_id).to eq(user_1.id) }
|
78
|
+
it { expect(subject.creator).to eq(user_1) }
|
79
|
+
it { expect(subject.updater_id).to eq(user_1.id) }
|
80
|
+
it { expect(subject.updater).to eq(user_1) }
|
81
|
+
|
82
|
+
context 'when updated by a user' do
|
83
|
+
before do
|
84
|
+
User.current = user_2
|
85
|
+
subject.save!
|
86
|
+
end
|
87
|
+
|
88
|
+
it { expect(subject.creator_id).to eq(user_1.id) }
|
89
|
+
it { expect(subject.creator).to eq(user_1) }
|
90
|
+
it { expect(subject.updater_id).to eq(user_2.id) }
|
91
|
+
it { expect(subject.updater).to eq(user_2) }
|
92
|
+
end
|
93
|
+
|
94
|
+
context 'when user has been destroyed' do
|
95
|
+
before do
|
96
|
+
User.current = user_2
|
97
|
+
subject.save!
|
98
|
+
user_1.destroy
|
99
|
+
user_2.destroy
|
100
|
+
subject.reload
|
101
|
+
end
|
102
|
+
|
103
|
+
it { expect(subject.creator_id).to eq(user_1.id) }
|
104
|
+
it { expect(subject.creator).to eq(nil) }
|
105
|
+
it { expect(subject.updater_id).to eq(user_2.id) }
|
106
|
+
it { expect(subject.updater).to eq(nil) }
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
describe '#config' do
|
111
|
+
before :all do
|
112
|
+
Mongoid::Userstamp.config do |c|
|
113
|
+
c.user_reader = :current_user
|
114
|
+
c.user_model = :user
|
115
|
+
|
116
|
+
c.creator_field = :c_by
|
117
|
+
c.updater_field = :u_by
|
118
|
+
end
|
119
|
+
|
120
|
+
# class definition must come after config
|
121
|
+
class Novel
|
122
|
+
include Mongoid::Document
|
123
|
+
include Mongoid::Userstamp
|
124
|
+
|
125
|
+
field :name
|
126
|
+
end
|
127
|
+
end
|
128
|
+
after :all do
|
129
|
+
Mongoid::Userstamp.config.reset!
|
130
|
+
end
|
131
|
+
subject { Novel.new(name: 'Ethyl the Aardvark goes Quantity Surveying') }
|
132
|
+
|
133
|
+
context 'when created by a user' do
|
134
|
+
before do
|
135
|
+
User.current = user_1
|
136
|
+
subject.save!
|
137
|
+
end
|
138
|
+
|
139
|
+
it { expect(subject.c_by_id).to eq(user_1.id) }
|
140
|
+
it { expect(subject.c_by).to eq(user_1) }
|
141
|
+
it { expect(subject.u_by_id).to eq(user_1.id) }
|
142
|
+
it { expect(subject.u_by).to eq(user_1) }
|
143
|
+
|
144
|
+
context 'when updated by a user' do
|
145
|
+
before do
|
146
|
+
User.current = user_2
|
147
|
+
subject.save!
|
148
|
+
end
|
149
|
+
|
150
|
+
it { expect(subject.c_by_id).to eq(user_1.id) }
|
151
|
+
it { expect(subject.c_by).to eq(user_1) }
|
152
|
+
it { expect(subject.u_by_id).to eq(user_2.id) }
|
153
|
+
it { expect(subject.u_by).to eq(user_2) }
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'rubygems'
|
3
|
+
|
4
|
+
$:.push File.expand_path('../../lib', __FILE__)
|
5
|
+
|
6
|
+
require 'active_support/all'
|
7
|
+
require 'mongoid'
|
8
|
+
require 'mongoid_userstamp'
|
9
|
+
|
10
|
+
Mongoid.configure do |config|
|
11
|
+
config.connect_to(
|
12
|
+
'mongoid_userstamp_test'
|
13
|
+
)
|
14
|
+
end
|
15
|
+
|
16
|
+
Object.send :remove_const, :Config
|
17
|
+
Config = RbConfig
|
18
|
+
|
19
|
+
Dir[File.expand_path('../support/**/*.rb', __FILE__)].each do |f|
|
20
|
+
require f
|
21
|
+
end
|
22
|
+
|
23
|
+
RSpec.configure do |config|
|
24
|
+
config.mock_with :rspec
|
25
|
+
|
26
|
+
config.after :suite do
|
27
|
+
Mongoid.purge!
|
28
|
+
end
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,140 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: glebtv_mongoid_userstamp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.4.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- GlebTv
|
8
|
+
- Thomas Boerger
|
9
|
+
- Johnny Shields
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2014-06-07 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: mongoid
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 4.0.0.beta1
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 4.0.0.beta1
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rake
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - ">="
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '0'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: rspec
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
- !ruby/object:Gem::Dependency
|
58
|
+
name: yard
|
59
|
+
requirement: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: '0'
|
64
|
+
type: :development
|
65
|
+
prerelease: false
|
66
|
+
version_requirements: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
- !ruby/object:Gem::Dependency
|
72
|
+
name: gem-release
|
73
|
+
requirement: !ruby/object:Gem::Requirement
|
74
|
+
requirements:
|
75
|
+
- - ">="
|
76
|
+
- !ruby/object:Gem::Version
|
77
|
+
version: '0'
|
78
|
+
type: :development
|
79
|
+
prerelease: false
|
80
|
+
version_requirements: !ruby/object:Gem::Requirement
|
81
|
+
requirements:
|
82
|
+
- - ">="
|
83
|
+
- !ruby/object:Gem::Version
|
84
|
+
version: '0'
|
85
|
+
description: Userstamp for creator and updater columns using Mongoid
|
86
|
+
email: glebtv@gmail.com
|
87
|
+
executables: []
|
88
|
+
extensions: []
|
89
|
+
extra_rdoc_files: []
|
90
|
+
files:
|
91
|
+
- ".gitignore"
|
92
|
+
- ".ruby-gemset"
|
93
|
+
- ".travis.yml"
|
94
|
+
- CHANGELOG.md
|
95
|
+
- Gemfile
|
96
|
+
- LICENSE
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- lib/glebtv_mongoid_userstamp.rb
|
100
|
+
- lib/mongoid/userstamp.rb
|
101
|
+
- lib/mongoid/userstamp/config.rb
|
102
|
+
- lib/mongoid/userstamp/railtie.rb
|
103
|
+
- lib/mongoid/userstamp/user.rb
|
104
|
+
- lib/mongoid/userstamp/version.rb
|
105
|
+
- lib/mongoid_userstamp.rb
|
106
|
+
- mongoid_mongoid_userstamp.gemspec
|
107
|
+
- spec/mongoid/userstamp_spec.rb
|
108
|
+
- spec/spec_helper.rb
|
109
|
+
- spec/support/book.rb
|
110
|
+
- spec/support/user.rb
|
111
|
+
homepage: https://github.com/glebtv/mongoid_userstamp
|
112
|
+
licenses:
|
113
|
+
- MIT
|
114
|
+
metadata: {}
|
115
|
+
post_install_message:
|
116
|
+
rdoc_options: []
|
117
|
+
require_paths:
|
118
|
+
- lib
|
119
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
120
|
+
requirements:
|
121
|
+
- - ">="
|
122
|
+
- !ruby/object:Gem::Version
|
123
|
+
version: '0'
|
124
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
requirements: []
|
130
|
+
rubyforge_project:
|
131
|
+
rubygems_version: 2.2.2
|
132
|
+
signing_key:
|
133
|
+
specification_version: 4
|
134
|
+
summary: Userstamp for Mongoid
|
135
|
+
test_files:
|
136
|
+
- spec/mongoid/userstamp_spec.rb
|
137
|
+
- spec/spec_helper.rb
|
138
|
+
- spec/support/book.rb
|
139
|
+
- spec/support/user.rb
|
140
|
+
has_rdoc:
|