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.
- checksums.yaml +4 -4
- data/{CONTRIBUTING.md → .github/CONTRIBUTING.md} +28 -9
- data/.github/ISSUE_TEMPLATE.md +13 -0
- data/.gitignore +2 -1
- data/.rubocop.yml +100 -0
- data/.rubocop_todo.yml +14 -0
- data/.travis.yml +8 -9
- data/Appraisals +41 -0
- data/CHANGELOG.md +49 -9
- data/Gemfile +1 -1
- data/README.md +130 -109
- data/Rakefile +19 -19
- data/doc/bug_report_template.rb +20 -14
- data/gemfiles/ar3.gemfile +10 -53
- data/gemfiles/ar4.gemfile +7 -0
- data/gemfiles/ar5.gemfile +13 -0
- data/lib/generators/paper_trail/install_generator.rb +26 -18
- data/lib/generators/paper_trail/templates/add_object_changes_to_versions.rb +4 -2
- data/lib/generators/paper_trail/templates/add_transaction_id_column_to_versions.rb +2 -0
- data/lib/generators/paper_trail/templates/create_version_associations.rb +9 -4
- data/lib/generators/paper_trail/templates/create_versions.rb +39 -5
- data/lib/paper_trail.rb +169 -146
- data/lib/paper_trail/attributes_serialization.rb +89 -17
- data/lib/paper_trail/cleaner.rb +15 -9
- data/lib/paper_trail/config.rb +28 -11
- data/lib/paper_trail/frameworks/active_record.rb +4 -0
- data/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb +5 -1
- data/lib/paper_trail/frameworks/active_record/models/paper_trail/version_association.rb +6 -2
- data/lib/paper_trail/frameworks/cucumber.rb +1 -0
- data/lib/paper_trail/frameworks/rails.rb +2 -7
- data/lib/paper_trail/frameworks/rails/controller.rb +29 -9
- data/lib/paper_trail/frameworks/rails/engine.rb +7 -1
- data/lib/paper_trail/frameworks/rspec.rb +5 -5
- data/lib/paper_trail/frameworks/rspec/helpers.rb +3 -1
- data/lib/paper_trail/frameworks/sinatra.rb +6 -4
- data/lib/paper_trail/has_paper_trail.rb +199 -106
- data/lib/paper_trail/record_history.rb +1 -3
- data/lib/paper_trail/reifier.rb +297 -118
- data/lib/paper_trail/serializers/json.rb +3 -3
- data/lib/paper_trail/serializers/yaml.rb +27 -8
- data/lib/paper_trail/version_association_concern.rb +3 -1
- data/lib/paper_trail/version_concern.rb +75 -35
- data/lib/paper_trail/version_number.rb +6 -9
- data/paper_trail.gemspec +44 -51
- data/spec/generators/install_generator_spec.rb +24 -25
- data/spec/generators/paper_trail/templates/create_versions_spec.rb +51 -0
- data/spec/models/animal_spec.rb +12 -12
- data/spec/models/boolit_spec.rb +8 -8
- data/spec/models/callback_modifier_spec.rb +47 -47
- data/spec/models/car_spec.rb +13 -0
- data/spec/models/fluxor_spec.rb +3 -3
- data/spec/models/gadget_spec.rb +19 -19
- data/spec/models/joined_version_spec.rb +3 -3
- data/spec/models/json_version_spec.rb +23 -24
- data/spec/models/kitchen/banana_spec.rb +3 -3
- data/spec/models/not_on_update_spec.rb +7 -4
- data/spec/models/post_with_status_spec.rb +13 -3
- data/spec/models/skipper_spec.rb +10 -10
- data/spec/models/thing_spec.rb +4 -4
- data/spec/models/truck_spec.rb +5 -0
- data/spec/models/vehicle_spec.rb +5 -0
- data/spec/models/version_spec.rb +103 -59
- data/spec/models/widget_spec.rb +82 -52
- data/spec/modules/paper_trail_spec.rb +2 -2
- data/spec/modules/version_concern_spec.rb +11 -12
- data/spec/modules/version_number_spec.rb +2 -4
- data/spec/paper_trail/config_spec.rb +10 -29
- data/spec/paper_trail_spec.rb +16 -14
- data/spec/rails_helper.rb +10 -9
- data/spec/requests/articles_spec.rb +11 -7
- data/spec/spec_helper.rb +41 -22
- data/spec/support/alt_db_init.rb +8 -13
- data/test/custom_json_serializer.rb +3 -3
- data/test/dummy/Rakefile +2 -2
- data/test/dummy/app/controllers/application_controller.rb +21 -8
- data/test/dummy/app/controllers/articles_controller.rb +11 -8
- data/test/dummy/app/controllers/widgets_controller.rb +13 -12
- data/test/dummy/app/models/animal.rb +1 -1
- data/test/dummy/app/models/article.rb +19 -11
- data/test/dummy/app/models/authorship.rb +1 -1
- data/test/dummy/app/models/bar_habtm.rb +4 -0
- data/test/dummy/app/models/book.rb +4 -4
- data/test/dummy/app/models/boolit.rb +1 -1
- data/test/dummy/app/models/callback_modifier.rb +6 -6
- data/test/dummy/app/models/car.rb +3 -0
- data/test/dummy/app/models/chapter.rb +4 -4
- data/test/dummy/app/models/customer.rb +1 -1
- data/test/dummy/app/models/document.rb +2 -2
- data/test/dummy/app/models/editor.rb +1 -1
- data/test/dummy/app/models/foo_habtm.rb +4 -0
- data/test/dummy/app/models/fruit.rb +2 -2
- data/test/dummy/app/models/gadget.rb +1 -1
- data/test/dummy/app/models/kitchen/banana.rb +1 -1
- data/test/dummy/app/models/legacy_widget.rb +2 -2
- data/test/dummy/app/models/line_item.rb +1 -1
- data/test/dummy/app/models/not_on_update.rb +1 -1
- data/test/dummy/app/models/person.rb +6 -6
- data/test/dummy/app/models/post.rb +1 -1
- data/test/dummy/app/models/post_with_status.rb +1 -1
- data/test/dummy/app/models/quotation.rb +1 -1
- data/test/dummy/app/models/section.rb +1 -1
- data/test/dummy/app/models/skipper.rb +2 -2
- data/test/dummy/app/models/song.rb +13 -4
- data/test/dummy/app/models/thing.rb +2 -2
- data/test/dummy/app/models/translation.rb +2 -2
- data/test/dummy/app/models/truck.rb +4 -0
- data/test/dummy/app/models/vehicle.rb +4 -0
- data/test/dummy/app/models/whatchamajigger.rb +1 -1
- data/test/dummy/app/models/widget.rb +7 -6
- data/test/dummy/app/versions/joined_version.rb +4 -3
- data/test/dummy/app/versions/json_version.rb +1 -1
- data/test/dummy/app/versions/kitchen/banana_version.rb +1 -1
- data/test/dummy/app/versions/post_version.rb +2 -2
- data/test/dummy/config.ru +1 -1
- data/test/dummy/config/application.rb +20 -9
- data/test/dummy/config/boot.rb +5 -5
- data/test/dummy/config/environment.rb +1 -1
- data/test/dummy/config/environments/development.rb +4 -3
- data/test/dummy/config/environments/production.rb +3 -2
- data/test/dummy/config/environments/test.rb +15 -5
- data/test/dummy/config/initializers/backtrace_silencers.rb +4 -2
- data/test/dummy/config/initializers/paper_trail.rb +1 -2
- data/test/dummy/config/initializers/secret_token.rb +3 -1
- data/test/dummy/config/initializers/session_store.rb +1 -1
- data/test/dummy/config/routes.rb +2 -2
- data/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +120 -74
- data/test/dummy/db/schema.rb +29 -6
- data/test/dummy/script/rails +6 -4
- data/test/functional/controller_test.rb +34 -35
- data/test/functional/enabled_for_controller_test.rb +6 -7
- data/test/functional/modular_sinatra_test.rb +43 -38
- data/test/functional/sinatra_test.rb +49 -40
- data/test/functional/thread_safety_test.rb +4 -6
- data/test/paper_trail_test.rb +15 -14
- data/test/test_helper.rb +68 -44
- data/test/time_travel_helper.rb +1 -15
- data/test/unit/associations_test.rb +517 -251
- data/test/unit/cleaner_test.rb +66 -60
- data/test/unit/inheritance_column_test.rb +17 -17
- data/test/unit/model_test.rb +611 -504
- data/test/unit/protected_attrs_test.rb +16 -12
- data/test/unit/serializer_test.rb +44 -43
- data/test/unit/serializers/json_test.rb +17 -18
- data/test/unit/serializers/mixin_json_test.rb +15 -14
- data/test/unit/serializers/mixin_yaml_test.rb +20 -16
- data/test/unit/serializers/yaml_test.rb +12 -13
- data/test/unit/timestamp_test.rb +10 -12
- data/test/unit/version_test.rb +7 -7
- metadata +92 -40
data/test/dummy/script/rails
CHANGED
@@ -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
|
-
|
5
|
-
|
6
|
-
|
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
|
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[
|
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
|
17
|
-
@request.env[
|
18
|
-
post :create, :
|
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
|
23
|
-
@request.env[
|
24
|
-
post :create, :
|
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, :
|
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
|
33
|
-
@request.env[
|
34
|
-
post :create, :
|
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, :
|
38
|
-
|
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
|
43
|
-
post :create, :
|
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
|
48
|
-
assert_equal
|
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
|
52
|
-
w = Widget.create :
|
50
|
+
test "update" do
|
51
|
+
w = Widget.create name: "Duvel"
|
53
52
|
assert_equal 1, w.versions.length
|
54
|
-
put :update, :
|
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
|
59
|
-
assert_equal
|
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
|
63
|
-
w = Widget.create :
|
61
|
+
test "destroy" do
|
62
|
+
w = Widget.create name: "Roundel"
|
64
63
|
assert_equal 1, w.versions.length
|
65
|
-
delete :destroy, :
|
64
|
+
delete :destroy, params_wrapper(id: w.id)
|
66
65
|
widget = assigns(:widget)
|
67
66
|
assert_equal 2, widget.versions.length
|
68
|
-
assert_equal
|
69
|
-
assert_equal
|
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
|
74
|
-
@request.env[
|
75
|
-
post :create, :
|
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
|
84
|
-
@request.env[
|
85
|
-
post :create, :
|
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
|
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
|
7
|
+
should "enabled_for_controller?.should == true" do
|
8
8
|
assert PaperTrail.enabled?
|
9
|
-
post :create, :
|
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
|
18
|
+
|
19
|
+
should "enabled_for_controller?.should == false" do
|
20
20
|
assert !PaperTrail.enabled?
|
21
|
-
post :create, :
|
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
|
-
|
2
|
-
|
3
|
-
|
4
|
-
#
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
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
|
-
|
17
|
-
|
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
|
-
|
30
|
+
class ModularSinatraTest < ActionDispatch::IntegrationTest
|
31
|
+
include Rack::Test::Methods
|
26
32
|
|
27
|
-
|
28
|
-
|
29
|
-
|
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
|
-
|
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
|
-
|
2
|
-
#
|
3
|
-
|
4
|
-
#
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
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
|
-
|
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
|
-
|
38
|
+
def app
|
39
|
+
@app ||= Sinatra::Application
|
40
|
+
end
|
37
41
|
|
38
|
-
|
39
|
-
|
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
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
class ThreadSafetyTest < ActionController::TestCase
|
4
|
-
test "
|
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
|
-
|
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 "
|
25
|
+
test "thread-safe when using #without_versioning" do
|
28
26
|
enabled = nil
|
29
27
|
|
30
28
|
slow_thread = Thread.new do
|
data/test/paper_trail_test.rb
CHANGED
@@ -1,38 +1,39 @@
|
|
1
|
-
require
|
1
|
+
require "test_helper"
|
2
2
|
|
3
3
|
class PaperTrailTest < ActiveSupport::TestCase
|
4
|
-
test
|
4
|
+
test "Sanity test" do
|
5
5
|
assert_kind_of Module, PaperTrail::Version
|
6
6
|
end
|
7
7
|
|
8
|
-
test
|
8
|
+
test "Version Number" do
|
9
9
|
assert PaperTrail.const_defined?(:VERSION)
|
10
10
|
end
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
PaperTrail.enabled = false
|
15
|
-
|
16
|
-
|
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
|
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
|
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(:
|
28
|
+
widget.update_attributes(name: "Bugle")
|
28
29
|
assert_equal 2, widget.versions.length
|
29
30
|
end
|
30
31
|
|
31
|
-
test
|
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(
|
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
|
data/test/test_helper.rb
CHANGED
@@ -1,13 +1,9 @@
|
|
1
|
-
|
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.
|
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",
|
14
|
+
require File.expand_path("../dummy/config/environment.rb", __FILE__)
|
19
15
|
require "rails/test_help"
|
20
|
-
require
|
21
|
-
require
|
22
|
-
require
|
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
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
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
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
52
|
+
teardown do
|
53
|
+
DatabaseCleaner.clean if using_mysql?
|
54
|
+
Thread.current[:paper_trail] = nil
|
55
|
+
end
|
46
56
|
|
47
|
-
|
57
|
+
private
|
48
58
|
|
49
|
-
|
50
|
-
|
51
|
-
|
59
|
+
def assert_attributes_equal(expected, actual)
|
60
|
+
if using_mysql?
|
61
|
+
expected = expected.dup
|
62
|
+
actual = actual.dup
|
52
63
|
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
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
|
-
|
61
|
-
|
71
|
+
assert_equal expected, actual
|
72
|
+
end
|
62
73
|
|
63
|
-
|
64
|
-
|
65
|
-
|
74
|
+
def assert_changes_equal(expected, actual)
|
75
|
+
if using_mysql?
|
76
|
+
expected = expected.dup
|
77
|
+
actual = actual.dup
|
66
78
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
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
|
-
|
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
|