critiqs-rails 0.4.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 86da5bc02f750c3dadfff61fbce3dcb9f58d6a6c69025fcdc14e4f2a2044b9fa
4
+ data.tar.gz: 18e388d4ca481f3c39338a03dbff233dbf4f0c1458fc53f477406ea54d006f58
5
+ SHA512:
6
+ metadata.gz: 65485a76ac5e86a3e738f8395be1bd80be65bb5d7ed46405ba3d71b564e6407882509f94e2f6d8c1460a98531d158e31a6b752ef4a6e62ba04775d85150a8f6d
7
+ data.tar.gz: 2d8d2254ec3e662d8c1a5ffff6ef1aff3ee7d1e7b43d07c6c47514e9dbcc6466161c5cb419c5f77d578f85e73ec60834fe9f476e02b92b6225fe4f38819164c0
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Noah Jung
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,140 @@
1
+ # critiqs-rails
2
+
3
+ [Critiqs](https://github.com/noahsjung/critiqs) for Ruby on Rails apps:
4
+ reviewers comment on your running prototype, discuss feedback, and find the
5
+ screen and state each comment was made in.
6
+
7
+ Requires Ruby 3.3 or later and Rails 7.1 or later with Propshaft or Sprockets
8
+ (every `rails new` app has one).
9
+
10
+ ## Set up
11
+
12
+ Add the gem and the tag:
13
+
14
+ ```sh
15
+ bundle add critiqs-rails
16
+ bin/rails generate critiqs:install
17
+ ```
18
+
19
+ The generator adds `<%= critiqs_tag %>` just before `</body>` in
20
+ `app/views/layouts/application.html.erb`. Or run `npx critiqs init`, which
21
+ does both. Both edit only what they can read the way Rails does (a whole
22
+ `<%= critiqs_tag ... %>` tag, whose arguments they change in place, and the
23
+ page's own `</body>` rather than one inside ERB code, a comment, a script, a
24
+ `<textarea>`, the `<title>` or an attribute value) and otherwise print the
25
+ steps to finish by hand. When a view already calls `critiqs_tag`, such as a
26
+ partial the layout renders, neither adds a second.
27
+
28
+ Keep `gem "critiqs-rails"` at the top level of the Gemfile, outside any
29
+ `group` or condition and without `require: false`: the layout calls
30
+ `critiqs_tag` in every environment, so production needs the gem even where
31
+ Critiqs stays hidden. Neither the generator nor `critiqs init` adds the tag
32
+ while the Gemfile might keep the gem from production (a group, `platforms`,
33
+ `require: false`, or a condition such as `if`, `install_if` or `env`, even one
34
+ this computer meets now, however the declaration is split across lines).
35
+
36
+ Then set your project's ID in each environment that should show Critiqs, and
37
+ restart the server:
38
+
39
+ ```sh
40
+ CRITIQS_PROJECT_ID=j1234… bin/rails server
41
+ ```
42
+
43
+ Where `CRITIQS_PROJECT_ID` is unset, the tag renders nothing: production can
44
+ run without Critiqs while a review deployment shows it. `npx critiqs init`
45
+ sets it for development: in `.env` when the app uses dotenv (or, when a plain
46
+ `ENV["CRITIQS_PROJECT_ID"] = "…"` line in `config/environments/development.rb`
47
+ would override `.env`, in that line), and otherwise as a default at the end of
48
+ `config/environments/development.rb`, which an environment variable still
49
+ overrides. It rewrites a line you wrote only when the line sets one string,
50
+ with nothing but a comment after it and no condition around it; it leaves any
51
+ other line as it is, puts its own value where Rails reads it first, and says
52
+ which value the app uses:
53
+
54
+ ```ruby
55
+ ENV["CRITIQS_PROJECT_ID"] ||= "<project ID>"
56
+ ```
57
+
58
+ ## `critiqs_tag`
59
+
60
+ ```erb
61
+ <%= critiqs_tag %>
62
+ <%= critiqs_tag(project_id: "j1234…", backend: "https://your-team.example/critiqs", version: ENV["GIT_SHA"]) %>
63
+ ```
64
+
65
+ | Option | Default | Meaning |
66
+ | --- | --- | --- |
67
+ | `project_id` | `ENV["CRITIQS_PROJECT_ID"]`, read on each render | The Critiqs project. |
68
+ | `require_project_id` | `true` | Show Critiqs only where `project_id` is set; without one, render nothing. Pass `false` to show project setup instead. |
69
+ | `backend` | Critiqs Cloud | Your team's own Critiqs backend URL. |
70
+ | `version` | none | The prototype's version, such as a release or commit, recorded with each message. |
71
+ | `screen` | the matched route's pattern | The screen threads on this page belong to; see [Routes](#routes). `false` uses the URL path. |
72
+ | Other options | | HTML attributes for the `<critiqs-app>` element, such as `id`. |
73
+
74
+ The tag renders a `<critiqs-app>` element carrying the page's route in its
75
+ `data-critiqs-context` attribute, and two `<script type="module">` tags: one
76
+ loads Critiqs, the other a few lines from this gem that hand the route to
77
+ Critiqs whenever the attribute changes. Both come from your asset pipeline,
78
+ fingerprinted and precompiled like your own assets. Apps with import maps need
79
+ no pin.
80
+
81
+ Critiqs draws above your page in its own shadow root and reads the page only
82
+ through the DOM. To report more of the view than the route, such as an open
83
+ tab, or to show a thread's view again without a page load, set `getContext`
84
+ and `restoreContext` on the element from your JavaScript; the
85
+ [critiqs package](https://github.com/noahsjung/critiqs/tree/main/packages/critiqs)
86
+ describes them and how they combine with the reported route.
87
+
88
+ ## Routes
89
+
90
+ Each page reports the route Rails matched, so every thread on any user's page
91
+ belongs to one screen:
92
+
93
+ | Route | Page | Screen | View state |
94
+ | --- | --- | --- | --- |
95
+ | `get "users/:id"` | `/users/42` | `users/:id` | `{ "route.id": "42" }` |
96
+ | `root "pages#home"` | `/` | `/` | `{}` |
97
+ | `get "docs/*path"` | `/docs/a/b` | `docs/:path*` | `{ "route.path": "a/b" }` |
98
+
99
+ `critiqs_tag(screen: "user-details")` names the screen yourself and keeps the
100
+ route's parameters; `critiqs_tag(screen: false)` reports nothing, so Critiqs
101
+ uses the URL path.
102
+
103
+ An optional parameter, `get "posts(/:page)"`, becomes `posts/:page?`. A route
104
+ with any other optional part, such as `get "posts(/page/:page)"`, reports the
105
+ URL path instead, since no one screen rebuilds both `/posts` and
106
+ `/posts/page/2`. So does a parameter inside a path segment, such as
107
+ `get "release-:version"`, since a screen fills in only whole segments.
108
+
109
+ Opening a thread saved on another page doesn't change the page by itself:
110
+ the sidebar shows the context the thread was saved with (its screen and
111
+ view state). To go to the thread's page, set `restoreContext` on the
112
+ `<critiqs-app>` element from your JavaScript; `routePath` from the critiqs
113
+ package's `critiqs/router` turns the screen and view state back into a path
114
+ such as `/users/42`, or null when it can't, for `Turbo.visit` or
115
+ `location.assign`.
116
+
117
+ ## Turbo and Content Security Policy
118
+
119
+ Critiqs works with Turbo Drive: each visit reports the new page's route, Back
120
+ and Turbo's page cache keep one Critiqs, and so do frame visits. A page
121
+ refresh that Turbo morphs (`turbo_refreshes_with method: :morph`, or a
122
+ broadcast refresh) reports the screen the refreshed page names, such as
123
+ `critiqs_tag(screen: @screen)` with a new `@screen`. A
124
+ `<turbo-frame>` visit that advances the URL (`data-turbo-action="advance"`)
125
+ renders only the frame, so the page keeps reporting the route it was loaded
126
+ with until the next visit that renders the layout.
127
+
128
+ Critiqs renders no inline script. When the app sets a nonce generator
129
+ (`content_security_policy_nonce_generator` with `script-src` in
130
+ `content_security_policy_nonce_directives`), both module scripts carry the
131
+ request's nonce; without one, a `script-src` with `'self'` loads them from
132
+ your asset host. With Turbo, use a nonce that stays the same for
133
+ the session, as Rails's generated initializer suggests, or Turbo visits can't
134
+ run any page scripts. Critiqs's styles are a constructed stylesheet in its
135
+ shadow root, so a `style-src` that allows only nonces, as Rails suggests,
136
+ still shows Critiqs.
137
+
138
+ ## License
139
+
140
+ MIT
@@ -0,0 +1,21 @@
1
+ // critiqs-rails: hands each page's context to Critiqs. critiqs_tag renders
2
+ // it as <critiqs-app data-critiqs-context="…">, so no inline script needs a
3
+ // CSP nonce. A module runs once per document, and pages change without a
4
+ // load: Turbo Drive visits and Back bring a new <critiqs-app>, and a morph
5
+ // refresh changes the attribute in place without running scripts. So this
6
+ // watches the page and reports each element's context whenever it changes.
7
+ const ATTRIBUTE = "data-critiqs-context";
8
+ const elements = document.getElementsByTagName("critiqs-app");
9
+ const reported = new WeakMap();
10
+
11
+ function report() {
12
+ for (const element of elements) {
13
+ const context = element.getAttribute(ATTRIBUTE) ?? "{}";
14
+ if ((reported.get(element) ?? "{}") === context) continue;
15
+ reported.set(element, context);
16
+ customElements.whenDefined("critiqs-app").then(() => element.setContext(JSON.parse(context)));
17
+ }
18
+ }
19
+
20
+ report();
21
+ new MutationObserver(report).observe(document.documentElement, { subtree: true, childList: true, attributes: true, attributeFilter: [ATTRIBUTE] });