access_trail 0.1.0 → 0.2.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: a132ad5951b2b5b94e0f96043d9cad04051a816c3a55d80a85d6b98a48f67f5e
4
- data.tar.gz: f90c0bf9d285731aee15ef935857fb1366e779da841bd47f15eb02cf42045474
3
+ metadata.gz: 376fa85df722728d8ef455a6fa6048f1060a310f291655594487fe39f82d025a
4
+ data.tar.gz: f80e1fcba306c4ad0927340f7f11c2cb80b1f9f71e98f1787fb28d98836bc80b
5
5
  SHA512:
6
- metadata.gz: 5b3ea98d3596ebf8589814fda625cde7f72f59c0374228dd25e8fa568905fc66d83a93b878d51597456f662f7f627cbcae40999c1a1b33f3483a5feb4dd1b237
7
- data.tar.gz: b494101c673d4c5cccdb410c7d4b0370283c1c8a1aaf05c5a80492948221e4924527fa9d0dc9b7ecf4d0baeb60f848782b7ebc8ec146a13cfd7ee847f0c65d7e
6
+ metadata.gz: a7f27853af831bcd430b6dfefd358c8b444edc2a97a46a57d8efe54c762317866faf7454bbcb0d7e80a6c31f7c566407b705eb1ebf5e8057f92f22eacda94d81
7
+ data.tar.gz: e7039901c832aab423605398e66e5ec0bb5a9f2a658e40f3128fae20e5510d72a8f39ecf09e6b4759a101a4bf3de9d0932c35e722296f704ae33f98866b31497
data/README.md CHANGED
@@ -1,9 +1,10 @@
1
1
  # access_trail
2
2
 
3
3
  Records who accessed which record in a host app, and lists those entries on a page.
4
- Each entry names the person who acted, the account they acted in, the type of entry,
5
- the record touched and when. The host decides which moments are recorded and who may
6
- read the trail.
4
+ Each entry names the person who acted, who they acted as, the account they acted in,
5
+ the type of entry, the record touched and when. The host decides which moments are
6
+ recorded and who may read the trail. The trail page can be narrowed by filters, and the
7
+ host can ask the gem for the same filtered entries to build its own export.
7
8
 
8
9
  ## Usage
9
10
 
@@ -38,6 +39,18 @@ The app refuses to boot while any of the five is unset. It raises
38
39
  `AccessTrail::UnsetSettingError` naming every unset setting in one message, and the
39
40
  check runs again after every code reload.
40
41
 
42
+ One more setting is optional:
43
+
44
+ ```ruby
45
+ AccessTrail.export_address = "/csv/exports/access_trail"
46
+ ```
47
+
48
+ - `export_address` is the address, as a String, of the host's own export of the
49
+ trail. When it is set, the trail page shows an Export link to it carrying the
50
+ page's current filters as query parameters, such as
51
+ `/csv/exports/access_trail?entry_type=Viewed&from=2026-11-01`. When it is not set,
52
+ the page shows no Export link.
53
+
41
54
  ### Recording an entry
42
55
 
43
56
  ```ruby
@@ -54,7 +67,8 @@ AccessTrail.record(
54
67
  ```
55
68
 
56
69
  `person`, `account` and `entry_type` are required, and the rest may be left out.
57
- People and accounts are stored by global id, so each must include
70
+ Pass `acted_as:` with the person whose identity was used when it differs from the
71
+ person who acted. People and accounts are stored by global id, so each must include
58
72
  `GlobalID::Identification`, as every Active Record model does. The entry keeps the
59
73
  time it was recorded. An entry cannot be changed once it is recorded.
60
74
 
@@ -62,6 +76,58 @@ The record is kept as its type, its public code and its label, copied at the tim
62
76
  of recording. Renaming the record later leaves the entry as it was. Never pass a
63
77
  credential, a secret or a request body as the label or the detail.
64
78
 
79
+ ### Filters
80
+
81
+ The trail is narrowed by five filters, each a query parameter of the same name:
82
+
83
+ - `person` — the global id of the person who acted, such as `gid://app/User/42`.
84
+ - `account` — the global id of the account.
85
+ - `entry_type` — the entry type, such as `Viewed`.
86
+ - `from` — a date as `YYYY-MM-DD`, keeping entries recorded on or after it.
87
+ - `to` — a date as `YYYY-MM-DD`, keeping entries recorded on or before it.
88
+
89
+ A blank filter and a date that cannot be read narrow nothing. Dates are read in the
90
+ app's time zone.
91
+
92
+ ### Asking for entries
93
+
94
+ ```ruby
95
+ AccessTrail.entries(params)
96
+ AccessTrail.entries(entry_type: "Viewed", from: "2026-11-01")
97
+ ```
98
+
99
+ `AccessTrail.entries` takes a request's parameters or a Hash with Symbol keys, reads
100
+ only the five filters from it, and returns the matching `AccessTrail::Entry`
101
+ records newest first, as an Active Record relation. Given the parameters of a
102
+ trail page address, it returns the same entries that page shows. With no argument it
103
+ returns every entry.
104
+
105
+ `AccessTrail.filters(params)` returns the filters picked out of a request's
106
+ parameters as a Hash with Symbol keys, leaving out blank ones.
107
+
108
+ Each entry carries:
109
+
110
+ - `created_at` — the time it was recorded.
111
+ - `person_name` — who acted.
112
+ - `acted_as_name` — who they acted as, or `nil` when they acted as themselves.
113
+ - `account_name` — the account.
114
+ - `entry_type`, `record_type`, `record_code`, `record_label`, `detail` and
115
+ `address` — as recorded.
116
+
117
+ The three names come from the host's `name_of` setting. When the person or account
118
+ was deleted, the name is its stored global id, so the entry still reads and can
119
+ still be filtered by it.
120
+
121
+ ### Recording an export
122
+
123
+ ```ruby
124
+ AccessTrail.record_export(person: current_user, account: current_account, filters: params, address: request.remote_ip)
125
+ ```
126
+
127
+ `AccessTrail.record_export` records an `Exported` entry naming who made the export,
128
+ with the filters it used in the detail, such as `Filters: entry_type Viewed, from
129
+ 2026-11-01`, or `No filters`. `address` may be left out.
130
+
65
131
  ### The trail page
66
132
 
67
133
  Mount the engine:
@@ -71,7 +137,12 @@ mount AccessTrail::Engine => "/access_trail"
71
137
  ```
72
138
 
73
139
  The page at the mount path lists every entry, newest first, showing who acted, the
74
- entry type, the record's label, the account and the time. On a phone screen each
140
+ entry type, the record's label, the account and the time. Above the list, a filter
141
+ form narrows it by person, account, entry type and a from and to date. The person and
142
+ account filters offer each person and account on the trail by name, and the entry
143
+ type filter offers each entry type recorded. The page reads its filters from its
144
+ query parameters and lists its entries through `AccessTrail.entries`, so a host's
145
+ export given the same parameters holds the same entries. On a phone screen each
75
146
  entry is shown as a list item with each value under its heading. The page is drawn
76
147
  with keystone_ui, so the host must run keystone_ui.
77
148
 
@@ -3,8 +3,13 @@ module AccessTrail
3
3
  before_action :refuse_unless_reader
4
4
 
5
5
  def index
6
- names = Hash.new { |known, gid| known[gid] = AccessTrail.name_of.call(GlobalID::Locator.locate(gid)) }
7
- @rows = Entry.order(created_at: :desc, id: :desc).map do |entry|
6
+ @filters = AccessTrail.filters(params)
7
+ @export_url = [ AccessTrail.export_address, @filters.to_query.presence ].compact.join("?") if AccessTrail.export_address
8
+ names = Hash.new { |known, gid| known[gid] = Entry.name_for(gid) }
9
+ @people = Entry.distinct.pluck(:person_gid).map { |gid| [ names[gid], gid ] }.sort
10
+ @accounts = Entry.distinct.pluck(:account_gid).map { |gid| [ names[gid], gid ] }.sort
11
+ @entry_types = Entry.distinct.order(:entry_type).pluck(:entry_type).map { |entry_type| [ entry_type, entry_type ] }
12
+ @rows = AccessTrail.entries(@filters).map do |entry|
8
13
  { who: names[entry.person_gid], entry_type: entry.entry_type, record: entry.record_label, account: names[entry.account_gid], time: helpers.l(entry.created_at, format: :long) }
9
14
  end
10
15
  end
@@ -1,5 +1,17 @@
1
1
  module AccessTrail
2
2
  class Entry < ActiveRecord::Base
3
+ def self.name_for(gid)
4
+ AccessTrail.name_of.call(GlobalID::Locator.locate(gid))
5
+ rescue ActiveRecord::RecordNotFound
6
+ gid
7
+ end
8
+
3
9
  def readonly? = persisted? || super
10
+
11
+ def person_name = self.class.name_for(person_gid)
12
+
13
+ def acted_as_name = acted_as_gid && self.class.name_for(acted_as_gid)
14
+
15
+ def account_name = self.class.name_for(account_gid)
4
16
  end
5
17
  end
@@ -1,4 +1,12 @@
1
1
  <%= ui_page(max_width: :xl) do %>
2
- <%= ui_page_header(title: "Access trail") %>
3
- <%= ui_data_table(items: @rows, columns: [ { who: "Who" }, { entry_type: "Entry type" }, { record: "Record" }, { account: "Account" }, { time: "Time" } ], empty_message: "No entries are recorded.", phone_list: true) %>
2
+ <%= ui_page_header(title: "Access trail", action_url: @export_url, action_label: "Export") %>
3
+ <%= ui_form(action: root_path, method: :get) do %>
4
+ <%= ui_form_field(attribute: "person", label: "Person", type: :select, options: @people, value: @filters[:person]) %>
5
+ <%= ui_form_field(attribute: "account", label: "Account", type: :select, options: @accounts, value: @filters[:account]) %>
6
+ <%= ui_form_field(attribute: "entry_type", label: "Entry type", type: :select, options: @entry_types, value: @filters[:entry_type]) %>
7
+ <%= ui_form_field(attribute: "from", label: "From", type: :date, value: @filters[:from]) %>
8
+ <%= ui_form_field(attribute: "to", label: "To", type: :date, value: @filters[:to]) %>
9
+ <%= ui_button(label: "Filter") %>
10
+ <% end %>
11
+ <%= ui_data_table(items: @rows, columns: [ { who: "Who" }, { entry_type: "Entry type" }, { record: "Record" }, { account: "Account" }, { time: "Time" } ], empty_message: @filters.any? ? "No entries match these filters." : "No entries are recorded.", phone_list: true) %>
4
12
  <% end %>
@@ -0,0 +1,5 @@
1
+ class AddActedAsGidToAccessTrailEntries < ActiveRecord::Migration[8.1]
2
+ def change
3
+ add_column :access_trail_entries, :acted_as_gid, :string
4
+ end
5
+ end
@@ -1,3 +1,3 @@
1
1
  module AccessTrail
2
- VERSION = "0.1.0"
2
+ VERSION = "0.2.0"
3
3
  end
data/lib/access_trail.rb CHANGED
@@ -6,11 +6,42 @@ module AccessTrail
6
6
 
7
7
  SETTINGS = %i[base_controller person_method layout reader name_of].freeze
8
8
 
9
+ FILTERS = %i[person account entry_type from to].freeze
10
+
9
11
  mattr_accessor(*SETTINGS)
12
+ mattr_accessor :export_address
13
+
14
+ def self.record(person:, account:, entry_type:, acted_as: nil, record_type: nil, record_code: nil, record_label: nil, detail: nil, address: nil)
15
+ Entry.create!(person_gid: person.to_global_id.to_s, acted_as_gid: acted_as&.to_global_id&.to_s, account_gid: account.to_global_id.to_s, entry_type: entry_type, record_type: record_type, record_code: record_code, record_label: record_label, detail: detail, address: address)
16
+ end
17
+
18
+ def self.record_export(person:, account:, filters:, address: nil)
19
+ used = filters(filters).map { |filter, value| "#{filter} #{value}" }
20
+ record(person: person, account: account, entry_type: "Exported", address: address, detail: used.any? ? "Filters: #{used.join(", ")}" : "No filters")
21
+ end
22
+
23
+ def self.filters(given)
24
+ FILTERS.index_with { |filter| given[filter].presence }.compact
25
+ end
26
+
27
+ def self.entries(given = {})
28
+ filters = filters(given)
29
+ entries = Entry.order(created_at: :desc, id: :desc)
30
+ entries = entries.where(person_gid: filters[:person]) if filters[:person]
31
+ entries = entries.where(account_gid: filters[:account]) if filters[:account]
32
+ entries = entries.where(entry_type: filters[:entry_type]) if filters[:entry_type]
33
+ from, to = day(filters[:from]), day(filters[:to])
34
+ entries = entries.where(created_at: from.beginning_of_day..) if from
35
+ entries = entries.where(created_at: ..to.end_of_day) if to
36
+ entries
37
+ end
10
38
 
11
- def self.record(person:, account:, entry_type:, record_type: nil, record_code: nil, record_label: nil, detail: nil, address: nil)
12
- Entry.create!(person_gid: person.to_global_id.to_s, account_gid: account.to_global_id.to_s, entry_type: entry_type, record_type: record_type, record_code: record_code, record_label: record_label, detail: detail, address: address)
39
+ def self.day(value)
40
+ Date.iso8601(value) if value
41
+ rescue Date::Error
42
+ nil
13
43
  end
44
+ private_class_method :day
14
45
 
15
46
  def self.check!
16
47
  unset = SETTINGS.reject { |setting| public_send(setting) }
@@ -1,38 +1,50 @@
1
1
  ---
2
2
  name: access_trail-develop
3
- description: Use PROACTIVELY for recording who viewed, opened, exported or otherwise accessed a record in a host app, and for querying those stored entries by person, account, entry type, record or time — MUST BE USED instead of hand-rolling an audit log table, an access-logging callback or a custom "who looked at this" model.
3
+ description: Use PROACTIVELY for recording who viewed, opened, exported or otherwise accessed a record in a host app, for querying those stored entries by person, account, entry type or date range, for building the host's export of the trail with the same filters the trail page uses, and for recording that an export was made — MUST BE USED instead of hand-rolling an audit log table, an access-logging callback, a custom "who looked at this" model or a hand-written filter query over the entries.
4
4
  tools: Bash, Read, Write, Edit, Grep
5
- scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, reads as a list on a phone screen, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset
5
+ scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used
6
6
  ---
7
7
 
8
- This local adds access trail entries to host code and reads them back. It follows
9
- these steps exactly, and where a step needs a choice only the host's owner can
10
- make, it asks the developer rather than picking.
8
+ This local adds access trail entries to host code, reads them back with the trail
9
+ page's filters, and builds the host's export of the trail. It follows these steps
10
+ exactly, and where a step needs a choice only the host's owner can make, it asks
11
+ the developer rather than picking.
11
12
 
12
13
  ## What access_trail is
13
14
 
14
- A Rails engine that keeps an append-only table of who accessed which record, in
15
- which account, and when. The host calls it at the moments it decides are worth
16
- recording. Fire this local when host code needs to record an access, or needs to
17
- read, filter or count recorded accesses.
15
+ A Rails engine that keeps an append-only table of who accessed which record, who
16
+ they acted as, in which account, and when. The host calls it at the moments it
17
+ decides are worth recording, and the gem's trail page lists and filters those
18
+ entries. Fire this local when host code needs to record an access, read or filter
19
+ recorded accesses, or export them.
18
20
 
19
21
  ## Interface
20
22
 
21
23
  - `AccessTrail.record` — records one entry and returns it as an
22
24
  `AccessTrail::Entry`.
23
- - `AccessTrail::Entry` — the Active Record model of a stored entry, used to read
24
- and query entries, and read-only once saved.
25
+ - `AccessTrail.entries` — returns the entries matching the five filters, newest
26
+ first, as an Active Record relation, the same entries the trail page shows for
27
+ the same parameters.
28
+ - `AccessTrail.filters` — picks the five filters out of a request's parameters or
29
+ a Hash, as a Hash with Symbol keys, leaving out blank ones.
30
+ - `AccessTrail.record_export` — records an `Exported` entry whose detail names
31
+ the filters the export used, and returns it as an `AccessTrail::Entry`.
32
+ - `AccessTrail::Entry` — the Active Record model of a stored entry, read-only once
33
+ saved, with the names of its person, acted-as person and account.
25
34
 
26
35
  ## How to use it
27
36
 
37
+ ### Recording an access
38
+
28
39
  1. Ask the developer which moments are recorded. Name each candidate as an action
29
40
  and a record, such as "showing an integration call" or "downloading an
30
41
  invoice PDF". The gem records nothing on its own.
31
- 2. Ask the developer for the entry type of each moment, such as `"Viewed"`,
32
- `"Exported"` or `"Downloaded"`. It is a free String shown on the trail page,
33
- so reuse the spelling the host already uses for the same type of moment rather
34
- than coining a second one. Grep the host for existing `AccessTrail.record`
35
- calls first.
42
+ 2. Ask the developer for the entry type of each moment, such as `"Viewed"` or
43
+ `"Downloaded"`. It is a free String shown on the trail page and offered in its
44
+ entry type filter, so grep the host for existing `AccessTrail.record` calls and
45
+ reuse the spelling already there rather than coining a second one. `"Exported"`
46
+ is the type `AccessTrail.record_export` writes, so use that call for exports
47
+ rather than recording `"Exported"` by hand.
36
48
  3. In the controller action, job or service where the moment happens, after the
37
49
  access has succeeded, call:
38
50
  ```ruby
@@ -51,41 +63,121 @@ read, filter or count recorded accesses.
51
63
  `to_global_id`, which every Active Record model does.
52
64
  - `account:` — required. The account the person acted in, under the same rule.
53
65
  - `entry_type:` — required. A String.
66
+ - `acted_as:` — optional. The person whose identity was used, under the same
67
+ rule, when it differs from `person:`, such as an admin impersonating a user.
68
+ Ask the developer whether the host has impersonation and, if so, pass the
69
+ impersonated person here and the real person as `person:`.
54
70
  - `record_type:` — optional String. A name a person would read, such as
55
71
  `"Integration call"`, not a class name.
56
- - `record_code:` — optional String. The record's public code. Ask the developer
57
- which value is safe to show to trail readers if the record has no public
58
- code.
72
+ - `record_code:` — optional String. The record's public code. If the record has
73
+ no public code, ask the developer which value is safe to show to trail
74
+ readers.
59
75
  - `record_label:` — optional String. The label the trail page shows for the
60
76
  record.
61
77
  - `detail:` — optional String. A short note on what was accessed.
62
78
  - `address:` — optional String. The network address, usually
63
79
  `request.remote_ip`. Leave it out where there is no request, such as a job.
80
+
64
81
  The record's type, code and label are copied at the moment of recording, so
65
82
  renaming the record later leaves the entry as it was.
66
83
  4. Never pass a credential, a token, a secret or a request or response body as
67
- `record_label:` or `detail:`. The trail page shows both values to every
68
- reader.
69
- 5. To read entries, query `AccessTrail::Entry` like any Active Record model. Its
70
- attributes are `person_gid`, `account_gid`, `entry_type`, `record_type`,
71
- `record_code`, `record_label`, `detail`, `address` and `created_at`. There is
72
- no `updated_at`. People and accounts are stored as global id Strings, so match
73
- them with:
84
+ `record_label:` or `detail:`. The trail page shows both values to every reader.
85
+ 5. Write a test that performs the moment and asserts one new entry exists with the
86
+ expected `entry_type`, `person_gid` and `record_code`.
87
+
88
+ ### Reading entries
89
+
90
+ 6. Read entries with `AccessTrail.entries`, never with a hand-written query that
91
+ repeats the filters:
74
92
  ```ruby
75
- AccessTrail::Entry.where(account_gid: account.to_global_id.to_s)
76
- .where(record_type: "Integration call", record_code: call.public_code)
77
- .order(created_at: :desc, id: :desc)
93
+ AccessTrail.entries(params)
94
+ AccessTrail.entries(entry_type: "Viewed", from: "2026-11-01")
95
+ AccessTrail.entries
78
96
  ```
79
- Turn a stored id back into its object with
80
- `GlobalID::Locator.locate(entry.person_gid)`, which returns `nil` when the
81
- object no longer exists. There are no associations or scopes on the model.
82
- 6. Write a test that performs the moment and asserts one new entry exists with
83
- the expected `entry_type`, `person_gid` and `record_code`.
97
+ It takes a request's parameters or a Hash with Symbol keys and reads only these
98
+ five filters, ignoring every other key:
99
+ - `person` — the global id String of the person who acted, such as
100
+ `user.to_global_id.to_s`.
101
+ - `account` — the global id String of the account.
102
+ - `entry_type` — the exact entry type, such as `"Viewed"`.
103
+ - `from` — a date as `"YYYY-MM-DD"`, keeping entries recorded on or after the
104
+ start of that day.
105
+ - `to` — a date as `"YYYY-MM-DD"`, keeping entries recorded on or before the end
106
+ of that day.
107
+
108
+ A blank filter narrows nothing, and so does a date that cannot be read as
109
+ `YYYY-MM-DD`. Days are read in the app's time zone. With no argument it returns
110
+ every entry. A Hash with String keys is read as having no filters, so build one
111
+ with Symbol keys.
112
+ 7. The result is a relation ordered newest first, so chain onto it as needed:
113
+ ```ruby
114
+ AccessTrail.entries(params)
115
+ .where(account_gid: current_account.to_global_id.to_s)
116
+ .where(record_type: "Integration call", record_code: call.public_code)
117
+ ```
118
+ `AccessTrail.entries` does not scope by account. Where host code shows entries
119
+ to anyone other than a trail reader, chain the current account onto it.
120
+ 8. To get the filters a request used, call `AccessTrail.filters(params)`. It
121
+ returns only the five filters that are present, such as
122
+ `{ entry_type: "Viewed", from: "2026-11-01" }`, and passing that Hash to
123
+ `AccessTrail.entries` gives the same result as passing `params`.
124
+ 9. Read each entry through its attributes and its three names:
125
+ - `created_at` — the time it was recorded. There is no `updated_at`.
126
+ - `person_name` — who acted.
127
+ - `acted_as_name` — who they acted as, or `nil` when no `acted_as:` was
128
+ recorded.
129
+ - `account_name` — the account.
130
+ - `entry_type`, `record_type`, `record_code`, `record_label`, `detail` and
131
+ `address` — as recorded.
132
+ - `person_gid`, `acted_as_gid` and `account_gid` — the stored global id
133
+ Strings, for matching and grouping.
134
+
135
+ The names come from the naming setting the host configured at install. When
136
+ the person or account has been deleted, its name is its stored global id
137
+ String, so the entry still reads. Each name call looks the person or account
138
+ up, so in a loop over many entries keep a Hash of names keyed by the global id
139
+ rather than calling the name method for every row.
140
+
141
+ ### Building the host's export
142
+
143
+ The trail page's Export link points at an address the host serves itself, and it
144
+ carries the page's filters as query parameters with the same five names. The
145
+ gem serves no export. When the host needs one:
146
+
147
+ 10. Ask the developer the export's format, such as CSV, and which columns it
148
+ holds.
149
+ 11. Ask the developer who may download it. The gem's reader check guards only the
150
+ trail page, not the host's export address, so the export action must refuse
151
+ everyone the trail page refuses. Reuse the host's own rule for trail readers
152
+ rather than writing a second one.
153
+ 12. In the export action, list the entries and record the export:
154
+ ```ruby
155
+ entries = AccessTrail.entries(params)
156
+ AccessTrail.record_export(
157
+ person: current_user,
158
+ account: current_account,
159
+ filters: params,
160
+ address: request.remote_ip
161
+ )
162
+ ```
163
+ `filters:` takes the same request parameters or Symbol-keyed Hash as
164
+ `AccessTrail.entries`. The recorded entry has the type `"Exported"`, and its
165
+ detail names each filter used in the order person, account, entry type, from,
166
+ to, such as `"Filters: entry_type Viewed, from 2026-11-01"`, or
167
+ `"No filters"` when none was used. `address:` may be left out.
168
+ 13. Route the export action at the export address the host configured at
169
+ install, so the trail page's Export link reaches it. If no export address is
170
+ configured yet, hand that setting to `access_trail-install`.
171
+ 14. Write a test that requests the export with a filter and asserts it holds only
172
+ the matching entries, and one that asserts an `Exported` entry was recorded
173
+ with that filter in its detail.
84
174
 
85
175
  ## Conventions
86
176
 
87
- - Record through `AccessTrail.record`, never through `AccessTrail::Entry.create`
88
- or `new`, so every entry is stored the same way.
177
+ - Record through `AccessTrail.record` or `AccessTrail.record_export`, never through
178
+ `AccessTrail::Entry.create` or `new`, so every entry is stored the same way.
179
+ - Filter through `AccessTrail.entries`, never by rewriting its date or filter
180
+ rules, so the host's export always holds what the trail page shows.
89
181
  - A saved entry is read-only: `update`, `save` and `destroy` on it raise
90
182
  `ActiveRecord::ReadOnlyRecord`. Never write code that changes or removes an
91
183
  entry.
@@ -94,8 +186,6 @@ read, filter or count recorded accesses.
94
186
  - `AccessTrail.record` raises on failure rather than returning `false`. Ask the
95
187
  developer whether a failed recording should stop the access, which is the
96
188
  result when the call is left unrescued.
97
- - Each entry is scoped by `account_gid`, and a query in host code that shows
98
- entries to users must filter by the current account.
99
- - Adding the gem, the migration, the mount and the five host settings, and the
100
- boot error for an unset setting, are out of scope here; hand that work to
189
+ - Adding the gem, the migration, mounting the engine, the host settings and the
190
+ boot error for an unset setting are out of scope here. Hand that work to
101
191
  `access_trail-install`.
@@ -1,8 +1,8 @@
1
1
  ---
2
2
  name: access_trail-info
3
- description: Use to learn what access_trail offers — an append-only record of who accessed which record in a host Rails app, the trail page that lists it, who may read that page, and the host settings it needs before the app will start.
3
+ description: Use to learn what access_trail offers — an append-only record of who accessed which record in a host Rails app, the trail page that lists it and narrows it by filters, the filtered entries a host can ask for to build its own export, recording that an export was made, who may read the trail, and the host settings it needs before the app will start.
4
4
  tools: Read
5
- scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, reads as a list on a phone screen, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset
5
+ scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used
6
6
  ---
7
7
 
8
8
  This local explains access_trail and makes no changes.
@@ -10,9 +10,10 @@ This local explains access_trail and makes no changes.
10
10
  ## What access_trail is
11
11
 
12
12
  access_trail is a Rails engine that keeps an append-only record of who accessed
13
- which record in a host app. Each entry says who acted, for which account, what
14
- type of access it was, which record it was about and when it happened. Once an
15
- entry is saved it cannot be changed.
13
+ which record in a host app. Each entry says who acted, who they acted as when
14
+ that was someone else, for which account, what type of access it was, which
15
+ record it was about and when it happened. Once an entry is saved it cannot be
16
+ changed.
16
17
 
17
18
  Reach for it when a host app has to answer "who looked at or touched this, and
18
19
  when" — for an audit, a client's question, or a compliance requirement. The gem
@@ -20,47 +21,72 @@ does not watch the app on its own. The host records an entry at each moment it
20
21
  decides is worth recording, such as opening a record's page or downloading a
21
22
  file.
22
23
 
23
- The gem also adds one page, the trail page, that lists every entry newest first.
24
- It is drawn with keystone_ui, reads as a table on a desktop and as a list on a
25
- phone screen, and is shown only to the people the host allows.
24
+ The gem adds one page, the trail page, that lists every entry newest first and
25
+ can be narrowed by person, account, entry type and a date range. It is drawn
26
+ with keystone_ui, reads as a table on a desktop and as a list on a phone screen,
27
+ and is shown only to the people the host allows. The gem does not build an
28
+ export file itself. The host builds its own export, asks the gem for the same
29
+ filtered entries the page shows, and records that the export was made.
26
30
 
27
31
  ## Interface
28
32
 
29
33
  This local declares no commands. The surface is split between the other two:
30
34
 
31
35
  - **access_trail-install** owns adding the gem to a host: the gem line, copying
32
- its migration, mounting the trail page, and the five host settings it needs.
33
- - **access_trail-develop** owns writing code against the gem: recording an entry
34
- from the host, and reading the stored entries.
36
+ its migration, mounting the trail page, the five required host settings, the
37
+ optional export address, and the error raised when a required setting is
38
+ unset.
39
+ - **access_trail-develop** owns writing code against the gem: recording an
40
+ entry, asking for the filtered entries, picking the filters out of a request,
41
+ recording an export, and reading a stored entry and its names.
35
42
 
36
43
  ## How to use it
37
44
 
38
- - The gem is not in the host yet, the trail page is not mounted, or the app
39
- refuses to start because a setting is unset — use **access_trail-install**.
40
- - The gem is installed and the work is recording access at a new moment, or
41
- reading what was recorded — use **access_trail-develop**.
45
+ - The gem is not in the host yet, the trail page is not mounted, the app refuses
46
+ to start because a setting is unset, or the trail page should link to the
47
+ host's export — use **access_trail-install**.
48
+ - The gem is installed and the work is recording access at a new moment,
49
+ building an export of the trail, or reading what was recorded — use
50
+ **access_trail-develop**.
42
51
 
43
52
  ## Conventions
44
53
 
45
- - **Entry** — one recorded access. It holds the person, the account, the entry
46
- type, the record's type, public code and label, a short detail, the network
47
- address and the time it was saved. Only the person, the account and the entry
48
- type are required.
54
+ - **Entry** — one recorded access. It holds the person, who they acted as, the
55
+ account, the entry type, the record's type, public code and label, a short
56
+ detail, the network address and the time it was saved. Only the person, the
57
+ account and the entry type are required.
49
58
  - **Append-only** — an entry is written once and never edited or removed through
50
59
  the gem.
51
60
  - **Person and account** — stored by their global id rather than by a foreign
52
- key, so any model in the host can be either. The trail page turns each back
53
- into a display name with a host setting.
61
+ key, so any model in the host can be either. The host's naming setting turns
62
+ each back into a display name. When the person or account has been deleted,
63
+ its stored global id is shown as the name, so the entry still reads and can
64
+ still be filtered on.
65
+ - **Acted as** — the person whose identity was used, recorded only when it
66
+ differs from the person who acted, such as an admin signed in as a customer.
54
67
  - **Entry type** — a short label the host chooses for the type of access, such
55
- as a view or a download. The gem does not fix the list.
68
+ as `Viewed`. The gem does not fix the list, apart from `Exported`, which it
69
+ uses for a recorded export.
56
70
  - **Record type, public code and label** — how an entry names the record that
57
71
  was accessed. They are plain text copied in at the time of recording, so an
58
72
  entry still reads correctly after the record changes or is deleted. The code
59
- is the code the host shows people for that record.
73
+ is the code the host shows people for that record. Never put a credential, a
74
+ secret or a request body in the label or the detail.
75
+ - **Filters** — five query parameters that narrow the trail: `person` and
76
+ `account` by global id, `entry_type`, and `from` and `to` as `YYYY-MM-DD`
77
+ dates read in the app's time zone. A blank filter or a date that cannot be read
78
+ narrows nothing. The page and the host's export read the same five, so the same
79
+ parameters give the same entries.
80
+ - **Export address** — the address of the host's own export. When it is set, the
81
+ trail page shows an Export link to it carrying the page's current filters.
82
+ When it is not, the page shows no Export link.
83
+ - **Recorded export** — an `Exported` entry whose detail lists the filters the
84
+ export used, or says `No filters`.
60
85
  - **Reader** — a person the host's reader setting allows to see the trail page.
61
86
  Everyone else, including a visitor who is not signed in, gets not found rather
62
87
  than a refusal, so the page's existence is not revealed.
63
- - **Host settings** — five values the host sets: the controller the trail page
88
+ - **Host settings** — five required values: the controller the trail page
64
89
  inherits from, the method that returns the signed-in person, the layout, the
65
90
  reader rule, and how a person or account is named. The app will not boot
66
- while any of them is unset, and the error lists each one that is missing.
91
+ while any of them is unset, and the error lists each one that is missing. The
92
+ export address is a sixth, optional setting.
@@ -1,8 +1,8 @@
1
1
  ---
2
2
  name: access_trail-install
3
- description: Use to hook access_trail into a project — adding the gem, copying and running its migration, mounting the trail page, setting the five host settings (base controller, person method, layout, reader, name of), and clearing the boot error that names an unset setting.
3
+ description: Use to hook access_trail into a project — adding the gem, copying and running its migrations, mounting the trail page, setting the five required host settings (base controller, person method, layout, reader, name of) and the optional export address, and clearing the boot error that names an unset setting.
4
4
  tools: Bash, Read, Edit
5
- scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, reads as a list on a phone screen, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset
5
+ scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used
6
6
  ---
7
7
 
8
8
  This local follows these steps exactly and invents none. Where a step needs a
@@ -11,15 +11,16 @@ value only the host's owner knows, it asks the developer rather than picking one
11
11
  ## What access_trail is
12
12
 
13
13
  A Rails engine that stores an append-only table of who accessed which record and
14
- shows it on a mounted trail page; hook it in when a host app has to answer who
15
- accessed a record and when.
14
+ shows it on a mounted, filterable trail page; hook it in when a host app has to
15
+ answer who accessed a record and when.
16
16
 
17
17
  ## Interface
18
18
 
19
19
  - `gem "access_trail"` — the Gemfile line that adds the gem; it brings in
20
20
  keystone_ui, which draws the trail page.
21
- - `bin/rails access_trail:install:migrations` — copies the migration that creates
22
- the `access_trail_entries` table into the host's `db/migrate`.
21
+ - `bin/rails access_trail:install:migrations` — copies the gem's migrations,
22
+ which create the `access_trail_entries` table and add its `acted_as_gid`
23
+ column, into the host's `db/migrate`.
23
24
  - `mount AccessTrail::Engine` — the line in the host's `config/routes.rb` that
24
25
  puts the trail page at a path the host chooses.
25
26
  - `AccessTrail.base_controller=` — the name, as a String, of the host controller
@@ -33,9 +34,14 @@ accessed a record and when.
33
34
  whether that person may read the trail.
34
35
  - `AccessTrail.name_of=` — a callable given a person or an account that returns
35
36
  the name the trail page shows for it.
37
+ - `AccessTrail.export_address=` — optional; the address, as a String, of the
38
+ host's own export of the trail. When set, the trail page shows an Export link
39
+ to it carrying the page's current filters as query parameters. When unset, the
40
+ page shows no Export link.
36
41
  - `AccessTrail::UnsetSettingError` — raised at boot, and again after every code
37
- reload, while any of the five settings is unset, with one line per unset
38
- setting in the form `AccessTrail.<setting> is not set`.
42
+ reload, while any of the five required settings is unset, with one line per
43
+ unset setting in the form `AccessTrail.<setting> is not set`. The export address
44
+ is not checked.
39
45
 
40
46
  ## How to use it
41
47
 
@@ -47,14 +53,15 @@ accessed a record and when.
47
53
  gem "access_trail"
48
54
  ```
49
55
  Then run `bundle install`.
50
- 3. Copy and run the migration:
56
+ 3. Copy and run the migrations:
51
57
  ```sh
52
58
  bin/rails access_trail:install:migrations
53
59
  bin/rails db:migrate
54
60
  ```
55
- This adds one migration file to `db/migrate` and the `access_trail_entries`
56
- table to `db/schema.rb`.
57
- 4. Ask the developer for each of the five settings. None has a safe default:
61
+ This adds two migration files to `db/migrate` and the `access_trail_entries`
62
+ table, with its `acted_as_gid` column, to `db/schema.rb`.
63
+ 4. Ask the developer for each of the five required settings. None has a safe
64
+ default:
58
65
  - **Base controller** — which host controller signs people in and should run
59
66
  before the trail page, such as `"ApplicationController"` or an admin base
60
67
  controller.
@@ -67,17 +74,24 @@ accessed a record and when.
67
74
  - **Name of** — how a person and an account are shown on the page. The same
68
75
  callable is given both, so ask whether the two models share a method
69
76
  such as `name`, or whether the callable must tell them apart.
70
- 5. Create `config/initializers/access_trail.rb` with the developer's answers:
77
+ 5. Ask the developer whether the trail page should show an Export link. If yes,
78
+ ask for the address of the host's export, such as
79
+ `"/csv/exports/access_trail"`. The link appends the page's filters as query
80
+ parameters named `person`, `account`, `entry_type`, `from` and `to`, so the
81
+ host's export must read those. Building that export is out of scope here.
82
+ 6. Create `config/initializers/access_trail.rb` with the developer's answers:
71
83
  ```ruby
72
84
  AccessTrail.base_controller = "ApplicationController"
73
85
  AccessTrail.person_method = :current_user
74
86
  AccessTrail.layout = "application"
75
87
  AccessTrail.reader = ->(person) { person.admin? }
76
88
  AccessTrail.name_of = ->(person_or_account) { person_or_account.name }
89
+ AccessTrail.export_address = "/csv/exports/access_trail"
77
90
  ```
91
+ Leave out the `export_address` line if the developer wants no Export link.
78
92
  The settings must be set in an initializer, since the boot check runs once
79
93
  initializers have loaded.
80
- 6. Ask the developer for the path the trail page should live at, then add the
94
+ 7. Ask the developer for the path the trail page should live at, then add the
81
95
  mount to `config/routes.rb`, inside any scope or constraint that should guard
82
96
  it:
83
97
  ```ruby
@@ -88,15 +102,19 @@ accessed a record and when.
88
102
  ## Conventions
89
103
 
90
104
  - Boot the app with `bin/rails runner 'puts :ok'`. An
91
- `AccessTrail::UnsetSettingError` lists every setting still unset, one per line;
92
- set each one in the initializer and boot again.
105
+ `AccessTrail::UnsetSettingError` lists every required setting still unset, one
106
+ per line; set each one in the initializer and boot again.
93
107
  - Visit the mount path signed in as a person the reader rule allows and confirm
94
- the page titled "Access trail" loads. Signed out, or signed in as anyone else,
95
- the same path answers 404 by design, not as a failure.
108
+ the page titled "Access trail" loads, with its Person, Account, Entry type,
109
+ From and To filters. Signed out, or signed in as anyone else, the same path
110
+ answers 404 by design, not as a failure.
111
+ - With an export address set, confirm the page's Export link points at it and
112
+ carries the filters applied on the page.
96
113
  - The people and accounts later passed to the gem are stored by global id, so
97
114
  their models must include `GlobalID::Identification`, which every Active Record
98
115
  model does.
99
116
  - After upgrading the gem, run `bin/rails access_trail:install:migrations` again
100
117
  to copy any migration the new version adds, then `bin/rails db:migrate`.
101
- - Recording entries and reading stored entries are out of scope here; hand that
102
- work to `access_trail-develop`.
118
+ - Recording entries, recording an export, reading filtered entries and building
119
+ the host's export are out of scope here; hand that work to
120
+ `access_trail-develop`.
@@ -1,4 +1,4 @@
1
- scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, reads as a list on a phone screen, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset
1
+ scope: access trail — an append-only record of who accessed which record in a host Rails app, each entry naming the person, who they acted as, the account, the entry type, the record's type, public code and label, a short detail, the network address and the time, with people and accounts stored by global id, recorded by the host at the moments it chooses, and a mounted trail page drawn with keystone_ui that lists every entry newest first, narrows it by person, account, entry type and a date range, reads as a list on a phone screen, shows an Export link to the host's export address carrying its filters, is shown only to a person the host's reader setting allows and answers not found to everyone else, and a boot check that refuses to start the app while a host setting is unset, and a query that hands the host the same filtered entries the page shows, with each entry's names, and a call that records an export with the filters it used
2
2
 
3
3
  install:
4
4
  - gem "access_trail"
@@ -9,10 +9,14 @@ install:
9
9
  - AccessTrail.layout=
10
10
  - AccessTrail.reader=
11
11
  - AccessTrail.name_of=
12
+ - AccessTrail.export_address=
12
13
  - AccessTrail::UnsetSettingError
13
14
 
14
15
  develop:
15
16
  - AccessTrail.record
17
+ - AccessTrail.entries
18
+ - AccessTrail.filters
19
+ - AccessTrail.record_export
16
20
  - AccessTrail::Entry
17
21
 
18
22
  sources:
@@ -20,6 +24,7 @@ sources:
20
24
  - lib/access_trail/engine.rb
21
25
  - config/routes.rb
22
26
  - db/migrate/20261130022543_create_access_trail_entries.rb
27
+ - db/migrate/20261130190029_add_acted_as_gid_to_access_trail_entries.rb
23
28
  - app/models/access_trail/entry.rb
24
29
  - app/controllers/access_trail/application_controller.rb
25
30
  - app/controllers/access_trail/entries_controller.rb
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: access_trail
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tylercschneider
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2026-11-30 00:00:00.000000000 Z
10
+ date: 1980-01-02 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rails
@@ -56,6 +55,7 @@ files:
56
55
  - app/views/access_trail/entries/index.html.erb
57
56
  - config/routes.rb
58
57
  - db/migrate/20261130022543_create_access_trail_entries.rb
58
+ - db/migrate/20261130190029_add_acted_as_gid_to_access_trail_entries.rb
59
59
  - lib/access_trail.rb
60
60
  - lib/access_trail/engine.rb
61
61
  - lib/access_trail/version.rb
@@ -70,7 +70,6 @@ metadata:
70
70
  allowed_push_host: https://rubygems.org
71
71
  homepage_uri: https://github.com/DYB-Development/access_trail
72
72
  source_code_uri: https://github.com/DYB-Development/access_trail
73
- post_install_message:
74
73
  rdoc_options: []
75
74
  require_paths:
76
75
  - lib
@@ -85,8 +84,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
85
84
  - !ruby/object:Gem::Version
86
85
  version: '0'
87
86
  requirements: []
88
- rubygems_version: 3.5.16
89
- signing_key:
87
+ rubygems_version: 4.0.20
90
88
  specification_version: 4
91
89
  summary: Records who accessed which record in a host app and lists those entries on
92
90
  a page.