paper_trail-actor 0.4.0 → 0.5.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 5feb0e3bd1d5d354acc67d9da8ce59737cd5f9d2003aac8a06d6d97abe9a964d
4
- data.tar.gz: 4c539f383eff32c652da404f3bf8edcc215cf3e1b1357b6d1566d0ad8abc37c0
3
+ metadata.gz: 7b84892ec641d12e2f30f9779c934369db0ff3b3246cc6c12837f3750a704392
4
+ data.tar.gz: 6d2dcde8f48e153acf1680c0f9dc278c2a5ed696dda0341557d0f104f12e5dbb
5
5
  SHA512:
6
- metadata.gz: 396ac19a6b0ec27b2bddbc46c58e34819d6a1761fc2acee5cff4b3e9143b939c3a0f0dda67c4463d8581d44ef9fbc79ba492326ba8d66ba2c0cb8e5e2ca357ce
7
- data.tar.gz: e8077674949aa3d963b543d74c495edb6ccf975fb86821c8dc97ecc9db0527702ca5f412ee28b12849b8edf2701319cf89dee9dc3354415ffc9830ed46cf5912
6
+ metadata.gz: 0cdefc8cb362df6ae343b23d60ff02ba28a3ebded2cc51f4a7e292cf70587b6a826074e0ed2a3554c5ff73f79893966b84314209247b509f9681153f818b45ff
7
+ data.tar.gz: 8a50a20d5d6183cd6d0871ac24f3c84a6461339ec08fd1aacbd78e95ae0a6a207601720161d268288e170605df012aae47f4cb05533525ae0e39f462583e4793
data/CHANGELOG.md CHANGED
@@ -52,3 +52,21 @@
52
52
  ### Fixed
53
53
 
54
54
  - None
55
+
56
+ ## [0.5.0] - 2025-04-29
57
+
58
+ - Small improvements.
59
+
60
+ ### Breaking Changes
61
+
62
+ - None
63
+
64
+ ### Added
65
+
66
+ - [#12](https://github.com/tttffff/paper_trail-actor/pull/12) Default user for paper trail.
67
+ When using `#set_paper_trail_whodunnit`, the default `#user_for_paper_trail` is now the `#current_user` object, not it's `id`.
68
+
69
+ ### Fixed
70
+
71
+ - [#10](https://github.com/tttffff/paper_trail-actor/pull/10) Allow other object types.
72
+ Can now use any object that implements globalid, not just ActiveRecord objects.
data/README.md CHANGED
@@ -1,82 +1,123 @@
1
1
  # PaperTrail-Actor
2
2
 
3
- Originally forked from the now unmaintained [paper_trail-globalid](https://github.com/ankit1910/paper_trail-globalid).
3
+ This gem is an extension for [PaperTrail](https://github.com/paper-trail-gem/paper_trail) that allows you to set an `ActiveRecord` object as the "whodunnit" field. This enhancement provides more granular tracking of who made changes, making it easier to trace modifications in your application.
4
4
 
5
- This gem is an extension to the [PaperTrail](https://github.com/paper-trail-gem/paper_trail) gem.
5
+ ## Features
6
6
 
7
- - Allows setting an `ActiveRecord` object for the whodunnit field on the PaperTrail request object and version objects.
8
- - Adds the method `#actor` to the PaperTrail request object which returns the `ActiveRecord` object that will be responsible for subsequent changes.
9
- - Adds the method `#actor` to PaperTrail version objects which returns the `ActiveRecord` object that was responsible for change.
7
+ - **Set an ActiveRecord Object for Whodunnit:** Use an `ActiveRecord` object to track who made changes.
8
+ - **Return the ActiveRecord Object:** The method `#actor` can be used to get the object responsible for changes.
9
+ - **Coexistence with Existing Versions:** Works seamlessly alongside existing PaperTrail versions, regardless of when they were created.
10
+
11
+ ## How it works
12
+
13
+ This gem works by storing the [globalid](https://github.com/rails/globalid) to the whodunnit field of PaperTrail version tables. This allows you to add different object types to the whodunnit field, know exactly which object is responsible, and retrive that object with ease.
14
+
15
+ For example, if you have both `Admin` objects and `User` objects that are capable of making changes, this gem ensures that you can clearly identify who made the change instead of using purely an ID and not knowing if it was for an `Admin` or a `User`
10
16
 
11
17
  ## Installation
12
18
 
13
- 1. Add PaperTrail-Actor to your `Gemfile`.
19
+ 1. Add `paper_trail-actor` to your Gemfile:
14
20
  ```ruby
15
21
  gem "paper_trail-actor"
16
22
  ```
17
- 2. And then execute:
23
+ 2. Run the following command:
18
24
  ```sh
19
25
  bundle install
20
26
  ```
21
27
 
22
- ## Basic Usage
28
+ ## Usage
29
+
30
+ ### Basic Setup
23
31
 
24
- This gem works by storing the [globalid](https://github.com/rails/globalid) to the whodunnit field of PaperTrail version tables.
25
- - It is designed not to hinder or break existing PaperTrail functionalities.
26
- - PaperTrail versions with and without a globalid can live side by side (e.g. versions created before installing this gem.)
32
+ To use this gem, you first need to set a user (or any `ActiveRecord` object) to the `whodunnit` field.
27
33
 
28
34
  ```ruby
29
35
  product = Product.find(42)
30
- admin = Admin.find(1) # <Admin:0x007fa2df9a5590>
36
+ admin = Admin.find(1)
31
37
 
32
38
  PaperTrail.request.whodunnit = admin
33
- PaperTrail.request.whodunnit # "gid://app/Admin/1"
34
- PaperTrail.request.actor # <Admin:0x007fa2df9a5590> returns the actual object
39
+ PaperTrail.request.whodunnit # "gid://app/Admin/1"
40
+ PaperTrail.request.actor # Returns the `Admin` object with id 1
41
+ ```
42
+
43
+ When you update the product, PaperTrail will remember who made the change:
35
44
 
45
+ ```ruby
36
46
  product.update(name: "Ice cream")
37
- product.versions.last.whodunnit # "gid://app/Admin/1"
38
- product.versions.last.actor # <Admin:0x007fa2df9a5590> returns the actual object
47
+ product.versions.last.whodunnit # "gid://app/Admin/1"
48
+ product.versions.last.actor # Returns the `Admin` object with id 1
39
49
  ```
40
50
 
41
- ### Setting whodunnit to something other than an ActiveRecord object
51
+ ### Flexible Whodunnit
42
52
 
43
- You can continue to set whodunnit to something other than an `ActiveRecord` object.
44
- - It follows standard PaperTrail functionality and adds the raw value to the whodunnit field.
45
- - The `#actor` method will return the raw value.
53
+ The gem is designed to be flexible. You can set `whodunnit` with various types of objects:
46
54
 
47
- ```ruby
48
- PaperTrail.request.whodunnit = "Alex the admin"
49
- PaperTrail.request.whodunnit # "Alex the admin"
50
- PaperTrail.request.actor # "Alex the admin"
55
+ - **Strings:**
56
+ ```ruby
57
+ PaperTrail.request.whodunnit = "Alex the admin" # "Alex the admin"
58
+ ```
59
+
60
+ - **New/unpersisted ActiveRecord Objects:**
61
+ ```ruby
62
+ PaperTrail.request.whodunnit = Admin.new # Sets the string representation of the admin
63
+ ```
64
+
65
+ - **Non-ActiveRecord Objects:**
66
+ ```ruby
67
+ class Job
68
+ def to_s
69
+ "A job in the system"
70
+ end
71
+ end
72
+
73
+ PaperTrail.request.whodunnit = Job.new # "A job in the system"
74
+ ```
51
75
 
52
- product.update(name: "99 Flake")
53
- product.versions.last.whodunnit # "Alex the admin"
54
- product.versions.last.actor # "Alex the admin"
76
+ When you update the product, PaperTrail will store the string for who made the change:
77
+
78
+ ```ruby
79
+ product.update(name: "Ice cream")
80
+ product.versions.last.whodunnit # "A job in the system"
81
+ product.versions.last.actor # "A job in the system"
55
82
  ```
56
83
 
57
- ### Updating whodunnit on existing PaperTrail versions
84
+ ### Updating Existing Versions
58
85
 
59
- You can retrospectivly update PaperTrail versions to have a new actor.
86
+ If you have existing PaperTrail versions and need to update them, you can do so by:
60
87
 
61
88
  ```ruby
62
- most_recent_product_version = product.versions.last
63
- admin_name = most_recent_product_version.actor # "Alex the admin"
64
- alex_the_admin = Admin.find_by(name: admin_name) # Do check for `nil` before updating the whodunit field
65
- most_recent_product_version.whodunnit = alex_the_admin
66
- most_recent_product_version.save
67
- most_recent_product_version.actor # <Admin:0x00000120fbb7d0>
89
+ first_product_version = product.versions.first
90
+ admin_id = first_product_version.actor # For example, "1"
91
+ admin = Admin.find_by(id: admin_id)
92
+
93
+ if admin.present?
94
+ first_product_version.whodunnit = admin
95
+ first_product_version.save
96
+ end
68
97
  ```
69
98
 
70
99
  ### Setting whodunnit with a controller callback
71
100
 
72
- Similar to how you can configure the PaperTrail gem, as described in [Setting Whodunnit with a Controller Callback](https://github.com/paper-trail-gem/paper_trail/?tab=readme-ov-file#setting-whodunnit-with-a-controller-callback), you can now set this to an ActiveRecord object. This allows storing the global ID and retrieving the actor using the `#actor` methods.
101
+ You can set the user for PaperTrail in the same way that the [PaperTrail documentation states](https://github.com/paper-trail-gem/paper_trail#setting-whodunnit-with-a-controller-callback).
102
+
103
+ However, setting this to an ActiveRecord object now records the globalid. It also allows the object to be retrived from created PaperTrail versions with the `#actor` method.
104
+
105
+ If your controller has a `#current_user` method, PaperTrail-Actor will assign the globalid of the current user to whodunnit. If your controller doesn't have a `#current_user`, or if the current user is `nil`, then whodunnit will not be set.
73
106
 
74
- Here’s an example in your `ApplicationController`:
107
+ You may want set `#user_for_paper_trail` to call a different method to find out who is responsible. To do so, override the `#user_for_paper_trail` method in your controller:
75
108
 
76
109
  ```ruby
77
110
  class ApplicationController < ActionController::Base
111
+ before_action :set_paper_trail_whodunnit
112
+
78
113
  def user_for_paper_trail
79
- logged_in? ? current_user : "Public user"
114
+ logged_in? ? current_admin : "Public user"
80
115
  end
81
116
  end
82
117
  ```
118
+
119
+ ## Contributing
120
+
121
+ 1. Please add tests for PRs that are created.
122
+ 2. Run the tests `bundle exec rake spec` to ensure that they all pass.
123
+ 3. Lint the code `bundle exec rake standard:fix` to ensure that convention is maintained.
@@ -0,0 +1,16 @@
1
+ module PaperTrailActor
2
+ module ActorMixin
3
+ # Used for #whodunnit= methods
4
+ def global_id_string_or_fallback(input_value)
5
+ if input_value.respond_to?(:to_global_id) && input_value.id
6
+ input_value.to_global_id.to_s
7
+ else
8
+ input_value.to_s
9
+ end
10
+ end
11
+
12
+ def actor
13
+ ::GlobalID::Locator.locate(whodunnit) || whodunnit
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,9 @@
1
+ module PaperTrailActor
2
+ module Rails
3
+ module Controller
4
+ def user_for_paper_trail
5
+ current_user if defined?(current_user)
6
+ end
7
+ end
8
+ end
9
+ end
@@ -1,15 +1,11 @@
1
+ require_relative "actor_mixin"
2
+
1
3
  module PaperTrailActor
2
4
  module Request
3
- def whodunnit=(value)
4
- if value.is_a? ActiveRecord::Base
5
- super(value.to_gid)
6
- else
7
- super
8
- end
9
- end
5
+ include ActorMixin
10
6
 
11
- def actor
12
- ::GlobalID::Locator.locate(store[:whodunnit]) || whodunnit
7
+ def whodunnit=(input_value)
8
+ super(global_id_string_or_fallback(input_value))
13
9
  end
14
10
  end
15
11
  end
@@ -1,3 +1,3 @@
1
1
  module PaperTrailActor
2
- VERSION = "0.4.0"
2
+ VERSION = "0.5.0"
3
3
  end
@@ -1,20 +1,12 @@
1
+ require_relative "actor_mixin"
2
+
1
3
  module PaperTrailActor
2
4
  module VersionConcern
5
+ include ActorMixin
6
+
3
7
  def whodunnit=(input_value)
4
- whodunnit_value = if input_value.is_a?(ActiveRecord::Base)
5
- input_value.to_gid
6
- else
7
- input_value
8
- end
8
+ whodunnit_value = global_id_string_or_fallback(input_value)
9
9
  _write_attribute("whodunnit", whodunnit_value)
10
10
  end
11
-
12
- # Returns an object which was responsible for a change
13
- # you need to store global_id to whodunnit field to make this method return the object(who was responsible)
14
- # for example, whodunnit => "gid://app/Order/1" then
15
- # whodunnit_user will return Order.find_by(id: 1) in application scope.
16
- def actor
17
- ::GlobalID::Locator.locate(whodunnit) || whodunnit
18
- end
19
11
  end
20
12
  end
@@ -1,6 +1,7 @@
1
1
  require "paper_trail-actor/version"
2
2
  require "paper_trail-actor/request"
3
3
  require "paper_trail-actor/version_concern"
4
+ require "paper_trail-actor/rails/controller"
4
5
 
5
6
  module ::PaperTrail
6
7
  module Request
@@ -8,10 +9,14 @@ module ::PaperTrail
8
9
  prepend ::PaperTrailActor::Request
9
10
  end
10
11
  end
11
- end
12
12
 
13
- module ::PaperTrail
14
13
  module VersionConcern
15
14
  include ::PaperTrailActor::VersionConcern
16
15
  end
16
+
17
+ module Rails
18
+ module Controller
19
+ prepend ::PaperTrailActor::Rails::Controller
20
+ end
21
+ end
17
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: paper_trail-actor
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.0
4
+ version: 0.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tttffff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2025-04-20 00:00:00.000000000 Z
11
+ date: 2025-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: paper_trail
@@ -53,35 +53,7 @@ dependencies:
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
- name: rspec
57
- requirement: !ruby/object:Gem::Requirement
58
- requirements:
59
- - - ">="
60
- - !ruby/object:Gem::Version
61
- version: '0'
62
- type: :development
63
- prerelease: false
64
- version_requirements: !ruby/object:Gem::Requirement
65
- requirements:
66
- - - ">="
67
- - !ruby/object:Gem::Version
68
- version: '0'
69
- - !ruby/object:Gem::Dependency
70
- name: activerecord
71
- requirement: !ruby/object:Gem::Requirement
72
- requirements:
73
- - - ">="
74
- - !ruby/object:Gem::Version
75
- version: '0'
76
- type: :development
77
- prerelease: false
78
- version_requirements: !ruby/object:Gem::Requirement
79
- requirements:
80
- - - ">="
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: activesupport
56
+ name: rspec-rails
85
57
  requirement: !ruby/object:Gem::Requirement
86
58
  requirements:
87
59
  - - ">="
@@ -149,6 +121,8 @@ files:
149
121
  - README.md
150
122
  - Rakefile
151
123
  - lib/paper_trail-actor.rb
124
+ - lib/paper_trail-actor/actor_mixin.rb
125
+ - lib/paper_trail-actor/rails/controller.rb
152
126
  - lib/paper_trail-actor/request.rb
153
127
  - lib/paper_trail-actor/version.rb
154
128
  - lib/paper_trail-actor/version_concern.rb
@@ -158,7 +132,7 @@ licenses:
158
132
  metadata:
159
133
  homepage_uri: https://github.com/tttffff/paper_trail-actor
160
134
  source_code_uri: https://github.com/tttffff/paper_trail-actor
161
- changelog_uri: https://github.com/tttffff/paper_trail-actor/blob/master/CHANGELOG.md
135
+ changelog_uri: https://github.com/tttffff/paper_trail-actor/blob/main/CHANGELOG.md
162
136
  post_install_message:
163
137
  rdoc_options: []
164
138
  require_paths: