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 +7 -0
- data/.gitignore +13 -0
- data/CHANGELOG.md +66 -0
- data/Gemfile +9 -0
- data/LICENSE +22 -0
- data/README.md +185 -0
- data/Rakefile +15 -0
- data/init.rb +2 -0
- data/lib/mongoid/userstamps/config/model.rb +43 -0
- data/lib/mongoid/userstamps/config/user.rb +20 -0
- data/lib/mongoid/userstamps/config.rb +79 -0
- data/lib/mongoid/userstamps/created.rb +29 -0
- data/lib/mongoid/userstamps/deleted.rb +29 -0
- data/lib/mongoid/userstamps/model.rb +26 -0
- data/lib/mongoid/userstamps/railtie.rb +20 -0
- data/lib/mongoid/userstamps/updated.rb +29 -0
- data/lib/mongoid/userstamps/user.rb +43 -0
- data/lib/mongoid/userstamps/version.rb +8 -0
- data/lib/mongoid/userstamps.rb +30 -0
- data/lib/mongoid-userstamps.rb +4 -0
- data/mongoid-userstamps.gemspec +26 -0
- data/spec/spec_helper.rb +25 -0
- data/spec/support/admin.rb +7 -0
- data/spec/support/book.rb +9 -0
- data/spec/support/post.rb +11 -0
- data/spec/support/user.rb +7 -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 +121 -0
- data/test/config_test.rb +17 -0
- data/test/model_test.rb +12 -0
- data/test/models/post.rb +7 -0
- data/test/models/user.rb +7 -0
- data/test/mongoid.yml +6 -0
- data/test/test_helper.rb +25 -0
- data/test/user_test.rb +10 -0
- metadata +133 -0
@@ -0,0 +1,30 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
|
3
|
+
require 'mongoid/userstamps/version'
|
4
|
+
require 'mongoid/userstamps/config'
|
5
|
+
require 'mongoid/userstamps/user'
|
6
|
+
require 'mongoid/userstamps/model'
|
7
|
+
require 'mongoid/userstamps/created'
|
8
|
+
require 'mongoid/userstamps/updated'
|
9
|
+
require 'mongoid/userstamps/deleted'
|
10
|
+
require 'mongoid/userstamps/railtie' if defined? Rails
|
11
|
+
|
12
|
+
module Mongoid
|
13
|
+
module Userstamps
|
14
|
+
extend ActiveSupport::Concern
|
15
|
+
|
16
|
+
include Created
|
17
|
+
include Updated
|
18
|
+
|
19
|
+
included do
|
20
|
+
if defined?(Mongoid::Paranoia) && self < Mongoid::Paranoia
|
21
|
+
include Deleted
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.config
|
26
|
+
Mongoid::Userstamps::Config.module_eval(&Proc.new)
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
@@ -0,0 +1,26 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path('../lib', __FILE__)
|
3
|
+
|
4
|
+
require 'mongoid/userstamps/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |s|
|
7
|
+
s.name = 'mongoid-userstamps'
|
8
|
+
s.version = Mongoid::Userstamps::VERSION
|
9
|
+
s.authors = ['Geoffroy Planquart', 'Thomas Boerger', 'Johnny Shields', 'Bharat Gupta']
|
10
|
+
s.homepage = 'https://github.com/tbpro/mongoid_userstamp'
|
11
|
+
s.license = 'MIT'
|
12
|
+
s.summary = 'Userstamps for Mongoid'
|
13
|
+
s.description = 'Userstamps for creator and updater fields using Mongoid'
|
14
|
+
s.email = ['geoffroy@planquart.fr', 'tboerger@tbpro.de']
|
15
|
+
|
16
|
+
s.files = `git ls-files`.split($/)
|
17
|
+
s.executables = s.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
s.test_files = s.files.grep(%r{^(test|spec|features)/})
|
19
|
+
s.require_paths = ['lib']
|
20
|
+
|
21
|
+
s.post_install_message = File.read('UPGRADING') if File.exists?('UPGRADING')
|
22
|
+
|
23
|
+
s.add_dependency 'mongoid', '~> 4.0'
|
24
|
+
s.add_dependency 'activesupport', '~> 4.0'
|
25
|
+
end
|
26
|
+
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'rubygems'
|
3
|
+
require 'ostruct'
|
4
|
+
|
5
|
+
$:.push File.expand_path('../../lib', __FILE__)
|
6
|
+
|
7
|
+
require 'active_support/all'
|
8
|
+
require 'mongoid'
|
9
|
+
require 'mongoid_userstamp'
|
10
|
+
|
11
|
+
%w(admin user book post).each do |file_name|
|
12
|
+
require "support/#{file_name}"
|
13
|
+
end
|
14
|
+
|
15
|
+
Mongoid.configure do |config|
|
16
|
+
config.connect_to 'mongoid_userstamp_test'
|
17
|
+
end
|
18
|
+
|
19
|
+
RSpec.configure do |config|
|
20
|
+
config.mock_with :rspec
|
21
|
+
|
22
|
+
config.after :suite do
|
23
|
+
Mongoid.purge!
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Mongoid::Userstamp::GemConfig do
|
5
|
+
|
6
|
+
subject { Mongoid::Userstamp::GemConfig.new }
|
7
|
+
|
8
|
+
describe '#initialize' do
|
9
|
+
|
10
|
+
context 'without block' do
|
11
|
+
it { should be_a Mongoid::Userstamp::GemConfig }
|
12
|
+
it { subject.created_name.should eq :created_by }
|
13
|
+
it { subject.updated_name.should eq :updated_by }
|
14
|
+
it { subject.user_reader.should eq :current_user }
|
15
|
+
end
|
16
|
+
|
17
|
+
context 'with block' do
|
18
|
+
subject do
|
19
|
+
Mongoid::Userstamp::GemConfig.new do |u|
|
20
|
+
u.created_name = :c_by
|
21
|
+
u.updated_name = :u_by
|
22
|
+
u.user_reader = :foo
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
it { should be_a Mongoid::Userstamp::GemConfig }
|
27
|
+
it { subject.created_name.should eq :c_by }
|
28
|
+
it { subject.updated_name.should eq :u_by }
|
29
|
+
it { subject.user_reader.should eq :foo }
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe 'deprecated methods' do
|
34
|
+
subject do
|
35
|
+
Mongoid::Userstamp::GemConfig.new do |u|
|
36
|
+
u.user_model = :bar
|
37
|
+
u.created_column = :bing
|
38
|
+
u.updated_column = :baz
|
39
|
+
end
|
40
|
+
end
|
41
|
+
it { ->{ subject }.should_not raise_error }
|
42
|
+
it { should be_a Mongoid::Userstamp::GemConfig }
|
43
|
+
it { subject.created_name.should eq :bing }
|
44
|
+
it { subject.updated_name.should eq :baz }
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,31 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Mongoid::Userstamp::ModelConfig do
|
5
|
+
|
6
|
+
subject { Mongoid::Userstamp::ModelConfig.new }
|
7
|
+
before { Mongoid::Userstamp.stub('config').and_return(OpenStruct.new(created_name: :created_by,
|
8
|
+
updated_name: :updated_by)) }
|
9
|
+
before { Mongoid::Userstamp.stub('user_classes').and_return(['User']) }
|
10
|
+
|
11
|
+
describe '#initialize' do
|
12
|
+
|
13
|
+
context 'with opts hash' do
|
14
|
+
subject { Mongoid::Userstamp::ModelConfig.new(user_model: :bar,
|
15
|
+
created_name: :c_by,
|
16
|
+
updated_name: :u_by) }
|
17
|
+
|
18
|
+
it { should be_a Mongoid::Userstamp::ModelConfig }
|
19
|
+
it { subject.user_model.should eq :bar }
|
20
|
+
it { subject.created_name.should eq :c_by }
|
21
|
+
it { subject.updated_name.should eq :u_by }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'without opts hash' do
|
25
|
+
it { should be_a Mongoid::Userstamp::ModelConfig }
|
26
|
+
it { subject.user_model.should eq 'User' }
|
27
|
+
it { subject.created_name.should eq :created_by }
|
28
|
+
it { subject.updated_name.should eq :updated_by }
|
29
|
+
end
|
30
|
+
end
|
31
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Mongoid::Userstamp::Model do
|
5
|
+
|
6
|
+
subject(:book) { Book.new(name: 'Crafting Rails Applications') }
|
7
|
+
subject(:post) { Post.new(title: 'Understanding Rails') }
|
8
|
+
|
9
|
+
let(:user_1) { User.create!(name: 'Charles Dikkens') }
|
10
|
+
let(:user_2) { User.create!(name: 'Edmund Wells') }
|
11
|
+
let(:admin_1) { Admin.create!(name: 'JK Rowling') }
|
12
|
+
let(:admin_2) { Admin.create!(name: 'Stephan Norway') }
|
13
|
+
|
14
|
+
describe '::mongoid_userstamp_config' do
|
15
|
+
before do
|
16
|
+
@config = Book.instance_variable_get(:'@mongoid_userstamp_config')
|
17
|
+
Book.instance_variable_set(:'@mongoid_userstamp_config', nil)
|
18
|
+
end
|
19
|
+
|
20
|
+
after do
|
21
|
+
Book.instance_variable_set(:'@mongoid_userstamp_config', @config)
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'when options are not given' do
|
25
|
+
subject{ Book.mongoid_userstamp_config }
|
26
|
+
it { should be_a Mongoid::Userstamp::ModelConfig }
|
27
|
+
it { subject.user_model.should eq Admin }
|
28
|
+
it { subject.created_name.should eq :created_by }
|
29
|
+
it { subject.updated_name.should eq :updated_by }
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when options are given' do
|
33
|
+
subject{ Book.mongoid_userstamp_config(user_model: 'User', created_name: :foo, updated_name: :bar) }
|
34
|
+
it { should be_a Mongoid::Userstamp::ModelConfig }
|
35
|
+
it { subject.user_model.should eq 'User' }
|
36
|
+
it { subject.created_name.should eq :foo }
|
37
|
+
it { subject.updated_name.should eq :bar }
|
38
|
+
end
|
39
|
+
|
40
|
+
context 'when mongoid_userstamp_user has been set' do
|
41
|
+
subject{ Book.mongoid_userstamp_config; Book.mongoid_userstamp_config(user_model: 'User', created_name: :foo, updated_name: :bar) }
|
42
|
+
it { should be_a Mongoid::Userstamp::ModelConfig }
|
43
|
+
it { subject.user_model.should eq Admin }
|
44
|
+
it { subject.created_name.should eq :created_by }
|
45
|
+
it { subject.updated_name.should eq :updated_by }
|
46
|
+
end
|
47
|
+
|
48
|
+
context 'when set via mongoid_userstamp method' do
|
49
|
+
subject{ Book.mongoid_userstamp(user_model: 'User', created_name: :foo, updated_name: :bar); Book.mongoid_userstamp_config }
|
50
|
+
it { should be_a Mongoid::Userstamp::ModelConfig }
|
51
|
+
it { subject.user_model.should eq 'User' }
|
52
|
+
it { subject.created_name.should eq :foo }
|
53
|
+
it { subject.updated_name.should eq :bar }
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
describe '::current_user' do
|
58
|
+
|
59
|
+
before { Admin.current = nil; User.current = nil }
|
60
|
+
|
61
|
+
context 'when current book user is not set' do
|
62
|
+
it { Book.current_user.should be_nil }
|
63
|
+
it { Post.current_user.should be_nil }
|
64
|
+
end
|
65
|
+
|
66
|
+
context 'when current book user is set' do
|
67
|
+
before{ User.current = user_1 }
|
68
|
+
it { Book.current_user.should eq user_1 }
|
69
|
+
it { Post.current_user.should be_nil }
|
70
|
+
end
|
71
|
+
|
72
|
+
context 'when current post user is set' do
|
73
|
+
before{ Admin.current = admin_1 }
|
74
|
+
it { Book.current_user.should be_nil }
|
75
|
+
it { Post.current_user.should eq admin_1 }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
describe 'relations and callbacks' do
|
80
|
+
|
81
|
+
context 'when created without a user' do
|
82
|
+
before do
|
83
|
+
User.current = nil
|
84
|
+
Admin.current = nil
|
85
|
+
book.save!
|
86
|
+
post.save!
|
87
|
+
end
|
88
|
+
|
89
|
+
it { book.created_by.should be_nil }
|
90
|
+
it { book.updated_by.should be_nil }
|
91
|
+
it { post.writer.should be_nil }
|
92
|
+
it { post.editor.should be_nil }
|
93
|
+
end
|
94
|
+
|
95
|
+
context 'when created with a user' do
|
96
|
+
before do
|
97
|
+
User.current = user_1
|
98
|
+
Admin.current = admin_1
|
99
|
+
book.save!
|
100
|
+
post.save!
|
101
|
+
end
|
102
|
+
|
103
|
+
it { book.created_by.should eq user_1 }
|
104
|
+
it { book.updated_by.should eq user_1 }
|
105
|
+
it { post.writer.should eq admin_1 }
|
106
|
+
it { post.editor.should eq admin_1 }
|
107
|
+
end
|
108
|
+
|
109
|
+
context 'when creator is manually set' do
|
110
|
+
before do
|
111
|
+
User.current = user_1
|
112
|
+
Admin.current = admin_1
|
113
|
+
book.created_by = user_2
|
114
|
+
book.save!
|
115
|
+
post.writer_id = admin_2._id
|
116
|
+
post.save!
|
117
|
+
end
|
118
|
+
|
119
|
+
it { book.created_by.should eq user_2 }
|
120
|
+
it { book.updated_by.should eq user_1 }
|
121
|
+
it { post.writer.should eq admin_2 }
|
122
|
+
it { post.editor.should eq admin_1 }
|
123
|
+
end
|
124
|
+
|
125
|
+
context 'when updater is manually set' do
|
126
|
+
before do
|
127
|
+
User.current = user_1
|
128
|
+
Admin.current = admin_1
|
129
|
+
book.updated_by = user_2
|
130
|
+
book.save!
|
131
|
+
post.editor_id = admin_2._id
|
132
|
+
post.save!
|
133
|
+
end
|
134
|
+
|
135
|
+
it { book.created_by.should eq user_1 }
|
136
|
+
it { book.updated_by.should eq user_2 }
|
137
|
+
it { post.writer.should eq admin_1 }
|
138
|
+
it { post.editor.should eq admin_2 }
|
139
|
+
end
|
140
|
+
|
141
|
+
context 'when user has been destroyed' do
|
142
|
+
before do
|
143
|
+
User.current = user_1
|
144
|
+
Admin.current = admin_1
|
145
|
+
book.save!
|
146
|
+
post.save!
|
147
|
+
user_1.destroy
|
148
|
+
admin_1.destroy
|
149
|
+
end
|
150
|
+
|
151
|
+
it { Book.first.created_by.should be_nil }
|
152
|
+
it { Book.first.updated_by.should be_nil }
|
153
|
+
it { Post.first.writer.should be_nil }
|
154
|
+
it { Post.first.editor.should be_nil }
|
155
|
+
end
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Mongoid::Userstamp::UserConfig do
|
5
|
+
|
6
|
+
subject { Mongoid::Userstamp::UserConfig.new }
|
7
|
+
before { Mongoid::Userstamp.stub('config').and_return(OpenStruct.new(user_reader: :foo)) }
|
8
|
+
|
9
|
+
describe '#initialize' do
|
10
|
+
|
11
|
+
context 'with opts hash' do
|
12
|
+
subject { Mongoid::Userstamp::UserConfig.new({reader: :bar}) }
|
13
|
+
|
14
|
+
it { should be_a Mongoid::Userstamp::UserConfig }
|
15
|
+
it { subject.reader.should eq :bar }
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'without opts hash' do
|
19
|
+
it { should be_a Mongoid::Userstamp::UserConfig }
|
20
|
+
it { subject.reader.should eq :foo }
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Mongoid::Userstamp::User do
|
5
|
+
|
6
|
+
subject(:book) { Book.new(name: 'Crafting Rails Applications') }
|
7
|
+
subject(:post) { Post.new(title: 'Understanding Rails') }
|
8
|
+
|
9
|
+
let(:user_1) { User.create!(name: 'Charles Dikkens') }
|
10
|
+
let(:user_2) { User.create!(name: 'Edmund Wells') }
|
11
|
+
let(:admin_1) { Admin.create!(name: 'JK Rowling') }
|
12
|
+
let(:admin_2) { Admin.create!(name: 'Stephan Norway') }
|
13
|
+
|
14
|
+
describe '::current and #current?' do
|
15
|
+
before { Admin.current = nil; User.current = nil }
|
16
|
+
|
17
|
+
context 'when current users are not set' do
|
18
|
+
it { Admin.current.should be_nil }
|
19
|
+
it { User.current.should be_nil }
|
20
|
+
it { admin_1.current?.should be_falsey }
|
21
|
+
it { admin_2.current?.should be_falsey }
|
22
|
+
it { user_1.current?.should be_falsey }
|
23
|
+
it { user_2.current?.should be_falsey }
|
24
|
+
end
|
25
|
+
|
26
|
+
context 'when current User is set' do
|
27
|
+
before{ User.current = user_1 }
|
28
|
+
it { User.current.should eq user_1 }
|
29
|
+
it { Admin.current.should be_nil }
|
30
|
+
it { admin_1.current?.should be_falsey }
|
31
|
+
it { admin_2.current?.should be_falsey }
|
32
|
+
it { user_1.current?.should be_truthy }
|
33
|
+
it { user_2.current?.should be_falsey }
|
34
|
+
end
|
35
|
+
|
36
|
+
context 'when current Admin is set' do
|
37
|
+
before{ Admin.current = admin_1 }
|
38
|
+
it { User.current.should be_nil }
|
39
|
+
it { Admin.current.should eq admin_1 }
|
40
|
+
it { admin_1.current?.should be_truthy }
|
41
|
+
it { admin_2.current?.should be_falsey }
|
42
|
+
it { user_1.current?.should be_falsey }
|
43
|
+
it { user_2.current?.should be_falsey }
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '::do_as' do
|
48
|
+
it 'should set the current user' do
|
49
|
+
Admin.current = admin_1
|
50
|
+
Admin.do_as admin_2 do
|
51
|
+
Admin.current.should eq admin_2
|
52
|
+
end
|
53
|
+
Admin.current.should eq admin_1
|
54
|
+
end
|
55
|
+
|
56
|
+
it 'should return the value of the block' do
|
57
|
+
Admin.do_as admin_2 do
|
58
|
+
'foo'
|
59
|
+
end.should eq 'foo'
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should revert user in case of error' do
|
63
|
+
Admin.current = admin_1
|
64
|
+
begin
|
65
|
+
Admin.do_as admin_2 do
|
66
|
+
raise
|
67
|
+
end
|
68
|
+
rescue
|
69
|
+
end
|
70
|
+
Admin.current.should eq admin_1
|
71
|
+
end
|
72
|
+
end
|
73
|
+
|
74
|
+
describe '::mongoid_userstamp_user' do
|
75
|
+
before{ User.instance_variable_set(:'@mongoid_userstamp_user', nil) }
|
76
|
+
|
77
|
+
context 'when options are not given' do
|
78
|
+
subject{ User.mongoid_userstamp_user }
|
79
|
+
it { should be_a Mongoid::Userstamp::UserConfig }
|
80
|
+
it { subject.reader.should eq :current_user }
|
81
|
+
end
|
82
|
+
|
83
|
+
context 'when options are given' do
|
84
|
+
subject{ User.mongoid_userstamp_user(reader: :foo) }
|
85
|
+
it { should be_a Mongoid::Userstamp::UserConfig }
|
86
|
+
it { subject.reader.should eq :foo }
|
87
|
+
end
|
88
|
+
|
89
|
+
context 'when mongoid_userstamp_user has been set' do
|
90
|
+
subject{ User.mongoid_userstamp_user; User.mongoid_userstamp_user(reader: :foo) }
|
91
|
+
it { should be_a Mongoid::Userstamp::UserConfig }
|
92
|
+
it { subject.reader.should eq :current_user }
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# -*- encoding : utf-8 -*-
|
2
|
+
require 'spec_helper'
|
3
|
+
|
4
|
+
describe Mongoid::Userstamp do
|
5
|
+
|
6
|
+
subject{ Mongoid::Userstamp }
|
7
|
+
|
8
|
+
let(:user_1){ User.create!(name: 'Edmund Wells') }
|
9
|
+
let(:user_2){ User.create!(name: 'Charles Dikkens') }
|
10
|
+
let(:admin_1){ Admin.create!(name: 'JK Rowling') }
|
11
|
+
|
12
|
+
describe '#config' do
|
13
|
+
|
14
|
+
before { Mongoid::Userstamp.instance_variable_set(:'@config', nil) }
|
15
|
+
|
16
|
+
context 'without block' do
|
17
|
+
subject{ Mongoid::Userstamp.config }
|
18
|
+
it { should be_a Mongoid::Userstamp::GemConfig }
|
19
|
+
it { subject.created_name.should eq :created_by }
|
20
|
+
it { subject.updated_name.should eq :updated_by }
|
21
|
+
it { subject.user_reader.should eq :current_user }
|
22
|
+
end
|
23
|
+
|
24
|
+
context 'with block' do
|
25
|
+
subject do
|
26
|
+
Mongoid::Userstamp.config do |u|
|
27
|
+
u.created_name = :c_by
|
28
|
+
u.updated_name = :u_by
|
29
|
+
u.user_reader = :foo
|
30
|
+
end
|
31
|
+
end
|
32
|
+
it { should be_a Mongoid::Userstamp::GemConfig }
|
33
|
+
it { subject.created_name.should eq :c_by }
|
34
|
+
it { subject.updated_name.should eq :u_by }
|
35
|
+
it { subject.user_reader.should eq :foo }
|
36
|
+
end
|
37
|
+
|
38
|
+
context 'deprecated method' do
|
39
|
+
subject{ Mongoid::Userstamp.configure }
|
40
|
+
it { should be_a Mongoid::Userstamp::GemConfig }
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
describe '#current_user' do
|
45
|
+
before do
|
46
|
+
Mongoid::Userstamp.set_current_user(user_1)
|
47
|
+
Mongoid::Userstamp.set_current_user(admin_1)
|
48
|
+
end
|
49
|
+
context 'when user_class is User' do
|
50
|
+
subject{ Mongoid::Userstamp.current_user('User') }
|
51
|
+
it{ should eq user_1 }
|
52
|
+
end
|
53
|
+
context 'when user_class is Admin' do
|
54
|
+
subject{ Mongoid::Userstamp.current_user('Admin') }
|
55
|
+
it{ should eq admin_1 }
|
56
|
+
end
|
57
|
+
context 'when user_class is other' do
|
58
|
+
subject{ Mongoid::Userstamp.current_user('foobar') }
|
59
|
+
it{ should be_nil }
|
60
|
+
end
|
61
|
+
context 'when user_class is not given' do
|
62
|
+
subject{ Mongoid::Userstamp.current_user }
|
63
|
+
it 'should use the default user_class' do
|
64
|
+
should eq admin_1
|
65
|
+
end
|
66
|
+
end
|
67
|
+
end
|
68
|
+
|
69
|
+
describe '#model_classes' do
|
70
|
+
before { Mongoid::Userstamp.instance_variable_set(:'@model_classes', nil) }
|
71
|
+
context 'default value' do
|
72
|
+
it { subject.model_classes.should eq [] }
|
73
|
+
end
|
74
|
+
context 'setting values' do
|
75
|
+
before do
|
76
|
+
subject.add_model_class 'Book'
|
77
|
+
subject.add_model_class 'Post'
|
78
|
+
end
|
79
|
+
it { subject.model_classes.should eq [Book, Post] }
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
describe '#user_classes' do
|
84
|
+
before { Mongoid::Userstamp.instance_variable_set(:'@user_classes', nil) }
|
85
|
+
context 'default value' do
|
86
|
+
it { subject.user_classes.should eq [] }
|
87
|
+
end
|
88
|
+
context 'setting values' do
|
89
|
+
before do
|
90
|
+
subject.add_user_class 'Book'
|
91
|
+
subject.add_user_class 'Post'
|
92
|
+
end
|
93
|
+
it { subject.user_classes.should eq [Book, Post] }
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
describe '#store' do
|
98
|
+
context 'when RequestStore is defined' do
|
99
|
+
before do
|
100
|
+
stub_const('RequestStore', Object.new)
|
101
|
+
RequestStore.stub('store').and_return('foobar')
|
102
|
+
end
|
103
|
+
it { subject.store.should eq RequestStore.store }
|
104
|
+
end
|
105
|
+
context 'when RequestStore is not defined' do
|
106
|
+
before{ hide_const('RequestStore') }
|
107
|
+
it { subject.store.should eq Thread.current }
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
describe '#userstamp_key' do
|
112
|
+
context 'when model is a Class' do
|
113
|
+
subject{ Mongoid::Userstamp.userstamp_key(User) }
|
114
|
+
it{ should eq :"mongoid_userstamp/user" }
|
115
|
+
end
|
116
|
+
context 'when model is a String' do
|
117
|
+
subject{ Mongoid::Userstamp.userstamp_key('MyNamespace::User') }
|
118
|
+
it{ should eq :"mongoid_userstamp/my_namespace/user" }
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
data/test/config_test.rb
ADDED
@@ -0,0 +1,17 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ConfigTest < BaseTest
|
4
|
+
test 'should config using block' do
|
5
|
+
Mongoid::Userstamps.config do |c|
|
6
|
+
c.created_name = :creator
|
7
|
+
c.updated_name = :updater
|
8
|
+
c.deleted_name = :deleter
|
9
|
+
c.user_reader = :get_user
|
10
|
+
end
|
11
|
+
assert_equal :creator, Mongoid::Userstamps::Config.created_name
|
12
|
+
assert_equal :updater, Mongoid::Userstamps::Config.updated_name
|
13
|
+
assert_equal :deleter, Mongoid::Userstamps::Config.deleted_name
|
14
|
+
assert_equal :get_user, Mongoid::Userstamps::Config.user_reader
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
data/test/model_test.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class ModelTest < BaseTest
|
4
|
+
test 'should set created_by and updated_by on creation' do
|
5
|
+
@user = User.create(name: 'John')
|
6
|
+
Mongoid::Userstamps::Config.set_current_user(User, @user)
|
7
|
+
@post = Post.create(title: 'Hello')
|
8
|
+
assert_equal @user, @post.created_by
|
9
|
+
assert_equal @user, @post.updated_by
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
data/test/models/post.rb
ADDED
data/test/models/user.rb
ADDED
data/test/mongoid.yml
ADDED
data/test/test_helper.rb
ADDED
@@ -0,0 +1,25 @@
|
|
1
|
+
require 'bundler/setup'
|
2
|
+
require 'simplecov'
|
3
|
+
SimpleCov.configure do
|
4
|
+
add_filter '/test/'
|
5
|
+
end
|
6
|
+
SimpleCov.start if ENV['COVERAGE']
|
7
|
+
|
8
|
+
require 'minitest/autorun'
|
9
|
+
require 'mongoid'
|
10
|
+
require 'pry'
|
11
|
+
|
12
|
+
require File.expand_path("../../lib/mongoid-userstamps", __FILE__)
|
13
|
+
|
14
|
+
Mongoid.load!("#{File.dirname(__FILE__)}/mongoid.yml", "test")
|
15
|
+
|
16
|
+
Dir["#{File.dirname(__FILE__)}/models/*.rb"].each { |f| require f }
|
17
|
+
|
18
|
+
ActiveSupport::TestCase.test_order = :random
|
19
|
+
|
20
|
+
class BaseTest < ActiveSupport::TestCase
|
21
|
+
def teardown
|
22
|
+
Mongoid::Sessions.default.use('mongoid_userstamps_test').drop
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|