action_trace 0.1.0 → 0.3.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 +4 -4
- data/README.md +76 -65
- data/app/interactors/action_trace/fetch_activity_logs.rb +1 -1
- data/app/interactors/action_trace/fetch_data_changes.rb +1 -1
- data/app/interactors/action_trace/fetch_page_visits.rb +1 -1
- data/app/interactors/action_trace/fetch_session_starts.rb +1 -1
- data/app/interactors/action_trace/initialize_context.rb +1 -1
- data/app/interactors/action_trace/merge_and_format_results.rb +1 -1
- data/app/models/ahoy/event.rb +10 -0
- data/app/models/ahoy/visit.rb +8 -0
- data/lib/action_trace/engine.rb +5 -1
- data/lib/action_trace/version.rb +1 -1
- data/lib/action_trace.rb +1 -0
- data/lib/generators/action_trace/install/install_generator.rb +12 -0
- data/lib/generators/action_trace/views/views_generator.rb +1 -1
- metadata +4 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: ac6e3bcd7ddba0d325500935c892b7832116e9f53035b352e757e258fb5b28d4
|
|
4
|
+
data.tar.gz: ddc86843cab5355f2c08b8134048b9d2e6bb00771112570ae037b44a59ca4e4e
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8f4a13e9befd1d7a05e99acd17cbb3521004cdc4e2ede8e2d0fc9f49f2c176f45a7d20a7a280fc017895927c6e69513b744a479350a109359537927b6067cfc2
|
|
7
|
+
data.tar.gz: ab8913e299894c6c8868384e96a1056d859ae0368e85eb214c57e745808eddad16889f1cf7290b24f0a1afc4ae578c666994461f5ccf5008fa98cd253be1ce73
|
data/README.md
CHANGED
|
@@ -1,9 +1,8 @@
|
|
|
1
1
|
# ActionTrace
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
It glues together [public_activity](https://github.com/chaps-io/public_activity), [ahoy_matey](https://github.com/ankane/ahoy), [paper_trail](https://github.com/paper-trail-gem/paper_trail), and [discard](https://github.com/jhawthorn/discard) so you don't have to configure each one individually.
|
|
3
|
+
[](https://rubygems.org/gems/action_trace)
|
|
5
4
|
|
|
6
|
-
|
|
5
|
+
ActionTrace is a Rails engine that consolidates user interaction tracking into a single integration point. Instead of configuring [public_activity](https://github.com/chaps-io/public_activity), [ahoy_matey](https://github.com/ankane/ahoy), [paper_trail](https://github.com/paper-trail-gem/paper_trail), and [discard](https://github.com/jhawthorn/discard) individually, ActionTrace wires them together and exposes a unified activity log with a ready-to-use UI.
|
|
7
6
|
|
|
8
7
|
| Source | Description | Backed by |
|
|
9
8
|
|---|---|---|
|
|
@@ -14,6 +13,8 @@ It glues together [public_activity](https://github.com/chaps-io/public_activity)
|
|
|
14
13
|
| `session_start` | User session begun | ahoy_matey (visit) |
|
|
15
14
|
| `session_end` | User logged out | ahoy_matey (event) |
|
|
16
15
|
|
|
16
|
+
---
|
|
17
|
+
|
|
17
18
|
## Installation
|
|
18
19
|
|
|
19
20
|
Add to your Gemfile:
|
|
@@ -30,18 +31,16 @@ rails generate action_trace:install
|
|
|
30
31
|
rails db:migrate
|
|
31
32
|
```
|
|
32
33
|
|
|
33
|
-
The installer runs the setup generators for all four gems and creates `config/initializers/action_trace.rb`.
|
|
34
|
+
The installer runs the setup generators for all four underlying gems and creates `config/initializers/action_trace.rb`.
|
|
34
35
|
|
|
35
36
|
### Skipping already-installed gems
|
|
36
37
|
|
|
37
|
-
If one or more of the underlying gems is already
|
|
38
|
+
If one or more of the underlying gems is already configured, pass `--skip-*` flags:
|
|
38
39
|
|
|
39
40
|
```bash
|
|
40
41
|
rails generate action_trace:install --skip-ahoy --skip-paper-trail
|
|
41
42
|
```
|
|
42
43
|
|
|
43
|
-
Available flags:
|
|
44
|
-
|
|
45
44
|
| Flag | Skips |
|
|
46
45
|
|---|---|
|
|
47
46
|
| `--skip-ahoy` | `ahoy:install` |
|
|
@@ -64,38 +63,54 @@ GET /action_trace/activity_logs
|
|
|
64
63
|
POST /action_trace/activity_logs/filter
|
|
65
64
|
```
|
|
66
65
|
|
|
67
|
-
|
|
66
|
+
### Copy views and controller (optional)
|
|
68
67
|
|
|
69
|
-
|
|
68
|
+
ActionTrace ships minimal default views that work out of the box. Copy them into your app to customise the UI:
|
|
70
69
|
|
|
71
|
-
|
|
70
|
+
```bash
|
|
71
|
+
# Copy views only
|
|
72
|
+
rails generate action_trace:views
|
|
72
73
|
|
|
73
|
-
|
|
74
|
-
|
|
75
|
-
|
|
76
|
-
config.excluded_controllers = %w[health_checks status]
|
|
74
|
+
# Copy views and controller
|
|
75
|
+
rails generate action_trace:views --controller
|
|
76
|
+
```
|
|
77
77
|
|
|
78
|
-
|
|
79
|
-
config.excluded_actions = %w[ping]
|
|
78
|
+
Views are placed in `app/views/action_trace/activity_logs/`. Rails will use your copies instead of the engine defaults.
|
|
80
79
|
|
|
81
|
-
|
|
82
|
-
# for PublicActivity::Activity, Ahoy::Visit and Ahoy::Event (default: 'User')
|
|
83
|
-
config.user_class = 'User'
|
|
80
|
+
The `--controller` flag also copies `ActivityLogsController` to `app/controllers/action_trace/activity_logs_controller.rb`. The file includes commented-out lines for Devise and CanCanCan authentication — uncomment what applies to your setup or replace with your own auth logic.
|
|
84
81
|
|
|
85
|
-
|
|
86
|
-
|
|
82
|
+
> **Note:** once you copy the controller, the engine's version is no longer used. Future updates to the engine's controller will not be applied automatically — keep that in mind when upgrading.
|
|
83
|
+
|
|
84
|
+
---
|
|
85
|
+
|
|
86
|
+
## Usage
|
|
87
|
+
|
|
88
|
+
### Models — tracking data changes
|
|
89
|
+
|
|
90
|
+
Include `ActionTrace::DataTrackable` in any ActiveRecord model you want to track:
|
|
91
|
+
|
|
92
|
+
```ruby
|
|
93
|
+
class Document < ApplicationRecord
|
|
94
|
+
include ActionTrace::DataTrackable
|
|
87
95
|
end
|
|
88
96
|
```
|
|
89
97
|
|
|
90
|
-
|
|
91
|
-
> activity records through the user when filtering by company (since those
|
|
92
|
-
> models store the user reference rather than a direct `company_id`).
|
|
98
|
+
This records a `public_activity` event on every `create`, `update`, and `destroy`, linked to the current user (via `PublicActivity.get_controller`) and, when paper_trail is active, to the corresponding version.
|
|
93
99
|
|
|
94
|
-
|
|
100
|
+
For paper_trail versioning to work, the model also needs `has_paper_trail`:
|
|
95
101
|
|
|
96
|
-
|
|
102
|
+
```ruby
|
|
103
|
+
class Document < ApplicationRecord
|
|
104
|
+
include ActionTrace::DataTrackable
|
|
105
|
+
has_paper_trail
|
|
106
|
+
end
|
|
107
|
+
```
|
|
108
|
+
|
|
109
|
+
For soft-delete tracking with discard, add `include Discard::Model` alongside `DataTrackable`. The `data_destroy` event is still recorded via the `before_destroy` callback.
|
|
97
110
|
|
|
98
|
-
|
|
111
|
+
### Controllers — tracking page visits and sessions
|
|
112
|
+
|
|
113
|
+
Include `ActivityTrackable` in any controller (or `ApplicationController`) to track page visits:
|
|
99
114
|
|
|
100
115
|
```ruby
|
|
101
116
|
class ApplicationController < ActionController::Base
|
|
@@ -103,34 +118,32 @@ class ApplicationController < ActionController::Base
|
|
|
103
118
|
end
|
|
104
119
|
```
|
|
105
120
|
|
|
106
|
-
This adds an `after_action :track_action` that records a `page_visit` event via Ahoy for every successful request made by a logged-in user.
|
|
121
|
+
This adds an `after_action :track_action` that records a `page_visit` event via Ahoy for every successful request made by a logged-in user. It also includes `PublicActivity::StoreController`, which makes the current controller available to model callbacks so that data events can be linked to the right user.
|
|
107
122
|
|
|
108
|
-
To
|
|
123
|
+
To skip tracking on a specific controller that inherits from `ApplicationController`:
|
|
109
124
|
|
|
110
125
|
```ruby
|
|
111
|
-
class
|
|
112
|
-
|
|
113
|
-
track_session_end
|
|
114
|
-
super
|
|
115
|
-
end
|
|
126
|
+
class HealthChecksController < ApplicationController
|
|
127
|
+
skip_after_action :track_action
|
|
116
128
|
end
|
|
117
129
|
```
|
|
118
130
|
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
Include `ActionTrace::DataTrackable` in any ActiveRecord model:
|
|
131
|
+
To record a session end on logout, call `track_session_end` before clearing the session:
|
|
122
132
|
|
|
123
133
|
```ruby
|
|
124
|
-
class
|
|
125
|
-
|
|
134
|
+
class SessionsController < Devise::SessionsController
|
|
135
|
+
def destroy
|
|
136
|
+
track_session_end
|
|
137
|
+
super
|
|
138
|
+
end
|
|
126
139
|
end
|
|
127
140
|
```
|
|
128
141
|
|
|
129
|
-
|
|
142
|
+
The engine's `ActivityLogsController` inherits from the host app's `ApplicationController`. Authentication and authorization are not enforced by default — copy the controller with the generator and uncomment the relevant lines for your setup (e.g. Devise's `authenticate_user!`, CanCanCan's `load_and_authorize_resource`).
|
|
130
143
|
|
|
131
|
-
###
|
|
144
|
+
### Querying activity logs directly
|
|
132
145
|
|
|
133
|
-
Use `ActionTrace::FetchActivityLogs`
|
|
146
|
+
Use `ActionTrace::FetchActivityLogs` to fetch and paginate the unified activity log programmatically:
|
|
134
147
|
|
|
135
148
|
```ruby
|
|
136
149
|
result = ActionTrace::FetchActivityLogs.call(
|
|
@@ -163,43 +176,39 @@ Each `ActionTrace::ActivityLog` exposes:
|
|
|
163
176
|
| `trackable` | ActiveRecord object | The changed record (data events only) |
|
|
164
177
|
| `trackable_type` | String | Class name of the changed record |
|
|
165
178
|
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
`ActionTrace::ActivityLog` also provides:
|
|
179
|
+
Presenter helpers are also available on each log:
|
|
169
180
|
|
|
170
181
|
```ruby
|
|
171
|
-
log.icon
|
|
172
|
-
log.color
|
|
182
|
+
log.icon # => 'fas fa-pencil-alt'
|
|
183
|
+
log.color # => 'text-primary'
|
|
173
184
|
log.data_change? # => true / false
|
|
174
185
|
log.page_visit? # => true / false
|
|
175
|
-
#
|
|
186
|
+
# data_create?, data_destroy?, session_start?, session_end?
|
|
176
187
|
```
|
|
177
188
|
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
ActionTrace ships minimal default views for `activity_logs#index`. These work out of the box but are intentionally bare — copy them into your app to customize the UI.
|
|
189
|
+
### Configuration
|
|
181
190
|
|
|
182
|
-
|
|
191
|
+
`config/initializers/action_trace.rb` is generated automatically. Available options:
|
|
183
192
|
|
|
184
|
-
```
|
|
185
|
-
|
|
186
|
-
|
|
193
|
+
```ruby
|
|
194
|
+
ActionTrace.configure do |config|
|
|
195
|
+
# Controller names to exclude from page_visit tracking (default: [])
|
|
196
|
+
config.excluded_controllers = %w[health_checks status]
|
|
187
197
|
|
|
188
|
-
|
|
198
|
+
# Action names to exclude from page_visit tracking (default: [])
|
|
199
|
+
config.excluded_actions = %w[ping]
|
|
189
200
|
|
|
190
|
-
|
|
201
|
+
# The user model class name used to resolve company filtering (default: 'User')
|
|
202
|
+
config.user_class = 'User'
|
|
191
203
|
|
|
192
|
-
|
|
193
|
-
|
|
204
|
+
# How long to retain activity records before purging (default: 1.year)
|
|
205
|
+
config.log_retention_period = 6.months
|
|
206
|
+
end
|
|
194
207
|
```
|
|
195
208
|
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
> After copying the controller, the engine's version is no longer used. Any future updates to the engine's controller will not be applied automatically — keep that in mind when upgrading.
|
|
209
|
+
> `user_class` must have a `company_id` column. ActionTrace uses it to filter activity records by company (since Ahoy and PublicActivity store the user reference rather than a direct `company_id`).
|
|
199
210
|
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
### Purge old records
|
|
211
|
+
### Purging old records
|
|
203
212
|
|
|
204
213
|
`ActionTrace::PurgeActivityLogJob` removes all `PublicActivity::Activity`, `Ahoy::Event`, and `Ahoy::Visit` records older than `log_retention_period` (default: 1 year). Schedule it with your preferred job scheduler:
|
|
205
214
|
|
|
@@ -208,6 +217,8 @@ Also copies `ActivityLogsController` to `app/controllers/action_trace/activity_l
|
|
|
208
217
|
ActionTrace::PurgeActivityLogJob.perform_later
|
|
209
218
|
```
|
|
210
219
|
|
|
220
|
+
---
|
|
221
|
+
|
|
211
222
|
## License
|
|
212
223
|
|
|
213
224
|
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
data/lib/action_trace/engine.rb
CHANGED
|
@@ -1,5 +1,9 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
|
+
require 'public_activity'
|
|
4
|
+
require 'ahoy_matey'
|
|
5
|
+
require 'paper_trail'
|
|
6
|
+
|
|
3
7
|
module ActionTrace
|
|
4
8
|
class Engine < ::Rails::Engine
|
|
5
9
|
isolate_namespace ActionTrace
|
|
@@ -29,4 +33,4 @@ module ActionTrace
|
|
|
29
33
|
end
|
|
30
34
|
end
|
|
31
35
|
end
|
|
32
|
-
end
|
|
36
|
+
end
|
data/lib/action_trace/version.rb
CHANGED
data/lib/action_trace.rb
CHANGED
|
@@ -5,6 +5,18 @@ require 'rails/generators'
|
|
|
5
5
|
module ActionTrace
|
|
6
6
|
module Generators
|
|
7
7
|
class InstallGenerator < Rails::Generators::Base
|
|
8
|
+
include Rails::Generators::Migration
|
|
9
|
+
|
|
10
|
+
source_root File.expand_path('templates', __dir__)
|
|
11
|
+
|
|
12
|
+
def self.source_paths
|
|
13
|
+
[File.expand_path('templates', __dir__), File.expand_path(__dir__)]
|
|
14
|
+
end
|
|
15
|
+
|
|
16
|
+
def self.next_migration_number(dirname)
|
|
17
|
+
next_migration_number = current_migration_number(dirname) + 1
|
|
18
|
+
ActiveRecord::Migration.next_migration_number(next_migration_number)
|
|
19
|
+
end
|
|
8
20
|
desc 'Install ActionTrace and all its dependencies'
|
|
9
21
|
|
|
10
22
|
class_option :skip_ahoy, type: :boolean, default: false,
|
|
@@ -22,7 +22,7 @@ module ActionTrace
|
|
|
22
22
|
return unless options[:controller]
|
|
23
23
|
|
|
24
24
|
copy_file(
|
|
25
|
-
File.expand_path('
|
|
25
|
+
File.expand_path('../../../../app/controllers/action_trace/activity_logs_controller.rb', __dir__),
|
|
26
26
|
'app/controllers/action_trace/activity_logs_controller.rb'
|
|
27
27
|
)
|
|
28
28
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: action_trace
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- gimbaro
|
|
@@ -247,6 +247,8 @@ files:
|
|
|
247
247
|
- app/jobs/action_trace/purge_activity_log_job.rb
|
|
248
248
|
- app/models/action_trace/activity_log.rb
|
|
249
249
|
- app/models/action_trace/application_record.rb
|
|
250
|
+
- app/models/ahoy/event.rb
|
|
251
|
+
- app/models/ahoy/visit.rb
|
|
250
252
|
- app/models/concerns/action_trace/data_trackable.rb
|
|
251
253
|
- app/presenters/action_trace/activity_log_presenter.rb
|
|
252
254
|
- app/views/action_trace/activity_logs/_activity_log.html.erb
|
|
@@ -291,7 +293,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
291
293
|
- !ruby/object:Gem::Version
|
|
292
294
|
version: '0'
|
|
293
295
|
requirements: []
|
|
294
|
-
rubygems_version:
|
|
296
|
+
rubygems_version: 4.0.8
|
|
295
297
|
specification_version: 4
|
|
296
298
|
summary: A Rails engine that consolidates user interaction tracking into a single
|
|
297
299
|
integration
|