paper_trail 4.0.0 → 5.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (158) hide show
  1. checksums.yaml +4 -4
  2. data/.github/CONTRIBUTING.md +105 -0
  3. data/.github/ISSUE_TEMPLATE.md +13 -0
  4. data/.gitignore +2 -0
  5. data/.rubocop.yml +100 -0
  6. data/.rubocop_todo.yml +14 -0
  7. data/.travis.yml +11 -10
  8. data/Appraisals +37 -0
  9. data/CHANGELOG.md +173 -8
  10. data/Gemfile +1 -1
  11. data/README.md +641 -470
  12. data/Rakefile +19 -19
  13. data/doc/bug_report_template.rb +71 -0
  14. data/doc/warning_about_not_setting_whodunnit.md +32 -0
  15. data/gemfiles/ar3.gemfile +18 -0
  16. data/gemfiles/ar4.gemfile +7 -0
  17. data/gemfiles/ar5.gemfile +13 -0
  18. data/lib/generators/paper_trail/install_generator.rb +26 -18
  19. data/lib/generators/paper_trail/templates/add_object_changes_to_versions.rb +3 -1
  20. data/lib/generators/paper_trail/templates/add_transaction_id_column_to_versions.rb +2 -0
  21. data/lib/generators/paper_trail/templates/create_version_associations.rb +9 -4
  22. data/lib/generators/paper_trail/templates/create_versions.rb +53 -5
  23. data/lib/paper_trail/attribute_serializers/README.md +10 -0
  24. data/lib/paper_trail/attribute_serializers/cast_attribute_serializer.rb +58 -0
  25. data/lib/paper_trail/attribute_serializers/legacy_active_record_shim.rb +48 -0
  26. data/lib/paper_trail/attribute_serializers/object_attribute.rb +39 -0
  27. data/lib/paper_trail/attribute_serializers/object_changes_attribute.rb +42 -0
  28. data/lib/paper_trail/cleaner.rb +41 -18
  29. data/lib/paper_trail/config.rb +42 -26
  30. data/lib/paper_trail/frameworks/active_record/models/paper_trail/version.rb +5 -1
  31. data/lib/paper_trail/frameworks/active_record/models/paper_trail/version_association.rb +6 -2
  32. data/lib/paper_trail/frameworks/active_record.rb +2 -2
  33. data/lib/paper_trail/frameworks/cucumber.rb +1 -0
  34. data/lib/paper_trail/frameworks/rails/controller.rb +50 -14
  35. data/lib/paper_trail/frameworks/rails/engine.rb +6 -1
  36. data/lib/paper_trail/frameworks/rails.rb +2 -7
  37. data/lib/paper_trail/frameworks/rspec/helpers.rb +3 -1
  38. data/lib/paper_trail/frameworks/rspec.rb +5 -5
  39. data/lib/paper_trail/frameworks/sinatra.rb +8 -5
  40. data/lib/paper_trail/has_paper_trail.rb +381 -221
  41. data/lib/paper_trail/record_history.rb +57 -0
  42. data/lib/paper_trail/reifier.rb +450 -0
  43. data/lib/paper_trail/serializers/json.rb +7 -7
  44. data/lib/paper_trail/serializers/yaml.rb +31 -12
  45. data/lib/paper_trail/version_association_concern.rb +6 -2
  46. data/lib/paper_trail/version_concern.rb +200 -287
  47. data/lib/paper_trail/version_number.rb +6 -9
  48. data/lib/paper_trail.rb +169 -137
  49. data/paper_trail.gemspec +41 -43
  50. data/spec/generators/install_generator_spec.rb +24 -25
  51. data/spec/generators/paper_trail/templates/create_versions_spec.rb +51 -0
  52. data/spec/models/animal_spec.rb +23 -6
  53. data/spec/models/boolit_spec.rb +8 -8
  54. data/spec/models/callback_modifier_spec.rb +96 -0
  55. data/spec/models/car_spec.rb +13 -0
  56. data/spec/models/fluxor_spec.rb +3 -3
  57. data/spec/models/gadget_spec.rb +19 -19
  58. data/spec/models/joined_version_spec.rb +3 -3
  59. data/spec/models/json_version_spec.rb +50 -28
  60. data/spec/models/kitchen/banana_spec.rb +3 -3
  61. data/spec/models/not_on_update_spec.rb +7 -4
  62. data/spec/models/post_with_status_spec.rb +13 -3
  63. data/spec/models/skipper_spec.rb +40 -11
  64. data/spec/models/thing_spec.rb +4 -4
  65. data/spec/models/truck_spec.rb +5 -0
  66. data/spec/models/vehicle_spec.rb +5 -0
  67. data/spec/models/version_spec.rb +103 -59
  68. data/spec/models/widget_spec.rb +86 -55
  69. data/spec/modules/paper_trail_spec.rb +2 -2
  70. data/spec/modules/version_concern_spec.rb +11 -12
  71. data/spec/modules/version_number_spec.rb +3 -4
  72. data/spec/paper_trail/config_spec.rb +33 -0
  73. data/spec/paper_trail_spec.rb +16 -14
  74. data/spec/rails_helper.rb +10 -9
  75. data/spec/requests/articles_spec.rb +11 -7
  76. data/spec/spec_helper.rb +42 -17
  77. data/spec/support/alt_db_init.rb +8 -13
  78. data/test/custom_json_serializer.rb +3 -3
  79. data/test/dummy/Rakefile +2 -2
  80. data/test/dummy/app/controllers/application_controller.rb +21 -8
  81. data/test/dummy/app/controllers/articles_controller.rb +11 -8
  82. data/test/dummy/app/controllers/widgets_controller.rb +13 -12
  83. data/test/dummy/app/models/animal.rb +1 -1
  84. data/test/dummy/app/models/article.rb +19 -11
  85. data/test/dummy/app/models/authorship.rb +1 -1
  86. data/test/dummy/app/models/bar_habtm.rb +4 -0
  87. data/test/dummy/app/models/book.rb +4 -4
  88. data/test/dummy/app/models/boolit.rb +1 -1
  89. data/test/dummy/app/models/callback_modifier.rb +45 -0
  90. data/test/dummy/app/models/car.rb +3 -0
  91. data/test/dummy/app/models/chapter.rb +9 -0
  92. data/test/dummy/app/models/citation.rb +5 -0
  93. data/test/dummy/app/models/customer.rb +1 -1
  94. data/test/dummy/app/models/document.rb +2 -2
  95. data/test/dummy/app/models/editor.rb +1 -1
  96. data/test/dummy/app/models/foo_habtm.rb +5 -0
  97. data/test/dummy/app/models/fruit.rb +2 -2
  98. data/test/dummy/app/models/gadget.rb +1 -1
  99. data/test/dummy/app/models/kitchen/banana.rb +1 -1
  100. data/test/dummy/app/models/legacy_widget.rb +2 -2
  101. data/test/dummy/app/models/line_item.rb +1 -1
  102. data/test/dummy/app/models/not_on_update.rb +1 -1
  103. data/test/dummy/app/models/paragraph.rb +5 -0
  104. data/test/dummy/app/models/person.rb +6 -6
  105. data/test/dummy/app/models/post.rb +1 -1
  106. data/test/dummy/app/models/post_with_status.rb +1 -1
  107. data/test/dummy/app/models/quotation.rb +5 -0
  108. data/test/dummy/app/models/section.rb +6 -0
  109. data/test/dummy/app/models/skipper.rb +2 -2
  110. data/test/dummy/app/models/song.rb +13 -4
  111. data/test/dummy/app/models/thing.rb +2 -2
  112. data/test/dummy/app/models/translation.rb +2 -2
  113. data/test/dummy/app/models/truck.rb +4 -0
  114. data/test/dummy/app/models/vehicle.rb +4 -0
  115. data/test/dummy/app/models/whatchamajigger.rb +1 -1
  116. data/test/dummy/app/models/widget.rb +7 -6
  117. data/test/dummy/app/versions/joined_version.rb +4 -3
  118. data/test/dummy/app/versions/json_version.rb +1 -1
  119. data/test/dummy/app/versions/kitchen/banana_version.rb +1 -1
  120. data/test/dummy/app/versions/post_version.rb +2 -2
  121. data/test/dummy/config/application.rb +20 -9
  122. data/test/dummy/config/boot.rb +5 -5
  123. data/test/dummy/config/database.postgres.yml +1 -1
  124. data/test/dummy/config/environment.rb +1 -1
  125. data/test/dummy/config/environments/development.rb +4 -3
  126. data/test/dummy/config/environments/production.rb +3 -2
  127. data/test/dummy/config/environments/test.rb +15 -5
  128. data/test/dummy/config/initializers/backtrace_silencers.rb +4 -2
  129. data/test/dummy/config/initializers/paper_trail.rb +4 -3
  130. data/test/dummy/config/initializers/secret_token.rb +3 -1
  131. data/test/dummy/config/initializers/session_store.rb +1 -1
  132. data/test/dummy/config/routes.rb +2 -2
  133. data/test/dummy/config.ru +1 -1
  134. data/test/dummy/db/migrate/20110208155312_set_up_test_tables.rb +148 -68
  135. data/test/dummy/db/schema.rb +119 -31
  136. data/test/dummy/script/rails +6 -4
  137. data/test/functional/controller_test.rb +34 -35
  138. data/test/functional/enabled_for_controller_test.rb +6 -7
  139. data/test/functional/modular_sinatra_test.rb +43 -38
  140. data/test/functional/sinatra_test.rb +49 -40
  141. data/test/functional/thread_safety_test.rb +4 -6
  142. data/test/paper_trail_test.rb +15 -14
  143. data/test/test_helper.rb +78 -18
  144. data/test/time_travel_helper.rb +1 -15
  145. data/test/unit/associations_test.rb +1016 -0
  146. data/test/unit/cleaner_test.rb +66 -60
  147. data/test/unit/inheritance_column_test.rb +19 -19
  148. data/test/unit/model_test.rb +646 -1071
  149. data/test/unit/protected_attrs_test.rb +19 -14
  150. data/test/unit/serializer_test.rb +44 -43
  151. data/test/unit/serializers/json_test.rb +28 -21
  152. data/test/unit/serializers/mixin_json_test.rb +15 -14
  153. data/test/unit/serializers/mixin_yaml_test.rb +20 -16
  154. data/test/unit/serializers/yaml_test.rb +16 -14
  155. data/test/unit/timestamp_test.rb +10 -12
  156. data/test/unit/version_test.rb +88 -70
  157. metadata +166 -72
  158. data/gemfiles/3.0.gemfile +0 -52
@@ -0,0 +1,33 @@
1
+ require "rails_helper"
2
+
3
+ module PaperTrail
4
+ RSpec.describe Config do
5
+ describe ".instance" do
6
+ it "returns the singleton instance" do
7
+ expect { described_class.instance }.to_not raise_error
8
+ end
9
+ end
10
+
11
+ describe ".new" do
12
+ it "raises NoMethodError" do
13
+ expect { described_class.new }.to raise_error(NoMethodError)
14
+ end
15
+ end
16
+
17
+ describe "track_associations?" do
18
+ context "@track_associations is nil" do
19
+ after do
20
+ PaperTrail.config.track_associations = true
21
+ end
22
+
23
+ it "returns false and prints a deprecation warning" do
24
+ config = described_class.instance
25
+ config.track_associations = nil
26
+ expect {
27
+ expect(config.track_associations?).to eq(false)
28
+ }.to output(/DEPRECATION WARNING/).to_stderr
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -1,11 +1,12 @@
1
- require 'rails_helper'
1
+ require "rails_helper"
2
2
 
3
- describe "PaperTrail RSpec Helper" do
4
- context 'default' do
5
- it 'should have versioning off by default' do
3
+ describe PaperTrail do
4
+ context "default" do
5
+ it "should have versioning off by default" do
6
6
  expect(PaperTrail).not_to be_enabled
7
7
  end
8
- it 'should turn versioning on in a `with_versioning` block' do
8
+
9
+ it "should turn versioning on in a `with_versioning` block" do
9
10
  expect(PaperTrail).not_to be_enabled
10
11
  with_versioning do
11
12
  expect(PaperTrail).to be_enabled
@@ -16,17 +17,18 @@ describe "PaperTrail RSpec Helper" do
16
17
  context "error within `with_versioning` block" do
17
18
  it "should revert the value of `PaperTrail.enabled?` to it's previous state" do
18
19
  expect(PaperTrail).not_to be_enabled
19
- expect { with_versioning { raise } }.to raise_error
20
+ expect { with_versioning { raise } }.to raise_error(RuntimeError)
20
21
  expect(PaperTrail).not_to be_enabled
21
22
  end
22
23
  end
23
24
  end
24
25
 
25
- context '`versioning: true`', :versioning => true do
26
- it 'should have versioning on by default' do
26
+ context "`versioning: true`", versioning: true do
27
+ it "should have versioning on by default" do
27
28
  expect(PaperTrail).to be_enabled
28
29
  end
29
- it 'should keep versioning on after a with_versioning block' do
30
+
31
+ it "should keep versioning on after a with_versioning block" do
30
32
  expect(PaperTrail).to be_enabled
31
33
  with_versioning do
32
34
  expect(PaperTrail).to be_enabled
@@ -35,21 +37,21 @@ describe "PaperTrail RSpec Helper" do
35
37
  end
36
38
  end
37
39
 
38
- context '`with_versioning` block at class level' do
40
+ context "`with_versioning` block at class level" do
39
41
  it { expect(PaperTrail).not_to be_enabled }
40
42
 
41
43
  with_versioning do
42
- it 'should have versioning on by default' do
44
+ it "should have versioning on by default" do
43
45
  expect(PaperTrail).to be_enabled
44
46
  end
45
47
  end
46
- it 'should not leak the `enabled?` state into successive tests' do
48
+ it "should not leak the `enabled?` state into successive tests" do
47
49
  expect(PaperTrail).not_to be_enabled
48
50
  end
49
51
  end
50
52
 
51
53
  describe :whodunnit do
52
- before(:all) { PaperTrail.whodunnit = 'foobar' }
54
+ before(:all) { PaperTrail.whodunnit = "foobar" }
53
55
 
54
56
  it "should get set to `nil` by default" do
55
57
  expect(PaperTrail.whodunnit).to be_nil
@@ -57,7 +59,7 @@ describe "PaperTrail RSpec Helper" do
57
59
  end
58
60
 
59
61
  describe :controller_info do
60
- before(:all) { ::PaperTrail.controller_info = {:foo => 'bar'} }
62
+ before(:all) { ::PaperTrail.controller_info = { foo: "bar" } }
61
63
 
62
64
  it "should get set to an empty hash before each test" do
63
65
  expect(PaperTrail.controller_info).to eq({})
data/spec/rails_helper.rb CHANGED
@@ -1,17 +1,18 @@
1
1
  # This file is copied to spec/ when you run 'rails generate rspec:install'
2
- ENV["RAILS_ENV"] ||= 'test'
3
- ENV["DB"] ||= 'sqlite'
2
+ ENV["RAILS_ENV"] ||= "test"
3
+ ENV["DB"] ||= "sqlite"
4
4
 
5
- unless File.exists?(File.expand_path('../../test/dummy/config/database.yml', __FILE__))
5
+ unless File.exist?(File.expand_path("../../test/dummy/config/database.yml", __FILE__))
6
6
  warn "WARNING: No database.yml detected for the dummy app, please run `rake prepare` first"
7
7
  end
8
8
 
9
- require 'spec_helper'
10
- require File.expand_path('../../test/dummy/config/environment', __FILE__)
11
- require 'rspec/rails'
12
- require 'paper_trail/frameworks/rspec'
13
- require 'shoulda/matchers'
14
- require 'ffaker'
9
+ require "spec_helper"
10
+ require File.expand_path("../../test/dummy/config/environment", __FILE__)
11
+ require "rspec/rails"
12
+ require "paper_trail/frameworks/rspec"
13
+ require "shoulda/matchers"
14
+ require "ffaker"
15
+ require "timecop"
15
16
 
16
17
  # prevent Test::Unit's AutoRunner from executing during RSpec's rake task
17
18
  Test::Unit.run = true if defined?(Test::Unit) && Test::Unit.respond_to?(:run=)
@@ -1,14 +1,16 @@
1
- require 'rails_helper'
1
+ require "rails_helper"
2
2
 
3
- describe "Articles management", :type => :request, :order => :defined do
4
- let(:valid_params) { { :article => { :title => 'Doh', :content => Faker::Lorem.sentence } } }
3
+ describe "Articles management", type: :request, order: :defined do
4
+ let(:valid_params) { { article: { title: "Doh", content: FFaker::Lorem.sentence } } }
5
5
 
6
6
  context "versioning disabled" do
7
7
  specify { expect(PaperTrail).not_to be_enabled }
8
8
 
9
9
  it "should not create a version" do
10
10
  expect(PaperTrail).to be_enabled_for_controller
11
- expect { post(articles_path, valid_params) }.to_not change(PaperTrail::Version, :count)
11
+ expect {
12
+ post articles_path, params_wrapper(valid_params)
13
+ }.to_not change(PaperTrail::Version, :count)
12
14
  end
13
15
 
14
16
  it "should not leak the state of the `PaperTrail.enabled_for_controller?` into the next test" do
@@ -21,9 +23,11 @@ describe "Articles management", :type => :request, :order => :defined do
21
23
 
22
24
  context "`current_user` method returns a `String`" do
23
25
  it "should set that value as the `whodunnit`" do
24
- expect { post articles_path, valid_params }.to change(PaperTrail::Version, :count).by(1)
25
- expect(article.title).to eq('Doh')
26
- expect(article.versions.last.whodunnit).to eq('foobar')
26
+ expect {
27
+ post articles_path, params_wrapper(valid_params)
28
+ }.to change(PaperTrail::Version, :count).by(1)
29
+ expect(article.title).to eq("Doh")
30
+ expect(article.versions.last.whodunnit).to eq("foobar")
27
31
  end
28
32
  end
29
33
  end
data/spec/spec_helper.rb CHANGED
@@ -1,3 +1,5 @@
1
+ require "pry-nav"
2
+
1
3
  # This file was generated by the `rspec --init` command. Conventionally, all
2
4
  # specs live under a `spec` directory, which RSpec adds to the `$LOAD_PATH`.
3
5
  # The generated `.rspec` file contains `--require spec_helper` which will cause this
@@ -38,52 +40,75 @@ RSpec.configure do |config|
38
40
  mocks.verify_partial_doubles = true
39
41
  end
40
42
 
41
- # The settings below are suggested to provide a good initial experience
42
- # with RSpec, but feel free to customize to your heart's content.
43
- =begin
43
+ # Support for disabling `verify_partial_doubles` on specific examples.
44
+ config.around(:each, verify_stubs: false) do |ex|
45
+ config.mock_with :rspec do |mocks|
46
+ mocks.verify_partial_doubles = false
47
+ ex.run
48
+ mocks.verify_partial_doubles = true
49
+ end
50
+ end
51
+
52
+ # The settings below are suggested to provide a good initial experience
53
+ # with RSpec, but feel free to customize to your heart's content.
54
+
44
55
  # These two settings work together to allow you to limit a spec run
45
56
  # to individual examples or groups you care about by tagging them with
46
57
  # `:focus` metadata. When nothing is tagged with `:focus`, all examples
47
58
  # get run.
48
- config.filter_run :focus
49
- config.run_all_when_everything_filtered = true
59
+ # config.filter_run :focus
60
+ # config.run_all_when_everything_filtered = true
50
61
 
51
62
  # Limits the available syntax to the non-monkey patched syntax that is recommended.
52
63
  # For more details, see:
53
64
  # - http://myronmars.to/n/dev-blog/2012/06/rspecs-new-expectation-syntax
54
65
  # - http://teaisaweso.me/blog/2013/05/27/rspecs-new-message-expectation-syntax/
55
66
  # - http://myronmars.to/n/dev-blog/2014/05/notable-changes-in-rspec-3#new__config_option_to_disable_rspeccore_monkey_patching
56
- config.disable_monkey_patching!
67
+ # config.disable_monkey_patching!
57
68
 
58
69
  # This setting enables warnings. It's recommended, but in some cases may
59
70
  # be too noisy due to issues in dependencies.
60
- config.warnings = true
71
+ # config.warnings = true
61
72
 
62
73
  # Many RSpec users commonly either run the entire suite or an individual
63
74
  # file, and it's useful to allow more verbose output when running an
64
75
  # individual spec file.
65
- if config.files_to_run.one?
66
- # Use the documentation formatter for detailed output,
67
- # unless a formatter has already been configured
68
- # (e.g. via a command-line flag).
69
- config.default_formatter = 'doc'
70
- end
76
+ # if config.files_to_run.one?
77
+ # # Use the documentation formatter for detailed output,
78
+ # # unless a formatter has already been configured
79
+ # # (e.g. via a command-line flag).
80
+ # config.default_formatter = 'doc'
81
+ # end
71
82
 
72
83
  # Print the 10 slowest examples and example groups at the
73
84
  # end of the spec run, to help surface which specs are running
74
85
  # particularly slow.
75
- config.profile_examples = 10
86
+ # config.profile_examples = 10
76
87
 
77
88
  # Run specs in random order to surface order dependencies. If you find an
78
89
  # order dependency and want to debug it, you can fix the order by providing
79
90
  # the seed, which is printed after each run.
80
91
  # --seed 1234
81
- config.order = :random
92
+ # config.order = :random
82
93
 
83
94
  # Seed global randomization in this process using the `--seed` CLI option.
84
95
  # Setting this allows you to use `--seed` to deterministically reproduce
85
96
  # test failures related to randomization by passing the same `--seed` value
86
97
  # as the one that triggered the failure.
87
- Kernel.srand config.seed
88
- =end
98
+ # Kernel.srand config.seed
99
+ end
100
+
101
+ def active_record_gem_version
102
+ Gem::Version.new(ActiveRecord::VERSION::STRING)
103
+ end
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
89
114
  end
@@ -2,21 +2,16 @@
2
2
  # then defines those namespaces, then establishes the sqlite3 connection for the namespaces
3
3
  # to simulate an application with multiple database connections.
4
4
 
5
- #Load database yaml to use
5
+ # Load database yaml to use
6
6
  configs = YAML.load_file("#{Rails.root}/config/database.yml")
7
7
 
8
- #If we are testing with sqlite make it quick
8
+ # If we are testing with sqlite make it quick
9
9
  db_directory = "#{Rails.root}/db"
10
- # setup alternate databases
10
+
11
+ # Set up alternate databases
11
12
  if ENV["DB"] == "sqlite"
12
- if RUBY_VERSION >= '1.9'
13
- FileUtils.cp "#{db_directory}/test.sqlite3", "#{db_directory}/test-foo.sqlite3"
14
- FileUtils.cp "#{db_directory}/test.sqlite3", "#{db_directory}/test-bar.sqlite3"
15
- else
16
- require 'ftools'
17
- File.cp "#{db_directory}/test.sqlite3", "#{db_directory}/test-foo.sqlite3"
18
- File.cp "#{db_directory}/test.sqlite3", "#{db_directory}/test-bar.sqlite3"
19
- end
13
+ FileUtils.cp "#{db_directory}/test.sqlite3", "#{db_directory}/test-foo.sqlite3"
14
+ FileUtils.cp "#{db_directory}/test.sqlite3", "#{db_directory}/test-bar.sqlite3"
20
15
  end
21
16
 
22
17
  module Foo
@@ -29,7 +24,7 @@ module Foo
29
24
  end
30
25
 
31
26
  class Document < Base
32
- has_paper_trail :class_name => 'Foo::Version'
27
+ has_paper_trail class_name: "Foo::Version"
33
28
  end
34
29
  end
35
30
 
@@ -48,7 +43,7 @@ module Bar
48
43
  end
49
44
 
50
45
  class Document < Base
51
- has_paper_trail :class_name => 'Bar::Version'
46
+ has_paper_trail class_name: "Bar::Version"
52
47
  end
53
48
  end
54
49
 
@@ -4,10 +4,10 @@ module CustomJsonSerializer
4
4
 
5
5
  def self.load(string)
6
6
  parsed_value = super(string)
7
- parsed_value.is_a?(Hash) ? parsed_value.reject { |k,v| k.blank? || v.blank? } : parsed_value
7
+ parsed_value.is_a?(Hash) ? parsed_value.reject { |k, v| k.blank? || v.blank? } : parsed_value
8
8
  end
9
9
 
10
10
  def self.dump(object)
11
- object.is_a?(Hash) ? super(object.reject { |k,v| v.nil? }) : super
11
+ object.is_a?(Hash) ? super(object.reject { |_k, v| v.nil? }) : super
12
12
  end
13
- end
13
+ end
data/test/dummy/Rakefile CHANGED
@@ -1,7 +1,7 @@
1
1
  # Add your own tasks in files placed in lib/tasks ending in .rake,
2
2
  # for example lib/tasks/capistrano.rake, and they will automatically be available to Rake.
3
3
 
4
- require File.expand_path('../config/application', __FILE__)
5
- require 'rake'
4
+ require File.expand_path("../config/application", __FILE__)
5
+ require "rake"
6
6
 
7
7
  Dummy::Application.load_tasks
@@ -1,20 +1,33 @@
1
1
  class ApplicationController < ActionController::Base
2
2
  protect_from_forgery
3
3
 
4
+ # Rails 5 deprecates `before_filter`
5
+ name_of_before_callback = respond_to?(:before_action) ? :before_action : :before_filter
6
+
7
+ # Some applications and libraries modify `current_user`. Their changes need
8
+ # to be reflected in `whodunnit`, so the `set_paper_trail_whodunnit` below
9
+ # must happen after this.
10
+ send(name_of_before_callback, :modify_current_user)
11
+
12
+ # Going forward, we'll no longer add this `before_filter`, requiring people
13
+ # to do so themselves, allowing them to control the order in which this filter
14
+ # happens.
15
+ send(name_of_before_callback, :set_paper_trail_whodunnit)
16
+
4
17
  def rescue_action(e)
5
18
  raise e
6
19
  end
7
20
 
8
21
  # Returns id of hypothetical current user
9
- def current_user
10
- @current_user ||= OpenStruct.new(:id => 153).tap do |obj|
11
- # Invoking `id` returns the `object_id` value in Ruby18 unless specifically overwritten
12
- def obj.id; 153; end if RUBY_VERSION < '1.9'
13
- end
14
- end
22
+ attr_reader :current_user
15
23
 
16
24
  def info_for_paper_trail
17
- {:ip => request.remote_ip, :user_agent => request.user_agent}
25
+ { ip: request.remote_ip, user_agent: request.user_agent }
26
+ end
27
+
28
+ private
29
+
30
+ def modify_current_user
31
+ @current_user = OpenStruct.new(id: 153)
18
32
  end
19
-
20
33
  end
@@ -1,17 +1,20 @@
1
1
  class ArticlesController < ApplicationController
2
2
  def create
3
- if PaperTrail.active_record_protected_attributes?
4
- @article = Article.create params[:article]
5
- else
6
- @article = Article.create params.require(:article).permit!
7
- end
3
+ @article = Article.create article_params
8
4
  head :ok
9
5
  end
10
6
 
11
7
  def current_user
12
- 'foobar'.tap do |string|
13
- # Invoking `id` returns the `object_id` value in Ruby18 by default
14
- string.class_eval { undef_method(:id) } if RUBY_VERSION < '1.9'
8
+ "foobar"
9
+ end
10
+
11
+ private
12
+
13
+ def article_params
14
+ if PaperTrail.active_record_protected_attributes?
15
+ params[:article]
16
+ else
17
+ params.require(:article).permit!
15
18
  end
16
19
  end
17
20
  end
@@ -1,25 +1,16 @@
1
1
  class WidgetsController < ApplicationController
2
-
3
2
  def paper_trail_enabled_for_controller
4
- request.user_agent != 'Disable User-Agent'
3
+ request.user_agent != "Disable User-Agent"
5
4
  end
6
5
 
7
6
  def create
8
- if PaperTrail.active_record_protected_attributes?
9
- @widget = Widget.create params[:widget]
10
- else
11
- @widget = Widget.create params[:widget].permit!
12
- end
7
+ @widget = Widget.create widget_params
13
8
  head :ok
14
9
  end
15
10
 
16
11
  def update
17
12
  @widget = Widget.find params[:id]
18
- if PaperTrail.active_record_protected_attributes?
19
- @widget.update_attributes params[:widget]
20
- else
21
- @widget.update_attributes params[:widget].permit!
22
- end
13
+ @widget.update_attributes widget_params
23
14
  head :ok
24
15
  end
25
16
 
@@ -28,4 +19,14 @@ class WidgetsController < ApplicationController
28
19
  @widget.destroy
29
20
  head :ok
30
21
  end
22
+
23
+ private
24
+
25
+ def widget_params
26
+ if PaperTrail.active_record_protected_attributes?
27
+ params[:widget]
28
+ else
29
+ params[:widget].permit!
30
+ end
31
+ end
31
32
  end
@@ -1,6 +1,6 @@
1
1
  class Animal < ActiveRecord::Base
2
2
  has_paper_trail
3
- self.inheritance_column = 'species'
3
+ self.inheritance_column = "species"
4
4
 
5
5
  attr_accessible :species, :name if ::PaperTrail.active_record_protected_attributes?
6
6
  end
@@ -1,16 +1,24 @@
1
1
  class Article < ActiveRecord::Base
2
- has_paper_trail :ignore => [:title, { :abstract => Proc.new { |obj| ['ignore abstract', 'Other abstract'].include? obj.abstract } } ],
3
- :only => [:content, { :abstract => Proc.new { |obj| obj.abstract.present? } } ],
4
- :skip => [:file_upload],
5
- :meta => {
6
- :answer => 42,
7
- :action => :action_data_provider_method,
8
- :question => Proc.new { "31 + 11 = #{31 + 11}" },
9
- :article_id => Proc.new { |article| article.id },
10
- :title => :title
11
- }
2
+ has_paper_trail(
3
+ ignore: [
4
+ :title, {
5
+ abstract: proc { |obj|
6
+ ["ignore abstract", "Other abstract"].include? obj.abstract
7
+ }
8
+ }
9
+ ],
10
+ only: [:content, { abstract: proc { |obj| obj.abstract.present? } }],
11
+ skip: [:file_upload],
12
+ meta: {
13
+ answer: 42,
14
+ action: :action_data_provider_method,
15
+ question: proc { "31 + 11 = #{31 + 11}" },
16
+ article_id: proc { |article| article.id },
17
+ title: :title
18
+ }
19
+ )
12
20
 
13
21
  def action_data_provider_method
14
- self.object_id.to_s
22
+ object_id.to_s
15
23
  end
16
24
  end
@@ -1,5 +1,5 @@
1
1
  class Authorship < ActiveRecord::Base
2
2
  belongs_to :book
3
- belongs_to :person
3
+ belongs_to :author, class_name: "Person"
4
4
  has_paper_trail
5
5
  end
@@ -0,0 +1,4 @@
1
+ class BarHabtm < ActiveRecord::Base
2
+ has_and_belongs_to_many :foo_habtms
3
+ has_paper_trail
4
+ end
@@ -1,9 +1,9 @@
1
1
  class Book < ActiveRecord::Base
2
- has_many :authorships, :dependent => :destroy
3
- has_many :authors, :through => :authorships, :source => :person
2
+ has_many :authorships, dependent: :destroy
3
+ has_many :authors, through: :authorships
4
4
 
5
- has_many :editorships, :dependent => :destroy
6
- has_many :editors, :through => :editorships
5
+ has_many :editorships, dependent: :destroy
6
+ has_many :editors, through: :editorships
7
7
 
8
8
  has_paper_trail
9
9
  end
@@ -1,4 +1,4 @@
1
1
  class Boolit < ActiveRecord::Base
2
- default_scope { where(:scoped => true) }
2
+ default_scope { where(scoped: true) }
3
3
  has_paper_trail
4
4
  end
@@ -0,0 +1,45 @@
1
+ class CallbackModifier < ActiveRecord::Base
2
+ has_paper_trail on: []
3
+
4
+ def test_destroy
5
+ transaction do
6
+ run_callbacks(:destroy) do
7
+ self.deleted = true
8
+ save!
9
+ end
10
+ end
11
+ end
12
+
13
+ def flagged_deleted?
14
+ deleted?
15
+ end
16
+ end
17
+
18
+ class BeforeDestroyModifier < CallbackModifier
19
+ has_paper_trail on: []
20
+ paper_trail_on_destroy :before
21
+ end
22
+
23
+ class AfterDestroyModifier < CallbackModifier
24
+ has_paper_trail on: []
25
+ paper_trail_on_destroy :after
26
+ end
27
+
28
+ class NoArgDestroyModifier < CallbackModifier
29
+ has_paper_trail on: []
30
+ paper_trail_on_destroy
31
+ end
32
+
33
+ class UpdateModifier < CallbackModifier
34
+ has_paper_trail on: []
35
+ paper_trail_on_update
36
+ end
37
+
38
+ class CreateModifier < CallbackModifier
39
+ has_paper_trail on: []
40
+ paper_trail_on_create
41
+ end
42
+
43
+ class DefaultModifier < CallbackModifier
44
+ has_paper_trail
45
+ end
@@ -0,0 +1,3 @@
1
+ class Car < Vehicle
2
+ has_paper_trail
3
+ end
@@ -0,0 +1,9 @@
1
+ class Chapter < ActiveRecord::Base
2
+ has_many :sections, dependent: :destroy
3
+ has_many :paragraphs, through: :sections
4
+
5
+ has_many :quotations, dependent: :destroy
6
+ has_many :citations, through: :quotations
7
+
8
+ has_paper_trail
9
+ end
@@ -0,0 +1,5 @@
1
+ class Citation < ActiveRecord::Base
2
+ belongs_to :quotation
3
+
4
+ has_paper_trail
5
+ end
@@ -1,4 +1,4 @@
1
1
  class Customer < ActiveRecord::Base
2
- has_many :orders, :dependent => :destroy
2
+ has_many :orders, dependent: :destroy
3
3
  has_paper_trail
4
4
  end
@@ -1,4 +1,4 @@
1
1
  class Document < ActiveRecord::Base
2
- has_paper_trail :versions => :paper_trail_versions,
3
- :on => [:create, :update]
2
+ has_paper_trail versions: :paper_trail_versions,
3
+ on: [:create, :update]
4
4
  end
@@ -1,4 +1,4 @@
1
1
  # to demonstrate a has_through association that does not have paper_trail enabled
2
2
  class Editor < ActiveRecord::Base
3
- has_many :editorships, :dependent => :destroy
3
+ has_many :editorships, dependent: :destroy
4
4
  end
@@ -0,0 +1,5 @@
1
+ class FooHabtm < ActiveRecord::Base
2
+ has_and_belongs_to_many :bar_habtms
3
+ accepts_nested_attributes_for :bar_habtms
4
+ has_paper_trail
5
+ end
@@ -1,5 +1,5 @@
1
1
  class Fruit < ActiveRecord::Base
2
- if ENV['DB'] == 'postgres' || JsonVersion.table_exists?
3
- has_paper_trail :class_name => 'JsonVersion'
2
+ if ENV["DB"] == "postgres" || JsonVersion.table_exists?
3
+ has_paper_trail class_name: "JsonVersion"
4
4
  end
5
5
  end
@@ -1,3 +1,3 @@
1
1
  class Gadget < ActiveRecord::Base
2
- has_paper_trail :ignore => :brand
2
+ has_paper_trail ignore: :brand
3
3
  end