activerecord-userstamp 2.1.1 → 3.0.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.
Files changed (72) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +5 -5
  3. data/.rspec +2 -2
  4. data/CHANGELOG.md +80 -0
  5. data/Gemfile +3 -3
  6. data/LICENSE +21 -21
  7. data/README.md +188 -220
  8. data/Rakefile +17 -17
  9. data/activerecord-userstamp.gemspec +34 -34
  10. data/lib/active_record/userstamp.rb +30 -14
  11. data/lib/active_record/userstamp/configuration.rb +49 -0
  12. data/lib/active_record/userstamp/controller_additions.rb +38 -41
  13. data/lib/active_record/userstamp/migration_additions.rb +14 -16
  14. data/lib/active_record/userstamp/model_additions.rb +10 -3
  15. data/lib/active_record/userstamp/stampable.rb +121 -174
  16. data/lib/active_record/userstamp/stamper.rb +47 -39
  17. data/lib/active_record/userstamp/utilities.rb +18 -0
  18. data/lib/active_record/userstamp/version.rb +4 -4
  19. data/lib/activerecord/userstamp.rb +1 -1
  20. data/spec/controllers/posts_controller_spec.rb +46 -46
  21. data/spec/controllers/users_controller_spec.rb +41 -41
  22. data/spec/dummy/README.rdoc +28 -28
  23. data/spec/dummy/Rakefile +6 -6
  24. data/spec/dummy/app/assets/javascripts/application.js +13 -13
  25. data/spec/dummy/app/assets/stylesheets/application.css +15 -15
  26. data/spec/dummy/app/controllers/application_controller.rb +13 -13
  27. data/spec/dummy/app/controllers/posts_controller.rb +32 -32
  28. data/spec/dummy/app/controllers/users_controller.rb +18 -18
  29. data/spec/dummy/app/helpers/application_helper.rb +2 -2
  30. data/spec/dummy/app/models/comment.rb +5 -6
  31. data/spec/dummy/app/models/person.rb +3 -4
  32. data/spec/dummy/app/models/post.rb +14 -13
  33. data/spec/dummy/app/models/user.rb +3 -4
  34. data/spec/dummy/app/views/layouts/application.html.erb +14 -14
  35. data/spec/dummy/bin/bundle +3 -3
  36. data/spec/dummy/bin/rails +4 -4
  37. data/spec/dummy/bin/rake +4 -4
  38. data/spec/dummy/bin/setup +29 -29
  39. data/spec/dummy/config.ru +4 -4
  40. data/spec/dummy/config/application.rb +30 -30
  41. data/spec/dummy/config/boot.rb +5 -5
  42. data/spec/dummy/config/database.yml +23 -25
  43. data/spec/dummy/config/environment.rb +5 -5
  44. data/spec/dummy/config/environments/development.rb +41 -41
  45. data/spec/dummy/config/environments/production.rb +79 -79
  46. data/spec/dummy/config/environments/test.rb +37 -37
  47. data/spec/dummy/config/initializers/assets.rb +11 -11
  48. data/spec/dummy/config/initializers/backtrace_silencers.rb +7 -7
  49. data/spec/dummy/config/initializers/cookies_serializer.rb +3 -3
  50. data/spec/dummy/config/initializers/filter_parameter_logging.rb +4 -4
  51. data/spec/dummy/config/initializers/inflections.rb +16 -16
  52. data/spec/dummy/config/initializers/mime_types.rb +4 -4
  53. data/spec/dummy/config/initializers/session_store.rb +3 -3
  54. data/spec/dummy/config/initializers/wrap_parameters.rb +14 -14
  55. data/spec/dummy/config/locales/en.yml +23 -23
  56. data/spec/dummy/config/routes.rb +56 -56
  57. data/spec/dummy/config/secrets.yml +22 -22
  58. data/spec/dummy/db/schema.rb +45 -55
  59. data/spec/dummy/public/404.html +67 -67
  60. data/spec/dummy/public/422.html +67 -67
  61. data/spec/dummy/public/500.html +66 -66
  62. data/spec/lib/configuration_spec.rb +20 -0
  63. data/spec/lib/migration_spec.rb +55 -12
  64. data/spec/lib/stamping_spec.rb +210 -170
  65. data/spec/lib/userstamp_spec.rb +7 -7
  66. data/spec/rails_helper.rb +7 -7
  67. data/spec/spec_helper.rb +97 -97
  68. data/spec/support/database_helpers.rb +20 -22
  69. metadata +6 -5
  70. data/CHANGELOG +0 -26
  71. data/spec/dummy/app/models/foo.rb +0 -3
  72. data/spec/lib/compatibility_stamping_spec.rb +0 -69
@@ -1,39 +1,47 @@
1
- module ActiveRecord::Userstamp
2
- module Stamper
3
- def self.included(base) # :nodoc:
4
- base.extend(ClassMethods)
5
- end
6
-
7
- module ClassMethods
8
- def model_stamper
9
- # don't allow multiple calls
10
- return if self.included_modules.include?(ActiveRecord::Userstamp::Stamper::InstanceMethods)
11
- send(:extend, ActiveRecord::Userstamp::Stamper::InstanceMethods)
12
- end
13
- end
14
-
15
- module InstanceMethods
16
- # Used to set the stamper for a particular request. See the Userstamp module for more
17
- # details on how to use this method.
18
- def stamper=(object)
19
- object_stamper = if object.is_a?(ActiveRecord::Base)
20
- object.send("#{object.class.primary_key}".to_sym)
21
- else
22
- object
23
- end
24
-
25
- Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = object_stamper
26
- end
27
-
28
- # Retrieves the existing stamper for the current request.
29
- def stamper
30
- Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"]
31
- end
32
-
33
- # Sets the stamper back to +nil+ to prepare for the next request.
34
- def reset_stamper
35
- Thread.current["#{self.to_s.downcase}_#{self.object_id}_stamper"] = nil
36
- end
37
- end
38
- end
39
- end
1
+ module ActiveRecord::Userstamp
2
+ module Stamper
3
+ extend ActiveSupport::Concern
4
+
5
+ included do
6
+ extend ClassMethods
7
+ end
8
+
9
+ module ClassMethods
10
+ def model_stamper
11
+ # don't allow multiple calls
12
+ return if self.included_modules.include?(ActiveRecord::Userstamp::Stamper::InstanceMethods)
13
+ send(:extend, ActiveRecord::Userstamp::Stamper::InstanceMethods)
14
+ end
15
+ end
16
+
17
+ module InstanceMethods
18
+ # Used to set the stamper for a particular request. See the Userstamp module for more
19
+ # details on how to use this method.
20
+ def stamper=(object)
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
29
+
30
+ # Retrieves the existing stamper for the current request.
31
+ def stamper
32
+ Thread.current[stamper_identifier]
33
+ end
34
+
35
+ # Sets the stamper back to +nil+ to prepare for the next request.
36
+ def reset_stamper
37
+ Thread.current[stamper_identifier] = nil
38
+ end
39
+
40
+ private
41
+
42
+ def stamper_identifier
43
+ "#{self.to_s.downcase}_#{self.object_id}_stamper"
44
+ end
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,18 @@
1
+ module ActiveRecord::Userstamp::Utilities
2
+ def self.remove_association(model, association)
3
+ methods = [
4
+ association,
5
+ "#{association}=",
6
+ "build_#{association}",
7
+ "create_#{association}",
8
+ "create_#{association}!"
9
+ ]
10
+
11
+ model.generated_association_methods.module_eval do
12
+ methods.each do |method|
13
+ remove_method(method) if method_defined?(method)
14
+ end
15
+ end
16
+ end
17
+ end
18
+
@@ -1,4 +1,4 @@
1
- module ActiveRecord; end
2
- module ActiveRecord::Userstamp
3
- VERSION = '2.1.1'
4
- end
1
+ module ActiveRecord; end
2
+ module ActiveRecord::Userstamp
3
+ VERSION = '3.0.0'
4
+ end
@@ -1 +1 @@
1
- require 'active_record/userstamp'
1
+ require 'active_record/userstamp'
@@ -1,46 +1,46 @@
1
- require 'rails_helper'
2
-
3
- RSpec.describe PostsController, type: :controller do
4
- controller do
5
- end
6
-
7
- before(:each) do
8
- reset_to_defaults
9
- end
10
-
11
- context 'when updating a Post' do
12
- it 'sets the correct updater' do
13
- request.session = { person_id: @delynn.id }
14
- post :update, id: @first_post.id, post: { title: 'Different' }
15
-
16
- expect(response.status).to eq(200)
17
- expect(controller.instance_variable_get(:@post).title).to eq('Different')
18
- expect(controller.instance_variable_get(:@post).updater).to eq(@delynn)
19
- end
20
- end
21
-
22
- context 'when handling multiple requests' do
23
- def simulate_second_request
24
- old_request_session = request.session
25
- request.session = { person_id: @nicole.id }
26
-
27
- post :update, id: @first_post.id, post: { title: 'Different Second'}
28
- expect(controller.instance_variable_get(:@post).updater).to eq(@nicole)
29
- ensure
30
- request.session = old_request_session
31
- end
32
-
33
- it 'sets the correct updater' do
34
- request.session = { person_id: @delynn.id }
35
- get :edit, id: @first_post.id
36
- expect(response.status).to eq(200)
37
-
38
- simulate_second_request
39
-
40
- post :update, id: @first_post.id, post: { title: 'Different' }
41
- expect(response.status).to eq(200)
42
- expect(controller.instance_variable_get(:@post).title).to eq('Different')
43
- expect(controller.instance_variable_get(:@post).updater).to eq(@delynn)
44
- end
45
- end
46
- end
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe PostsController, type: :controller do
4
+ controller do
5
+ end
6
+
7
+ before(:each) do
8
+ reset_to_defaults
9
+ end
10
+
11
+ context 'when updating a Post' do
12
+ it 'sets the correct updater' do
13
+ request.session = { person_id: @delynn.id }
14
+ post :update, id: @first_post.id, post: { title: 'Different' }
15
+
16
+ expect(response.status).to eq(200)
17
+ expect(controller.instance_variable_get(:@post).title).to eq('Different')
18
+ expect(controller.instance_variable_get(:@post).updater).to eq(@delynn)
19
+ end
20
+ end
21
+
22
+ context 'when handling multiple requests' do
23
+ def simulate_second_request
24
+ old_request_session = request.session
25
+ request.session = { person_id: @nicole.id }
26
+
27
+ post :update, id: @first_post.id, post: { title: 'Different Second'}
28
+ expect(controller.instance_variable_get(:@post).updater).to eq(@nicole)
29
+ ensure
30
+ request.session = old_request_session
31
+ end
32
+
33
+ it 'sets the correct updater' do
34
+ request.session = { person_id: @delynn.id }
35
+ get :edit, id: @first_post.id
36
+ expect(response.status).to eq(200)
37
+
38
+ simulate_second_request
39
+
40
+ post :update, id: @first_post.id, post: { title: 'Different' }
41
+ expect(response.status).to eq(200)
42
+ expect(controller.instance_variable_get(:@post).title).to eq('Different')
43
+ expect(controller.instance_variable_get(:@post).updater).to eq(@delynn)
44
+ end
45
+ end
46
+ end
@@ -1,41 +1,41 @@
1
- require 'rails_helper'
2
-
3
- RSpec.describe UsersController, type: :controller do
4
- controller do
5
- end
6
-
7
- before(:each) do
8
- reset_to_defaults
9
- end
10
-
11
- context 'when updating a User' do
12
- it 'sets the correct updater' do
13
- request.session = { user_id: @hera.id }
14
- patch :update, id: @hera.id, user: { name: 'Different'}
15
-
16
- expect(response.status).to eq(200)
17
- expect(controller.instance_variable_get(:@user).name).to eq('Different')
18
- expect(controller.instance_variable_get(:@user).updater).to eq(@hera)
19
- end
20
- end
21
-
22
- context 'when handling multiple requests' do
23
- def simulate_second_request
24
- old_request_session = request.session
25
- request.session = { user_id: @zeus.id }
26
-
27
- post :update, id: @hera.id, user: { name: 'Different Second' }
28
- expect(controller.instance_variable_get(:@user).updater).to eq(@zeus)
29
- ensure
30
- request.session = old_request_session
31
- end
32
-
33
- it 'sets the correct updater' do
34
- request.session = { user_id: @hera.id }
35
- get :edit, id: @hera.id
36
- expect(response.status).to eq(200)
37
-
38
- simulate_second_request
39
- end
40
- end
41
- end
1
+ require 'rails_helper'
2
+
3
+ RSpec.describe UsersController, type: :controller do
4
+ controller do
5
+ end
6
+
7
+ before(:each) do
8
+ reset_to_defaults
9
+ end
10
+
11
+ context 'when updating a User' do
12
+ it 'sets the correct updater' do
13
+ request.session = { user_id: @hera.id }
14
+ patch :update, id: @hera.id, user: { name: 'Different'}
15
+
16
+ expect(response.status).to eq(200)
17
+ expect(controller.instance_variable_get(:@user).name).to eq('Different')
18
+ expect(controller.instance_variable_get(:@user).updater).to eq(@hera)
19
+ end
20
+ end
21
+
22
+ context 'when handling multiple requests' do
23
+ def simulate_second_request
24
+ old_request_session = request.session
25
+ request.session = { user_id: @zeus.id }
26
+
27
+ post :update, id: @hera.id, user: { name: 'Different Second' }
28
+ expect(controller.instance_variable_get(:@user).updater).to eq(@zeus)
29
+ ensure
30
+ request.session = old_request_session
31
+ end
32
+
33
+ it 'sets the correct updater' do
34
+ request.session = { user_id: @hera.id }
35
+ get :edit, id: @hera.id
36
+ expect(response.status).to eq(200)
37
+
38
+ simulate_second_request
39
+ end
40
+ end
41
+ end
@@ -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>.
@@ -1,6 +1,6 @@
1
- # Add your own tasks in files placed in lib/tasks ending in .rake,
2
- # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
-
4
- require File.expand_path('../config/application', __FILE__)
5
-
6
- Rails.application.load_tasks
1
+ # Add your own tasks in files placed in lib/tasks ending in .rake,
2
+ # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
+
4
+ require File.expand_path('../config/application', __FILE__)
5
+
6
+ Rails.application.load_tasks
@@ -1,13 +1,13 @@
1
- // This is a manifest file that'll be compiled into application.js, which will include all the files
2
- // listed below.
3
- //
4
- // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
- // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
- //
7
- // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
- // compiled file.
9
- //
10
- // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
- // about supported directives.
12
- //
13
- //= require_tree .
1
+ // This is a manifest file that'll be compiled into application.js, which will include all the files
2
+ // listed below.
3
+ //
4
+ // Any JavaScript/Coffee file within this directory, lib/assets/javascripts, vendor/assets/javascripts,
5
+ // or any plugin's vendor/assets/javascripts directory can be referenced here using a relative path.
6
+ //
7
+ // It's not advisable to add code directly here, but if you do, it'll appear at the bottom of the
8
+ // compiled file.
9
+ //
10
+ // Read Sprockets README (https://github.com/rails/sprockets#sprockets-directives) for details
11
+ // about supported directives.
12
+ //
13
+ //= require_tree .
@@ -1,15 +1,15 @@
1
- /*
2
- * This is a manifest file that'll be compiled into application.css, which will include all the files
3
- * listed below.
4
- *
5
- * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
- * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
- *
8
- * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
- * compiled file so the styles you add here take precedence over styles defined in any styles
10
- * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
- * file per style scope.
12
- *
13
- *= require_tree .
14
- *= require_self
15
- */
1
+ /*
2
+ * This is a manifest file that'll be compiled into application.css, which will include all the files
3
+ * listed below.
4
+ *
5
+ * Any CSS and SCSS file within this directory, lib/assets/stylesheets, vendor/assets/stylesheets,
6
+ * or any plugin's vendor/assets/stylesheets directory can be referenced here using a relative path.
7
+ *
8
+ * You're free to add application-wide styles to this file and they'll appear at the bottom of the
9
+ * compiled file so the styles you add here take precedence over styles defined in any styles
10
+ * defined in the other CSS/SCSS files in this directory. It is generally better to create a new
11
+ * file per style scope.
12
+ *
13
+ *= require_tree .
14
+ *= require_self
15
+ */
@@ -1,13 +1,13 @@
1
- class ApplicationController < ActionController::Base
2
- include ActiveRecord::Userstamp::ControllerAdditions
3
-
4
- # Prevent CSRF attacks by raising an exception.
5
- # For APIs, you may want to use :null_session instead.
6
- protect_from_forgery with: :exception
7
-
8
- protected
9
-
10
- def current_user
11
- User.find(session[:user_id])
12
- end
13
- end
1
+ class ApplicationController < ActionController::Base
2
+ include ActiveRecord::Userstamp::ControllerAdditions
3
+
4
+ # Prevent CSRF attacks by raising an exception.
5
+ # For APIs, you may want to use :null_session instead.
6
+ protect_from_forgery with: :exception
7
+
8
+ protected
9
+
10
+ def current_user
11
+ User.find(session[:user_id])
12
+ end
13
+ end