paper_trail 4.2.0 → 5.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (149) hide show
  1. checksums.yaml +4 -4
  2. data/{CONTRIBUTING.md → .github/CONTRIBUTING.md} +28 -9
  3. data/.github/ISSUE_TEMPLATE.md +13 -0
  4. data/.gitignore +2 -1
  5. data/.rubocop.yml +100 -0
  6. data/.rubocop_todo.yml +14 -0
  7. data/.travis.yml +8 -9
  8. data/Appraisals +41 -0
  9. data/CHANGELOG.md +49 -9
  10. data/Gemfile +1 -1
  11. data/README.md +130 -109
  12. data/Rakefile +19 -19
  13. data/doc/bug_report_template.rb +20 -14
  14. data/gemfiles/ar3.gemfile +10 -53
  15. data/gemfiles/ar4.gemfile +7 -0
  16. data/gemfiles/ar5.gemfile +13 -0
  17. data/lib/generators/paper_trail/install_generator.rb +26 -18
  18. data/lib/generators/paper_trail/templates/add_object_changes_to_versions.rb +4 -2
  19. data/lib/generators/paper_trail/templates/add_transaction_id_column_to_versions.rb +2 -0
  20. data/lib/generators/paper_trail/templates/create_version_associations.rb +9 -4
  21. data/lib/generators/paper_trail/templates/create_versions.rb +39 -5
  22. data/lib/paper_trail.rb +169 -146
  23. data/lib/paper_trail/attributes_serialization.rb +89 -17
  24. data/lib/paper_trail/cleaner.rb +15 -9
  25. data/lib/paper_trail/config.rb +28 -11
  26. data/lib/paper_trail/frameworks/active_record.rb +4 -0
  27. data/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb +5 -1
  28. data/lib/paper_trail/frameworks/active_record/models/paper_trail/version_association.rb +6 -2
  29. data/lib/paper_trail/frameworks/cucumber.rb +1 -0
  30. data/lib/paper_trail/frameworks/rails.rb +2 -7
  31. data/lib/paper_trail/frameworks/rails/controller.rb +29 -9
  32. data/lib/paper_trail/frameworks/rails/engine.rb +7 -1
  33. data/lib/paper_trail/frameworks/rspec.rb +5 -5
  34. data/lib/paper_trail/frameworks/rspec/helpers.rb +3 -1
  35. data/lib/paper_trail/frameworks/sinatra.rb +6 -4
  36. data/lib/paper_trail/has_paper_trail.rb +199 -106
  37. data/lib/paper_trail/record_history.rb +1 -3
  38. data/lib/paper_trail/reifier.rb +297 -118
  39. data/lib/paper_trail/serializers/json.rb +3 -3
  40. data/lib/paper_trail/serializers/yaml.rb +27 -8
  41. data/lib/paper_trail/version_association_concern.rb +3 -1
  42. data/lib/paper_trail/version_concern.rb +75 -35
  43. data/lib/paper_trail/version_number.rb +6 -9
  44. data/paper_trail.gemspec +44 -51
  45. data/spec/generators/install_generator_spec.rb +24 -25
  46. data/spec/generators/paper_trail/templates/create_versions_spec.rb +51 -0
  47. data/spec/models/animal_spec.rb +12 -12
  48. data/spec/models/boolit_spec.rb +8 -8
  49. data/spec/models/callback_modifier_spec.rb +47 -47
  50. data/spec/models/car_spec.rb +13 -0
  51. data/spec/models/fluxor_spec.rb +3 -3
  52. data/spec/models/gadget_spec.rb +19 -19
  53. data/spec/models/joined_version_spec.rb +3 -3
  54. data/spec/models/json_version_spec.rb +23 -24
  55. data/spec/models/kitchen/banana_spec.rb +3 -3
  56. data/spec/models/not_on_update_spec.rb +7 -4
  57. data/spec/models/post_with_status_spec.rb +13 -3
  58. data/spec/models/skipper_spec.rb +10 -10
  59. data/spec/models/thing_spec.rb +4 -4
  60. data/spec/models/truck_spec.rb +5 -0
  61. data/spec/models/vehicle_spec.rb +5 -0
  62. data/spec/models/version_spec.rb +103 -59
  63. data/spec/models/widget_spec.rb +82 -52
  64. data/spec/modules/paper_trail_spec.rb +2 -2
  65. data/spec/modules/version_concern_spec.rb +11 -12
  66. data/spec/modules/version_number_spec.rb +2 -4
  67. data/spec/paper_trail/config_spec.rb +10 -29
  68. data/spec/paper_trail_spec.rb +16 -14
  69. data/spec/rails_helper.rb +10 -9
  70. data/spec/requests/articles_spec.rb +11 -7
  71. data/spec/spec_helper.rb +41 -22
  72. data/spec/support/alt_db_init.rb +8 -13
  73. data/test/custom_json_serializer.rb +3 -3
  74. data/test/dummy/Rakefile +2 -2
  75. data/test/dummy/app/controllers/application_controller.rb +21 -8
  76. data/test/dummy/app/controllers/articles_controller.rb +11 -8
  77. data/test/dummy/app/controllers/widgets_controller.rb +13 -12
  78. data/test/dummy/app/models/animal.rb +1 -1
  79. data/test/dummy/app/models/article.rb +19 -11
  80. data/test/dummy/app/models/authorship.rb +1 -1
  81. data/test/dummy/app/models/bar_habtm.rb +4 -0
  82. data/test/dummy/app/models/book.rb +4 -4
  83. data/test/dummy/app/models/boolit.rb +1 -1
  84. data/test/dummy/app/models/callback_modifier.rb +6 -6
  85. data/test/dummy/app/models/car.rb +3 -0
  86. data/test/dummy/app/models/chapter.rb +4 -4
  87. data/test/dummy/app/models/customer.rb +1 -1
  88. data/test/dummy/app/models/document.rb +2 -2
  89. data/test/dummy/app/models/editor.rb +1 -1
  90. data/test/dummy/app/models/foo_habtm.rb +4 -0
  91. data/test/dummy/app/models/fruit.rb +2 -2
  92. data/test/dummy/app/models/gadget.rb +1 -1
  93. data/test/dummy/app/models/kitchen/banana.rb +1 -1
  94. data/test/dummy/app/models/legacy_widget.rb +2 -2
  95. data/test/dummy/app/models/line_item.rb +1 -1
  96. data/test/dummy/app/models/not_on_update.rb +1 -1
  97. data/test/dummy/app/models/person.rb +6 -6
  98. data/test/dummy/app/models/post.rb +1 -1
  99. data/test/dummy/app/models/post_with_status.rb +1 -1
  100. data/test/dummy/app/models/quotation.rb +1 -1
  101. data/test/dummy/app/models/section.rb +1 -1
  102. data/test/dummy/app/models/skipper.rb +2 -2
  103. data/test/dummy/app/models/song.rb +13 -4
  104. data/test/dummy/app/models/thing.rb +2 -2
  105. data/test/dummy/app/models/translation.rb +2 -2
  106. data/test/dummy/app/models/truck.rb +4 -0
  107. data/test/dummy/app/models/vehicle.rb +4 -0
  108. data/test/dummy/app/models/whatchamajigger.rb +1 -1
  109. data/test/dummy/app/models/widget.rb +7 -6
  110. data/test/dummy/app/versions/joined_version.rb +4 -3
  111. data/test/dummy/app/versions/json_version.rb +1 -1
  112. data/test/dummy/app/versions/kitchen/banana_version.rb +1 -1
  113. data/test/dummy/app/versions/post_version.rb +2 -2
  114. data/test/dummy/config.ru +1 -1
  115. data/test/dummy/config/application.rb +20 -9
  116. data/test/dummy/config/boot.rb +5 -5
  117. data/test/dummy/config/environment.rb +1 -1
  118. data/test/dummy/config/environments/development.rb +4 -3
  119. data/test/dummy/config/environments/production.rb +3 -2
  120. data/test/dummy/config/environments/test.rb +15 -5
  121. data/test/dummy/config/initializers/backtrace_silencers.rb +4 -2
  122. data/test/dummy/config/initializers/paper_trail.rb +1 -2
  123. data/test/dummy/config/initializers/secret_token.rb +3 -1
  124. data/test/dummy/config/initializers/session_store.rb +1 -1
  125. data/test/dummy/config/routes.rb +2 -2
  126. data/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +120 -74
  127. data/test/dummy/db/schema.rb +29 -6
  128. data/test/dummy/script/rails +6 -4
  129. data/test/functional/controller_test.rb +34 -35
  130. data/test/functional/enabled_for_controller_test.rb +6 -7
  131. data/test/functional/modular_sinatra_test.rb +43 -38
  132. data/test/functional/sinatra_test.rb +49 -40
  133. data/test/functional/thread_safety_test.rb +4 -6
  134. data/test/paper_trail_test.rb +15 -14
  135. data/test/test_helper.rb +68 -44
  136. data/test/time_travel_helper.rb +1 -15
  137. data/test/unit/associations_test.rb +517 -251
  138. data/test/unit/cleaner_test.rb +66 -60
  139. data/test/unit/inheritance_column_test.rb +17 -17
  140. data/test/unit/model_test.rb +611 -504
  141. data/test/unit/protected_attrs_test.rb +16 -12
  142. data/test/unit/serializer_test.rb +44 -43
  143. data/test/unit/serializers/json_test.rb +17 -18
  144. data/test/unit/serializers/mixin_json_test.rb +15 -14
  145. data/test/unit/serializers/mixin_yaml_test.rb +20 -16
  146. data/test/unit/serializers/yaml_test.rb +12 -13
  147. data/test/unit/timestamp_test.rb +10 -12
  148. data/test/unit/version_test.rb +7 -7
  149. metadata +92 -40
@@ -1,6 +1,8 @@
1
1
  #!/usr/bin/env ruby
2
- # This command will automatically be run when you run "rails" with Rails 3 gems installed from the root of your application.
3
2
 
4
- APP_PATH = File.expand_path('../../config/application', __FILE__)
5
- require File.expand_path('../../config/boot', __FILE__)
6
- require 'rails/commands'
3
+ # This command will automatically be run when you run "rails" with Rails 3 gems
4
+ # installed from the root of your application.
5
+
6
+ APP_PATH = File.expand_path("../../config/application", __FILE__)
7
+ require File.expand_path("../../config/boot", __FILE__)
8
+ require "rails/commands"
@@ -1,10 +1,10 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
3
  class ControllerTest < ActionController::TestCase
4
4
  tests WidgetsController
5
5
 
6
6
  setup do
7
- @request.env['REMOTE_ADDR'] = '127.0.0.1'
7
+ @request.env["REMOTE_ADDR"] = "127.0.0.1"
8
8
  end
9
9
 
10
10
  # Mimick what RequestStore will do outside of the test env, since it is
@@ -13,66 +13,65 @@ class ControllerTest < ActionController::TestCase
13
13
  RequestStore.store[:paper_trail] = nil
14
14
  end
15
15
 
16
- test 'disable on create' do
17
- @request.env['HTTP_USER_AGENT'] = 'Disable User-Agent'
18
- post :create, :widget => { :name => 'Flugel' }
16
+ test "disable on create" do
17
+ @request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
18
+ post :create, params_wrapper(widget: { name: "Flugel" })
19
19
  assert_equal 0, assigns(:widget).versions.length
20
20
  end
21
21
 
22
- test 'disable on update' do
23
- @request.env['HTTP_USER_AGENT'] = 'Disable User-Agent'
24
- post :create, :widget => { :name => 'Flugel' }
22
+ test "disable on update" do
23
+ @request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
24
+ post :create, params_wrapper(widget: { name: "Flugel" })
25
25
  w = assigns(:widget)
26
26
  assert_equal 0, w.versions.length
27
- put :update, :id => w.id, :widget => { :name => 'Bugle' }
27
+ put :update, params_wrapper(id: w.id, widget: { name: "Bugle" })
28
28
  widget = assigns(:widget)
29
29
  assert_equal 0, widget.versions.length
30
30
  end
31
31
 
32
- test 'disable on destroy' do
33
- @request.env['HTTP_USER_AGENT'] = 'Disable User-Agent'
34
- post :create, :widget => { :name => 'Flugel' }
32
+ test "disable on destroy" do
33
+ @request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
34
+ post :create, params_wrapper(widget: { name: "Flugel" })
35
35
  w = assigns(:widget)
36
36
  assert_equal 0, w.versions.length
37
- delete :destroy, :id => w.id
38
- widget = assigns(:widget)
39
- assert_equal 0, PaperTrail::Version.with_item_keys('Widget', w.id).size
37
+ delete :destroy, params_wrapper(id: w.id)
38
+ assert_equal 0, PaperTrail::Version.with_item_keys("Widget", w.id).size
40
39
  end
41
40
 
42
- test 'create' do
43
- post :create, :widget => { :name => 'Flugel' }
41
+ test "create" do
42
+ post :create, params_wrapper(widget: { name: "Flugel" })
44
43
  widget = assigns(:widget)
45
44
  assert_equal 1, widget.versions.length
46
45
  assert_equal 153, widget.versions.last.whodunnit.to_i
47
- assert_equal '127.0.0.1', widget.versions.last.ip
48
- assert_equal 'Rails Testing', widget.versions.last.user_agent
46
+ assert_equal "127.0.0.1", widget.versions.last.ip
47
+ assert_equal "Rails Testing", widget.versions.last.user_agent
49
48
  end
50
49
 
51
- test 'update' do
52
- w = Widget.create :name => 'Duvel'
50
+ test "update" do
51
+ w = Widget.create name: "Duvel"
53
52
  assert_equal 1, w.versions.length
54
- put :update, :id => w.id, :widget => { :name => 'Bugle' }
53
+ put :update, params_wrapper(id: w.id, widget: { name: "Bugle" })
55
54
  widget = assigns(:widget)
56
55
  assert_equal 2, widget.versions.length
57
56
  assert_equal 153, widget.versions.last.whodunnit.to_i
58
- assert_equal '127.0.0.1', widget.versions.last.ip
59
- assert_equal 'Rails Testing', widget.versions.last.user_agent
57
+ assert_equal "127.0.0.1", widget.versions.last.ip
58
+ assert_equal "Rails Testing", widget.versions.last.user_agent
60
59
  end
61
60
 
62
- test 'destroy' do
63
- w = Widget.create :name => 'Roundel'
61
+ test "destroy" do
62
+ w = Widget.create name: "Roundel"
64
63
  assert_equal 1, w.versions.length
65
- delete :destroy, :id => w.id
64
+ delete :destroy, params_wrapper(id: w.id)
66
65
  widget = assigns(:widget)
67
66
  assert_equal 2, widget.versions.length
68
- assert_equal '127.0.0.1', widget.versions.last.ip
69
- assert_equal 'Rails Testing', widget.versions.last.user_agent
67
+ assert_equal "127.0.0.1", widget.versions.last.ip
68
+ assert_equal "Rails Testing", widget.versions.last.user_agent
70
69
  assert_equal 153, widget.versions.last.whodunnit.to_i
71
70
  end
72
71
 
73
- test "controller metadata methods should get evaluated if paper trail is enabled for controller" do
74
- @request.env['HTTP_USER_AGENT'] = 'User-Agent'
75
- post :create, :widget => { :name => 'Flugel' }
72
+ test "controller metadata methods should get evaluated if PT enabled for controller" do
73
+ @request.env["HTTP_USER_AGENT"] = "User-Agent"
74
+ post :create, params_wrapper(widget: { name: "Flugel" })
76
75
  assert PaperTrail.enabled_for_controller?
77
76
  assert_equal 153, PaperTrail.whodunnit
78
77
  assert PaperTrail.controller_info.present?
@@ -80,9 +79,9 @@ class ControllerTest < ActionController::TestCase
80
79
  assert PaperTrail.controller_info.keys.include?(:user_agent)
81
80
  end
82
81
 
83
- test "controller metadata methods should not get evaluated if paper trail is disabled for controller" do
84
- @request.env['HTTP_USER_AGENT'] = 'Disable User-Agent'
85
- post :create, :widget => { :name => 'Flugel' }
82
+ test "controller metadata methods should not get evaluated if PT disabled for controller" do
83
+ @request.env["HTTP_USER_AGENT"] = "Disable User-Agent"
84
+ post :create, params_wrapper(widget: { name: "Flugel" })
86
85
  assert_equal 0, assigns(:widget).versions.length
87
86
  assert !PaperTrail.enabled_for_controller?
88
87
  assert PaperTrail.whodunnit.nil?
@@ -1,12 +1,12 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
3
  class EnabledForControllerTest < ActionController::TestCase
4
4
  tests ArticlesController
5
5
 
6
6
  context "`PaperTrail.enabled? == true`" do
7
- should 'enabled_for_controller?.should == true' do
7
+ should "enabled_for_controller?.should == true" do
8
8
  assert PaperTrail.enabled?
9
- post :create, :article => { :title => 'Doh', :content => Faker::Lorem.sentence }
9
+ post :create, params_wrapper(article: { title: "Doh", content: FFaker::Lorem.sentence })
10
10
  assert_not_nil assigns(:article)
11
11
  assert PaperTrail.enabled_for_controller?
12
12
  assert_equal 1, assigns(:article).versions.length
@@ -15,15 +15,14 @@ class EnabledForControllerTest < ActionController::TestCase
15
15
 
16
16
  context "`PaperTrail.enabled? == false`" do
17
17
  setup { PaperTrail.enabled = false }
18
-
19
- should 'enabled_for_controller?.should == false' do
18
+
19
+ should "enabled_for_controller?.should == false" do
20
20
  assert !PaperTrail.enabled?
21
- post :create, :article => { :title => 'Doh', :content => Faker::Lorem.sentence }
21
+ post :create, params_wrapper(article: { title: "Doh", content: FFaker::Lorem.sentence })
22
22
  assert !PaperTrail.enabled_for_controller?
23
23
  assert_equal 0, assigns(:article).versions.length
24
24
  end
25
25
 
26
26
  teardown { PaperTrail.enabled = true }
27
27
  end
28
-
29
28
  end
@@ -1,48 +1,53 @@
1
- require 'test_helper'
2
- require 'sinatra/base'
3
-
4
- # --- Tests for modular `Sinatra::Base` style ----
5
- class BaseApp < Sinatra::Base
6
- configs = YAML.load_file(File.expand_path('../../dummy/config/database.yml', __FILE__))
7
- ActiveRecord::Base.configurations = configs
8
- ActiveRecord::Base.establish_connection(:test)
9
- register PaperTrail::Sinatra
10
-
11
- get '/test' do
12
- Widget.create!(:name => 'foo')
13
- 'Hello'
14
- end
1
+ # Our ActiveRecord 5 gemfile (see `Appraisals`) must use `rack 2.0.0.alpha`
2
+ # which renames several files that sinatra 1 depended on. Until there is
3
+ # a released version of sinatra that supports `rack 2.0.0.alpha`, we
4
+ # must exclude sinatra from our test suite. This is done in two files:
5
+ #
6
+ # - test/functional/sinatra_test.rb
7
+ # - test/functional/modular_sinatra_test.rb
8
+ #
9
+ if Gem::Version.new(Rack.release) < Gem::Version.new("2.0.0.alpha")
10
+ require "test_helper"
11
+ require "sinatra/base"
12
+
13
+ # --- Tests for modular `Sinatra::Base` style ----
14
+ class BaseApp < Sinatra::Base
15
+ configs = YAML.load_file(File.expand_path("../../dummy/config/database.yml", __FILE__))
16
+ ActiveRecord::Base.configurations = configs
17
+ ActiveRecord::Base.establish_connection(:test)
18
+ register PaperTrail::Sinatra
19
+
20
+ get "/test" do
21
+ Widget.create!(name: "foo")
22
+ "Hello"
23
+ end
15
24
 
16
- def current_user
17
- @current_user ||= OpenStruct.new(:id => 'foobar').tap do |obj|
18
- # Invoking `id` returns the `object_id` value in Ruby18 unless specifically overwritten
19
- def obj.id; 'foobar'; end if RUBY_VERSION < '1.9'
25
+ def current_user
26
+ @current_user ||= OpenStruct.new(id: "foobar")
20
27
  end
21
28
  end
22
- end
23
29
 
24
- class ModularSinatraTest < ActionDispatch::IntegrationTest
25
- include Rack::Test::Methods
30
+ class ModularSinatraTest < ActionDispatch::IntegrationTest
31
+ include Rack::Test::Methods
26
32
 
27
- def app
28
- @app ||= BaseApp
29
- end
30
-
31
- test 'baseline' do
32
- assert_nil Widget.create.versions.first.whodunnit
33
- end
33
+ def app
34
+ @app ||= BaseApp
35
+ end
34
36
 
35
- context "`PaperTrail::Sinatra` in a `Sinatra::Base` application" do
36
-
37
- should "sets the `user_for_paper_trail` from the `current_user` method" do
38
- get '/test'
39
- assert_equal 'Hello', last_response.body
40
- widget = Widget.last
41
- assert_not_nil widget
42
- assert_equal 'foo', widget.name
43
- assert_equal 1, widget.versions.size
44
- assert_equal 'foobar', widget.versions.first.whodunnit
37
+ test "baseline" do
38
+ assert_nil Widget.create.versions.first.whodunnit
45
39
  end
46
40
 
41
+ context "`PaperTrail::Sinatra` in a `Sinatra::Base` application" do
42
+ should "sets the `user_for_paper_trail` from the `current_user` method" do
43
+ get "/test"
44
+ assert_equal "Hello", last_response.body
45
+ widget = Widget.last
46
+ assert_not_nil widget
47
+ assert_equal "foo", widget.name
48
+ assert_equal 1, widget.versions.size
49
+ assert_equal "foobar", widget.versions.first.whodunnit
50
+ end
51
+ end
47
52
  end
48
53
  end
@@ -1,49 +1,58 @@
1
- require 'test_helper'
2
- # require 'sinatra/main'
3
-
4
- # --- Tests for non-modular `Sinatra::Application` style ----
5
- class Sinatra::Application
6
- configs = YAML.load_file(File.expand_path('../../dummy/config/database.yml', __FILE__))
7
- ActiveRecord::Base.configurations = configs
8
- ActiveRecord::Base.establish_connection(:test)
9
- register PaperTrail::Sinatra # we shouldn't actually need this line if I'm not mistaken but the tests seem to fail without it ATM
10
-
11
- get '/test' do
12
- Widget.create!(:name => 'bar')
13
- 'Hai'
14
- end
15
-
16
- def current_user
17
- @current_user ||= OpenStruct.new(:id => 'raboof').tap do |obj|
18
- # Invoking `id` returns the `object_id` value in Ruby18 unless specifically overwritten
19
- def obj.id; 'raboof'; end if RUBY_VERSION < '1.9'
1
+ # Our ActiveRecord 5 gemfile (see `Appraisals`) must use `rack 2.0.0.alpha`
2
+ # which renames several files that sinatra 1 depended on. Until there is
3
+ # a released version of sinatra that supports `rack 2.0.0.alpha`, we
4
+ # must exclude sinatra from our test suite. This is done in two files:
5
+ #
6
+ # - test/functional/sinatra_test.rb
7
+ # - test/functional/modular_sinatra_test.rb
8
+ #
9
+ if Gem::Version.new(Rack.release) < Gem::Version.new("2.0.0.alpha")
10
+ require "test_helper"
11
+ # require 'sinatra/main'
12
+
13
+ # --- Tests for non-modular `Sinatra::Application` style ----
14
+ module Sinatra
15
+ class Application
16
+ configs = YAML.load_file(File.expand_path("../../dummy/config/database.yml", __FILE__))
17
+ ActiveRecord::Base.configurations = configs
18
+ ActiveRecord::Base.establish_connection(:test)
19
+
20
+ # We shouldn't actually need this line if I'm not mistaken but the tests
21
+ # seem to fail without it ATM
22
+ register PaperTrail::Sinatra
23
+
24
+ get "/test" do
25
+ Widget.create!(name: "bar")
26
+ "Hai"
27
+ end
28
+
29
+ def current_user
30
+ @current_user ||= OpenStruct.new(id: "raboof")
31
+ end
20
32
  end
21
33
  end
22
34
 
23
- end
24
-
25
- class SinatraTest < ActionDispatch::IntegrationTest
26
- include Rack::Test::Methods
27
-
28
- def app
29
- @app ||= Sinatra::Application
30
- end
31
-
32
- test 'baseline' do
33
- assert_nil Widget.create.versions.first.whodunnit
34
- end
35
+ class SinatraTest < ActionDispatch::IntegrationTest
36
+ include Rack::Test::Methods
35
37
 
36
- context "`PaperTrail::Sinatra` in a `Sinatra::Application` application" do
38
+ def app
39
+ @app ||= Sinatra::Application
40
+ end
37
41
 
38
- should "sets the `user_for_paper_trail` from the `current_user` method" do
39
- get '/test'
40
- assert_equal 'Hai', last_response.body
41
- widget = Widget.last
42
- assert_not_nil widget
43
- assert_equal 'bar', widget.name
44
- assert_equal 1, widget.versions.size
45
- assert_equal 'raboof', widget.versions.first.whodunnit
42
+ test "baseline" do
43
+ assert_nil Widget.create.versions.first.whodunnit
46
44
  end
47
45
 
46
+ context "`PaperTrail::Sinatra` in a `Sinatra::Application` application" do
47
+ should "sets the `user_for_paper_trail` from the `current_user` method" do
48
+ get "/test"
49
+ assert_equal "Hai", last_response.body
50
+ widget = Widget.last
51
+ assert_not_nil widget
52
+ assert_equal "bar", widget.name
53
+ assert_equal 1, widget.versions.size
54
+ assert_equal "raboof", widget.versions.first.whodunnit
55
+ end
56
+ end
48
57
  end
49
58
  end
@@ -1,15 +1,13 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
3
  class ThreadSafetyTest < ActionController::TestCase
4
- test "be thread safe when using #set_paper_trail_whodunnit" do
4
+ test "thread-safe when using #set_paper_trail_whodunnit" do
5
5
  blocked = true
6
6
 
7
7
  slow_thread = Thread.new do
8
8
  controller = TestController.new
9
9
  controller.send :set_paper_trail_whodunnit
10
- begin
11
- sleep 0.001
12
- end while blocked
10
+ sleep 0.001 while blocked
13
11
  PaperTrail.whodunnit
14
12
  end
15
13
 
@@ -24,7 +22,7 @@ class ThreadSafetyTest < ActionController::TestCase
24
22
  assert_not_equal slow_thread.value, fast_thread.value
25
23
  end
26
24
 
27
- test "be thread safe when using #without_versioning" do
25
+ test "thread-safe when using #without_versioning" do
28
26
  enabled = nil
29
27
 
30
28
  slow_thread = Thread.new do
@@ -1,38 +1,39 @@
1
- require 'test_helper'
1
+ require "test_helper"
2
2
 
3
3
  class PaperTrailTest < ActiveSupport::TestCase
4
- test 'Sanity test' do
4
+ test "Sanity test" do
5
5
  assert_kind_of Module, PaperTrail::Version
6
6
  end
7
7
 
8
- test 'Version Number' do
8
+ test "Version Number" do
9
9
  assert PaperTrail.const_defined?(:VERSION)
10
10
  end
11
-
12
- test 'enabled is thread-safe' do
13
- Thread.new do
14
- PaperTrail.enabled = false
15
- end.join
16
- assert PaperTrail.enabled?
11
+
12
+ context "setting enabled" do
13
+ should "affect all threads" do
14
+ Thread.new { PaperTrail.enabled = false }.join
15
+ assert_equal false, PaperTrail.enabled?
16
+ end
17
+ teardown { PaperTrail.enabled = true }
17
18
  end
18
19
 
19
- test 'create with plain model class' do
20
+ test "create with plain model class" do
20
21
  widget = Widget.create
21
22
  assert_equal 1, widget.versions.length
22
23
  end
23
24
 
24
- test 'update with plain model class' do
25
+ test "update with plain model class" do
25
26
  widget = Widget.create
26
27
  assert_equal 1, widget.versions.length
27
- widget.update_attributes(:name => 'Bugle')
28
+ widget.update_attributes(name: "Bugle")
28
29
  assert_equal 2, widget.versions.length
29
30
  end
30
31
 
31
- test 'destroy with plain model class' do
32
+ test "destroy with plain model class" do
32
33
  widget = Widget.create
33
34
  assert_equal 1, widget.versions.length
34
35
  widget.destroy
35
- versions_for_widget = PaperTrail::Version.with_item_keys('Widget', widget.id)
36
+ versions_for_widget = PaperTrail::Version.with_item_keys("Widget", widget.id)
36
37
  assert_equal 2, versions_for_widget.length
37
38
  end
38
39
  end
@@ -1,13 +1,9 @@
1
- begin
2
- require 'pry-nav'
3
- rescue LoadError
4
- # It's OK, we don't include pry in e.g. gemfiles/3.0.gemfile
5
- end
1
+ require "pry-nav"
6
2
 
7
3
  ENV["RAILS_ENV"] = "test"
8
4
  ENV["DB"] ||= "sqlite"
9
5
 
10
- unless File.exists?(File.expand_path('../../test/dummy/config/database.yml', __FILE__))
6
+ unless File.exist?(File.expand_path("../../test/dummy/config/database.yml", __FILE__))
11
7
  warn "WARNING: No database.yml detected for the dummy app, please run `rake prepare` first"
12
8
  end
13
9
 
@@ -15,11 +11,20 @@ def using_mysql?
15
11
  @using_mysql ||= ActiveRecord::Base.connection_config[:adapter].to_sym == :mysql2
16
12
  end
17
13
 
18
- require File.expand_path("../dummy/config/environment.rb", __FILE__)
14
+ require File.expand_path("../dummy/config/environment.rb", __FILE__)
19
15
  require "rails/test_help"
20
- require 'shoulda'
21
- require 'ffaker'
22
- require 'database_cleaner'
16
+ require "shoulda"
17
+ require "ffaker"
18
+ require "database_cleaner"
19
+
20
+ def active_record_gem_version
21
+ Gem::Version.new(ActiveRecord::VERSION::STRING)
22
+ end
23
+
24
+ if active_record_gem_version >= Gem::Version.new("5.0.0.beta1")
25
+ # See https://github.com/rails/rails-controller-testing/issues/5
26
+ ActionController::TestCase.send(:include, Rails::Controller::Testing::TestProcess)
27
+ end
23
28
 
24
29
  Rails.backtrace_cleaner.remove_silencers!
25
30
 
@@ -33,45 +38,53 @@ Dir["#{File.dirname(__FILE__)}/support/**/*.rb"].each { |f| require f }
33
38
  DatabaseCleaner.strategy = :truncation
34
39
 
35
40
  # global setup block resetting Thread.current
36
- class ActiveSupport::TestCase
37
- if using_mysql?
38
- self.use_transactional_fixtures = false
39
- setup { DatabaseCleaner.start }
40
- end
41
+ module ActiveSupport
42
+ class TestCase
43
+ if using_mysql?
44
+ if respond_to? :use_transactional_tests=
45
+ self.use_transactional_tests = false
46
+ else
47
+ self.use_transactional_fixtures = false
48
+ end
49
+ setup { DatabaseCleaner.start }
50
+ end
41
51
 
42
- teardown do
43
- DatabaseCleaner.clean if using_mysql?
44
- Thread.current[:paper_trail] = nil
45
- end
52
+ teardown do
53
+ DatabaseCleaner.clean if using_mysql?
54
+ Thread.current[:paper_trail] = nil
55
+ end
46
56
 
47
- private
57
+ private
48
58
 
49
- def assert_attributes_equal(expected, actual)
50
- if using_mysql?
51
- expected, actual = expected.dup, actual.dup
59
+ def assert_attributes_equal(expected, actual)
60
+ if using_mysql?
61
+ expected = expected.dup
62
+ actual = actual.dup
52
63
 
53
- # Adjust timestamps for missing fractional seconds precision.
54
- %w(created_at updated_at).each do |timestamp|
55
- expected[timestamp] = expected[timestamp].change(:usec => 0)
56
- actual[timestamp] = actual[timestamp].change(:usec => 0)
64
+ # Adjust timestamps for missing fractional seconds precision.
65
+ %w(created_at updated_at).each do |timestamp|
66
+ expected[timestamp] = expected[timestamp].change(usec: 0)
67
+ actual[timestamp] = actual[timestamp].change(usec: 0)
68
+ end
57
69
  end
58
- end
59
70
 
60
- assert_equal expected, actual
61
- end
71
+ assert_equal expected, actual
72
+ end
62
73
 
63
- def assert_changes_equal(expected, actual)
64
- if using_mysql?
65
- expected, actual = expected.dup, actual.dup
74
+ def assert_changes_equal(expected, actual)
75
+ if using_mysql?
76
+ expected = expected.dup
77
+ actual = actual.dup
66
78
 
67
- # Adjust timestamps for missing fractional seconds precision.
68
- %w(created_at updated_at).each do |timestamp|
69
- expected[timestamp][1] = expected[timestamp][1].change(:usec => 0)
70
- actual[timestamp][1] = actual[timestamp][1].change(:usec => 0)
79
+ # Adjust timestamps for missing fractional seconds precision.
80
+ %w(created_at updated_at).each do |timestamp|
81
+ expected[timestamp][1] = expected[timestamp][1].change(usec: 0)
82
+ actual[timestamp][1] = actual[timestamp][1].change(usec: 0)
83
+ end
71
84
  end
72
- end
73
85
 
74
- assert_equal expected, actual
86
+ assert_equal expected, actual
87
+ end
75
88
  end
76
89
  end
77
90
 
@@ -89,6 +102,22 @@ def change_schema
89
102
  reset_version_class_column_info!
90
103
  end
91
104
 
105
+ # Wrap args in a hash to support the ActionController::TestCase and
106
+ # ActionDispatch::Integration HTTP request method switch to keyword args
107
+ # (see https://github.com/rails/rails/blob/master/actionpack/CHANGELOG.md)
108
+ def params_wrapper(args)
109
+ if defined?(::Rails) && active_record_gem_version >= Gem::Version.new("5.0.0.beta1")
110
+ { params: args }
111
+ else
112
+ args
113
+ end
114
+ end
115
+
116
+ def reset_version_class_column_info!
117
+ PaperTrail::Version.connection.schema_cache.clear!
118
+ PaperTrail::Version.reset_column_information
119
+ end
120
+
92
121
  def restore_schema
93
122
  ActiveRecord::Migration.verbose = false
94
123
  ActiveRecord::Schema.define do
@@ -98,8 +127,3 @@ def restore_schema
98
127
  ActiveRecord::Migration.verbose = true
99
128
  reset_version_class_column_info!
100
129
  end
101
-
102
- def reset_version_class_column_info!
103
- PaperTrail::Version.connection.schema_cache.clear!
104
- PaperTrail::Version.reset_column_information
105
- end