activerecord-userstamp 3.0.2 → 3.0.3
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 +4 -4
- data/.gitignore +5 -5
- data/.rspec +2 -2
- data/CHANGELOG.md +103 -99
- data/Gemfile +3 -3
- data/LICENSE +21 -21
- data/README.md +189 -189
- data/Rakefile +17 -17
- data/activerecord-userstamp.gemspec +34 -34
- data/lib/active_record/userstamp.rb +30 -30
- data/lib/active_record/userstamp/configuration.rb +49 -49
- data/lib/active_record/userstamp/controller_additions.rb +50 -50
- data/lib/active_record/userstamp/migration_additions.rb +14 -14
- data/lib/active_record/userstamp/model_additions.rb +10 -10
- data/lib/active_record/userstamp/stampable.rb +123 -123
- data/lib/active_record/userstamp/stamper.rb +54 -54
- data/lib/active_record/userstamp/utilities.rb +55 -54
- data/lib/active_record/userstamp/version.rb +4 -4
- data/lib/activerecord/userstamp.rb +1 -1
- data/spec/controllers/posts_controller_spec.rb +44 -44
- data/spec/controllers/users_controller_spec.rb +50 -50
- data/spec/dummy/README.rdoc +28 -28
- data/spec/dummy/Rakefile +6 -6
- data/spec/dummy/app/assets/javascripts/application.js +13 -13
- data/spec/dummy/app/assets/stylesheets/application.css +15 -15
- data/spec/dummy/app/controllers/application_controller.rb +13 -13
- data/spec/dummy/app/controllers/posts_controller.rb +36 -36
- data/spec/dummy/app/controllers/users_controller.rb +22 -22
- data/spec/dummy/app/helpers/application_helper.rb +2 -2
- data/spec/dummy/app/models/comment.rb +5 -5
- data/spec/dummy/app/models/person.rb +3 -3
- data/spec/dummy/app/models/post.rb +14 -14
- data/spec/dummy/app/models/tag.rb +3 -0
- data/spec/dummy/app/models/user.rb +3 -3
- data/spec/dummy/app/views/layouts/application.html.erb +14 -14
- data/spec/dummy/bin/bundle +3 -3
- data/spec/dummy/bin/rails +4 -4
- data/spec/dummy/bin/rake +4 -4
- data/spec/dummy/bin/setup +29 -29
- data/spec/dummy/config.ru +4 -4
- data/spec/dummy/config/application.rb +30 -30
- data/spec/dummy/config/boot.rb +5 -5
- data/spec/dummy/config/database.yml +23 -23
- data/spec/dummy/config/environment.rb +5 -5
- data/spec/dummy/config/environments/development.rb +41 -41
- data/spec/dummy/config/environments/production.rb +79 -79
- data/spec/dummy/config/environments/test.rb +37 -37
- data/spec/dummy/config/initializers/assets.rb +11 -11
- data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -7
- data/spec/dummy/config/initializers/cookies_serializer.rb +3 -3
- data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -4
- data/spec/dummy/config/initializers/inflections.rb +16 -16
- data/spec/dummy/config/initializers/mime_types.rb +4 -4
- data/spec/dummy/config/initializers/session_store.rb +3 -3
- data/spec/dummy/config/initializers/wrap_parameters.rb +14 -14
- data/spec/dummy/config/locales/en.yml +23 -23
- data/spec/dummy/config/routes.rb +56 -56
- data/spec/dummy/config/secrets.yml +22 -22
- data/spec/dummy/db/schema.rb +54 -45
- data/spec/dummy/public/404.html +67 -67
- data/spec/dummy/public/422.html +67 -67
- data/spec/dummy/public/500.html +66 -66
- data/spec/lib/configuration_spec.rb +20 -20
- data/spec/lib/migration_spec.rb +71 -71
- data/spec/lib/stamper_spec.rb +66 -66
- data/spec/lib/stamping_spec.rb +250 -238
- data/spec/lib/userstamp_spec.rb +7 -7
- data/spec/rails_helper.rb +7 -7
- data/spec/spec_helper.rb +97 -97
- data/spec/support/database_helpers.rb +22 -22
- data/spec/support/with_temporary_table.rb +51 -51
- metadata +3 -2
@@ -1,54 +1,54 @@
|
|
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
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
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
|
23
|
-
end
|
24
|
-
|
25
|
-
# Retrieves the existing stamper.
|
26
|
-
def stamper
|
27
|
-
Thread.current[stamper_identifier]
|
28
|
-
end
|
29
|
-
|
30
|
-
# Sets the stamper back to +nil+ to prepare for the next request.
|
31
|
-
def reset_stamper
|
32
|
-
self.stamper = nil
|
33
|
-
end
|
34
|
-
|
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
|
47
|
-
|
48
|
-
private
|
49
|
-
|
50
|
-
def stamper_identifier
|
51
|
-
"#{self.to_s.downcase}_#{self.object_id}_stamper"
|
52
|
-
end
|
53
|
-
end
|
54
|
-
end
|
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
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
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
|
23
|
+
end
|
24
|
+
|
25
|
+
# Retrieves the existing stamper.
|
26
|
+
def stamper
|
27
|
+
Thread.current[stamper_identifier]
|
28
|
+
end
|
29
|
+
|
30
|
+
# Sets the stamper back to +nil+ to prepare for the next request.
|
31
|
+
def reset_stamper
|
32
|
+
self.stamper = nil
|
33
|
+
end
|
34
|
+
|
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
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def stamper_identifier
|
51
|
+
"#{self.to_s.downcase}_#{self.object_id}_stamper"
|
52
|
+
end
|
53
|
+
end
|
54
|
+
end
|
@@ -1,54 +1,55 @@
|
|
1
|
-
module ActiveRecord::Userstamp::Utilities
|
2
|
-
# Removes the association methods from the model.
|
3
|
-
#
|
4
|
-
# @param [Class] model The model to remove methods from.
|
5
|
-
# @param [Symbol] association The name of the association to remove.
|
6
|
-
# @return [void]
|
7
|
-
def self.remove_association(model, association)
|
8
|
-
methods = [
|
9
|
-
association,
|
10
|
-
"#{association}=",
|
11
|
-
"build_#{association}",
|
12
|
-
"create_#{association}",
|
13
|
-
"create_#{association}!"
|
14
|
-
]
|
15
|
-
|
16
|
-
model.generated_association_methods.module_eval do
|
17
|
-
methods.each do |method|
|
18
|
-
remove_method(method) if method_defined?(method)
|
19
|
-
end
|
20
|
-
end
|
21
|
-
end
|
22
|
-
|
23
|
-
# Obtains the creator/updater/deleter columns which are present in the model.
|
24
|
-
#
|
25
|
-
# @param [Class] model The model to query.
|
26
|
-
# @return [nil|Array<(bool, bool, bool)>] Nil if the model does not have a table defined.
|
27
|
-
# Otherwise, a tuple of booleans indicating the presence of the created, updated, and deleted
|
28
|
-
# columns.
|
29
|
-
def self.available_association_columns(model)
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
config.
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
#
|
42
|
-
#
|
43
|
-
# is assigned to the
|
44
|
-
#
|
45
|
-
#
|
46
|
-
# @param [
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end
|
1
|
+
module ActiveRecord::Userstamp::Utilities
|
2
|
+
# Removes the association methods from the model.
|
3
|
+
#
|
4
|
+
# @param [Class] model The model to remove methods from.
|
5
|
+
# @param [Symbol] association The name of the association to remove.
|
6
|
+
# @return [void]
|
7
|
+
def self.remove_association(model, association)
|
8
|
+
methods = [
|
9
|
+
association,
|
10
|
+
"#{association}=",
|
11
|
+
"build_#{association}",
|
12
|
+
"create_#{association}",
|
13
|
+
"create_#{association}!"
|
14
|
+
]
|
15
|
+
|
16
|
+
model.generated_association_methods.module_eval do
|
17
|
+
methods.each do |method|
|
18
|
+
remove_method(method) if method_defined?(method)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
# Obtains the creator/updater/deleter columns which are present in the model.
|
24
|
+
#
|
25
|
+
# @param [Class] model The model to query.
|
26
|
+
# @return [nil|Array<(bool, bool, bool)>] Nil if the model does not have a table defined.
|
27
|
+
# Otherwise, a tuple of booleans indicating the presence of the created, updated, and deleted
|
28
|
+
# columns.
|
29
|
+
def self.available_association_columns(model)
|
30
|
+
return nil if model.table_name.empty?
|
31
|
+
columns = Set[*model.column_names]
|
32
|
+
config = ActiveRecord::Userstamp.config
|
33
|
+
|
34
|
+
[config.creator_attribute.present? && columns.include?(config.creator_attribute.to_s),
|
35
|
+
config.updater_attribute.present? && columns.include?(config.updater_attribute.to_s),
|
36
|
+
config.deleter_attribute.present? && columns.include?(config.deleter_attribute.to_s)]
|
37
|
+
rescue ActiveRecord::StatementInvalid => _
|
38
|
+
nil
|
39
|
+
end
|
40
|
+
|
41
|
+
# Assigns the given attribute to the record, based on the model's stamper.
|
42
|
+
#
|
43
|
+
# If the stamper is a record, then it is assigned to the attribute; if it is a number, then it
|
44
|
+
# is assigned to the +_id+ attribute
|
45
|
+
#
|
46
|
+
# @param [ActiveRecord::Base] record The record to assign.
|
47
|
+
# @param [Symbol] attribute The attribute to assign.
|
48
|
+
def self.assign_attribute(record, attribute)
|
49
|
+
attribute = attribute.to_s
|
50
|
+
stamp_value = record.class.stamper_class.stamper
|
51
|
+
|
52
|
+
attribute = attribute[0..-4] if !stamp_value.is_a?(Fixnum) && attribute.end_with?('_id')
|
53
|
+
record.send("#{attribute}=", stamp_value)
|
54
|
+
end
|
55
|
+
end
|
@@ -1,4 +1,4 @@
|
|
1
|
-
module ActiveRecord; end
|
2
|
-
module ActiveRecord::Userstamp
|
3
|
-
VERSION = '3.0.
|
4
|
-
end
|
1
|
+
module ActiveRecord; end
|
2
|
+
module ActiveRecord::Userstamp
|
3
|
+
VERSION = '3.0.3'
|
4
|
+
end
|
@@ -1 +1 @@
|
|
1
|
-
require 'active_record/userstamp'
|
1
|
+
require 'active_record/userstamp'
|
@@ -1,44 +1,44 @@
|
|
1
|
-
require 'rails_helper'
|
2
|
-
|
3
|
-
RSpec.describe PostsController, type: :controller do
|
4
|
-
controller do
|
5
|
-
end
|
6
|
-
|
7
|
-
before(:each) { define_first_post }
|
8
|
-
|
9
|
-
context 'when updating a Post' do
|
10
|
-
it 'sets the correct updater' do
|
11
|
-
request.session = { person_id: @delynn.id }
|
12
|
-
post :update, id: @first_post.id, post: { title: 'Different' }
|
13
|
-
|
14
|
-
expect(response.status).to eq(200)
|
15
|
-
expect(controller.instance_variable_get(:@post).title).to eq('Different')
|
16
|
-
expect(controller.instance_variable_get(:@post).updater).to eq(@delynn)
|
17
|
-
end
|
18
|
-
end
|
19
|
-
|
20
|
-
context 'when handling multiple requests' do
|
21
|
-
def simulate_second_request
|
22
|
-
old_request_session = request.session
|
23
|
-
request.session = { person_id: @nicole.id }
|
24
|
-
|
25
|
-
post :update, id: @first_post.id, post: { title: 'Different Second'}
|
26
|
-
expect(controller.instance_variable_get(:@post).updater).to eq(@nicole)
|
27
|
-
ensure
|
28
|
-
request.session = old_request_session
|
29
|
-
end
|
30
|
-
|
31
|
-
it 'sets the correct updater' do
|
32
|
-
request.session = { person_id: @delynn.id }
|
33
|
-
get :edit, id: @first_post.id
|
34
|
-
expect(response.status).to eq(200)
|
35
|
-
|
36
|
-
simulate_second_request
|
37
|
-
|
38
|
-
post :update, id: @first_post.id, post: { title: 'Different' }
|
39
|
-
expect(response.status).to eq(200)
|
40
|
-
expect(controller.instance_variable_get(:@post).title).to eq('Different')
|
41
|
-
expect(controller.instance_variable_get(:@post).updater).to eq(@delynn)
|
42
|
-
end
|
43
|
-
end
|
44
|
-
end
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
RSpec.describe PostsController, type: :controller do
|
4
|
+
controller do
|
5
|
+
end
|
6
|
+
|
7
|
+
before(:each) { define_first_post }
|
8
|
+
|
9
|
+
context 'when updating a Post' do
|
10
|
+
it 'sets the correct updater' do
|
11
|
+
request.session = { person_id: @delynn.id }
|
12
|
+
post :update, id: @first_post.id, post: { title: 'Different' }
|
13
|
+
|
14
|
+
expect(response.status).to eq(200)
|
15
|
+
expect(controller.instance_variable_get(:@post).title).to eq('Different')
|
16
|
+
expect(controller.instance_variable_get(:@post).updater).to eq(@delynn)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when handling multiple requests' do
|
21
|
+
def simulate_second_request
|
22
|
+
old_request_session = request.session
|
23
|
+
request.session = { person_id: @nicole.id }
|
24
|
+
|
25
|
+
post :update, id: @first_post.id, post: { title: 'Different Second'}
|
26
|
+
expect(controller.instance_variable_get(:@post).updater).to eq(@nicole)
|
27
|
+
ensure
|
28
|
+
request.session = old_request_session
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'sets the correct updater' do
|
32
|
+
request.session = { person_id: @delynn.id }
|
33
|
+
get :edit, id: @first_post.id
|
34
|
+
expect(response.status).to eq(200)
|
35
|
+
|
36
|
+
simulate_second_request
|
37
|
+
|
38
|
+
post :update, id: @first_post.id, post: { title: 'Different' }
|
39
|
+
expect(response.status).to eq(200)
|
40
|
+
expect(controller.instance_variable_get(:@post).title).to eq('Different')
|
41
|
+
expect(controller.instance_variable_get(:@post).updater).to eq(@delynn)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -1,50 +1,50 @@
|
|
1
|
-
require 'rails_helper'
|
2
|
-
|
3
|
-
RSpec.describe UsersController, type: :controller do
|
4
|
-
controller do
|
5
|
-
end
|
6
|
-
|
7
|
-
context 'when updating a User' do
|
8
|
-
it 'sets the correct updater' do
|
9
|
-
request.session = { user_id: @hera.id }
|
10
|
-
patch :update, id: @hera.id, user: { name: 'Different'}
|
11
|
-
|
12
|
-
expect(response.status).to eq(200)
|
13
|
-
expect(controller.instance_variable_get(:@user).name).to eq('Different')
|
14
|
-
expect(controller.instance_variable_get(:@user).updater).to eq(@hera)
|
15
|
-
end
|
16
|
-
end
|
17
|
-
|
18
|
-
context 'when handling multiple requests' do
|
19
|
-
def simulate_second_request
|
20
|
-
old_request_session = request.session
|
21
|
-
request.session = { user_id: @zeus.id }
|
22
|
-
|
23
|
-
post :update, id: @hera.id, user: { name: 'Different Second' }
|
24
|
-
expect(controller.instance_variable_get(:@user).updater).to eq(@zeus)
|
25
|
-
ensure
|
26
|
-
request.session = old_request_session
|
27
|
-
end
|
28
|
-
|
29
|
-
it 'sets the correct updater' do
|
30
|
-
request.session = { user_id: @hera.id }
|
31
|
-
get :edit, id: @hera.id
|
32
|
-
expect(response.status).to eq(200)
|
33
|
-
|
34
|
-
simulate_second_request
|
35
|
-
end
|
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
|
50
|
-
end
|
1
|
+
require 'rails_helper'
|
2
|
+
|
3
|
+
RSpec.describe UsersController, type: :controller do
|
4
|
+
controller do
|
5
|
+
end
|
6
|
+
|
7
|
+
context 'when updating a User' do
|
8
|
+
it 'sets the correct updater' do
|
9
|
+
request.session = { user_id: @hera.id }
|
10
|
+
patch :update, id: @hera.id, user: { name: 'Different'}
|
11
|
+
|
12
|
+
expect(response.status).to eq(200)
|
13
|
+
expect(controller.instance_variable_get(:@user).name).to eq('Different')
|
14
|
+
expect(controller.instance_variable_get(:@user).updater).to eq(@hera)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
context 'when handling multiple requests' do
|
19
|
+
def simulate_second_request
|
20
|
+
old_request_session = request.session
|
21
|
+
request.session = { user_id: @zeus.id }
|
22
|
+
|
23
|
+
post :update, id: @hera.id, user: { name: 'Different Second' }
|
24
|
+
expect(controller.instance_variable_get(:@user).updater).to eq(@zeus)
|
25
|
+
ensure
|
26
|
+
request.session = old_request_session
|
27
|
+
end
|
28
|
+
|
29
|
+
it 'sets the correct updater' do
|
30
|
+
request.session = { user_id: @hera.id }
|
31
|
+
get :edit, id: @hera.id
|
32
|
+
expect(response.status).to eq(200)
|
33
|
+
|
34
|
+
simulate_second_request
|
35
|
+
end
|
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
|
50
|
+
end
|
data/spec/dummy/README.rdoc
CHANGED
@@ -1,28 +1,28 @@
|
|
1
|
-
== README
|
2
|
-
|
3
|
-
This README would normally document whatever steps are necessary to get the
|
4
|
-
application up and running.
|
5
|
-
|
6
|
-
Things you may want to cover:
|
7
|
-
|
8
|
-
* Ruby version
|
9
|
-
|
10
|
-
* System dependencies
|
11
|
-
|
12
|
-
* Configuration
|
13
|
-
|
14
|
-
* Database creation
|
15
|
-
|
16
|
-
* Database initialization
|
17
|
-
|
18
|
-
* How to run the test suite
|
19
|
-
|
20
|
-
* Services (job queues, cache servers, search engines, etc.)
|
21
|
-
|
22
|
-
* Deployment instructions
|
23
|
-
|
24
|
-
* ...
|
25
|
-
|
26
|
-
|
27
|
-
Please feel free to use a different markup language if you do not plan to run
|
28
|
-
<tt>rake doc:app</tt>.
|
1
|
+
== README
|
2
|
+
|
3
|
+
This README would normally document whatever steps are necessary to get the
|
4
|
+
application up and running.
|
5
|
+
|
6
|
+
Things you may want to cover:
|
7
|
+
|
8
|
+
* Ruby version
|
9
|
+
|
10
|
+
* System dependencies
|
11
|
+
|
12
|
+
* Configuration
|
13
|
+
|
14
|
+
* Database creation
|
15
|
+
|
16
|
+
* Database initialization
|
17
|
+
|
18
|
+
* How to run the test suite
|
19
|
+
|
20
|
+
* Services (job queues, cache servers, search engines, etc.)
|
21
|
+
|
22
|
+
* Deployment instructions
|
23
|
+
|
24
|
+
* ...
|
25
|
+
|
26
|
+
|
27
|
+
Please feel free to use a different markup language if you do not plan to run
|
28
|
+
<tt>rake doc:app</tt>.
|