mongoid-userstamps 3.1.0 → 3.3.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,24 +1,23 @@
1
- # -*- encoding : utf-8 -*-
2
- require 'rubygems'
3
- require 'ostruct'
1
+ require "rubygems"
2
+ require "ostruct"
4
3
 
5
- $:.push File.expand_path('../../lib', __FILE__)
4
+ $:.push File.expand_path("../../lib", __FILE__)
6
5
 
7
- require 'active_support/all'
8
- require 'mongoid'
9
- require 'mongoid_userstamp'
6
+ require "active_support/all"
7
+ require "mongoid"
8
+ require "mongoid_userstamp"
10
9
 
11
- %w(admin user book post).each do |file_name|
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 'mongoid_userstamp_test'
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
@@ -1,7 +1,6 @@
1
- # -*- encoding : utf-8 -*-
2
1
  class Admin
3
2
  include Mongoid::Document
4
3
  include Mongoid::Userstamp::User
5
4
 
6
5
  field :name
7
- end
6
+ end
@@ -1,9 +1,8 @@
1
- # -*- encoding : utf-8 -*-
2
1
  class Book
3
2
  include Mongoid::Document
4
3
  include Mongoid::Userstamp
5
4
 
6
- mongoid_userstamp user_model: 'User'
5
+ mongoid_userstamp user_model: "User"
7
6
 
8
7
  field :name
9
- end
8
+ end
@@ -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: 'Admin',
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
@@ -1,4 +1,3 @@
1
- # -*- encoding : utf-8 -*-
2
1
  class User
3
2
  include Mongoid::Document
4
3
  include Mongoid::Userstamp::User
@@ -1,24 +1,21 @@
1
- # -*- encoding : utf-8 -*-
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 '#initialize' do
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 'with block' do
14
+ context "with block" do
18
15
  subject do
19
16
  Mongoid::Userstamp::GemConfig.new do |u|
20
- u.created_name = :c_by
21
- u.updated_name = :u_by
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 'deprecated methods' do
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
- # -*- encoding : utf-8 -*-
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 { 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
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
- context 'with opts hash' do
14
- subject { Mongoid::Userstamp::ModelConfig.new(user_model: :bar,
15
- created_name: :c_by,
16
- updated_name: :u_by) }
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 'without opts hash' do
25
+ context "without opts hash" do
25
26
  it { should be_a Mongoid::Userstamp::ModelConfig }
26
- it { subject.user_model.should eq 'User' }
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
@@ -1,17 +1,15 @@
1
- # -*- encoding : utf-8 -*-
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
- subject(:book) { Book.new(name: 'Crafting Rails Applications') }
7
- subject(:post) { Post.new(title: 'Understanding Rails') }
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
- 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
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 'when options are not given' do
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 'when options are given' do
33
- subject{ Book.mongoid_userstamp_config(user_model: 'User', created_name: :foo, updated_name: :bar) }
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 'User' }
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 '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) }
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 '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 }
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 'User' }
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 '::current_user' do
58
-
59
- before { Admin.current = nil; User.current = nil }
61
+ describe "::current_user" do
62
+ before do
63
+ Admin.current = nil
64
+ User.current = nil
65
+ end
60
66
 
61
- context 'when current book user is not set' do
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 'when current book user is set' do
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 'when current post user is set' do
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 'relations and callbacks' do
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 'when created with a user' do
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 'when creator is manually set' do
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 'when updater is manually set' do
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 'when user has been destroyed' do
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
- # -*- encoding : utf-8 -*-
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 { Mongoid::Userstamp.stub('config').and_return(OpenStruct.new(user_reader: :foo)) }
8
-
9
- describe '#initialize' do
5
+ before { Mongoid::Userstamp.stub("config").and_return(OpenStruct.new(user_reader: :foo)) }
10
6
 
11
- context 'with opts hash' do
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 'without opts hash' do
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
@@ -1,20 +1,21 @@
1
- # -*- encoding : utf-8 -*-
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
- subject(:book) { Book.new(name: 'Crafting Rails Applications') }
7
- subject(:post) { Post.new(title: 'Understanding Rails') }
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
- 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 }
12
+ describe "::current and #current?" do
13
+ before do
14
+ Admin.current = nil
15
+ User.current = nil
16
+ end
16
17
 
17
- context 'when current users are not set' do
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 'when current User is set' do
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 'when current Admin is set' do
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 '::do_as' do
48
- it 'should set the current user' do
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 'should return the value of the block' do
57
- Admin.do_as admin_2 do
58
- 'foo'
59
- end.should eq 'foo'
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 'should revert user in case of error' do
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 '::mongoid_userstamp_user' do
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 'when options are not given' do
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 'when options are given' do
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 'when mongoid_userstamp_user has been set' do
90
- subject{ User.mongoid_userstamp_user; User.mongoid_userstamp_user(reader: :foo) }
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