paper_trail_viewer 1.0.0 → 1.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 06cbe395c995ac810d7164e0efcc15786a545fd5abe4277a6c2e800ed2cb0ef4
4
- data.tar.gz: 991c4f1ed961a5d8a5c5967d3b14c78534cdb549d6d7a235e3e38de634797e14
3
+ metadata.gz: c575ab1f007c5581b6a24b67e2d3b2843719d2bbf8399bcf7d1823cd3194b7dd
4
+ data.tar.gz: bb92946d7a3a688a7a1e5d6175d7deba19516330e745aa6850d679d8a480c5d9
5
5
  SHA512:
6
- metadata.gz: af134d7971dff9b4d2344c534aa47f61e4bcd52fa06bcba99e7336db544a50aa0e0ac262db3e03b44830a2df7cf517e9aa9c44e9ce2912c37c0342bf18eaf29b
7
- data.tar.gz: 8508f49cf1d1267b793f20917ec7f52363ce186898f6d8c36827e50619387bff0eecb3ee2f5a9bfe9c9d47c5974cc03cef2edd32958e6a4d082da91b16467f11
6
+ metadata.gz: 1754241bae44d5e79f91d570fb4fe81d2b9cad78fc8289ad02353e88bdf7fa7b29c727530ec81230e7025d231b50af4fe2fbe1977ae56c535a3fd736f300f313
7
+ data.tar.gz: 1f473d3e9fedd0c69d3209557573d79d85bc7b64526f8d997f6400f3447ee9bfe355bdc933da1b2b4063d05b20ef2bde1abfdaa6550f57d5187586c0fb7365f3
File without changes
data/CHANGELOG.md CHANGED
@@ -4,6 +4,19 @@ All notable changes to this project will be documented in this file.
4
4
  The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
5
  and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [1.1.0] - 2022-03-27
8
+
9
+ ### Added
10
+
11
+ - Basic support for custom version classes
12
+ - Rollback functionality
13
+ - Track item functionality
14
+
15
+ ### Fixed
16
+
17
+ - `LoadError` with data source `ActiveRecord`
18
+ - `bin/setup` being considered a gem executable
19
+
7
20
  ## [1.0.0] - 2022-03-26
8
21
 
9
22
  Initial release.
data/README.md CHANGED
@@ -1,23 +1,33 @@
1
1
  [![Gem Version](https://badge.fury.io/rb/paper_trail_viewer.svg)](http://badge.fury.io/rb/paper_trail_viewer)
2
- [![Build Status](https://github.com/jaynetics/paper_trail_viewer/workflows/tests/badge.svg)](https://github.com/jaynetics/paper_trail_viewer/actions)
2
+ [![Build Status](https://github.com/jaynetics/paper_trail_viewer/actions/workflows/tests.yml/badge.svg)](https://github.com/jaynetics/paper_trail_viewer/actions)
3
3
 
4
4
  # PaperTrailViewer
5
5
 
6
- Browse changes to records when using Ruby on Rails and the [`paper_trail` gem](https://github.com/paper-trail-gem/paper_trail).
6
+ Browse changes to records when using Ruby on Rails and the [`paper_trail`](https://github.com/paper-trail-gem/paper_trail) gem.
7
+
8
+ [
9
+ ![Screenshot thumbnail showing the webapp](
10
+ https://user-images.githubusercontent.com/10758879/160241794-9b9e9552-722f-48e2-9b1d-9ad463b99020.png
11
+ )
12
+ ](https://user-images.githubusercontent.com/10758879/160241664-fa1a6c08-54f1-4d32-9010-16b501b70f60.png)
7
13
 
8
14
  ## Installation
9
15
 
10
16
  Add `paper_trail_viewer` to your bundle and add the following line to your `config/routes.rb`:
11
17
 
12
- mount PaperTrailViewer::Engine => '/changes'
18
+ ```ruby
19
+ mount PaperTrailViewer::Engine => '/changes'
20
+ ```
13
21
 
14
22
  You can pick any path. Restart the server and go to the chosen path to view your versions.
15
23
 
16
- To limit access to this view, do something like:
24
+ To limit access, do something like this:
17
25
 
18
- authenticate :user, ->*{ |u| u.superadmin? } do
19
- mount PaperTrailViewer::Engine => '/changes'
20
- end
26
+ ```ruby
27
+ authenticate :user, ->*{ |u| u.superadmin? } do
28
+ mount PaperTrailViewer::Engine => '/changes'
29
+ end
30
+ ```
21
31
 
22
32
  ### Configuration
23
33
 
@@ -25,8 +35,15 @@ Put configuration in `config/initializers/paper_trail_viewer.rb`.
25
35
 
26
36
  E.g. for linking (or not) to the whodunnit user with a custom path helper:
27
37
 
28
- PaperTrailViewer.user_path_method = :admin_path # default is :user_path
29
- PaperTrailViewer.user_path_method = nil # don't link to the user
38
+ ```ruby
39
+ PaperTrailViewer.user_path_method = :admin_path # default is :user_path
40
+ PaperTrailViewer.user_path_method = nil # don't link to the user
41
+ ```
42
+
43
+ Other options are:
44
+
45
+ - `PaperTrailViewer.allow_rollback` (default is `true`)
46
+ - `PaperTrailViewer.data_source` (default is `ActiveRecord`)
30
47
 
31
48
  ## Development
32
49
 
@@ -38,7 +55,8 @@ E.g. for linking (or not) to the whodunnit user with a custom path helper:
38
55
 
39
56
  ### Running tests
40
57
 
41
- * This repo uses the [Appraisal](https://github.com/thoughtbot/appraisal) gem
58
+ This repo uses the [`appraisal`](https://github.com/thoughtbot/appraisal) gem.
59
+
42
60
  * Run `appraisal generate`
43
61
  * Run `appraisal install`
44
62
  * Run `appraisal rake generate_spec_app`
@@ -1,119 +1,13 @@
1
1
  class PaperTrailViewer::VersionsController < ActionController::Base
2
2
  # Return the queried versions as JSON.
3
3
  def index
4
- query = sanitized_params
5
- versions = PaperTrailViewer.data_source.call(**query)
6
- versions_as_json = versions.map { |v| version_as_json(v) }
7
-
8
- render json: {
9
- hasNextPage: !versions.last_page? && versions_as_json.any?,
10
- query: query,
11
- versions: versions_as_json,
12
- }
4
+ result = PaperTrailViewer.data_source.call(params, main_app)
5
+ render json: result
13
6
  end
14
7
 
15
- # Rollback a change
8
+ # Roll back a change.
16
9
  def update
17
- unless version = PaperTrail::Version.find_by(id: params[:id])
18
- flash[:error] = 'No such version.'
19
- return redirect_to(root_url)
20
- end
21
-
22
- result =
23
- if version.event == 'create'
24
- version.item_type.constantize.find(version.item_id).destroy
25
- else
26
- version.reify.save
27
- end
28
-
29
- if result
30
- if version.event == 'create'
31
- flash[:notice] = 'Rolled back newly-created record by destroying it.'
32
- redirect_to root_url
33
- else
34
- flash[:notice] = 'Rolled back changes to this record.'
35
- redirect_to change_item_url(version)
36
- end
37
- else
38
- flash[:error] = "Couldn't rollback. Sorry."
39
- redirect_to root_url
40
- end
41
- end
42
-
43
- private
44
-
45
- def sanitized_params
46
- {
47
- event: params[:event].presence_in(%w[create update destroy]),
48
- filter: (params[:filter].presence if params[:filter] != '%%'),
49
- item_id: params[:item_id].presence,
50
- item_type: params[:item_type].presence,
51
- page: (page = params[:page].to_i) > 0 ? page : 1,
52
- per_page: (per = params[:per_page].to_i).clamp(1, 1000),
53
- }
54
- end
55
-
56
- def version_as_json(version)
57
- {
58
- **version.attributes.slice(*%w[id whodunnit event created_at]),
59
- changeset: changeset_for(version),
60
- object: load_object(version),
61
- item_id: version.item_id.to_s,
62
- item_type: version.item_type,
63
- item_url: change_item_url(version),
64
- user_url: user_url(version),
65
- }
66
- end
67
-
68
- def changeset_for(version)
69
- case version.event
70
- when 'create', 'update'
71
- version.changeset || {}
72
- when 'destroy'
73
- record = version.reify rescue nil
74
- return {} unless record
75
-
76
- record.attributes.each_with_object({}) do |changes, (k, v)|
77
- changes[k] = [v, nil] unless v.nil?
78
- end
79
- else
80
- raise ArgumentError, "Unknown event: #{version.event}"
81
- end
82
- end
83
-
84
- # Return the URL for the item represented by the +version+,
85
- # e.g. a Company record instance referenced by a version.
86
- def change_item_url(version)
87
- version_type = version.item_type.underscore.split('/').last
88
- main_app.try("#{version_type}_url", version.item_id)
89
- end
90
-
91
- def user_url(version)
92
- (path_method = PaperTrailViewer.user_path_method).present? &&
93
- (id = version.whodunnit).present? &&
94
- !id.start_with?('#') &&
95
- main_app.try(path_method, id) ||
96
- nil
97
- end
98
-
99
- def load_object(version)
100
- obj = version.object
101
- if obj.is_a?(String)
102
- PaperTrail.serializer.load(obj)
103
- elsif obj.is_a?(Hash)
104
- OpenStruct.new(obj)
105
- else
106
- obj
107
- end
108
- end
109
- end
110
-
111
- # backport deserialization fix for recent rubies
112
- # https://github.com/paper-trail-gem/paper_trail/pull/1338
113
- if PaperTrail::VERSION.to_s < '12.2.0'
114
- module PaperTrail::Serializers::YAML
115
- def load(string)
116
- ::YAML.respond_to?(:unsafe_load) ? ::YAML.unsafe_load(string) : ::YAML.load(string)
117
- end
10
+ result = PaperTrailViewer::Rollback.call(version_id: params[:id])
11
+ render json: result
118
12
  end
119
13
  end
@@ -2,4 +2,4 @@
2
2
 
3
3
  <script src='<%= "#{js_url}?v=#{PaperTrailViewer::VERSION}" %>' type='text/javascript'></script>
4
4
 
5
- <div id='mount-paper-trail-viewer'/>
5
+ <div id='mount-paper-trail-viewer' data-allow-rollback='<%= PaperTrailViewer.allow_rollback ? 1 : 0 %>' />