activerecord-userstamp 3.0.1 → 3.0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/CHANGELOG.md +14 -0
- data/lib/active_record/userstamp/controller_additions.rb +17 -5
- data/lib/active_record/userstamp/stampable.rb +10 -10
- data/lib/active_record/userstamp/stamper.rb +43 -36
- data/lib/active_record/userstamp/utilities.rb +15 -1
- data/lib/active_record/userstamp/version.rb +1 -1
- data/spec/controllers/posts_controller_spec.rb +1 -3
- data/spec/controllers/users_controller_spec.rb +13 -4
- data/spec/dummy/app/controllers/posts_controller.rb +5 -1
- data/spec/dummy/app/controllers/users_controller.rb +4 -0
- data/spec/lib/migration_spec.rb +17 -15
- data/spec/lib/stamper_spec.rb +66 -0
- data/spec/lib/stamping_spec.rb +52 -24
- data/spec/support/database_helpers.rb +17 -15
- data/spec/support/with_temporary_table.rb +8 -7
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 56c177f02be82c8c7a1a66cf7b7392d862b25efa
|
4
|
+
data.tar.gz: 7f8d80e37b76903dcc04125b62657355d96e0619
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: ff83831f18094fc7fd05d4f8396b38eae47e1b3d45a180f9909964d6d333fdd660ca420df7d79c0df1cc45c3df7ecf0b500844b84ae0d83d5ad4bfe27429a4e0
|
7
|
+
data.tar.gz: 1b5a4812a59624987bd4a9201b5845ed3a23f7af40c1b7b01bf749dffc2410047e50a841d39786a40d0b50b12412d90417791cc7926ee5874b155d86cd745660
|
data/.gitignore
CHANGED
data/CHANGELOG.md
CHANGED
@@ -1,4 +1,18 @@
|
|
1
1
|
# Changelog
|
2
|
+
## 3.0.2 (12-7-2015)
|
3
|
+
* Joel Low - Depending on what was set to a stamper (ID or record object), the
|
4
|
+
association ID or association setter is used to assign the
|
5
|
+
creator/updater/deleter attributes. This only applies if the attributes
|
6
|
+
end with `_id`; otherwise the attribute would be used verbatim (e.g. the
|
7
|
+
compatibility mode `created_by`).
|
8
|
+
* Joel Low - Provide a `with_stamper` method to specify the stamper for a given
|
9
|
+
stamper class during the execution of the block.
|
10
|
+
* Joel Low - Ensure that the `set_stamper` and `reset_stamper` calls from the
|
11
|
+
controller are always paired so that the stamper state is always properly
|
12
|
+
restored. `set_stamper` and `reset_stamper` is now deprecated and will be
|
13
|
+
removed in ActiveRecord::Userstamp 3.1 and replaced with a single
|
14
|
+
`with_stamper` `around_action` callback.
|
15
|
+
|
2
16
|
## 3.0.1 (11-7-2015)
|
3
17
|
* Joel Low - Only declare the creator/updater/deleter associations when the table has
|
4
18
|
the attribute columns. If the columns cannot be determined (e.g. if the
|
@@ -8,26 +8,38 @@ module ActiveRecord::Userstamp::ControllerAdditions
|
|
8
8
|
extend ActiveSupport::Concern
|
9
9
|
|
10
10
|
included do
|
11
|
-
|
12
|
-
after_filter :reset_stamper
|
11
|
+
around_action :with_stamper
|
13
12
|
end
|
14
13
|
|
15
14
|
private
|
16
15
|
|
17
|
-
#
|
16
|
+
# This {#with_stamper} method sets the stamper for the duration of the action. This ensures
|
17
|
+
# that exceptions raised within the controller action would properly restore the previous stamper.
|
18
|
+
#
|
19
|
+
# TODO: Remove set_stamper/reset_stamper
|
20
|
+
def with_stamper
|
21
|
+
set_stamper
|
22
|
+
yield
|
23
|
+
ensure
|
24
|
+
reset_stamper
|
25
|
+
end
|
26
|
+
|
27
|
+
# The {#set_stamper} method as implemented here assumes a couple of things. First, that you are
|
18
28
|
# using a +User+ model as the stamper and second that your controller has a +current_user+
|
19
29
|
# method that contains the currently logged in stamper. If either of these are not the case in
|
20
30
|
# your application you will want to manually add your own implementation of this method to the
|
21
31
|
# private section of your +ApplicationController+
|
22
32
|
def set_stamper
|
33
|
+
@_userstamp_stamper = ActiveRecord::Userstamp.config.default_stamper_class.stamper
|
23
34
|
ActiveRecord::Userstamp.config.default_stamper_class.stamper = current_user
|
24
35
|
end
|
25
36
|
|
26
|
-
# The
|
37
|
+
# The {#reset_stamper} method as implemented here assumes that a +User+ model is being used as
|
27
38
|
# the stamper. If this is not the case then you will need to manually add your own
|
28
39
|
# implementation of this method to the private section of your +ApplicationController+
|
29
40
|
def reset_stamper
|
30
|
-
ActiveRecord::Userstamp.config.default_stamper_class.
|
41
|
+
ActiveRecord::Userstamp.config.default_stamper_class.stamper = @_userstamp_stamper
|
42
|
+
@_userstamp_stamper = nil
|
31
43
|
end
|
32
44
|
end
|
33
45
|
|
@@ -95,29 +95,29 @@ module ActiveRecord::Userstamp::Stampable
|
|
95
95
|
end
|
96
96
|
|
97
97
|
def set_creator_attribute
|
98
|
-
|
99
|
-
return if !has_stamper? ||
|
98
|
+
attribute = ActiveRecord::Userstamp.config.creator_attribute
|
99
|
+
return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
|
100
100
|
|
101
|
-
current_attribute_value = send(
|
101
|
+
current_attribute_value = send(attribute)
|
102
102
|
return if current_attribute_value.present?
|
103
103
|
|
104
|
-
|
104
|
+
ActiveRecord::Userstamp::Utilities.assign_attribute(self, attribute)
|
105
105
|
end
|
106
106
|
|
107
107
|
def set_updater_attribute
|
108
|
-
|
109
|
-
return if !has_stamper? ||
|
108
|
+
attribute = ActiveRecord::Userstamp.config.updater_attribute
|
109
|
+
return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
|
110
110
|
|
111
111
|
return if !self.new_record? && !self.changed?
|
112
112
|
|
113
|
-
|
113
|
+
ActiveRecord::Userstamp::Utilities.assign_attribute(self, attribute)
|
114
114
|
end
|
115
115
|
|
116
116
|
def set_deleter_attribute
|
117
|
-
|
118
|
-
return if !has_stamper? ||
|
117
|
+
attribute = ActiveRecord::Userstamp.config.deleter_attribute
|
118
|
+
return if !has_stamper? || attribute.nil? || !has_attribute?(attribute)
|
119
119
|
|
120
|
-
|
120
|
+
ActiveRecord::Userstamp::Utilities.assign_attribute(self, attribute)
|
121
121
|
save
|
122
122
|
end
|
123
123
|
end
|
@@ -1,47 +1,54 @@
|
|
1
|
-
module ActiveRecord::Userstamp
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
|
1
|
+
module ActiveRecord::Userstamp::Stamper
|
2
|
+
extend ActiveSupport::Concern
|
3
|
+
|
4
|
+
module ClassMethods
|
5
|
+
def model_stamper
|
6
|
+
# don't allow multiple calls
|
7
|
+
return if singleton_class.included_modules.include?(InstanceMethods)
|
8
|
+
extend ActiveRecord::Userstamp::Stamper::InstanceMethods
|
7
9
|
end
|
10
|
+
end
|
8
11
|
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
12
|
+
module InstanceMethods
|
13
|
+
# Used to set the current stamper for this model.
|
14
|
+
#
|
15
|
+
# @overload stamper=(object_id)
|
16
|
+
# @param [Fixnum] object_id The ID of the stamper.
|
17
|
+
# @return [Fixnum]
|
18
|
+
# @overload stamper=(object)
|
19
|
+
# @param [ActiveRecord::Base] object The stamper object.
|
20
|
+
# @return [ActiveRecord::Base]
|
21
|
+
def stamper=(object)
|
22
|
+
Thread.current[stamper_identifier] = object
|
15
23
|
end
|
16
24
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
object_stamper = if object.is_a?(ActiveRecord::Base)
|
22
|
-
object.send("#{object.class.primary_key}".to_sym)
|
23
|
-
else
|
24
|
-
object
|
25
|
-
end
|
26
|
-
|
27
|
-
Thread.current[stamper_identifier] = object_stamper
|
28
|
-
end
|
25
|
+
# Retrieves the existing stamper.
|
26
|
+
def stamper
|
27
|
+
Thread.current[stamper_identifier]
|
28
|
+
end
|
29
29
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
30
|
+
# Sets the stamper back to +nil+ to prepare for the next request.
|
31
|
+
def reset_stamper
|
32
|
+
self.stamper = nil
|
33
|
+
end
|
34
34
|
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
# For the duration that execution is within the provided block, the stamper for this class
|
36
|
+
# would be the specified value.
|
37
|
+
#
|
38
|
+
# This replaces the {#stamper=} and {#reset_stamper} pair because this guarantees exception
|
39
|
+
# safety.
|
40
|
+
def with_stamper(stamper)
|
41
|
+
old_stamper = self.stamper
|
42
|
+
self.stamper = stamper
|
43
|
+
yield
|
44
|
+
ensure
|
45
|
+
self.stamper = old_stamper
|
46
|
+
end
|
39
47
|
|
40
|
-
|
48
|
+
private
|
41
49
|
|
42
|
-
|
43
|
-
|
44
|
-
end
|
50
|
+
def stamper_identifier
|
51
|
+
"#{self.to_s.downcase}_#{self.object_id}_stamper"
|
45
52
|
end
|
46
53
|
end
|
47
54
|
end
|
@@ -36,5 +36,19 @@ module ActiveRecord::Userstamp::Utilities
|
|
36
36
|
rescue ActiveRecord::StatementInvalid => _
|
37
37
|
nil
|
38
38
|
end
|
39
|
-
end
|
40
39
|
|
40
|
+
# Assigns the given attribute to the record, based on the model's stamper.
|
41
|
+
#
|
42
|
+
# If the stamper is a record, then it is assigned to the attribute; if it is a number, then it
|
43
|
+
# is assigned to the +_id+ attribute
|
44
|
+
#
|
45
|
+
# @param [ActiveRecord::Base] record The record to assign.
|
46
|
+
# @param [Symbol] attribute The attribute to assign.
|
47
|
+
def self.assign_attribute(record, attribute)
|
48
|
+
attribute = attribute.to_s
|
49
|
+
stamp_value = record.class.stamper_class.stamper
|
50
|
+
|
51
|
+
attribute = attribute[0..-4] if !stamp_value.is_a?(Fixnum) && attribute.end_with?('_id')
|
52
|
+
record.send("#{attribute}=", stamp_value)
|
53
|
+
end
|
54
|
+
end
|
@@ -4,10 +4,6 @@ RSpec.describe UsersController, type: :controller do
|
|
4
4
|
controller do
|
5
5
|
end
|
6
6
|
|
7
|
-
before(:each) do
|
8
|
-
reset_to_defaults
|
9
|
-
end
|
10
|
-
|
11
7
|
context 'when updating a User' do
|
12
8
|
it 'sets the correct updater' do
|
13
9
|
request.session = { user_id: @hera.id }
|
@@ -38,4 +34,17 @@ RSpec.describe UsersController, type: :controller do
|
|
38
34
|
simulate_second_request
|
39
35
|
end
|
40
36
|
end
|
37
|
+
|
38
|
+
context 'when the handler raises an exception' do
|
39
|
+
before { @stamper = User.stamper }
|
40
|
+
it 'restores the correct stamper' do
|
41
|
+
begin
|
42
|
+
request.session = { user_id: @zeus.id }
|
43
|
+
post :create
|
44
|
+
rescue
|
45
|
+
end
|
46
|
+
|
47
|
+
expect(User.stamper).to be(@stamper)
|
48
|
+
end
|
49
|
+
end
|
41
50
|
end
|
@@ -10,6 +10,10 @@ class PostsController < ApplicationController
|
|
10
10
|
render(inline: "<%= @post.title %>")
|
11
11
|
end
|
12
12
|
|
13
|
+
def create
|
14
|
+
fail
|
15
|
+
end
|
16
|
+
|
13
17
|
protected
|
14
18
|
|
15
19
|
def current_user
|
@@ -17,7 +21,7 @@ class PostsController < ApplicationController
|
|
17
21
|
end
|
18
22
|
|
19
23
|
def set_stamper
|
20
|
-
Person.stamper =
|
24
|
+
Person.stamper = current_user
|
21
25
|
end
|
22
26
|
|
23
27
|
def reset_stamper
|
data/spec/lib/migration_spec.rb
CHANGED
@@ -1,14 +1,6 @@
|
|
1
1
|
require 'rails_helper'
|
2
2
|
|
3
3
|
RSpec.describe 'Migration helpers', type: :model do
|
4
|
-
after do
|
5
|
-
ActiveRecord::Userstamp.configure do |config|
|
6
|
-
config.creator_attribute = :creator_id
|
7
|
-
config.updater_attribute = :updater_id
|
8
|
-
config.deleter_attribute = :deleter_id
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
4
|
context 'when default attribute names are used' do
|
13
5
|
class self::DefaultRandom < ActiveRecord::Base
|
14
6
|
stampable
|
@@ -38,21 +30,31 @@ RSpec.describe 'Migration helpers', type: :model do
|
|
38
30
|
end
|
39
31
|
|
40
32
|
context 'when overridden attribute names are used' do
|
41
|
-
|
42
|
-
stampable
|
43
|
-
end
|
44
|
-
subject { self.class::OverriddenRandom.new }
|
45
|
-
|
46
|
-
temporary_table(:overridden_randoms) do |t|
|
33
|
+
before(:each) do
|
47
34
|
ActiveRecord::Userstamp.configure do |config|
|
48
35
|
config.creator_attribute = :created_by
|
49
36
|
config.updater_attribute = :updated_by
|
50
37
|
config.deleter_attribute = :deleted_by
|
51
38
|
end
|
39
|
+
class self.class::OverriddenRandom < ActiveRecord::Base
|
40
|
+
stampable
|
41
|
+
end
|
42
|
+
end
|
43
|
+
after(:each) do
|
44
|
+
ActiveRecord::Userstamp.configure do |config|
|
45
|
+
config.creator_attribute = :creator_id
|
46
|
+
config.updater_attribute = :updater_id
|
47
|
+
config.deleter_attribute = :deleter_id
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
subject { self.class::OverriddenRandom.new }
|
52
|
+
|
53
|
+
temporary_table(:overridden_randoms) do |t|
|
52
54
|
t.userstamps
|
53
55
|
end
|
54
56
|
|
55
|
-
with_temporary_table(:overridden_randoms) do
|
57
|
+
with_temporary_table(:overridden_randoms, :each) do
|
56
58
|
it 'has a created_by attribute' do
|
57
59
|
expect(subject.has_attribute?(:created_by)).to be true
|
58
60
|
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
RSpec.describe 'Stamper' do
|
4
|
+
describe '.model_stamper' do
|
5
|
+
it 'can only be included once' do
|
6
|
+
expect(User.singleton_class.included_modules.count(
|
7
|
+
ActiveRecord::Userstamp::Stamper::InstanceMethods)).to eq(1)
|
8
|
+
|
9
|
+
User.class_eval do
|
10
|
+
stamper
|
11
|
+
end
|
12
|
+
|
13
|
+
expect(User.singleton_class.included_modules.count(
|
14
|
+
ActiveRecord::Userstamp::Stamper::InstanceMethods)).to eq(1)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
describe '.stamper' do
|
19
|
+
it 'defaults to nil' do
|
20
|
+
User.reset_stamper
|
21
|
+
expect(User.stamper).to be_nil
|
22
|
+
end
|
23
|
+
end
|
24
|
+
|
25
|
+
describe '.stamper=' do
|
26
|
+
it 'assigns the stamper' do
|
27
|
+
stamper = User.new
|
28
|
+
User.stamper = stamper
|
29
|
+
expect(User.stamper).to eq(stamper)
|
30
|
+
end
|
31
|
+
|
32
|
+
context 'when the stamper is nil' do
|
33
|
+
it 'resets the stamper' do
|
34
|
+
User.stamper = nil
|
35
|
+
expect(User.stamper).to be(nil)
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe '.reset_stamper' do
|
41
|
+
it 'resets the stamper' do
|
42
|
+
User.reset_stamper
|
43
|
+
expect(User.stamper).to be_nil
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe '.with_stamper' do
|
48
|
+
context 'when within the block' do
|
49
|
+
it 'uses the correct stamper' do
|
50
|
+
stamper = User.create(name: 'Joel')
|
51
|
+
User.with_stamper(stamper) do
|
52
|
+
expect(User.stamper).to eq(stamper)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
context 'when exiting the block' do
|
58
|
+
it 'restores the stamper' do
|
59
|
+
expect do
|
60
|
+
User.with_stamper(User.create(name: 'Joel')) do
|
61
|
+
end
|
62
|
+
end.not_to change { User.stamper }
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
data/spec/lib/stamping_spec.rb
CHANGED
@@ -2,15 +2,24 @@ require 'rails_helper'
|
|
2
2
|
|
3
3
|
RSpec.describe 'Stamping', type: :model do
|
4
4
|
before(:each) do
|
5
|
-
|
5
|
+
define_first_post
|
6
6
|
User.stamper = @zeus
|
7
7
|
Person.stamper = @delynn
|
8
8
|
end
|
9
9
|
|
10
10
|
context 'when creating a Person' do
|
11
11
|
context 'when the stamper is an object' do
|
12
|
+
it 'sets using the the association' do
|
13
|
+
User.stamper = @zeus
|
14
|
+
expect(User.stamper).to eq(@zeus)
|
15
|
+
|
16
|
+
person = Person.new
|
17
|
+
expect(person).to receive(:creator=)
|
18
|
+
person.valid?
|
19
|
+
end
|
20
|
+
|
12
21
|
it 'sets the correct creator and updater' do
|
13
|
-
expect(User.stamper).to eq(@zeus
|
22
|
+
expect(User.stamper).to eq(@zeus)
|
14
23
|
|
15
24
|
person = Person.create(name: 'David')
|
16
25
|
expect(person.creator_id).to eq(@zeus.id)
|
@@ -21,7 +30,7 @@ RSpec.describe 'Stamping', type: :model do
|
|
21
30
|
|
22
31
|
context 'when a creator is specified' do
|
23
32
|
it 'does not reset the creator' do
|
24
|
-
expect(User.stamper).to eq(@zeus
|
33
|
+
expect(User.stamper).to eq(@zeus)
|
25
34
|
|
26
35
|
person = Person.create(name: 'David', creator: @hera)
|
27
36
|
expect(person.creator_id).to eq(@hera.id)
|
@@ -29,11 +38,21 @@ RSpec.describe 'Stamping', type: :model do
|
|
29
38
|
expect(person.creator).to eq(@hera)
|
30
39
|
expect(person.updater).to eq(@zeus)
|
31
40
|
end
|
41
|
+
|
42
|
+
it 'does not reset the creator' do
|
43
|
+
expect(User.stamper).to eq(@zeus)
|
44
|
+
|
45
|
+
person = Person.create(name: 'David', creator_id: @hera.id)
|
46
|
+
expect(person.creator_id).to eq(@hera.id)
|
47
|
+
expect(person.updater_id).to eq(@zeus.id)
|
48
|
+
expect(person.creator).to eq(@hera)
|
49
|
+
expect(person.updater).to eq(@zeus)
|
50
|
+
end
|
32
51
|
end
|
33
52
|
|
34
53
|
context 'when saving without validations' do
|
35
54
|
it 'sets the correct creator and updater' do
|
36
|
-
expect(User.stamper).to eq(@zeus
|
55
|
+
expect(User.stamper).to eq(@zeus)
|
37
56
|
|
38
57
|
person = Person.new(name: 'David')
|
39
58
|
person.save(validate: false)
|
@@ -46,7 +65,7 @@ RSpec.describe 'Stamping', type: :model do
|
|
46
65
|
|
47
66
|
context 'when temporarily disabling stampng' do
|
48
67
|
it 'does not set the creator and updater' do
|
49
|
-
expect(User.stamper).to eq(@zeus
|
68
|
+
expect(User.stamper).to eq(@zeus)
|
50
69
|
|
51
70
|
Person.without_stamps do
|
52
71
|
person = Person.create(name: 'David')
|
@@ -60,6 +79,15 @@ RSpec.describe 'Stamping', type: :model do
|
|
60
79
|
end
|
61
80
|
|
62
81
|
context 'when the stamper is an ID' do
|
82
|
+
it 'sets using the the association ID' do
|
83
|
+
User.stamper = @zeus.id
|
84
|
+
expect(User.stamper).to eq(@zeus.id)
|
85
|
+
|
86
|
+
person = Person.new
|
87
|
+
expect(person).to receive(:creator_id=)
|
88
|
+
person.valid?
|
89
|
+
end
|
90
|
+
|
63
91
|
it 'sets the correct creator and updater' do
|
64
92
|
User.stamper = @hera.id
|
65
93
|
expect(User.stamper).to eq(@hera.id)
|
@@ -76,7 +104,7 @@ RSpec.describe 'Stamping', type: :model do
|
|
76
104
|
context 'when creating a Post' do
|
77
105
|
context 'when the stamper is an object' do
|
78
106
|
it 'sets the correct creator and updater' do
|
79
|
-
expect(Person.stamper).to eq(@delynn
|
107
|
+
expect(Person.stamper).to eq(@delynn)
|
80
108
|
|
81
109
|
post = Post.create(title: 'Test Post - 1')
|
82
110
|
expect(post.creator_id).to eq(@delynn.id)
|
@@ -104,7 +132,7 @@ RSpec.describe 'Stamping', type: :model do
|
|
104
132
|
context 'when the stamper is an object' do
|
105
133
|
it 'sets the correct updater' do
|
106
134
|
User.stamper = @hera
|
107
|
-
expect(User.stamper).to eq(@hera
|
135
|
+
expect(User.stamper).to eq(@hera)
|
108
136
|
|
109
137
|
@delynn.name << " Berry"
|
110
138
|
@delynn.save
|
@@ -116,10 +144,25 @@ RSpec.describe 'Stamping', type: :model do
|
|
116
144
|
end
|
117
145
|
end
|
118
146
|
|
147
|
+
context 'when the stamper is an ID' do
|
148
|
+
it 'sets the correct updater' do
|
149
|
+
User.stamper = @hera.id
|
150
|
+
expect(User.stamper).to eq(@hera.id)
|
151
|
+
|
152
|
+
@delynn.name << " Berry"
|
153
|
+
@delynn.save
|
154
|
+
@delynn.reload
|
155
|
+
expect(@delynn.creator_id).to eq(@zeus.id)
|
156
|
+
expect(@delynn.updater_id).to eq(@hera.id)
|
157
|
+
expect(@delynn.creator).to eq(@zeus)
|
158
|
+
expect(@delynn.updater).to eq(@hera)
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
119
162
|
context 'when temporarily disabling stamping' do
|
120
163
|
it 'does not set the updater' do
|
121
164
|
User.stamper = @zeus
|
122
|
-
expect(User.stamper).to eq(@zeus
|
165
|
+
expect(User.stamper).to eq(@zeus)
|
123
166
|
|
124
167
|
original_updater = @delynn.updater
|
125
168
|
Person.without_stamps do
|
@@ -135,21 +178,6 @@ RSpec.describe 'Stamping', type: :model do
|
|
135
178
|
end
|
136
179
|
end
|
137
180
|
|
138
|
-
context 'when the stamper is an ID' do
|
139
|
-
it 'sets the correct updater' do
|
140
|
-
User.stamper = @hera.id
|
141
|
-
expect(User.stamper).to eq(@hera.id)
|
142
|
-
|
143
|
-
@delynn.name << " Berry"
|
144
|
-
@delynn.save
|
145
|
-
@delynn.reload
|
146
|
-
expect(@delynn.creator_id).to eq(@zeus.id)
|
147
|
-
expect(@delynn.updater_id).to eq(@hera.id)
|
148
|
-
expect(@delynn.creator).to eq(@zeus)
|
149
|
-
expect(@delynn.updater).to eq(@hera)
|
150
|
-
end
|
151
|
-
end
|
152
|
-
|
153
181
|
context 'when updating a Post' do
|
154
182
|
context 'when the stamper is an ID' do
|
155
183
|
it 'sets the correct updater' do
|
@@ -169,7 +197,7 @@ RSpec.describe 'Stamping', type: :model do
|
|
169
197
|
context 'when the stamper is an object' do
|
170
198
|
it 'sets the correct updater' do
|
171
199
|
Person.stamper = @nicole
|
172
|
-
expect(Person.stamper).to eq(@nicole
|
200
|
+
expect(Person.stamper).to eq(@nicole)
|
173
201
|
|
174
202
|
@first_post.title << " - Updated"
|
175
203
|
@first_post.save
|
@@ -1,20 +1,22 @@
|
|
1
|
-
def
|
2
|
-
|
1
|
+
def define_first_post
|
2
|
+
@first_post = Post.create!(title: 'a title')
|
3
3
|
end
|
4
4
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
User.stamper = @zeus.id
|
5
|
+
RSpec.configure do |config|
|
6
|
+
config.before(:each) do
|
7
|
+
User.delete_all
|
8
|
+
Person.delete_all
|
9
|
+
Post.delete_all
|
10
|
+
Comment.delete_all
|
11
|
+
User.reset_stamper
|
12
|
+
Person.reset_stamper
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
@zeus = User.create!(name: 'Zeus')
|
15
|
+
@hera = User.create!(name: 'Hera')
|
16
|
+
User.stamper = @zeus.id
|
18
17
|
|
19
|
-
|
18
|
+
@delynn = Person.create!(name: 'Delynn')
|
19
|
+
@nicole = Person.create!(name: 'Nicole')
|
20
|
+
Person.stamper = @delynn.id
|
21
|
+
end
|
20
22
|
end
|
@@ -17,28 +17,29 @@ module ActiveRecord::TemporaryTable::TestGroupHelpers
|
|
17
17
|
# Using the temporary table defined previously, run the examples in this group.
|
18
18
|
#
|
19
19
|
# @param [Symbol] table_name The name of the table to use.
|
20
|
+
# @param [Symbol] create_at When to create the table. Defaults to :context.
|
20
21
|
# @param [Proc] proc The examples requiring the use of the temporary table.
|
21
|
-
def with_temporary_table(table_name, &proc)
|
22
|
+
def with_temporary_table(table_name, create_at = :context, &proc)
|
22
23
|
context "with temporary table #{table_name}" do |*params|
|
23
|
-
before(
|
24
|
-
ActiveRecord::TemporaryTable::TestGroupHelpers.
|
24
|
+
before(create_at) do
|
25
|
+
ActiveRecord::TemporaryTable::TestGroupHelpers.create_table(table_name, send(table_name))
|
25
26
|
end
|
26
27
|
|
27
|
-
after(
|
28
|
-
ActiveRecord::TemporaryTable::TestGroupHelpers.
|
28
|
+
after(create_at) do
|
29
|
+
ActiveRecord::TemporaryTable::TestGroupHelpers.drop_table(table_name)
|
29
30
|
end
|
30
31
|
|
31
32
|
module_exec(*params, &proc)
|
32
33
|
end
|
33
34
|
end
|
34
35
|
|
35
|
-
def self.
|
36
|
+
def self.create_table(table_name, table_definition)
|
36
37
|
ActiveRecord::Migration.suppress_messages do
|
37
38
|
ActiveRecord::Migration.create_table(table_name, &table_definition)
|
38
39
|
end
|
39
40
|
end
|
40
41
|
|
41
|
-
def self.
|
42
|
+
def self.drop_table(table_name)
|
42
43
|
ActiveRecord::Migration.suppress_messages do
|
43
44
|
ActiveRecord::Migration.drop_table(table_name)
|
44
45
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activerecord-userstamp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 3.0.
|
4
|
+
version: 3.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Joel Low
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-07-
|
11
|
+
date: 2015-07-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activesupport
|
@@ -244,6 +244,7 @@ files:
|
|
244
244
|
- spec/dummy/public/favicon.ico
|
245
245
|
- spec/lib/configuration_spec.rb
|
246
246
|
- spec/lib/migration_spec.rb
|
247
|
+
- spec/lib/stamper_spec.rb
|
247
248
|
- spec/lib/stamping_spec.rb
|
248
249
|
- spec/lib/userstamp_spec.rb
|
249
250
|
- spec/rails_helper.rb
|