mongoid_userstamp 0.3.0 → 0.3.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +8 -8
- data/.gitignore +13 -0
- data/.travis.yml +20 -8
- data/CHANGELOG.md +41 -26
- data/Gemfile +3 -9
- data/{LICENSE.md → LICENSE} +22 -20
- data/README.md +92 -72
- data/Rakefile +23 -41
- data/init.rb +2 -2
- data/lib/mongoid/userstamp.rb +89 -86
- data/lib/mongoid/userstamp/config.rb +30 -30
- data/lib/mongoid/userstamp/railtie.rb +22 -19
- data/lib/mongoid/userstamp/user.rb +37 -37
- data/lib/mongoid/userstamp/version.rb +6 -13
- data/lib/mongoid_userstamp.rb +6 -6
- data/mongoid_userstamp.gemspec +26 -69
- data/spec/mongoid/userstamp_spec.rb +163 -163
- data/spec/spec_helper.rb +26 -26
- data/spec/support/book.rb +7 -7
- data/spec/support/user.rb +7 -7
- metadata +32 -17
- data/.ruby-version +0 -1
- data/Gemfile.lock +0 -50
@@ -1,30 +1,30 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
module Mongoid
|
3
|
-
module Userstamp
|
4
|
-
class Config
|
5
|
-
attr_writer :user_model
|
6
|
-
attr_accessor :user_reader
|
7
|
-
attr_accessor :created_column
|
8
|
-
attr_accessor :created_column_opts
|
9
|
-
attr_accessor :created_accessor
|
10
|
-
attr_accessor :updated_column
|
11
|
-
attr_accessor :updated_column_opts
|
12
|
-
attr_accessor :updated_accessor
|
13
|
-
|
14
|
-
def initialize(&block)
|
15
|
-
@user_model = :user
|
16
|
-
@user_reader = :current_user
|
17
|
-
@created_column = :created_by
|
18
|
-
@created_accessor = :creator
|
19
|
-
@updated_column = :updated_by
|
20
|
-
@updated_accessor = :updater
|
21
|
-
|
22
|
-
instance_eval(&block) if block_given?
|
23
|
-
end
|
24
|
-
|
25
|
-
def user_model
|
26
|
-
@user_model.to_s.classify.constantize
|
27
|
-
end
|
28
|
-
end
|
29
|
-
end
|
30
|
-
end
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Mongoid
|
3
|
+
module Userstamp
|
4
|
+
class Config
|
5
|
+
attr_writer :user_model
|
6
|
+
attr_accessor :user_reader
|
7
|
+
attr_accessor :created_column
|
8
|
+
attr_accessor :created_column_opts
|
9
|
+
attr_accessor :created_accessor
|
10
|
+
attr_accessor :updated_column
|
11
|
+
attr_accessor :updated_column_opts
|
12
|
+
attr_accessor :updated_accessor
|
13
|
+
|
14
|
+
def initialize(&block)
|
15
|
+
@user_model = :user
|
16
|
+
@user_reader = :current_user
|
17
|
+
@created_column = :created_by
|
18
|
+
@created_accessor = :creator
|
19
|
+
@updated_column = :updated_by
|
20
|
+
@updated_accessor = :updater
|
21
|
+
|
22
|
+
instance_eval(&block) if block_given?
|
23
|
+
end
|
24
|
+
|
25
|
+
def user_model
|
26
|
+
@user_model.to_s.classify.constantize
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
@@ -1,19 +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
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
end
|
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
|
@@ -1,37 +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
|
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
|
@@ -1,13 +1,6 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
module Mongoid
|
3
|
-
module Userstamp
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
PATCH = 0
|
8
|
-
BUILD = nil
|
9
|
-
|
10
|
-
STRING = [MAJOR, MINOR, PATCH, BUILD].compact.join('.')
|
11
|
-
end
|
12
|
-
end
|
13
|
-
end
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
module Mongoid
|
3
|
+
module Userstamp
|
4
|
+
VERSION = '0.3.2'
|
5
|
+
end
|
6
|
+
end
|
data/lib/mongoid_userstamp.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
|
-
require 'mongoid/userstamp'
|
3
|
-
require 'mongoid/userstamp/version'
|
4
|
-
require 'mongoid/userstamp/config'
|
5
|
-
require 'mongoid/userstamp/user'
|
6
|
-
require 'mongoid/userstamp/railtie' if defined? Rails
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'mongoid/userstamp'
|
3
|
+
require 'mongoid/userstamp/version'
|
4
|
+
require 'mongoid/userstamp/config'
|
5
|
+
require 'mongoid/userstamp/user'
|
6
|
+
require 'mongoid/userstamp/railtie' if defined? Rails
|
data/mongoid_userstamp.gemspec
CHANGED
@@ -1,69 +1,26 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
s.
|
9
|
-
s.
|
10
|
-
|
11
|
-
s.
|
12
|
-
s.
|
13
|
-
|
14
|
-
s.
|
15
|
-
s.
|
16
|
-
s.
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
"README.md",
|
28
|
-
"Rakefile",
|
29
|
-
"init.rb",
|
30
|
-
"lib/mongoid/userstamp.rb",
|
31
|
-
"lib/mongoid/userstamp/config.rb",
|
32
|
-
"lib/mongoid/userstamp/railtie.rb",
|
33
|
-
"lib/mongoid/userstamp/user.rb",
|
34
|
-
"lib/mongoid/userstamp/version.rb",
|
35
|
-
"lib/mongoid_userstamp.rb",
|
36
|
-
"mongoid_userstamp.gemspec",
|
37
|
-
"spec/mongoid/userstamp_spec.rb",
|
38
|
-
"spec/spec_helper.rb",
|
39
|
-
"spec/support/book.rb",
|
40
|
-
"spec/support/user.rb"
|
41
|
-
]
|
42
|
-
s.homepage = "https://github.com/tbpro/mongoid_userstamp"
|
43
|
-
s.licenses = ["MIT"]
|
44
|
-
s.require_paths = ["lib"]
|
45
|
-
s.rubygems_version = "2.1.5"
|
46
|
-
s.summary = "Userstamp for mongoid"
|
47
|
-
|
48
|
-
if s.respond_to? :specification_version then
|
49
|
-
s.specification_version = 4
|
50
|
-
|
51
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
52
|
-
s.add_runtime_dependency(%q<mongoid>, [">= 3.0.4"])
|
53
|
-
s.add_development_dependency(%q<rspec>, [">= 2.13.0"])
|
54
|
-
s.add_development_dependency(%q<yard>, [">= 0.8.2.1"])
|
55
|
-
s.add_development_dependency(%q<jeweler>, [">= 1.8.3"])
|
56
|
-
else
|
57
|
-
s.add_dependency(%q<mongoid>, [">= 3.0.4"])
|
58
|
-
s.add_dependency(%q<rspec>, [">= 2.13.0"])
|
59
|
-
s.add_dependency(%q<yard>, [">= 0.8.2.1"])
|
60
|
-
s.add_dependency(%q<jeweler>, [">= 1.8.3"])
|
61
|
-
end
|
62
|
-
else
|
63
|
-
s.add_dependency(%q<mongoid>, [">= 3.0.4"])
|
64
|
-
s.add_dependency(%q<rspec>, [">= 2.13.0"])
|
65
|
-
s.add_dependency(%q<yard>, [">= 0.8.2.1"])
|
66
|
-
s.add_dependency(%q<jeweler>, [">= 1.8.3"])
|
67
|
-
end
|
68
|
-
end
|
69
|
-
|
1
|
+
$:.push File.expand_path('../lib', __FILE__)
|
2
|
+
require 'mongoid/userstamp/version'
|
3
|
+
|
4
|
+
Gem::Specification.new do |s|
|
5
|
+
s.name = 'mongoid_userstamp'
|
6
|
+
s.version = Mongoid::Userstamp::VERSION
|
7
|
+
s.authors = ['Thomas Boerger', 'Johnny Shields']
|
8
|
+
s.homepage = 'https://github.com/tbpro/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 = 'tboerger@tbpro.de'
|
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', '>= 3.0.4'
|
22
|
+
s.add_development_dependency 'rake'
|
23
|
+
s.add_development_dependency 'rspec', '>= 2.13.0'
|
24
|
+
s.add_development_dependency 'yard'
|
25
|
+
s.add_development_dependency 'gem-release'
|
26
|
+
end
|
@@ -1,163 +1,163 @@
|
|
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 { should respond_to :created_by }
|
10
|
-
it { should respond_to :creator }
|
11
|
-
it { should respond_to :updated_by }
|
12
|
-
it { should 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 { should be_nil }
|
20
|
-
end
|
21
|
-
|
22
|
-
context 'when current user is set' do
|
23
|
-
before{ User.current = user_1 }
|
24
|
-
it { should 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 { subject.created_by.should be_nil }
|
35
|
-
it { subject.creator.should be_nil }
|
36
|
-
it { subject.updated_by.should be_nil }
|
37
|
-
it { subject.updater.should 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.created_by = user_2._id
|
46
|
-
subject.save!
|
47
|
-
end
|
48
|
-
|
49
|
-
it 'should not be overridden when saved' do
|
50
|
-
subject.created_by.should eq user_2.id
|
51
|
-
subject.creator.should eq user_2
|
52
|
-
subject.updated_by.should eq user_1.id
|
53
|
-
subject.updater.should 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
|
-
subject.created_by.should eq user_2.id
|
64
|
-
subject.creator.should eq user_2
|
65
|
-
subject.updated_by.should eq user_1.id
|
66
|
-
subject.updater.should 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 { subject.created_by.should == user_1.id }
|
78
|
-
it { subject.creator.should == user_1 }
|
79
|
-
it { subject.updated_by.should == user_1.id }
|
80
|
-
it { subject.updater.should == 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 { subject.created_by.should == user_1.id }
|
89
|
-
it { subject.creator.should == user_1 }
|
90
|
-
it { subject.updated_by.should == user_2.id }
|
91
|
-
it { subject.updater.should == 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
|
-
end
|
101
|
-
|
102
|
-
it { subject.created_by.should == user_1.id }
|
103
|
-
it { subject.creator.should == nil }
|
104
|
-
it { subject.updated_by.should == user_2.id }
|
105
|
-
it { subject.updater.should == nil }
|
106
|
-
end
|
107
|
-
end
|
108
|
-
|
109
|
-
describe '#config' do
|
110
|
-
before do
|
111
|
-
Mongoid::Userstamp.config do |c|
|
112
|
-
c.user_reader = :current_user
|
113
|
-
c.user_model = :user
|
114
|
-
|
115
|
-
c.created_column = :c_by
|
116
|
-
c.created_column_opts = {as: :created_by_id}
|
117
|
-
c.created_accessor = :created_by
|
118
|
-
|
119
|
-
c.updated_column = :u_by
|
120
|
-
c.updated_column_opts = {as: :updated_by_id}
|
121
|
-
c.updated_accessor = :updated_by
|
122
|
-
end
|
123
|
-
|
124
|
-
# class definition must come after config
|
125
|
-
class Novel
|
126
|
-
include Mongoid::Document
|
127
|
-
include Mongoid::Userstamp
|
128
|
-
|
129
|
-
field :name
|
130
|
-
end
|
131
|
-
end
|
132
|
-
subject { Novel.new(name: 'Ethyl the Aardvark goes Quantity Surveying') }
|
133
|
-
|
134
|
-
context 'when created by a user' do
|
135
|
-
before do
|
136
|
-
User.current = user_1
|
137
|
-
subject.save!
|
138
|
-
end
|
139
|
-
|
140
|
-
it { subject.c_by.should == user_1.id }
|
141
|
-
it { subject.created_by_id.should == user_1.id }
|
142
|
-
it { subject.created_by.should == user_1 }
|
143
|
-
it { subject.u_by.should == user_1.id }
|
144
|
-
it { subject.updated_by_id.should == user_1.id }
|
145
|
-
it { subject.updated_by.should == user_1 }
|
146
|
-
|
147
|
-
context 'when updated by a user' do
|
148
|
-
before do
|
149
|
-
User.current = user_2
|
150
|
-
subject.save!
|
151
|
-
end
|
152
|
-
|
153
|
-
it { subject.c_by.should == user_1.id }
|
154
|
-
it { subject.created_by_id.should == user_1.id }
|
155
|
-
it { subject.created_by.should == user_1 }
|
156
|
-
it { subject.u_by.should == user_2.id }
|
157
|
-
it { subject.updated_by_id.should == user_2.id }
|
158
|
-
it { subject.updated_by.should == user_2 }
|
159
|
-
end
|
160
|
-
end
|
161
|
-
end
|
162
|
-
|
163
|
-
end
|
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 { should respond_to :created_by }
|
10
|
+
it { should respond_to :creator }
|
11
|
+
it { should respond_to :updated_by }
|
12
|
+
it { should 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 { should be_nil }
|
20
|
+
end
|
21
|
+
|
22
|
+
context 'when current user is set' do
|
23
|
+
before{ User.current = user_1 }
|
24
|
+
it { should 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 { subject.created_by.should be_nil }
|
35
|
+
it { subject.creator.should be_nil }
|
36
|
+
it { subject.updated_by.should be_nil }
|
37
|
+
it { subject.updater.should 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.created_by = user_2._id
|
46
|
+
subject.save!
|
47
|
+
end
|
48
|
+
|
49
|
+
it 'should not be overridden when saved' do
|
50
|
+
subject.created_by.should eq user_2.id
|
51
|
+
subject.creator.should eq user_2
|
52
|
+
subject.updated_by.should eq user_1.id
|
53
|
+
subject.updater.should 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
|
+
subject.created_by.should eq user_2.id
|
64
|
+
subject.creator.should eq user_2
|
65
|
+
subject.updated_by.should eq user_1.id
|
66
|
+
subject.updater.should 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 { subject.created_by.should == user_1.id }
|
78
|
+
it { subject.creator.should == user_1 }
|
79
|
+
it { subject.updated_by.should == user_1.id }
|
80
|
+
it { subject.updater.should == 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 { subject.created_by.should == user_1.id }
|
89
|
+
it { subject.creator.should == user_1 }
|
90
|
+
it { subject.updated_by.should == user_2.id }
|
91
|
+
it { subject.updater.should == 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
|
+
end
|
101
|
+
|
102
|
+
it { subject.created_by.should == user_1.id }
|
103
|
+
it { subject.creator.should == nil }
|
104
|
+
it { subject.updated_by.should == user_2.id }
|
105
|
+
it { subject.updater.should == nil }
|
106
|
+
end
|
107
|
+
end
|
108
|
+
|
109
|
+
describe '#config' do
|
110
|
+
before do
|
111
|
+
Mongoid::Userstamp.config do |c|
|
112
|
+
c.user_reader = :current_user
|
113
|
+
c.user_model = :user
|
114
|
+
|
115
|
+
c.created_column = :c_by
|
116
|
+
c.created_column_opts = {as: :created_by_id}
|
117
|
+
c.created_accessor = :created_by
|
118
|
+
|
119
|
+
c.updated_column = :u_by
|
120
|
+
c.updated_column_opts = {as: :updated_by_id}
|
121
|
+
c.updated_accessor = :updated_by
|
122
|
+
end
|
123
|
+
|
124
|
+
# class definition must come after config
|
125
|
+
class Novel
|
126
|
+
include Mongoid::Document
|
127
|
+
include Mongoid::Userstamp
|
128
|
+
|
129
|
+
field :name
|
130
|
+
end
|
131
|
+
end
|
132
|
+
subject { Novel.new(name: 'Ethyl the Aardvark goes Quantity Surveying') }
|
133
|
+
|
134
|
+
context 'when created by a user' do
|
135
|
+
before do
|
136
|
+
User.current = user_1
|
137
|
+
subject.save!
|
138
|
+
end
|
139
|
+
|
140
|
+
it { subject.c_by.should == user_1.id }
|
141
|
+
it { subject.created_by_id.should == user_1.id }
|
142
|
+
it { subject.created_by.should == user_1 }
|
143
|
+
it { subject.u_by.should == user_1.id }
|
144
|
+
it { subject.updated_by_id.should == user_1.id }
|
145
|
+
it { subject.updated_by.should == user_1 }
|
146
|
+
|
147
|
+
context 'when updated by a user' do
|
148
|
+
before do
|
149
|
+
User.current = user_2
|
150
|
+
subject.save!
|
151
|
+
end
|
152
|
+
|
153
|
+
it { subject.c_by.should == user_1.id }
|
154
|
+
it { subject.created_by_id.should == user_1.id }
|
155
|
+
it { subject.created_by.should == user_1 }
|
156
|
+
it { subject.u_by.should == user_2.id }
|
157
|
+
it { subject.updated_by_id.should == user_2.id }
|
158
|
+
it { subject.updated_by.should == user_2 }
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
162
|
+
|
163
|
+
end
|