mongoid-userstamps 3.1.0 → 3.3.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 +5 -5
- data/.rubocop.yml +6 -0
- data/.travis.yml +17 -0
- data/CHANGELOG.md +29 -0
- data/Gemfile +12 -4
- data/Rakefile +9 -10
- data/init.rb +1 -2
- data/lib/mongoid-userstamps.rb +1 -4
- data/lib/mongoid/userstamps.rb +8 -11
- data/lib/mongoid/userstamps/config.rb +9 -10
- data/lib/mongoid/userstamps/config/model.rb +13 -8
- data/lib/mongoid/userstamps/config/user.rb +0 -3
- data/lib/mongoid/userstamps/created.rb +4 -7
- data/lib/mongoid/userstamps/deleted.rb +4 -7
- data/lib/mongoid/userstamps/model.rb +0 -1
- data/lib/mongoid/userstamps/railtie.rb +3 -8
- data/lib/mongoid/userstamps/updated.rb +4 -7
- data/lib/mongoid/userstamps/user.rb +2 -4
- data/lib/mongoid/userstamps/version.rb +1 -4
- data/mongoid-userstamps.gemspec +17 -19
- data/spec/spec_helper.rb +9 -10
- data/spec/support/admin.rb +1 -2
- data/spec/support/book.rb +2 -3
- data/spec/support/post.rb +2 -3
- data/spec/support/user.rb +0 -1
- data/spec/unit/gem_config_spec.rb +8 -11
- data/spec/unit/model_config_spec.rb +15 -14
- data/spec/unit/model_spec.rb +41 -36
- data/spec/unit/user_config_spec.rb +5 -8
- data/spec/unit/user_spec.rb +38 -34
- data/spec/unit/userstamp_spec.rb +48 -51
- data/test/config_test.rb +12 -11
- data/test/model_test.rb +12 -13
- data/test/models/archive.rb +0 -1
- data/test/models/comment.rb +1 -2
- data/test/models/person.rb +0 -1
- data/test/models/post.rb +2 -1
- data/test/models/user.rb +0 -1
- data/test/paranoia_test.rb +2 -3
- data/test/test_helper.rb +9 -8
- data/test/user_test.rb +9 -10
- metadata +12 -11
data/spec/spec_helper.rb
CHANGED
@@ -1,24 +1,23 @@
|
|
1
|
-
|
2
|
-
require
|
3
|
-
require 'ostruct'
|
1
|
+
require "rubygems"
|
2
|
+
require "ostruct"
|
4
3
|
|
5
|
-
$:.push File.expand_path(
|
4
|
+
$:.push File.expand_path("../../lib", __FILE__)
|
6
5
|
|
7
|
-
require
|
8
|
-
require
|
9
|
-
require
|
6
|
+
require "active_support/all"
|
7
|
+
require "mongoid"
|
8
|
+
require "mongoid_userstamp"
|
10
9
|
|
11
|
-
%w
|
10
|
+
%w[admin user book post].each do |file_name|
|
12
11
|
require "support/#{file_name}"
|
13
12
|
end
|
14
13
|
|
15
14
|
Mongoid.configure do |config|
|
16
|
-
config.connect_to
|
15
|
+
config.connect_to "mongoid_userstamp_test"
|
17
16
|
end
|
18
17
|
|
19
18
|
RSpec.configure do |config|
|
20
19
|
config.mock_with :rspec
|
21
|
-
|
20
|
+
|
22
21
|
config.after :suite do
|
23
22
|
Mongoid.purge!
|
24
23
|
end
|
data/spec/support/admin.rb
CHANGED
data/spec/support/book.rb
CHANGED
data/spec/support/post.rb
CHANGED
@@ -1,11 +1,10 @@
|
|
1
|
-
# -*- encoding : utf-8 -*-
|
2
1
|
class Post
|
3
2
|
include Mongoid::Document
|
4
3
|
include Mongoid::Userstamp
|
5
4
|
|
6
|
-
mongoid_userstamp user_model:
|
5
|
+
mongoid_userstamp user_model: "Admin",
|
7
6
|
created_name: :writer,
|
8
7
|
updated_name: :editor
|
9
8
|
|
10
9
|
field :title
|
11
|
-
end
|
10
|
+
end
|
data/spec/support/user.rb
CHANGED
@@ -1,24 +1,21 @@
|
|
1
|
-
|
2
|
-
require 'spec_helper'
|
1
|
+
require "spec_helper"
|
3
2
|
|
4
3
|
describe Mongoid::Userstamp::GemConfig do
|
5
|
-
|
6
4
|
subject { Mongoid::Userstamp::GemConfig.new }
|
7
5
|
|
8
|
-
describe
|
9
|
-
|
10
|
-
context 'without block' do
|
6
|
+
describe "#initialize" do
|
7
|
+
context "without block" do
|
11
8
|
it { should be_a Mongoid::Userstamp::GemConfig }
|
12
9
|
it { subject.created_name.should eq :created_by }
|
13
10
|
it { subject.updated_name.should eq :updated_by }
|
14
11
|
it { subject.user_reader.should eq :current_user }
|
15
12
|
end
|
16
13
|
|
17
|
-
context
|
14
|
+
context "with block" do
|
18
15
|
subject do
|
19
16
|
Mongoid::Userstamp::GemConfig.new do |u|
|
20
|
-
u.created_name
|
21
|
-
u.updated_name
|
17
|
+
u.created_name = :c_by
|
18
|
+
u.updated_name = :u_by
|
22
19
|
u.user_reader = :foo
|
23
20
|
end
|
24
21
|
end
|
@@ -30,7 +27,7 @@ describe Mongoid::Userstamp::GemConfig do
|
|
30
27
|
end
|
31
28
|
end
|
32
29
|
|
33
|
-
describe
|
30
|
+
describe "deprecated methods" do
|
34
31
|
subject do
|
35
32
|
Mongoid::Userstamp::GemConfig.new do |u|
|
36
33
|
u.user_model = :bar
|
@@ -38,7 +35,7 @@ describe Mongoid::Userstamp::GemConfig do
|
|
38
35
|
u.updated_column = :baz
|
39
36
|
end
|
40
37
|
end
|
41
|
-
it { ->{ subject }.should_not raise_error }
|
38
|
+
it { -> { subject }.should_not raise_error }
|
42
39
|
it { should be_a Mongoid::Userstamp::GemConfig }
|
43
40
|
it { subject.created_name.should eq :bing }
|
44
41
|
it { subject.updated_name.should eq :baz }
|
@@ -1,19 +1,20 @@
|
|
1
|
-
|
2
|
-
require 'spec_helper'
|
1
|
+
require "spec_helper"
|
3
2
|
|
4
3
|
describe Mongoid::Userstamp::ModelConfig do
|
5
|
-
|
6
4
|
subject { Mongoid::Userstamp::ModelConfig.new }
|
7
|
-
before
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
5
|
+
before do
|
6
|
+
Mongoid::Userstamp.stub("config").and_return(OpenStruct.new(created_name: :created_by,
|
7
|
+
updated_name: :updated_by))
|
8
|
+
end
|
9
|
+
before { Mongoid::Userstamp.stub("user_classes").and_return(["User"]) }
|
12
10
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
describe "#initialize" do
|
12
|
+
context "with opts hash" do
|
13
|
+
subject {
|
14
|
+
Mongoid::Userstamp::ModelConfig.new(user_model: :bar,
|
15
|
+
created_name: :c_by,
|
16
|
+
updated_name: :u_by)
|
17
|
+
}
|
17
18
|
|
18
19
|
it { should be_a Mongoid::Userstamp::ModelConfig }
|
19
20
|
it { subject.user_model.should eq :bar }
|
@@ -21,9 +22,9 @@ describe Mongoid::Userstamp::ModelConfig do
|
|
21
22
|
it { subject.updated_name.should eq :u_by }
|
22
23
|
end
|
23
24
|
|
24
|
-
context
|
25
|
+
context "without opts hash" do
|
25
26
|
it { should be_a Mongoid::Userstamp::ModelConfig }
|
26
|
-
it { subject.user_model.should eq
|
27
|
+
it { subject.user_model.should eq "User" }
|
27
28
|
it { subject.created_name.should eq :created_by }
|
28
29
|
it { subject.updated_name.should eq :updated_by }
|
29
30
|
end
|
data/spec/unit/model_spec.rb
CHANGED
@@ -1,17 +1,15 @@
|
|
1
|
-
|
2
|
-
require 'spec_helper'
|
1
|
+
require "spec_helper"
|
3
2
|
|
4
3
|
describe Mongoid::Userstamp::Model do
|
4
|
+
subject(:book) { Book.new(name: "Crafting Rails Applications") }
|
5
|
+
subject(:post) { Post.new(title: "Understanding Rails") }
|
5
6
|
|
6
|
-
|
7
|
-
|
7
|
+
let(:user_1) { User.create!(name: "Charles Dikkens") }
|
8
|
+
let(:user_2) { User.create!(name: "Edmund Wells") }
|
9
|
+
let(:admin_1) { Admin.create!(name: "JK Rowling") }
|
10
|
+
let(:admin_2) { Admin.create!(name: "Stephan Norway") }
|
8
11
|
|
9
|
-
|
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
|
12
|
+
describe "::mongoid_userstamp_config" do
|
15
13
|
before do
|
16
14
|
@config = Book.instance_variable_get(:'@mongoid_userstamp_config')
|
17
15
|
Book.instance_variable_set(:'@mongoid_userstamp_config', nil)
|
@@ -21,64 +19,71 @@ describe Mongoid::Userstamp::Model do
|
|
21
19
|
Book.instance_variable_set(:'@mongoid_userstamp_config', @config)
|
22
20
|
end
|
23
21
|
|
24
|
-
context
|
25
|
-
subject{ Book.mongoid_userstamp_config }
|
22
|
+
context "when options are not given" do
|
23
|
+
subject { Book.mongoid_userstamp_config }
|
26
24
|
it { should be_a Mongoid::Userstamp::ModelConfig }
|
27
25
|
it { subject.user_model.should eq Admin }
|
28
26
|
it { subject.created_name.should eq :created_by }
|
29
27
|
it { subject.updated_name.should eq :updated_by }
|
30
28
|
end
|
31
29
|
|
32
|
-
context
|
33
|
-
subject{ Book.mongoid_userstamp_config(user_model:
|
30
|
+
context "when options are given" do
|
31
|
+
subject { Book.mongoid_userstamp_config(user_model: "User", created_name: :foo, updated_name: :bar) }
|
34
32
|
it { should be_a Mongoid::Userstamp::ModelConfig }
|
35
|
-
it { subject.user_model.should eq
|
33
|
+
it { subject.user_model.should eq "User" }
|
36
34
|
it { subject.created_name.should eq :foo }
|
37
35
|
it { subject.updated_name.should eq :bar }
|
38
36
|
end
|
39
37
|
|
40
|
-
context
|
41
|
-
subject
|
38
|
+
context "when mongoid_userstamp_user has been set" do
|
39
|
+
subject do
|
40
|
+
Book.mongoid_userstamp_config
|
41
|
+
Book.mongoid_userstamp_config(user_model: "User", created_name: :foo, updated_name: :bar)
|
42
|
+
end
|
42
43
|
it { should be_a Mongoid::Userstamp::ModelConfig }
|
43
44
|
it { subject.user_model.should eq Admin }
|
44
45
|
it { subject.created_name.should eq :created_by }
|
45
46
|
it { subject.updated_name.should eq :updated_by }
|
46
47
|
end
|
47
48
|
|
48
|
-
context
|
49
|
-
subject
|
49
|
+
context "when set via mongoid_userstamp method" do
|
50
|
+
subject do
|
51
|
+
Book.mongoid_userstamp(user_model: "User", created_name: :foo, updated_name: :bar)
|
52
|
+
Book.mongoid_userstamp_config
|
53
|
+
end
|
50
54
|
it { should be_a Mongoid::Userstamp::ModelConfig }
|
51
|
-
it { subject.user_model.should eq
|
55
|
+
it { subject.user_model.should eq "User" }
|
52
56
|
it { subject.created_name.should eq :foo }
|
53
57
|
it { subject.updated_name.should eq :bar }
|
54
58
|
end
|
55
59
|
end
|
56
60
|
|
57
|
-
describe
|
58
|
-
|
59
|
-
|
61
|
+
describe "::current_user" do
|
62
|
+
before do
|
63
|
+
Admin.current = nil
|
64
|
+
User.current = nil
|
65
|
+
end
|
60
66
|
|
61
|
-
context
|
67
|
+
context "when current book user is not set" do
|
62
68
|
it { Book.current_user.should be_nil }
|
63
69
|
it { Post.current_user.should be_nil }
|
64
70
|
end
|
65
71
|
|
66
|
-
context
|
67
|
-
before{ User.current = user_1 }
|
72
|
+
context "when current book user is set" do
|
73
|
+
before { User.current = user_1 }
|
68
74
|
it { Book.current_user.should eq user_1 }
|
69
75
|
it { Post.current_user.should be_nil }
|
70
76
|
end
|
71
77
|
|
72
|
-
context
|
73
|
-
before{ Admin.current = admin_1 }
|
78
|
+
context "when current post user is set" do
|
79
|
+
before { Admin.current = admin_1 }
|
74
80
|
it { Book.current_user.should be_nil }
|
75
81
|
it { Post.current_user.should eq admin_1 }
|
76
82
|
end
|
77
83
|
end
|
78
84
|
|
79
|
-
describe
|
80
|
-
|
81
|
-
context 'when created without a user' do
|
85
|
+
describe "relations and callbacks" do
|
86
|
+
context "when created without a user" do
|
82
87
|
before do
|
83
88
|
User.current = nil
|
84
89
|
Admin.current = nil
|
@@ -92,7 +97,7 @@ describe Mongoid::Userstamp::Model do
|
|
92
97
|
it { post.editor.should be_nil }
|
93
98
|
end
|
94
99
|
|
95
|
-
context
|
100
|
+
context "when created with a user" do
|
96
101
|
before do
|
97
102
|
User.current = user_1
|
98
103
|
Admin.current = admin_1
|
@@ -106,7 +111,7 @@ describe Mongoid::Userstamp::Model do
|
|
106
111
|
it { post.editor.should eq admin_1 }
|
107
112
|
end
|
108
113
|
|
109
|
-
context
|
114
|
+
context "when creator is manually set" do
|
110
115
|
before do
|
111
116
|
User.current = user_1
|
112
117
|
Admin.current = admin_1
|
@@ -122,7 +127,7 @@ describe Mongoid::Userstamp::Model do
|
|
122
127
|
it { post.editor.should eq admin_1 }
|
123
128
|
end
|
124
129
|
|
125
|
-
context
|
130
|
+
context "when updater is manually set" do
|
126
131
|
before do
|
127
132
|
User.current = user_1
|
128
133
|
Admin.current = admin_1
|
@@ -138,7 +143,7 @@ describe Mongoid::Userstamp::Model do
|
|
138
143
|
it { post.editor.should eq admin_2 }
|
139
144
|
end
|
140
145
|
|
141
|
-
context
|
146
|
+
context "when user has been destroyed" do
|
142
147
|
before do
|
143
148
|
User.current = user_1
|
144
149
|
Admin.current = admin_1
|
@@ -154,4 +159,4 @@ describe Mongoid::Userstamp::Model do
|
|
154
159
|
it { Post.first.editor.should be_nil }
|
155
160
|
end
|
156
161
|
end
|
157
|
-
end
|
162
|
+
end
|
@@ -1,21 +1,18 @@
|
|
1
|
-
|
2
|
-
require 'spec_helper'
|
1
|
+
require "spec_helper"
|
3
2
|
|
4
3
|
describe Mongoid::Userstamp::UserConfig do
|
5
|
-
|
6
4
|
subject { Mongoid::Userstamp::UserConfig.new }
|
7
|
-
before
|
8
|
-
|
9
|
-
describe '#initialize' do
|
5
|
+
before { Mongoid::Userstamp.stub("config").and_return(OpenStruct.new(user_reader: :foo)) }
|
10
6
|
|
11
|
-
|
7
|
+
describe "#initialize" do
|
8
|
+
context "with opts hash" do
|
12
9
|
subject { Mongoid::Userstamp::UserConfig.new({reader: :bar}) }
|
13
10
|
|
14
11
|
it { should be_a Mongoid::Userstamp::UserConfig }
|
15
12
|
it { subject.reader.should eq :bar }
|
16
13
|
end
|
17
14
|
|
18
|
-
context
|
15
|
+
context "without opts hash" do
|
19
16
|
it { should be_a Mongoid::Userstamp::UserConfig }
|
20
17
|
it { subject.reader.should eq :foo }
|
21
18
|
end
|
data/spec/unit/user_spec.rb
CHANGED
@@ -1,20 +1,21 @@
|
|
1
|
-
|
2
|
-
require 'spec_helper'
|
1
|
+
require "spec_helper"
|
3
2
|
|
4
3
|
describe Mongoid::Userstamp::User do
|
4
|
+
subject(:book) { Book.new(name: "Crafting Rails Applications") }
|
5
|
+
subject(:post) { Post.new(title: "Understanding Rails") }
|
5
6
|
|
6
|
-
|
7
|
-
|
7
|
+
let(:user_1) { User.create!(name: "Charles Dikkens") }
|
8
|
+
let(:user_2) { User.create!(name: "Edmund Wells") }
|
9
|
+
let(:admin_1) { Admin.create!(name: "JK Rowling") }
|
10
|
+
let(:admin_2) { Admin.create!(name: "Stephan Norway") }
|
8
11
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
describe '::current and #current?' do
|
15
|
-
before { Admin.current = nil; User.current = nil }
|
12
|
+
describe "::current and #current?" do
|
13
|
+
before do
|
14
|
+
Admin.current = nil
|
15
|
+
User.current = nil
|
16
|
+
end
|
16
17
|
|
17
|
-
context
|
18
|
+
context "when current users are not set" do
|
18
19
|
it { Admin.current.should be_nil }
|
19
20
|
it { User.current.should be_nil }
|
20
21
|
it { admin_1.current?.should be_falsey }
|
@@ -23,8 +24,8 @@ describe Mongoid::Userstamp::User do
|
|
23
24
|
it { user_2.current?.should be_falsey }
|
24
25
|
end
|
25
26
|
|
26
|
-
context
|
27
|
-
before{ User.current = user_1 }
|
27
|
+
context "when current User is set" do
|
28
|
+
before { User.current = user_1 }
|
28
29
|
it { User.current.should eq user_1 }
|
29
30
|
it { Admin.current.should be_nil }
|
30
31
|
it { admin_1.current?.should be_falsey }
|
@@ -33,8 +34,8 @@ describe Mongoid::Userstamp::User do
|
|
33
34
|
it { user_2.current?.should be_falsey }
|
34
35
|
end
|
35
36
|
|
36
|
-
context
|
37
|
-
before{ Admin.current = admin_1 }
|
37
|
+
context "when current Admin is set" do
|
38
|
+
before { Admin.current = admin_1 }
|
38
39
|
it { User.current.should be_nil }
|
39
40
|
it { Admin.current.should eq admin_1 }
|
40
41
|
it { admin_1.current?.should be_truthy }
|
@@ -44,8 +45,8 @@ describe Mongoid::Userstamp::User do
|
|
44
45
|
end
|
45
46
|
end
|
46
47
|
|
47
|
-
describe
|
48
|
-
it
|
48
|
+
describe "::do_as" do
|
49
|
+
it "should set the current user" do
|
49
50
|
Admin.current = admin_1
|
50
51
|
Admin.do_as admin_2 do
|
51
52
|
Admin.current.should eq admin_2
|
@@ -53,13 +54,13 @@ describe Mongoid::Userstamp::User do
|
|
53
54
|
Admin.current.should eq admin_1
|
54
55
|
end
|
55
56
|
|
56
|
-
it
|
57
|
-
Admin.do_as admin_2
|
58
|
-
|
59
|
-
|
57
|
+
it "should return the value of the block" do
|
58
|
+
Admin.do_as admin_2 {
|
59
|
+
"foo"
|
60
|
+
}.should eq "foo"
|
60
61
|
end
|
61
62
|
|
62
|
-
it
|
63
|
+
it "should revert user in case of error" do
|
63
64
|
Admin.current = admin_1
|
64
65
|
begin
|
65
66
|
Admin.do_as admin_2 do
|
@@ -71,25 +72,28 @@ describe Mongoid::Userstamp::User do
|
|
71
72
|
end
|
72
73
|
end
|
73
74
|
|
74
|
-
describe
|
75
|
-
before{ User.instance_variable_set(:'@mongoid_userstamp_user', nil) }
|
75
|
+
describe "::mongoid_userstamp_user" do
|
76
|
+
before { User.instance_variable_set(:'@mongoid_userstamp_user', nil) }
|
76
77
|
|
77
|
-
context
|
78
|
-
subject{ User.mongoid_userstamp_user }
|
78
|
+
context "when options are not given" do
|
79
|
+
subject { User.mongoid_userstamp_user }
|
79
80
|
it { should be_a Mongoid::Userstamp::UserConfig }
|
80
|
-
it { subject.reader.should eq :current_user
|
81
|
+
it { subject.reader.should eq :current_user }
|
81
82
|
end
|
82
83
|
|
83
|
-
context
|
84
|
-
subject{ User.mongoid_userstamp_user(reader: :foo) }
|
84
|
+
context "when options are given" do
|
85
|
+
subject { User.mongoid_userstamp_user(reader: :foo) }
|
85
86
|
it { should be_a Mongoid::Userstamp::UserConfig }
|
86
|
-
it { subject.reader.should eq :foo
|
87
|
+
it { subject.reader.should eq :foo }
|
87
88
|
end
|
88
89
|
|
89
|
-
context
|
90
|
-
subject
|
90
|
+
context "when mongoid_userstamp_user has been set" do
|
91
|
+
subject do
|
92
|
+
User.mongoid_userstamp_user
|
93
|
+
User.mongoid_userstamp_user(reader: :foo)
|
94
|
+
end
|
91
95
|
it { should be_a Mongoid::Userstamp::UserConfig }
|
92
|
-
it { subject.reader.should eq :current_user
|
96
|
+
it { subject.reader.should eq :current_user }
|
93
97
|
end
|
94
98
|
end
|
95
99
|
end
|