mongoid_userstamp 0.3.2 → 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.
@@ -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,120 @@
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){ Admin.create!(name: 'JK Rowling') }
10
+
11
+ describe '#config' do
12
+
13
+ before { Mongoid::Userstamp.instance_variable_set(:'@config', nil) }
14
+
15
+ context 'without block' do
16
+ subject{ Mongoid::Userstamp.config }
17
+ it { should be_a Mongoid::Userstamp::GemConfig }
18
+ it { subject.created_name.should eq :created_by }
19
+ it { subject.updated_name.should eq :updated_by }
20
+ it { subject.user_reader.should eq :current_user }
21
+ end
22
+
23
+ context 'with block' do
24
+ subject do
25
+ Mongoid::Userstamp.config do |u|
26
+ u.created_name = :c_by
27
+ u.updated_name = :u_by
28
+ u.user_reader = :foo
29
+ end
30
+ end
31
+ it { should be_a Mongoid::Userstamp::GemConfig }
32
+ it { subject.created_name.should eq :c_by }
33
+ it { subject.updated_name.should eq :u_by }
34
+ it { subject.user_reader.should eq :foo }
35
+ end
36
+
37
+ context 'deprecated method' do
38
+ subject{ Mongoid::Userstamp.configure }
39
+ it { should be_a Mongoid::Userstamp::GemConfig }
40
+ end
41
+ end
42
+
43
+ describe '#current_user' do
44
+ before do
45
+ Mongoid::Userstamp.set_current_user(user_1)
46
+ Mongoid::Userstamp.set_current_user(user_2)
47
+ end
48
+ context 'when user_class is User' do
49
+ subject{ Mongoid::Userstamp.current_user('User') }
50
+ it{ should eq user_1 }
51
+ end
52
+ context 'when user_class is Admin' do
53
+ subject{ Mongoid::Userstamp.current_user('Admin') }
54
+ it{ should eq user_2 }
55
+ end
56
+ context 'when user_class is other' do
57
+ subject{ Mongoid::Userstamp.current_user('foobar') }
58
+ it{ should be_nil }
59
+ end
60
+ context 'when user_class is not given' do
61
+ subject{ Mongoid::Userstamp.current_user }
62
+ it 'should use the default user_class' do
63
+ should eq user_2
64
+ end
65
+ end
66
+ end
67
+
68
+ describe '#model_classes' do
69
+ before { Mongoid::Userstamp.instance_variable_set(:'@model_classes', nil) }
70
+ context 'default value' do
71
+ it { subject.model_classes.should eq [] }
72
+ end
73
+ context 'setting values' do
74
+ before do
75
+ subject.add_model_class 'Book'
76
+ subject.add_model_class 'Post'
77
+ end
78
+ it { subject.model_classes.should eq [Book, Post] }
79
+ end
80
+ end
81
+
82
+ describe '#user_classes' do
83
+ before { Mongoid::Userstamp.instance_variable_set(:'@user_classes', nil) }
84
+ context 'default value' do
85
+ it { subject.user_classes.should eq [] }
86
+ end
87
+ context 'setting values' do
88
+ before do
89
+ subject.add_user_class 'Book'
90
+ subject.add_user_class 'Post'
91
+ end
92
+ it { subject.user_classes.should eq [Book, Post] }
93
+ end
94
+ end
95
+
96
+ describe '#store' do
97
+ context 'when RequestStore is defined' do
98
+ before do
99
+ stub_const('RequestStore', Object.new)
100
+ RequestStore.stub('store').and_return('foobar')
101
+ end
102
+ it { subject.store.should eq RequestStore.store }
103
+ end
104
+ context 'when RequestStore is not defined' do
105
+ before{ hide_const('RequestStore') }
106
+ it { subject.store.should eq Thread.current }
107
+ end
108
+ end
109
+
110
+ describe '#userstamp_key' do
111
+ context 'when model is a Class' do
112
+ subject{ Mongoid::Userstamp.userstamp_key(User) }
113
+ it{ should eq :"mongoid_userstamp/user" }
114
+ end
115
+ context 'when model is a String' do
116
+ subject{ Mongoid::Userstamp.userstamp_key('MyNamespace::User') }
117
+ it{ should eq :"mongoid_userstamp/my_namespace/user" }
118
+ end
119
+ end
120
+ end
metadata CHANGED
@@ -1,15 +1,16 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mongoid_userstamp
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.2
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Thomas Boerger
8
8
  - Johnny Shields
9
+ - Bharat Gupta
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2014-01-12 00:00:00.000000000 Z
13
+ date: 2014-08-09 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: mongoid
@@ -45,14 +46,14 @@ dependencies:
45
46
  requirements:
46
47
  - - ! '>='
47
48
  - !ruby/object:Gem::Version
48
- version: 2.13.0
49
+ version: 3.0.0
49
50
  type: :development
50
51
  prerelease: false
51
52
  version_requirements: !ruby/object:Gem::Requirement
52
53
  requirements:
53
54
  - - ! '>='
54
55
  - !ruby/object:Gem::Version
55
- version: 2.13.0
56
+ version: 3.0.0
56
57
  - !ruby/object:Gem::Dependency
57
58
  name: yard
58
59
  requirement: !ruby/object:Gem::Requirement
@@ -81,6 +82,20 @@ dependencies:
81
82
  - - ! '>='
82
83
  - !ruby/object:Gem::Version
83
84
  version: '0'
85
+ - !ruby/object:Gem::Dependency
86
+ name: request_store
87
+ requirement: !ruby/object:Gem::Requirement
88
+ requirements:
89
+ - - ! '>='
90
+ - !ruby/object:Gem::Version
91
+ version: '0'
92
+ type: :development
93
+ prerelease: false
94
+ version_requirements: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
84
99
  description: Userstamp for creator and updater columns using Mongoid
85
100
  email: tboerger@tbpro.de
86
101
  executables: []
@@ -96,16 +111,26 @@ files:
96
111
  - Rakefile
97
112
  - init.rb
98
113
  - lib/mongoid/userstamp.rb
99
- - lib/mongoid/userstamp/config.rb
114
+ - lib/mongoid/userstamp/config/gem_config.rb
115
+ - lib/mongoid/userstamp/config/model_config.rb
116
+ - lib/mongoid/userstamp/config/user_config.rb
117
+ - lib/mongoid/userstamp/mixins/model.rb
118
+ - lib/mongoid/userstamp/mixins/user.rb
100
119
  - lib/mongoid/userstamp/railtie.rb
101
- - lib/mongoid/userstamp/user.rb
102
120
  - lib/mongoid/userstamp/version.rb
103
121
  - lib/mongoid_userstamp.rb
104
122
  - mongoid_userstamp.gemspec
105
- - spec/mongoid/userstamp_spec.rb
106
123
  - spec/spec_helper.rb
124
+ - spec/support/admin.rb
107
125
  - spec/support/book.rb
126
+ - spec/support/post.rb
108
127
  - spec/support/user.rb
128
+ - spec/unit/gem_config_spec.rb
129
+ - spec/unit/model_config_spec.rb
130
+ - spec/unit/model_spec.rb
131
+ - spec/unit/user_config_spec.rb
132
+ - spec/unit/user_spec.rb
133
+ - spec/unit/userstamp_spec.rb
109
134
  homepage: https://github.com/tbpro/mongoid_userstamp
110
135
  licenses:
111
136
  - MIT
@@ -126,13 +151,20 @@ required_rubygems_version: !ruby/object:Gem::Requirement
126
151
  version: '0'
127
152
  requirements: []
128
153
  rubyforge_project:
129
- rubygems_version: 2.1.8
154
+ rubygems_version: 2.2.1
130
155
  signing_key:
131
156
  specification_version: 4
132
157
  summary: Userstamp for Mongoid
133
158
  test_files:
134
- - spec/mongoid/userstamp_spec.rb
135
159
  - spec/spec_helper.rb
160
+ - spec/support/admin.rb
136
161
  - spec/support/book.rb
162
+ - spec/support/post.rb
137
163
  - spec/support/user.rb
164
+ - spec/unit/gem_config_spec.rb
165
+ - spec/unit/model_config_spec.rb
166
+ - spec/unit/model_spec.rb
167
+ - spec/unit/user_config_spec.rb
168
+ - spec/unit/user_spec.rb
169
+ - spec/unit/userstamp_spec.rb
138
170
  has_rdoc:
@@ -1,30 +0,0 @@
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,37 +0,0 @@
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,163 +0,0 @@
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