mongoid_userstamp 0.3.2 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,46 @@
1
+ # -*- encoding : utf-8 -*-
2
+
3
+ module Mongoid
4
+ module Userstamp
5
+
6
+ module User
7
+
8
+ extend ActiveSupport::Concern
9
+
10
+ included do
11
+
12
+ Mongoid::Userstamp.add_user_class(self)
13
+
14
+ def current?
15
+ self._id == Mongoid::Userstamp.current_user(self.class).try(:_id)
16
+ end
17
+ end
18
+
19
+ module ClassMethods
20
+
21
+ def current
22
+ Mongoid::Userstamp.current_user(self)
23
+ end
24
+
25
+ def current=(value)
26
+ Mongoid::Userstamp.set_current_user(value, self)
27
+ end
28
+
29
+ def do_as(user, &block)
30
+ old = self.current
31
+ begin
32
+ self.current = user
33
+ response = block.call unless block.nil?
34
+ ensure
35
+ self.current = old
36
+ end
37
+ response
38
+ end
39
+
40
+ def mongoid_userstamp_user(opts = {})
41
+ @mongoid_userstamp_user ||= Mongoid::Userstamp::UserConfig.new(opts)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -1,18 +1,35 @@
1
1
  # -*- encoding : utf-8 -*-
2
+
2
3
  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
4
+ module Userstamp
5
+
6
+ class Railtie < Rails::Railtie
7
+
8
+ # Include Mongoid::Userstamp::User into User class, if not already done
9
+ config.to_prepare do
10
+ Mongoid::Userstamp.user_classes.each do |user_class|
11
+ unless user_class.included_modules.include?(Mongoid::Userstamp::User)
12
+ user_class.send(:include, Mongoid::Userstamp::User)
13
+ end
14
+ end
15
+ end
16
+
17
+ # Add userstamp to models where Mongoid::Userstamp was included, but
18
+ # mongoid_userstamp was not explicitly called
19
+ config.to_prepare do
20
+ Mongoid::Userstamp.model_classes.each do |model_class|
21
+ unless model_class.included_modules.include?(Mongoid::Userstamp::Model)
22
+ model_class.send(:include, Mongoid::Userstamp::Model)
23
+ end
24
+ end
25
+ end
13
26
 
27
+ # Set current_user from controller reader method
28
+ ActiveSupport.on_load :action_controller do
29
+ before_filter do |c|
30
+ Mongoid::Userstamp.user_classes.each do |user_class|
14
31
  begin
15
- Mongoid::Userstamp.config.user_model.current = c.send(Mongoid::Userstamp.config.user_reader)
32
+ user_class.current = c.send(user_class.mongoid_userstamp_user.reader)
16
33
  rescue
17
34
  end
18
35
  end
@@ -20,3 +37,4 @@ module Mongoid
20
37
  end
21
38
  end
22
39
  end
40
+ end
@@ -1,6 +1,7 @@
1
1
  # -*- encoding : utf-8 -*-
2
+
2
3
  module Mongoid
3
4
  module Userstamp
4
- VERSION = '0.3.2'
5
+ VERSION = '0.4.0'
5
6
  end
6
7
  end
@@ -1,6 +1,10 @@
1
1
  # -*- encoding : utf-8 -*-
2
+
2
3
  require 'mongoid/userstamp'
3
4
  require 'mongoid/userstamp/version'
4
- require 'mongoid/userstamp/config'
5
- require 'mongoid/userstamp/user'
5
+ require 'mongoid/userstamp/config/gem_config'
6
+ require 'mongoid/userstamp/config/model_config'
7
+ require 'mongoid/userstamp/config/user_config'
8
+ require 'mongoid/userstamp/mixins/user'
9
+ require 'mongoid/userstamp/mixins/model'
6
10
  require 'mongoid/userstamp/railtie' if defined? Rails
@@ -4,7 +4,7 @@ require 'mongoid/userstamp/version'
4
4
  Gem::Specification.new do |s|
5
5
  s.name = 'mongoid_userstamp'
6
6
  s.version = Mongoid::Userstamp::VERSION
7
- s.authors = ['Thomas Boerger', 'Johnny Shields']
7
+ s.authors = ['Thomas Boerger', 'Johnny Shields', 'Bharat Gupta']
8
8
  s.homepage = 'https://github.com/tbpro/mongoid_userstamp'
9
9
  s.license = 'MIT'
10
10
  s.summary = 'Userstamp for Mongoid'
@@ -20,7 +20,8 @@ Gem::Specification.new do |s|
20
20
 
21
21
  s.add_runtime_dependency 'mongoid', '>= 3.0.4'
22
22
  s.add_development_dependency 'rake'
23
- s.add_development_dependency 'rspec', '>= 2.13.0'
23
+ s.add_development_dependency 'rspec', '>= 3.0.0'
24
24
  s.add_development_dependency 'yard'
25
25
  s.add_development_dependency 'gem-release'
26
+ s.add_development_dependency 'request_store'
26
27
  end
data/spec/spec_helper.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
  require 'rubygems'
3
+ require 'ostruct'
3
4
 
4
5
  $:.push File.expand_path('../../lib', __FILE__)
5
6
 
@@ -7,14 +8,12 @@ require 'active_support/all'
7
8
  require 'mongoid'
8
9
  require 'mongoid_userstamp'
9
10
 
10
- Mongoid.configure do |config|
11
- config.connect_to(
12
- 'mongoid_userstamp_test'
13
- )
11
+ %w(admin user book post).each do |file_name|
12
+ require "support/#{file_name}"
14
13
  end
15
14
 
16
- Dir[File.expand_path('../support/**/*.rb', __FILE__)].each do |f|
17
- require f
15
+ Mongoid.configure do |config|
16
+ config.connect_to 'mongoid_userstamp_test'
18
17
  end
19
18
 
20
19
  RSpec.configure do |config|
@@ -0,0 +1,7 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class Admin
3
+ include Mongoid::Document
4
+ include Mongoid::Userstamp::User
5
+
6
+ field :name
7
+ end
data/spec/support/book.rb CHANGED
@@ -3,5 +3,7 @@ class Book
3
3
  include Mongoid::Document
4
4
  include Mongoid::Userstamp
5
5
 
6
+ mongoid_userstamp user_model: 'User'
7
+
6
8
  field :name
7
- end
9
+ end
@@ -0,0 +1,11 @@
1
+ # -*- encoding : utf-8 -*-
2
+ class Post
3
+ include Mongoid::Document
4
+ include Mongoid::Userstamp
5
+
6
+ mongoid_userstamp user_model: 'Admin',
7
+ created_name: :writer,
8
+ updated_name: :editor
9
+
10
+ field :title
11
+ 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 = admin_2
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 = admin_2
132
+ post.save!
133
+ end
134
+
135
+ it { book.created_by.should eq user_1 }
136
+ it { book.updated_by.should eq user_1 }
137
+ it { post.writer.should eq admin_1 }
138
+ it { post.editor.should eq admin_1 }
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