janela 0.2.1 → 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.
Files changed (71) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +63 -1
  3. data/LICENSE.txt +1 -1
  4. data/README.md +244 -23
  5. data/UPGRADING.md +172 -0
  6. data/app/assets/javascripts/janela/chart_controller.js +23 -10
  7. data/app/assets/javascripts/janela/frame_controller.js +188 -0
  8. data/app/assets/javascripts/janela/vitral_controller.js +263 -0
  9. data/app/assets/stylesheets/janela.css +164 -0
  10. data/app/assets/stylesheets/vitral.css +343 -0
  11. data/app/controllers/janela/application_controller.rb +25 -0
  12. data/app/controllers/janela/frames_controller.rb +57 -0
  13. data/app/controllers/janela/panes_controller.rb +66 -13
  14. data/app/controllers/janela/queries_controller.rb +17 -0
  15. data/app/controllers/janela/{snapshot_panes_controller.rb → snapshot_queries_controller.rb} +7 -5
  16. data/app/helpers/janela/{dashboard_helper.rb → frames_helper.rb} +26 -9
  17. data/app/models/janela/frame.rb +28 -0
  18. data/app/models/janela/pane.rb +102 -99
  19. data/app/models/janela/query.rb +162 -0
  20. data/app/models/janela/snapshot.rb +5 -5
  21. data/app/views/janela/frames/_card.html.erb +7 -0
  22. data/app/views/janela/frames/_form.html.erb +23 -0
  23. data/app/views/janela/frames/_frame.html.erb +5 -0
  24. data/app/views/janela/frames/_pane.html.erb +12 -0
  25. data/app/views/janela/frames/edit.html.erb +25 -0
  26. data/app/views/janela/frames/index.html.erb +15 -0
  27. data/app/views/janela/frames/new.html.erb +5 -0
  28. data/app/views/janela/frames/show.html.erb +9 -0
  29. data/app/views/janela/panes/_form.html.erb +58 -0
  30. data/app/views/janela/panes/_row.html.erb +12 -0
  31. data/app/views/janela/panes/edit.html.erb +5 -0
  32. data/app/views/janela/panes/new.html.erb +22 -0
  33. data/app/views/janela/panes/show.html.erb +3 -46
  34. data/app/views/janela/queries/_query.html.erb +47 -0
  35. data/app/views/janela/queries/show.html.erb +4 -0
  36. data/app/views/janela/shared/_errors.html.erb +7 -0
  37. data/app/views/layouts/janela/application.html.erb +22 -5
  38. data/config/importmap.rb +2 -1
  39. data/config/locales/en.yml +65 -0
  40. data/config/routes.rb +15 -2
  41. data/db/migrate/20260916000001_create_janela_frames.rb +13 -0
  42. data/db/migrate/20260916000002_create_janela_panes.rb +22 -0
  43. data/docs/decisions/001-built-to-be-forked.md +4 -0
  44. data/docs/decisions/009-snapshots.md +5 -2
  45. data/docs/decisions/010-agent-guidance-ships-the-agent-waits.md +8 -4
  46. data/docs/decisions/012-frames-and-panes-are-data.md +166 -0
  47. data/docs/decisions/013-naming-and-addressing-frames.md +119 -0
  48. data/docs/decisions/014-corrections-before-frames-are-built.md +222 -0
  49. data/docs/decisions/015-how-breaking-change-is-communicated.md +114 -0
  50. data/docs/decisions/016-the-styling-vocabulary.md +100 -0
  51. data/docs/decisions/017-janela-owns-no-data-store.md +113 -0
  52. data/docs/decisions/018-a-table-is-the-universal-renderer.md +75 -0
  53. data/docs/decisions/019-a-created-frame-asks-the-host-who-owns-it.md +79 -0
  54. data/docs/decisions/020-formatting-belongs-to-the-measure.md +93 -0
  55. data/docs/decisions/021-a-check-has-a-name-a-host-can-silence.md +84 -0
  56. data/docs/decisions/022-host-route-helpers-work-inside-the-engine.md +104 -0
  57. data/docs/decisions/023-vitral-is-a-theme-not-the-stylesheet.md +86 -0
  58. data/docs/decisions/024-selecting-more-than-one-value.md +127 -0
  59. data/docs/decisions/INDEX.md +25 -7
  60. data/docs/multi-tenancy.md +175 -0
  61. data/docs/naming.md +172 -0
  62. data/lib/janela/definition.rb +5 -5
  63. data/lib/janela/doctor.rb +219 -0
  64. data/lib/janela/engine.rb +24 -2
  65. data/lib/janela/host_routes.rb +31 -0
  66. data/lib/janela/measure.rb +65 -4
  67. data/lib/janela/version.rb +1 -1
  68. data/lib/janela.rb +24 -0
  69. data/lib/tasks/janela.rake +6 -0
  70. metadata +57 -4
  71. data/app/assets/javascripts/janela/dashboard_controller.js +0 -58
@@ -0,0 +1,104 @@
1
+ ---
2
+ Date: 2026-09-16
3
+ Status: Accepted
4
+ Related: ADR 004, ADR 005, ADR 011, ADR 019
5
+ Triggers:
6
+ - a route helper raising inside Janela's controllers
7
+ - host code inherited from ApplicationController running in the engine
8
+ - authentication redirects
9
+ - anything that would make the engine less isolated
10
+ Topics: host-integration, routing, engine-isolation, authentication
11
+ ---
12
+
13
+ # ADR 022: A Host's Route Helpers Work Inside the Engine
14
+
15
+ ## Context
16
+
17
+ `isolate_namespace Janela` points every route helper evaluated inside
18
+ Janela's controllers at the engine's own route set. That is the right
19
+ default for the engine's own code. It is the wrong answer for the
20
+ host's, and the host's runs there constantly, because
21
+ `Janela::ApplicationController` inherits the host's
22
+ `ApplicationController` on purpose (ADR 004).
23
+
24
+ Three reports, one cause, and each host found it separately:
25
+
26
+ 1. A host's application layout calling `root_path` in its nav, which
27
+ made every pane 500. Solved another way in 0.2.1 by not rendering
28
+ panes in the host's layout (ADR 011).
29
+ 2. A host authenticating per controller, whose `before_action`
30
+ redirected to `new_session_path`.
31
+ 3. A host authenticating globally, whose `Authentication` concern
32
+ redirected to `new_session_path` from inside the engine and raised
33
+ `ActionController::UrlGenerationError`. An unauthenticated visitor
34
+ got a 500 instead of a sign-in page.
35
+
36
+ Case 3 is the one that shows the shape of the problem. The README had
37
+ framed the fix as something to do "if your app authenticates per
38
+ controller", which is not the trigger at all. The trigger is that a
39
+ URL is generated by host code while it is inside the engine. A
40
+ `rescue_from` that redirects, an `after_action`, a flash partial and an
41
+ audit callback all qualify. Each host discovers it on its own, in
42
+ production, as a 500.
43
+
44
+ `main_app.new_session_path` is the documented fix and it works. It
45
+ also asks every host to know that Janela is an isolated engine, and to
46
+ remember it in code that has nothing to do with Janela. That is the
47
+ opposite of what installing this gem is supposed to feel like.
48
+
49
+ The option of including the host's `url_helpers` wholesale was
50
+ considered and rejected. Both sets of helpers would then be present,
51
+ and a name in both would resolve by module order, so `frames_path`
52
+ inside Janela's own view could silently become the host's `/frames`.
53
+ Trading a loud `UrlGenerationError` for a quietly wrong link is a bad
54
+ trade.
55
+
56
+ ## Decision
57
+
58
+ **Janela forwards to the host exactly those route helpers the engine
59
+ does not define, and no others.**
60
+
61
+ The set is computed when routes are loaded, as the host's helper names
62
+ minus the engine's, and each one is defined to call `main_app`. It is
63
+ rebuilt whenever routes reload, so a name that goes away goes away
64
+ here too.
65
+
66
+ ```ruby
67
+ # In a host's ApplicationController, running inside Janela:
68
+ redirect_to new_session_path # works, forwarded to the host
69
+ frames_path # Janela's, never the host's /frames
70
+ main_app.frames_path # the host's, said unambiguously
71
+ ```
72
+
73
+ Two properties matter more than the convenience:
74
+
75
+ **The engine's own routes can never be shadowed.** A name is forwarded
76
+ only when Janela does not define it, so no host route can change what
77
+ one of Janela's own views means. Precedence is decided by
78
+ construction, not by the order modules happened to be included.
79
+
80
+ **`main_app` still means what it means.** Nothing is taken away. A
81
+ host that prefers to be explicit, or that needs a name Janela also
82
+ uses, says `main_app.` and is unambiguous.
83
+
84
+ **This covers named helpers only.** `url_for(record)` and
85
+ `redirect_to @record` resolve polymorphically at call time, so there
86
+ is no name to forward and they still need `main_app.`. That boundary
87
+ is documented rather than papered over.
88
+
89
+ ## Consequences
90
+
91
+ - A host's authentication redirect works inside Janela with no
92
+ configuration, no reopened class and no knowledge that an engine is
93
+ involved. That was the whole complaint in three reports.
94
+ - Janela is slightly less isolated than a textbook engine, in one
95
+ direction only: host names in, never Janela names out. ADR 005's
96
+ mount independence is untouched, since the engine still generates
97
+ every one of its own URLs through its own routes.
98
+ - A host route helper shares a name with one of Janela's and the host
99
+ loses the bare name inside the engine. Janela's route names are few
100
+ and dull, which makes this rare, and `main_app.` remains.
101
+ - The README's explanation was wrong about the cause and is corrected
102
+ here and there.
103
+ - Polymorphic routing is a known gap. If it bites someone the answer
104
+ is another ADR, not a quiet widening of this one.
@@ -0,0 +1,86 @@
1
+ ---
2
+ Date: 2026-09-16
3
+ Status: Accepted
4
+ Related: ADR 011, ADR 016, ADR 021
5
+ Triggers:
6
+ - changing how Janela looks
7
+ - adding a stylesheet, a class or a custom property
8
+ - adding a Stimulus controller to the gem
9
+ - adding a setting
10
+ Topics: styling, host-integration, configuration, naming
11
+ ---
12
+
13
+ # ADR 023: Vitral Is a Theme, Not the Stylesheet
14
+
15
+ ## Context
16
+
17
+ ADR 016 gave Janela one stylesheet, deliberately plain: the grid classes
18
+ an analyst's numbers choose from, and just enough style that a pane is
19
+ legible on install. That was right, and it left an obvious gap. A host
20
+ that wants a dashboard to look like something has to write all of it,
21
+ and the first thing anyone does with a new library is judge how it
22
+ looks.
23
+
24
+ The temptation is to make `janela.css` prettier. That would be a
25
+ mistake. Structure and taste have different lifetimes: the grid classes
26
+ are load bearing and must not change, while taste is the first thing a
27
+ host will want to replace and the first thing we will want to revise.
28
+ Putting both in one file means every visual revision risks the layout,
29
+ and every host that dislikes the look has to fight rules it also needs.
30
+
31
+ ## Decision
32
+
33
+ **A second stylesheet, `vitral.css`, optional and separate.**
34
+
35
+ A *vitral* is a stained glass window. Janela is a window, its parts are
36
+ frames and panes, and this is what the glass looks like. The name
37
+ follows the same rule as the rest: an uncommon but conceivable word, so
38
+ it never collides with the vocabulary a host already uses for its own
39
+ things.
40
+
41
+ ```erb
42
+ <%= stylesheet_link_tag "janela" %>
43
+ <%= stylesheet_link_tag "vitral" %>
44
+ ```
45
+
46
+ Four rules hold it in place.
47
+
48
+ **Nothing is repainted until asked.** The light and the leading hang off
49
+ a `vitral` class. A host that links the stylesheet and adds no class
50
+ gets nothing, which means linking it can never be the thing that broke
51
+ a page.
52
+
53
+ **It is a small library, not a private skin.** `vitral-pane`,
54
+ `vitral-panes` and `vitral-button` are public, so the page around a
55
+ dashboard can be made of the same window. Everything else is a custom
56
+ property, so a host rethemes from its own stylesheet rather than by
57
+ forking this one.
58
+
59
+ **The theme must be complete without JavaScript.** Janela's own pages
60
+ load none at all (ADR 011), so the stained glass is CSS, and the part
61
+ that answers the pointer is a separate optional controller. The
62
+ stylesheet carries a static lattice for everyone who never loads it.
63
+
64
+ **Janela's own pages wear it by name.** `Janela.theme = "vitral"` is the
65
+ third setting this gem has, and it earns that the same way the second
66
+ did: which theme, if any, is a judgement made once about the whole
67
+ application, and that is what configuration is for (ADR 021). A host
68
+ that leaves it unset gets the structural stylesheet, exactly as before.
69
+
70
+ ## Consequences
71
+
72
+ - Janela looks like something out of the box, and a host that hates it
73
+ removes one line rather than overriding a hundred rules.
74
+ - `janela.css` can stay frozen and boring while the theme moves. A
75
+ revision to the look is not a revision to the grid.
76
+ - The lattice is an inline SVG data URI rather than an image, because a
77
+ stylesheet shipped in a gem cannot rely on a host's asset pipeline to
78
+ resolve a `url()`.
79
+ - Accessibility is the theme's problem, not the host's: reduced motion,
80
+ reduced transparency and increased contrast each fall back to a
81
+ still, solid window, and so does a browser without `backdrop-filter`.
82
+ - A visual change to vitral is visible to every host that opted in, so
83
+ it belongs in the changelog like any other change a host can see
84
+ (ADR 015). It is not a breaking change, because nothing depends on it.
85
+ - A third stylesheet is a third thing for a Sprockets host to declare
86
+ for precompilation. The engine declares both itself.
@@ -0,0 +1,127 @@
1
+ ---
2
+ Date: 2026-09-16
3
+ Status: Accepted
4
+ Related: ADR 003, ADR 005, ADR 008, ADR 009, ADR 018
5
+ Triggers:
6
+ - changing what a click on a value does
7
+ - adding a filter predicate or changing the URL grammar
8
+ - adding a keyboard shortcut
9
+ - anything a mouse can do that a keyboard cannot
10
+ Topics: cross-filtering, urls, accessibility, ransack
11
+ ---
12
+
13
+ # ADR 024: Selecting More Than One Value
14
+
15
+ ## Context
16
+
17
+ A click on a value writes one Ransack condition, `q[status_eq]=paid`,
18
+ and clicking the same value again removes it. One value per dimension.
19
+ Every real dashboard eventually needs two: paid and pending, APAC and
20
+ EU. A commercial tool does this with a modifier click, and everyone
21
+ already knows the gesture.
22
+
23
+ The gesture is the easy part. `toggle(event)` already receives the
24
+ event, so `ctrlKey || metaKey` is there for a table button, and
25
+ Chart.js hands its own click handler the native event. What needs
26
+ deciding is what the URL says, what happens to the null group, and
27
+ what someone without a mouse does instead, because a modifier click is
28
+ invisible, absent on touch and unreachable from a keyboard. Deciding
29
+ the gesture without deciding that last part would build a feature only
30
+ some people can use (#35, #36).
31
+
32
+ Three things were measured against the dummy before writing this,
33
+ rather than assumed:
34
+
35
+ - `status_in: [paid, pending]` passes Janela's allowlist guard and
36
+ returns the sum of both. A dimension's allowlist is per attribute, so
37
+ the predicate needs nothing added.
38
+ - `status_eq: paid` still works, so an existing link keeps working
39
+ whatever a new click writes.
40
+ - `channel_null: 1` together with `channel_in: [web]` **returns zero**.
41
+ Ransack ANDs its conditions, so that combination asks for rows whose
42
+ channel is both null and web.
43
+
44
+ That last one is the whole difficulty. It does not raise. It renders
45
+ an empty dashboard, which reads as a bug in Janela rather than as an
46
+ impossible question.
47
+
48
+ ## Decision
49
+
50
+ **A click selects, a modifier click adds, and the selection is a set.**
51
+
52
+ Following the convention every list in every operating system already
53
+ uses, so that nothing has to be learned:
54
+
55
+ | Gesture | Result |
56
+ | --- | --- |
57
+ | Click an unselected value | That value alone is selected |
58
+ | Click the only selected value | The dimension is cleared |
59
+ | Click a value while others are selected | That value replaces them |
60
+ | Ctrl or Cmd click | That value is added or removed, the rest stay |
61
+
62
+ **Janela writes `_in`, and keeps reading `_eq`.** A click always
63
+ produces `q[status_in][]=paid`, one value or five, so there is one
64
+ shape in the controller, one in the view and one in a stored snapshot.
65
+ A hand written or previously shared `_eq` link keeps working, because
66
+ Ransack accepts it and because a URL somebody already sent should not
67
+ stop working to suit us (ADR 005).
68
+
69
+ **The null group is exclusive within its dimension.** Selecting
70
+ `(none)` clears the other values of that dimension, and selecting a
71
+ value clears `(none)`. The combination is unanswerable, and the honest
72
+ options are to refuse it or to render nothing and let it look broken.
73
+
74
+ Ransack can express the OR through its `g[]` grouping, and that is
75
+ rejected. It would make the URL unreadable, and ADR 005's grammar is
76
+ built on a URL a person can read and edit. It would also have to be
77
+ understood by every stored snapshot and every hand written link
78
+ forever, to serve a question almost nobody asks.
79
+
80
+ **The keyboard gets the same two gestures, not a different feature.**
81
+ A value is already a real `<button>` (ADR 018), so Enter is a click
82
+ and **Ctrl or Cmd with Enter is a modifier click**, using the same
83
+ flags on the same event. Nothing new is invented, and nothing has to
84
+ be learned twice.
85
+
86
+ **Janela binds exactly two keys, and only inside the frame.**
87
+
88
+ - `Escape` clears the frame's filters.
89
+ - `Enter` and `Space` act on the focused value, as they already do.
90
+
91
+ No single letter keys. A single letter belongs to the host
92
+ application, to its own shortcuts, and to any text field on the page.
93
+ Janela is a guest in someone else's application and will not take a
94
+ key that could mean something there. A host that wants `c` for clear
95
+ binds its own control to `janela--frame#clear`, which is how the clear
96
+ button already works.
97
+
98
+ **A chart stays a mouse surface, and the table is the accessible one.**
99
+ A bar is painted pixels with nothing focusable behind it. Rather than
100
+ build a parallel focus model inside a canvas, Janela says plainly that
101
+ the table renders the same data and is operable by keyboard, which is
102
+ what ADR 018 made a table the universal renderer for. A chart
103
+ highlights every selected value rather than one.
104
+
105
+ ## Consequences
106
+
107
+ - A dashboard can answer "paid and pending", which is the ordinary
108
+ question this could not previously express.
109
+ - `Query#selected_value` becomes `selected_values` and returns an
110
+ array. A host that overrode a pane view touches it, so this is a
111
+ breaking change, and it goes in UPGRADING.md with the version that
112
+ carries it (ADR 015).
113
+ - A shared link's shape changes from `q[status_eq]=paid` to
114
+ `q[status_in][]=paid`. Older links keep working, so nothing that was
115
+ sent stops working.
116
+ - The page URL and every pane `src` now carry repeated parameters. The
117
+ controller sorts filters so an unchanged `src` is never reloaded, and
118
+ it must sort the values inside a dimension too or a set will
119
+ serialise two ways and refetch every pane for nothing.
120
+ - Touch has no modifier key, so a touch user gets replace-only
121
+ selection. That is a real gap and this ADR does not close it. The
122
+ answer, when someone needs it, is a control the host can render that
123
+ makes the next clicks additive, with the modifier as its accelerator
124
+ rather than the only path. Not built, because the simple thing has
125
+ not yet failed.
126
+ - Snapshots store filters as JSON, so an array needs nothing new, and
127
+ a snapshot taken under `_eq` still reads.
@@ -20,18 +20,23 @@ them when in doubt: `grep -l "Triggers:.*fork" docs/decisions/*.md`.
20
20
 
21
21
  | Topic | ADRs |
22
22
  |-------|------|
23
- | **Vision, scope, forkability** | 001, 010 |
23
+ | **Vision, scope, forkability** | 001, 010, 012 |
24
24
  | **Open-source & host-decoupling** | 001 |
25
25
  | **DSL & query layer** | 002, 006, 007 |
26
- | **Dependencies** | 002, 003, 004, 006 |
27
- | **Authorisation** | 002, 003, 004, 009 |
26
+ | **Dependencies** | 002, 003, 004, 006, 017 |
27
+ | **Authorisation** | 002, 003, 004, 009, 017, 019 |
28
+ | **Performance & storage** | 007, 017 |
28
29
  | **Cross-filtering & Hotwire** | 003, 004, 005, 008 |
29
- | **Layouts & views** | 011 |
30
+ | **Layouts & views** | 011, 012, 016, 018 |
31
+ | **CSS & styling** | 016, 018 |
32
+ | **Frames, panes & persistence** | 012, 013, 014, 019 |
33
+ | **Naming rule** | 014 |
30
34
  | **JavaScript delivery & charts** | 004, 006 |
31
35
  | **Time dimensions** | 006 |
32
- | **Routes, URLs & naming** | 005, 007, 008, 009, 011 |
36
+ | **Routes, URLs & naming** | 005, 007, 008, 009, 011, 013 |
33
37
  | **Snapshots & publishing** | 009 |
34
- | **AI agents & guidance** | 010 |
38
+ | **AI agents & guidance** | 010, 015 |
39
+ | **Releases & upgrades** | 015 |
35
40
  | **Security** | 003 |
36
41
  | **Testing** | 003 |
37
42
 
@@ -50,7 +55,20 @@ them when in doubt: `grep -l "Triggers:.*fork" docs/decisions/*.md`.
50
55
  | 009 | Snapshots | 2026-09-15 | Accepted |
51
56
  | 010 | Agent Guidance Ships, the Agent Waits | 2026-09-15 | Accepted |
52
57
  | 011 | Panes Do Not Render in the Host Layout | 2026-09-15 | Accepted |
58
+ | 012 | Frames and Panes Are Data | 2026-09-15 | Accepted |
59
+ | 013 | Naming and Addressing Frames | 2026-09-15 | Accepted |
60
+ | 014 | Corrections Before Frames Are Built | 2026-09-15 | Accepted |
61
+ | 015 | How Breaking Change Is Communicated | 2026-09-16 | Accepted |
62
+ | 016 | The Styling Vocabulary | 2026-09-16 | Accepted |
63
+ | 017 | Janela Owns No Data Store | 2026-09-16 | Accepted |
64
+ | 018 | A Table Is the Universal Renderer | 2026-09-16 | Accepted |
65
+ | 019 | A Created Frame Asks the Host Who Owns It | 2026-09-16 | Accepted |
66
+ | 020 | Formatting Belongs to the Measure | 2026-09-16 | Accepted |
67
+ | 021 | A Check Has a Name, and a Host Can Silence It | 2026-09-16 | Accepted |
68
+ | 022 | A Host's Route Helpers Work Inside the Engine | 2026-09-16 | Accepted |
69
+ | 023 | Vitral Is a Theme, Not the Stylesheet | 2026-09-16 | Accepted |
70
+ | 024 | Selecting More Than One Value | 2026-09-16 | Accepted |
53
71
 
54
72
  ## Next number
55
73
 
56
- Next ADR: 012
74
+ Next ADR: 025
@@ -0,0 +1,175 @@
1
+ # Fitting Janela into a multi tenant application
2
+
3
+ Janela holds no tenancy of its own. There is no tenant setting, no
4
+ default scope of ours on your models, and nothing to configure. The
5
+ engine asks your application two questions and does what it is told.
6
+
7
+ | Question | How your application answers | If it says nothing |
8
+ | --- | --- | --- |
9
+ | What may this request read? | `policy_scope(model)` on the controller Janela inherits | Everything: `model.all` |
10
+ | What owns a frame being created? | `janela_frame_owner` on the same controller | Nothing: a nil owner |
11
+
12
+ Both are ordinary methods on your `ApplicationController`, found by
13
+ duck typing. Pundit defines the first for you. Anything else, you
14
+ define in about five lines. ADR 019 has the reasoning for asking
15
+ rather than being configured: ownership is per-request state, and a
16
+ setting cannot hold it.
17
+
18
+ ## What goes through your scope
19
+
20
+ Everything. There is no path through the engine that reads a record
21
+ without asking first:
22
+
23
+ - The index of frames, and each frame page.
24
+ - Every editing action on a frame or a pane, so another tenant's frame
25
+ is a 404 to rename or delete as much as to read.
26
+ - Every pane's own query, on Janela's pages and inline in yours. The
27
+ measure is calculated over `policy_scope(Order)`, not over `Order`.
28
+ - An ad hoc pane URL, which is the same query by another route.
29
+ - A snapshot, read through `policy_scope(Janela::Snapshot)`.
30
+
31
+ A pane rendered inline in your own page runs in your request, so the
32
+ same scope applies there as in the engine's controllers. Nothing is
33
+ calculated in a background context where the current tenant would have
34
+ gone missing.
35
+
36
+ ## With Pundit
37
+
38
+ Write a policy for Janela's own record. The class name is namespaced,
39
+ because the model is:
40
+
41
+ ```ruby
42
+ # app/policies/janela/frame_policy.rb
43
+ module Janela
44
+ class FramePolicy < ApplicationPolicy
45
+ class Scope < ApplicationPolicy::Scope
46
+ def resolve
47
+ scope.where(owner: Current.account)
48
+ end
49
+ end
50
+ end
51
+ end
52
+ ```
53
+
54
+ ```ruby
55
+ class ApplicationController < ActionController::Base
56
+ include Pundit::Authorization
57
+
58
+ private
59
+ def janela_frame_owner
60
+ Current.account
61
+ end
62
+ end
63
+ ```
64
+
65
+ Your own models keep the policies they already have. Janela calls
66
+ `policy_scope(Order)` and gets whatever `OrderPolicy::Scope` returns.
67
+
68
+ ## With acts_as_tenant
69
+
70
+ Your own models are already scoped, so the only thing Janela needs
71
+ told about is its own tables. Define `policy_scope` as the adapter:
72
+
73
+ ```ruby
74
+ class ApplicationController < ActionController::Base
75
+ set_current_tenant_through_filter
76
+ before_action :set_tenant
77
+
78
+ private
79
+ def policy_scope(model)
80
+ case model.name
81
+ when "Janela::Frame" then model.where(owner: ActsAsTenant.current_tenant)
82
+ else model.all # acts_as_tenant has already scoped your own models
83
+ end
84
+ end
85
+
86
+ def janela_frame_owner
87
+ ActsAsTenant.current_tenant
88
+ end
89
+ end
90
+ ```
91
+
92
+ ## With CanCanCan, or with your own
93
+
94
+ The same adapter, pointed at whatever you use:
95
+
96
+ ```ruby
97
+ def policy_scope(model)
98
+ model.accessible_by(current_ability)
99
+ end
100
+ ```
101
+
102
+ Janela never asks how the answer was arrived at. A method that takes a
103
+ class and returns a relation is the whole contract.
104
+
105
+ ## With one tenant
106
+
107
+ Define nothing. `policy_scope` is absent, every query runs over
108
+ `model.all`, and a frame is created with a nil owner because nothing
109
+ is filtering on one.
110
+
111
+ ## What owns a frame
112
+
113
+ `Janela::Frame belongs_to :owner, polymorphic: true, optional: true`.
114
+ It can point at an account, a team, a user or anything else you scope
115
+ by, and Janela reads nothing from it. It exists so your scope has a
116
+ column.
117
+
118
+ Return the tenant from `janela_frame_owner` and the engine assigns it
119
+ to a frame it creates. Skip that method while your scope filters by
120
+ owner and the analyst's new dashboard is saved with no owner, then
121
+ hidden by your own policy the instant it is saved. It looks like the
122
+ save failed silently. `bin/rails janela:doctor` reports this for you:
123
+ it asks your policy for a scope over frames and looks for an owner in
124
+ what comes back.
125
+
126
+ ## Snapshots are the rough edge
127
+
128
+ `janela_snapshots` has no owner column. A snapshot is a name, an
129
+ instant, the filters it was taken under and the results. So a multi
130
+ tenant application has to scope it by something else:
131
+
132
+ - Keep snapshots to one tenant, or to whoever publishes.
133
+ - Add your own column with a migration on `janela_snapshots` and scope
134
+ on that.
135
+ - Encode the tenant in the filters a snapshot is taken under, and
136
+ scope on the stored name.
137
+
138
+ None of those is as clean as a frame's owner. Giving a snapshot the
139
+ same polymorphic owner is [issue #32](https://github.com/retail-tasker/janela/issues/32).
140
+
141
+ ## Proving your wiring
142
+
143
+ Run the doctor first. It reads your application and reports the traps
144
+ that are visible from outside a request:
145
+
146
+ ```bash
147
+ bin/rails janela:doctor
148
+ ```
149
+
150
+ Then write the test that matters, which is the one that fails if a
151
+ scope is ever loosened. Create a frame as one tenant and ask for it as
152
+ another:
153
+
154
+ ```ruby
155
+ test "another tenant's frame is a 404" do
156
+ frame = Janela::Frame.create!(name: "Theirs", owner: accounts(:acme))
157
+
158
+ sign_in users(:globex_analyst)
159
+ get janela.frame_path(frame)
160
+
161
+ assert_response :not_found
162
+ end
163
+ ```
164
+
165
+ This repository's dummy application is a worked example of all of the
166
+ above, in
167
+ [test/dummy/app/controllers/application_controller.rb](https://github.com/retail-tasker/janela/blob/main/test/dummy/app/controllers/application_controller.rb),
168
+ scoped by a `?tenant=` parameter standing in for a session.
169
+
170
+ ## What Janela will never do
171
+
172
+ Add a tenant filter of its own. Your scope is the only one, so there
173
+ is nothing to double filter and nothing that looks enforced while
174
+ being unenforceable. Janela can be handed any relation through `on:`,
175
+ so a guarantee made here would be a guarantee it cannot keep.