demingfactor-paper_trail_manager 0.7.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (63) hide show
  1. checksums.yaml +7 -0
  2. data/.github/workflows/test.yml +88 -0
  3. data/.gitignore +16 -0
  4. data/.rubocop.yml +33 -0
  5. data/.tool-versions +1 -0
  6. data/Appraisals +34 -0
  7. data/CHANGES.md +62 -0
  8. data/Gemfile +7 -0
  9. data/LICENSE.txt +20 -0
  10. data/README.md +109 -0
  11. data/Rakefile +36 -0
  12. data/app/controllers/paper_trail_manager/changes_controller.rb +131 -0
  13. data/app/helpers/paper_trail_manager/changes_helper.rb +82 -0
  14. data/app/views/paper_trail_manager/changes/_version.html.erb +40 -0
  15. data/app/views/paper_trail_manager/changes/index.atom.builder +48 -0
  16. data/app/views/paper_trail_manager/changes/index.html.erb +35 -0
  17. data/app/views/paper_trail_manager/changes/show.html.erb +10 -0
  18. data/demingfactor-paper_trail_manager.gemspec +32 -0
  19. data/gemfiles/rails_4.2.2_paper_trail_3.0_kaminari.gemfile +11 -0
  20. data/gemfiles/rails_4.2.2_paper_trail_3.0_will_paginate.gemfile +11 -0
  21. data/gemfiles/rails_4.2.2_paper_trail_4.0_kaminari.gemfile +11 -0
  22. data/gemfiles/rails_4.2.2_paper_trail_4.0_will_paginate.gemfile +11 -0
  23. data/gemfiles/rails_4.2.2_paper_trail_5.0_kaminari.gemfile +11 -0
  24. data/gemfiles/rails_4.2.2_paper_trail_5.0_will_paginate.gemfile +11 -0
  25. data/gemfiles/rails_4.2.2_paper_trail_6.0_kaminari.gemfile +11 -0
  26. data/gemfiles/rails_4.2.2_paper_trail_6.0_will_paginate.gemfile +11 -0
  27. data/gemfiles/rails_4.2.2_paper_trail_7.0_kaminari.gemfile +11 -0
  28. data/gemfiles/rails_4.2.2_paper_trail_7.0_will_paginate.gemfile +11 -0
  29. data/gemfiles/rails_4.2.2_paper_trail_8.0_kaminari.gemfile +11 -0
  30. data/gemfiles/rails_4.2.2_paper_trail_8.0_will_paginate.gemfile +11 -0
  31. data/gemfiles/rails_5.0.0_paper_trail_5.0_kaminari.gemfile +11 -0
  32. data/gemfiles/rails_5.0.0_paper_trail_5.0_will_paginate.gemfile +11 -0
  33. data/gemfiles/rails_5.0.0_paper_trail_6.0_kaminari.gemfile +11 -0
  34. data/gemfiles/rails_5.0.0_paper_trail_6.0_will_paginate.gemfile +11 -0
  35. data/gemfiles/rails_5.0.0_paper_trail_7.0_kaminari.gemfile +11 -0
  36. data/gemfiles/rails_5.0.0_paper_trail_7.0_will_paginate.gemfile +11 -0
  37. data/gemfiles/rails_5.0.0_paper_trail_8.0_kaminari.gemfile +11 -0
  38. data/gemfiles/rails_5.0.0_paper_trail_8.0_will_paginate.gemfile +11 -0
  39. data/gemfiles/rails_5.1.0_paper_trail_10.0_kaminari.gemfile +11 -0
  40. data/gemfiles/rails_5.1.0_paper_trail_10.0_will_paginate.gemfile +11 -0
  41. data/gemfiles/rails_5.1.0_paper_trail_7.0_kaminari.gemfile +11 -0
  42. data/gemfiles/rails_5.1.0_paper_trail_7.0_will_paginate.gemfile +11 -0
  43. data/gemfiles/rails_5.1.0_paper_trail_8.0_kaminari.gemfile +11 -0
  44. data/gemfiles/rails_5.1.0_paper_trail_8.0_will_paginate.gemfile +11 -0
  45. data/gemfiles/rails_5.1.0_paper_trail_9.0_kaminari.gemfile +11 -0
  46. data/gemfiles/rails_5.1.0_paper_trail_9.0_will_paginate.gemfile +11 -0
  47. data/gemfiles/rails_5.2.0_paper_trail_10.0_kaminari.gemfile +11 -0
  48. data/gemfiles/rails_5.2.0_paper_trail_10.0_will_paginate.gemfile +11 -0
  49. data/gemfiles/rails_5.2.0_paper_trail_9.0_kaminari.gemfile +11 -0
  50. data/gemfiles/rails_5.2.0_paper_trail_9.0_will_paginate.gemfile +11 -0
  51. data/gemfiles/rails_6.0.0_paper_trail_10.0_kaminari.gemfile +12 -0
  52. data/gemfiles/rails_6.0.0_paper_trail_10.0_will_paginate.gemfile +12 -0
  53. data/gemfiles/rails_6.1.0_paper_trail_11.1_kaminari.gemfile +12 -0
  54. data/gemfiles/rails_6.1.0_paper_trail_11.1_will_paginate.gemfile +12 -0
  55. data/lib/paper_trail_manager.rb +67 -0
  56. data/spec/app_template.rb +30 -0
  57. data/spec/integration/navigation_spec.rb +9 -0
  58. data/spec/integration/paper_trail_manager_spec.rb +185 -0
  59. data/spec/rails_helper.rb +29 -0
  60. data/spec/spec_helper.rb +91 -0
  61. data/spec/support/factories.rb +15 -0
  62. data/spec/support/rspec_html_matchers.rb +7 -0
  63. metadata +263 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 4f7e7c1a58fca8d70bbe18bd8fda125a12f42c7a3f8cb19b32c3ec0c161f17e0
4
+ data.tar.gz: 84a5d5b7b5059f4b425627aeb490d04a474ffbddaccd7bbbd9b8ac57faf47d0d
5
+ SHA512:
6
+ metadata.gz: 770d87358b86af3fd1fe36805a419a8dacfff51a31c7c847bc26632a6f19a46ce126686a5542db0025d9b743f3bb01977914c450fa1b6962efdba838b6c6a203
7
+ data.tar.gz: 0fdcefb2ef7c262909b8ba2deea18baf1cf61ff8d560f5f771afc43ca1ac17ccea8fbfba76e4610f23b760148a5291a49a6b03d704924f175f52981efe4a868b
@@ -0,0 +1,88 @@
1
+ name: Tests
2
+
3
+ on:
4
+ pull_request:
5
+ types:
6
+ - opened
7
+ - reopened
8
+ - synchronize
9
+ - ready_for_review
10
+ push:
11
+ branches:
12
+ - 'master'
13
+
14
+ jobs:
15
+ test:
16
+ runs-on: ubuntu-latest
17
+
18
+ strategy:
19
+ fail-fast: false
20
+ matrix:
21
+ ruby:
22
+ - 2.5.7
23
+ - 2.6.5
24
+ gemfile:
25
+ - gemfiles/rails_4.2.2_paper_trail_3.0_kaminari.gemfile
26
+ - gemfiles/rails_4.2.2_paper_trail_3.0_will_paginate.gemfile
27
+ - gemfiles/rails_4.2.2_paper_trail_4.0_kaminari.gemfile
28
+ - gemfiles/rails_4.2.2_paper_trail_4.0_will_paginate.gemfile
29
+ - gemfiles/rails_4.2.2_paper_trail_5.0_kaminari.gemfile
30
+ - gemfiles/rails_4.2.2_paper_trail_5.0_will_paginate.gemfile
31
+ - gemfiles/rails_4.2.2_paper_trail_6.0_kaminari.gemfile
32
+ - gemfiles/rails_4.2.2_paper_trail_6.0_will_paginate.gemfile
33
+ - gemfiles/rails_4.2.2_paper_trail_7.0_kaminari.gemfile
34
+ - gemfiles/rails_4.2.2_paper_trail_7.0_will_paginate.gemfile
35
+ - gemfiles/rails_4.2.2_paper_trail_8.0_kaminari.gemfile
36
+ - gemfiles/rails_4.2.2_paper_trail_8.0_will_paginate.gemfile
37
+ - gemfiles/rails_5.0.0_paper_trail_5.0_kaminari.gemfile
38
+ - gemfiles/rails_5.0.0_paper_trail_5.0_will_paginate.gemfile
39
+ - gemfiles/rails_5.0.0_paper_trail_6.0_kaminari.gemfile
40
+ - gemfiles/rails_5.0.0_paper_trail_6.0_will_paginate.gemfile
41
+ - gemfiles/rails_5.0.0_paper_trail_7.0_kaminari.gemfile
42
+ - gemfiles/rails_5.0.0_paper_trail_7.0_will_paginate.gemfile
43
+ - gemfiles/rails_5.0.0_paper_trail_8.0_kaminari.gemfile
44
+ - gemfiles/rails_5.0.0_paper_trail_8.0_will_paginate.gemfile
45
+ - gemfiles/rails_5.1.0_paper_trail_10.0_kaminari.gemfile
46
+ - gemfiles/rails_5.1.0_paper_trail_10.0_will_paginate.gemfile
47
+ - gemfiles/rails_5.1.0_paper_trail_7.0_kaminari.gemfile
48
+ - gemfiles/rails_5.1.0_paper_trail_7.0_will_paginate.gemfile
49
+ - gemfiles/rails_5.1.0_paper_trail_8.0_kaminari.gemfile
50
+ - gemfiles/rails_5.1.0_paper_trail_8.0_will_paginate.gemfile
51
+ - gemfiles/rails_5.1.0_paper_trail_9.0_kaminari.gemfile
52
+ - gemfiles/rails_5.1.0_paper_trail_9.0_will_paginate.gemfile
53
+ - gemfiles/rails_5.2.0_paper_trail_10.0_kaminari.gemfile
54
+ - gemfiles/rails_5.2.0_paper_trail_10.0_will_paginate.gemfile
55
+ - gemfiles/rails_5.2.0_paper_trail_9.0_kaminari.gemfile
56
+ - gemfiles/rails_5.2.0_paper_trail_9.0_will_paginate.gemfile
57
+ - gemfiles/rails_6.0.0_paper_trail_10.0_kaminari.gemfile
58
+ - gemfiles/rails_6.0.0_paper_trail_10.0_will_paginate.gemfile
59
+
60
+ env:
61
+ BUNDLER_VERSION: 1.17.3
62
+ BUNDLE_GEMFILE: ${{ matrix.gemfile }}
63
+
64
+ steps:
65
+ - uses: actions/checkout@v2
66
+ - uses: actions/setup-ruby@v1
67
+ with:
68
+ ruby-version: ${{ matrix.ruby }}
69
+ - uses: actions/cache@v1
70
+ with:
71
+ path: vendor/bundle
72
+ key: ${{ runner.os }}-${{ matrix.ruby}}-gems-${{ hashFiles('**/paper_trail_manager.gemspec') }}
73
+ restore-keys: |
74
+ ${{ runner.os }}-${{ matrix.ruby }}-gems-
75
+ - name: Install System Dependencies
76
+ run: |
77
+ sudo apt-get update
78
+ sudo apt-get install sqlite3 libsqlite3-dev
79
+ gem install bundler -v $BUNDLER_VERSION
80
+ - name: bundle install
81
+ run: |
82
+ bundle config path vendor/bundle
83
+ bundle install --jobs 4 --retry 3
84
+ - name: Generate host app for testing
85
+ run: bundle exec rake generate_spec_app
86
+ - name: Run RSpec
87
+ run: |
88
+ bundle exec rake spec
data/.gitignore ADDED
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.bundle
11
+ *.so
12
+ *.o
13
+ *.a
14
+ mkmf.log
15
+ *.gemfile.lock
16
+ spec/dummy
data/.rubocop.yml ADDED
@@ -0,0 +1,33 @@
1
+ require:
2
+ - rubocop-performance
3
+ - rubocop-rails
4
+ - rubocop-rspec
5
+
6
+ AllCops:
7
+ TargetRubyVersion: 2.5
8
+ Exclude:
9
+ - 'spec/dummy/**/*'
10
+
11
+ Rails:
12
+ Enabled: true
13
+
14
+ Style/HashEachMethods:
15
+ Enabled: true
16
+
17
+ Style/HashTransformKeys:
18
+ Enabled: true
19
+
20
+ Style/HashTransformValues:
21
+ Enabled: true
22
+
23
+ Layout/LineLength:
24
+ Max: 120
25
+
26
+ RSpec/NestedGroups:
27
+ Enabled: false
28
+
29
+ RSpec/MultipleExpectations:
30
+ Enabled: false
31
+
32
+ Metrics/BlockLength:
33
+ Enabled: false
data/.tool-versions ADDED
@@ -0,0 +1 @@
1
+ ruby 2.6.10
data/Appraisals ADDED
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ {
4
+ '4.2.2' => %w[3.0 4.0 5.0 6.0 7.0 8.0],
5
+ '5.0.0' => %w[5.0 6.0 7.0 8.0],
6
+ '5.1.0' => %w[7.0 8.0 9.0 10.0],
7
+ '5.2.0' => %w[9.0 10.0],
8
+ '6.0.0' => %w[10.0],
9
+ '6.1.0' => %w[11.1]
10
+ }.each do |rails_version, paper_trail_versions|
11
+ paper_trail_versions.each do |paper_trail_version|
12
+ rails_6 = %w[6.0.0 6.1.0].include?(rails_version)
13
+
14
+ appraise "rails-#{rails_version}-paper_trail-#{paper_trail_version}-will-paginate" do
15
+ gem 'rails', "~> #{rails_version}"
16
+ gem 'jquery-rails'
17
+ gem 'sqlite3', rails_6 ? '~> 1.5.4' : '~> 1.3.6'
18
+ gem 'paper_trail', "~> #{paper_trail_version}"
19
+ gem 'will_paginate', '~> 3.0'
20
+
21
+ gem 'webpacker' if rails_6
22
+ end
23
+
24
+ appraise "rails-#{rails_version}-paper_trail-#{paper_trail_version}-kaminari" do
25
+ gem 'rails', "~> #{rails_version}"
26
+ gem 'jquery-rails'
27
+ gem 'sqlite3', rails_6 ? '~> 1.5.4' : '~> 1.3.6'
28
+ gem 'paper_trail', "~> #{paper_trail_version}"
29
+ gem 'kaminari', '>= 0.16'
30
+
31
+ gem 'webpacker' if rails_6
32
+ end
33
+ end
34
+ end
data/CHANGES.md ADDED
@@ -0,0 +1,62 @@
1
+ Changes to `paper_trail_manager`
2
+ ================================
3
+
4
+ * 0.7.0
5
+ * Add support for Rails 5 and 6
6
+ * Add support for paper_trail 5.x – 10.x
7
+ * Drop support for Rails < 4.1
8
+ * Drop support for Ruby < 2.5
9
+
10
+ * 0.6.0
11
+ * Add support for linking changes to user records via `PaperTrailManager.whodunnit_name_method` and `PaperTrailManager.user_path_method`
12
+ * Add new `PaperTrailManager.item_name_method` configuration option
13
+ * Add support for paper_trail 4.x
14
+ * Refactoring, code simplification, and performance improvements
15
+
16
+ * 0.5.0
17
+ * Add support for pagination with [Kaminari](https://github.com/amatsuda/kaminari) in addition to [will_paginate](https://github.com/mislav/will_paginate).
18
+ * **[!]** Removed direct dependency on will_paginate. You must now include either `will_paginate` or `kaminari` in your Gemfile.
19
+ * Fix a bug with route generation when paper_trail_manager is used within another Rails engine.
20
+
21
+ * 0.4.0
22
+ * Allow configuration of ChangesController's parent class, route helpers, and layout
23
+
24
+ * 0.3.0
25
+ * Support Rails 3.2, 4.0, 4.1, and 4.2
26
+ * Drop support for Rails 3.0 and 3.1
27
+ * Improve test suite across multiple Rails versions
28
+
29
+ * 0.2.0
30
+ * Add support for Rails 3.1 and 3.2, while retaining support for Rails 3.0.
31
+ * Add test suite to check engine against multiple versions. This test suite is derived from Mike Dalessio's "loofah-activerecord" gem, available at https://github.com/flavorjones/loofah-activerecord
32
+
33
+ * 0.1.6
34
+ * Fix `Gemfile.lock` issue with 0.1.5.
35
+
36
+ * 0.1.5
37
+ * Fix exceptions typically caused by bots submitting invalid pagination parameters. Now all pagination parameters are sanitized.
38
+ * Restrict use to Rails 3.0.x because newer versions are not compatible. Will try to add support for newer Rails releases later.
39
+ * Update test/development dependencies.
40
+ * Add travis-ci support.
41
+
42
+ * 0.1.4
43
+ * Fix `changes_row` CSS, was `#{EVENT},` with a trailing comma. Kept it for backwards compatibility, and added `#{EVENT}` and `change_event_#{EVENT}` classes.
44
+ * Add sample CSS stylesheet: `spec/dummy/public/stylesheets/changes.css`.
45
+
46
+
47
+ * 0.1.3
48
+ * Fix deprecation warnings for Rails Engines paths. [Nate Bird]
49
+ * Add homepage to included demo application that provides links and usage instructions.
50
+
51
+ * 0.1.2
52
+ * Switch template format from `haml` to `erb` to eliminate extra dependencies and improve compatibility. [Reid Beels]
53
+
54
+ * 0.1.1
55
+ * Fix `rake spec` to work with `factory_girl` 2.0.x gem.
56
+
57
+ * 0.1.0
58
+ * Fix database database query to be more SQL compatible. [Linus Graybill]
59
+ * Fix Gemfile to reduce run-time dependencies and relax their version requirements.
60
+
61
+ * 0.0.0
62
+ * Initial release.
data/Gemfile ADDED
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ # Specify your gem's dependencies in paper_trail_manager.gemspec
6
+
7
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,20 @@
1
+ Copyright 2011 Igal Koshevoy
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,109 @@
1
+ [![Build Status](https://secure.travis-ci.org/fusion94/paper_trail_manager.png)](http://travis-ci.org/fusion94/paper_trail_manager)
2
+
3
+ # PaperTrailManager
4
+
5
+ Browse, subscribe, view and revert changes to records when using Ruby on Rails 3 and the `paper_trail` gem.
6
+
7
+ This software has been in use for a year at http://calagator.org and http://epdx.org. It works well. It has reasonable tests. However, it could definitely use more work.
8
+
9
+ ## Installation
10
+
11
+ If you have a Ruby on Rails 3 or 4 application where you're using the `paper_trail` gem to track changes to your records, you can make use of this like:
12
+
13
+ Add the following line to your `Gemfile`:
14
+
15
+ gem 'demingfactor-paper_trail_manager'
16
+
17
+ PaperTrailManager will use your existing paging library ([will_paginate](https://github.com/mislav/will_paginate) or [Kaminari](https://github.com/amatsuda/kaminari)). If you don't currently use one in your app, add one of the following lines to your `Gemfile`:
18
+
19
+ gem 'kaminari'
20
+ #or
21
+ gem 'will_paginate'
22
+
23
+ Install the libary:
24
+
25
+ bundle install
26
+
27
+ Add the following line to your `config/routes.rb`:
28
+
29
+ resources :changes, :controller => 'paper_trail_manager/changes'
30
+
31
+ Restart the server and go to the `/changes` URI to browse, subscribe, view and revert your changes. The top-level URL will look something like this:
32
+
33
+ http://localhost:3000/changes
34
+
35
+ ### Configuration
36
+
37
+ Several aspects of PaperTrailManager may be optionally configured
38
+ by creating an initializer in your application
39
+ (e.g. `config/initializers/paper_trail_manager.rb`).
40
+
41
+ To specify when reverts are allowed:
42
+
43
+ PaperTrailManager.allow_revert_when do |controller, version|
44
+ controller.current_user and controller.current_user.admin?
45
+ end
46
+
47
+ To specify how to look up users/memebers/etc specified in Paper Trail's 'whodunnit' column:
48
+
49
+ PaperTrailManager.whodunnit_class = User
50
+ PaperTrailManager.whodunnit_name_method = :nicename # defaults to :name
51
+
52
+ To specify a method to call to identify an item on an index page:
53
+
54
+ PaperTrailManager.item_name_method = :nicename
55
+
56
+ And for linking (or not) to the user with a custom path helper:
57
+
58
+ PaperTrailManager.user_path_method = :admin_path # defaults to :user_path
59
+ PaperTrailManager.user_path_method = nil # no "show user" page in app
60
+
61
+ When including PaperTrailManager within another Rails engine, you may need to
62
+ override PaperTrailManager::ChangesController's parent class to reference the
63
+ engine's ApplicationController configure it to use your engine's url helpers:
64
+
65
+ PaperTrailManager.base_controller = "MyEngine::ApplicationController"
66
+ PaperTrailManager.route_helpers = MyEngine::Engine.routes.url_helpers
67
+
68
+ You can also specify the layout:
69
+
70
+ PaperTrailManager.layout = 'my_engine/application'
71
+
72
+ ## Development
73
+
74
+ Setup:
75
+
76
+ * Clone the repository
77
+ * Go into the directory
78
+ * Run `bundle` to install the development dependencies
79
+
80
+ Running tests:
81
+
82
+ * Run `appraisal rake` to run the tests against all supported gem combinations. Note that the first time tests are run, gems will need to be downloaded for each individual version of Rails this app is tested against, which may take a while.
83
+
84
+ Adding support for new Rails versions:
85
+
86
+ * This repo uses the [Appraisal](https://github.com/thoughtbot/appraisal) gem, to add a new rails version modify the Appraisals file
87
+ - Add both a 'will_paginate' and a 'kaminari' version like so:
88
+ ```
89
+ appraise "rails-5.0-will-paginate" do
90
+ gem "rails", "5.0.0"
91
+ gem "will_paginate", "~> 3.0"
92
+ end
93
+ appraise "rails-5.0-will-kaminari" do
94
+ gem "rails", "5.0.0"
95
+ gem "kaminari", "~> 0.16"
96
+ end
97
+ ```
98
+ * Run `appraisal generate`
99
+ * Run `appraisal install`
100
+ * Fix whatever breaks.
101
+ * Please contribute your fixes with a Github pull request.
102
+
103
+ ## License
104
+
105
+ This program is provided under an MIT open source license, read the [LICENSE.txt](http://github.com/igal/paper_trail_manager/blob/master/LICENSE.txt) file for details.
106
+
107
+ ## To Note:
108
+
109
+ This project was originally devloped by [Igal Koshevoy](http://github.com/igal). Unfortunately @igal passed away on April 9th, 2013 and I took over the project afterwords.
data/Rakefile ADDED
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'rubygems'
4
+
5
+ require 'bundler/gem_tasks'
6
+ Bundler::GemHelper.install_tasks
7
+
8
+ app_rakefile_path = File.expand_path('spec/dummy/Rakefile', __dir__)
9
+
10
+ if File.exist?(app_rakefile_path)
11
+ APP_RAKEFILE = app_rakefile_path
12
+ load 'rails/tasks/engine.rake'
13
+ end
14
+
15
+ require 'rake'
16
+ require 'rdoc/task'
17
+
18
+ require 'rspec/core/rake_task'
19
+ RSpec::Core::RakeTask.new
20
+ task default: ['db:create', 'db:migrate', 'spec']
21
+
22
+ Rake::RDocTask.new(:rdoc) do |rdoc|
23
+ rdoc.rdoc_dir = 'rdoc'
24
+ rdoc.title = 'PaperTrailManager'
25
+ rdoc.options << '--line-numbers' << '--inline-source'
26
+ rdoc.rdoc_files.include('README.rdoc')
27
+ rdoc.rdoc_files.include('lib/**/*.rb')
28
+ end
29
+
30
+ task :generate_spec_app do
31
+ sh 'rm -rf spec/dummy'
32
+ sh 'rails new spec/dummy --skip-bootsnap --skip-bundle --skip-yarn \
33
+ --skip-git --skip-action-mailer --skip-puma --skip-test --skip-coffee \
34
+ --skip-spring --skip-listen --skip-turbolinks \
35
+ --template=spec/app_template.rb'
36
+ end
@@ -0,0 +1,131 @@
1
+ # frozen_string_literal: true
2
+
3
+ # Allow the parent class of ChangesController to be configured in the host app
4
+ PaperTrailManager::ChangesController = Class.new(PaperTrailManager.base_controller.constantize)
5
+
6
+ class PaperTrailManager
7
+ class ChangesController
8
+ # Default number of changes to list on a pagenated index page.
9
+ PER_PAGE = 50
10
+
11
+ helper PaperTrailManager.route_helpers if PaperTrailManager.route_helpers
12
+ helper PaperTrailManager::ChangesHelper
13
+ layout PaperTrailManager.layout if PaperTrailManager.layout
14
+
15
+ # List changes
16
+ def index
17
+ unless change_index_allowed?
18
+ flash[:error] = 'You do not have permission to list changes.'
19
+ return(redirect_to root_url)
20
+ end
21
+
22
+ @versions = PaperTrail::Version.order('created_at DESC, id DESC')
23
+ @versions = @versions.where(item_type: params[:type]) if params[:type]
24
+ @versions = @versions.where(item_id: params[:id]) if params[:id]
25
+
26
+ # Ensure pagination parameters have sensible values
27
+ @page = params[:page].to_i
28
+ @page = nil if @page.zero?
29
+
30
+ @per_page = params[:per_page].to_i
31
+ @per_page = nil if @per_page.zero?
32
+
33
+ @versions = if defined?(WillPaginate)
34
+ @versions.paginate(page: @page, per_page: @per_page)
35
+ else
36
+ @versions.page(@page).per(@per_page)
37
+ end
38
+
39
+ respond_to do |format|
40
+ format.html # index.html.erb
41
+ format.atom # index.atom.builder
42
+ format.json { render json: @versions }
43
+ end
44
+ end
45
+
46
+ # Show a change
47
+ def show
48
+ begin
49
+ @version = PaperTrail::Version.find(params[:id])
50
+ rescue ActiveRecord::RecordNotFound
51
+ flash[:error] = 'No such version.'
52
+ return(redirect_to action: :index)
53
+ end
54
+
55
+ unless change_show_allowed?(@version)
56
+ flash[:error] = 'You do not have permission to show that change.'
57
+ return(redirect_to action: :index)
58
+ end
59
+
60
+ respond_to do |format|
61
+ format.html # show.html.erb
62
+ format.json { render json: @version }
63
+ end
64
+ end
65
+
66
+ # Rollback a change
67
+ def update
68
+ begin
69
+ @version = PaperTrail::Version.find(params[:id])
70
+ rescue ActiveRecord::RecordNotFound
71
+ flash[:error] = 'No such version.'
72
+ return(redirect_to(changes_path))
73
+ end
74
+
75
+ unless change_revert_allowed?(@version)
76
+ flash[:error] = 'You do not have permission to revert this change.'
77
+ return(redirect_to changes_path)
78
+ end
79
+
80
+ if @version.event == 'create'
81
+ @record = @version.item_type.constantize.find(@version.item_id)
82
+ @result = @record.destroy
83
+ else
84
+ @record = @version.reify
85
+ @result = @record.save
86
+ end
87
+
88
+ if @result
89
+ if @version.event == 'create'
90
+ flash[:notice] = 'Rolled back newly-created record by destroying it.'
91
+ redirect_to changes_path
92
+ else
93
+ flash[:notice] = 'Rolled back changes to this record.'
94
+ redirect_to change_item_url(@version)
95
+ end
96
+ else
97
+ flash[:error] = "Couldn't rollback. Sorry."
98
+ redirect_to changes_path
99
+ end
100
+ end
101
+
102
+ protected
103
+
104
+ # Return the URL for the item represented by the +version+, e.g. a Company record instance referenced by a version.
105
+ def change_item_url(version)
106
+ version_type = version.item_type.underscore.split('/').last
107
+ send("#{version_type}_url", version.item_id)
108
+ rescue NoMethodError
109
+ nil
110
+ end
111
+ helper_method :change_item_url
112
+
113
+ # Allow index?
114
+ def change_index_allowed?
115
+ PaperTrailManager.allow_index?(self)
116
+ end
117
+ helper_method :change_index_allowed?
118
+
119
+ # Allow show?
120
+ def change_show_allowed?(version)
121
+ PaperTrailManager.allow_show?(self, version)
122
+ end
123
+ helper_method :change_show_allowed?
124
+
125
+ # Allow revert?
126
+ def change_revert_allowed?(version)
127
+ PaperTrailManager.allow_revert?(self, version)
128
+ end
129
+ helper_method :change_revert_allowed?
130
+ end
131
+ end
@@ -0,0 +1,82 @@
1
+ # frozen_string_literal: true
2
+
3
+ class PaperTrailManager
4
+ module ChangesHelper
5
+ # Return HTML representing the +object+, which is either its text or a stylized "nil".
6
+ def text_or_nil(object)
7
+ if object.nil?
8
+ content_tag('em', 'nil')
9
+ else
10
+ h(object)
11
+ end
12
+ end
13
+
14
+ # Return an hash of changes for the given +PaperTrail::Version+ record. The resulting
15
+ # data structure is a hash whose keys are the names of changed columns and
16
+ # values containing a hash with current and previous value. E.g.,:
17
+ #
18
+ # {
19
+ # "my_column_name" => {
20
+ # :previous => "past value",
21
+ # :current => "current_value",
22
+ # },
23
+ # "title" => {
24
+ # :previous => "puppies",
25
+ # :current => "kittens",
26
+ # },
27
+ # ...
28
+ # }
29
+ def changes_for(version)
30
+ case version.event
31
+ when 'create', 'update'
32
+ return {} unless version.changeset
33
+
34
+ version.changeset.inject({}) do |changes, (attr, (prev, curr))|
35
+ changes.store(attr, { previous: prev, current: curr }) && changes
36
+ end
37
+ when 'destroy'
38
+ record = version_reify(version)
39
+ return {} unless record
40
+
41
+ record.attributes.reject { |_k, v| v.nil? }.inject({}) do |changes, (attr, value)|
42
+ changes.store(attr, { previous: value, current: nil }) && changes
43
+ end
44
+ else
45
+ raise ArgumentError, "Unknown event: #{version.event}"
46
+ end
47
+ end
48
+
49
+ # Returns string title for the versioned record.
50
+ def change_title_for(version)
51
+ if PaperTrailManager.item_name_method
52
+ record = version_reify(version) || version.next.try(:reify) || version.item
53
+ name = record.send(PaperTrailManager.item_name_method)
54
+ else
55
+ name = "#{version.item_type} #{version.item_id}"
56
+ end
57
+
58
+ h(name)
59
+ end
60
+
61
+ def change_item_types
62
+ ActiveRecord::Base.subclasses.select do |klass|
63
+ klass.include?(PaperTrail::Model::InstanceMethods)
64
+ end.map(&:to_s)
65
+ end
66
+
67
+ # Returns HTML link for the item stored in the version, e.g. a link to a Company record stored in the version.
68
+ def change_item_link(version)
69
+ if (url = change_item_url(version))
70
+ link_to(change_title_for(version), url, class: 'change_item')
71
+ else
72
+ content_tag(:span, change_title_for(version), class: 'change_item')
73
+ end
74
+ end
75
+
76
+ def version_reify(version)
77
+ version.reify
78
+ rescue ArgumentError
79
+ nil
80
+ end
81
+ end
82
+ end
@@ -0,0 +1,40 @@
1
+ <tr class='change_row <%= "change_event_#{version.event} #{version.event} #{version.event}, #{version.item_type.downcase}" %>'>
2
+ <td class='change_time'>
3
+ <span class='change_id'>Change #<%= version.id %></span>
4
+ <div class='date'> <%= version.created_at.strftime('%Y-%m-%d') %> </div>
5
+ <div class='time'> <%= version.created_at.strftime('%H:%M:%S') %> </div>
6
+ </td>
7
+ <td class='change_details'>
8
+ <p class='change_details_description'>
9
+ <strong class='event'><%= version.event %></strong>
10
+ <%= change_item_link(version) %>
11
+ <% if PaperTrailManager.whodunnit_class && version.whodunnit %>
12
+ <% if user = PaperTrailManager.whodunnit_class.find(version.whodunnit) rescue nil %>
13
+ <% if PaperTrailManager.user_path_method && user %>
14
+ by <%= link_to(h(user.send(PaperTrailManager.whodunnit_name_method)), send(:user_path_method, user)) %>
15
+ <% else %>
16
+ by <%= h(user.send(PaperTrailManager.whodunnit_name_method)) %>
17
+ <% end %>
18
+ <% else %>
19
+ by <%= version.whodunnit %>
20
+ <% end %>
21
+ <% end %>
22
+ <% if change_revert_allowed?(version) %>
23
+ <%= link_to 'Roll back', change_path(version), :method => 'put', :class => 'rollback', :data => { :confirm => 'Are you sure?' } %>
24
+ <% end %>
25
+ </p>
26
+ <% if version.event == 'update' or version.event == 'create' %>
27
+ <% changes = changes_for(version) %>
28
+ <table class='change_details_table'>
29
+ <% changes.keys.sort.each_with_index do |key, i| %>
30
+ <tr class='<%= i%2==0 ? 'even' : 'odd' %>'>
31
+ <td class='change_detail_key previous'><%= key %></td>
32
+ <td class='change_detail_value previous'><%= text_or_nil(changes[key][:previous]) %></td>
33
+ <td class='change_detail_spacer'> &rarr; </td>
34
+ <td class='change_detail_value current'><%= text_or_nil(changes[key][:current]) %></td>
35
+ </tr>
36
+ <% end %>
37
+ </table>
38
+ <% end %>
39
+ </td>
40
+ </tr>