qube_rails 0.2.1 → 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.
Files changed (45) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +105 -0
  3. data/README.md +213 -56
  4. data/app/assets/javascripts/qube_rails/question.js +80 -41
  5. data/app/assets/stylesheets/qube_rails.css +143 -0
  6. data/app/controllers/qube_rails/connections_controller.rb +19 -3
  7. data/app/controllers/qube_rails/storyboard_controller.rb +1 -1
  8. data/app/models/qube_rails/connection.rb +110 -8
  9. data/app/models/qube_rails/quickbooks_record.rb +22 -0
  10. data/app/views/qube_rails/_manage_connection.html.erb +19 -0
  11. data/app/views/qube_rails/question/_choice.html.erb +2 -4
  12. data/app/views/qube_rails/question/_disclosure.html.erb +7 -0
  13. data/app/views/qube_rails/question/_footer.html.erb +2 -8
  14. data/app/views/qube_rails/question/_inline_search.html.erb +12 -0
  15. data/app/views/qube_rails/question/_pick.html.erb +25 -7
  16. data/app/views/qube_rails/question/_picks.html.erb +2 -3
  17. data/app/views/qube_rails/question/_question.html.erb +22 -13
  18. data/app/views/qube_rails/question/fields/_search.html.erb +3 -2
  19. data/app/views/qube_rails/storyboard/show.html.erb +4 -3
  20. data/config/locales/en.yml +7 -0
  21. data/config/routes.rb +2 -0
  22. data/db/migrate/20261124000000_add_qube_company_file_change.rb +17 -0
  23. data/lib/generators/qube_rails/templates/connection_events.rb.tt +17 -1
  24. data/lib/qube_rails/configuration.rb +2 -0
  25. data/lib/qube_rails/connection_events.rb +67 -7
  26. data/lib/qube_rails/engine.rb +1 -0
  27. data/lib/qube_rails/link_check.rb +274 -0
  28. data/lib/qube_rails/operation.rb +1 -0
  29. data/lib/qube_rails/question_view.rb +170 -118
  30. data/lib/qube_rails/request.rb +2 -0
  31. data/lib/qube_rails/storyboard/ask_about_add.json +1 -1
  32. data/lib/qube_rails/storyboard/choose.json +2 -2
  33. data/lib/qube_rails/storyboard/enum_select.json +1 -1
  34. data/lib/qube_rails/storyboard/missing_record.json +1 -1
  35. data/lib/qube_rails/storyboard/name_clash.json +41 -3
  36. data/lib/qube_rails/storyboard/near_match.json +8 -3
  37. data/lib/qube_rails/storyboard/number_boolean.json +1 -1
  38. data/lib/qube_rails/storyboard/record_search.json +2 -2
  39. data/lib/qube_rails/storyboard/refusal.json +7 -2
  40. data/lib/qube_rails/storyboard/rows.json +35 -18
  41. data/lib/qube_rails/storyboard.rb +21 -5
  42. data/lib/qube_rails/sync_status.rb +12 -1
  43. data/lib/qube_rails/version.rb +1 -1
  44. data/lib/qube_rails.rb +9 -0
  45. metadata +6 -1
@@ -1,5 +1,5 @@
1
- // qube_rails: the behaviour of a QuickBooks question (search boxes, a chain's branches, a rows field and
2
- // its steps) and the storyboard's copy buttons. Listeners sit on document and key only on data-qube-* attributes, so
1
+ // qube_rails: the behaviour of a QuickBooks question (search boxes, a picked match's row, a form behind
2
+ // "or …", a rows field and its steps) and the storyboard's copy buttons. Listeners sit on document and key only on data-qube-* attributes, so
3
3
  // any markup carrying them works, after Turbo navigation too. Plain JavaScript, no build. README,
4
4
  // "Styling", has the attribute contract.
5
5
  (function () {
@@ -29,9 +29,25 @@
29
29
  };
30
30
  };
31
31
 
32
- // Until a match is picked, the form won't send what was typed.
32
+ // A search box under offered records: its search's choice has a row among them for its pick
33
+ // (a pick form of role search, QuestionView#picks).
34
+ var slotOf = function (box) {
35
+ var form = box.closest('form[data-qube-choice-role="search"]');
36
+ var question = form && form.closest("[data-qube-question]");
37
+ if (!question || box.closest("[data-qube-rows]")) return null;
38
+ var id = form.getAttribute("data-qube-choice");
39
+ return Array.prototype.find.call(question.querySelectorAll('form[data-qube-choice-kind="pick"][data-qube-choice-role="search"]'),
40
+ function (row) { return row.getAttribute("data-qube-choice") === id; }) || null;
41
+ };
42
+
43
+ var inline = function (box) {
44
+ return !!slotOf(box);
45
+ };
46
+
47
+ // Until a match is picked, the form won't send what was typed (a box under offered records
48
+ // sends nothing itself, so it never says so).
33
49
  var check = function (box, p) {
34
- var unpicked = p.input.value && !p.value.value;
50
+ var unpicked = p.input.value && !p.value.value && !inline(box);
35
51
  p.input.setCustomValidity(unpicked ? words(box, "pick-a-match", "Pick one of the matches.") : "");
36
52
  };
37
53
 
@@ -175,22 +191,59 @@
175
191
  .then(function (matches) {
176
192
  if (asked.get(box) !== n) return; // a later search, or a close, since
177
193
  clearTimeout(slow.get(box));
194
+ if (document.activeElement !== p.input) return close(box); // only the box in use opens
178
195
  if (matches && matches.changed) {
179
196
  return note(box, p, words(box, "search-changed", "This question has changed. Reload the page to see the latest."), "changed");
180
197
  }
181
198
  showMatches(box, p, matches);
182
- if (matches.length || q.length >= 2) declineFirst(box, !matches.length);
199
+ if (matches.length || q.length >= 2) declineFirst(box, !matches.length && !pickedRow(box));
183
200
  })
184
201
  .catch(function () {
185
202
  if (asked.get(box) !== n) return;
186
203
  clearTimeout(slow.get(box));
204
+ if (document.activeElement !== p.input) return close(box);
187
205
  note(box, p, words(box, "search-failed", "Search isn't available right now."), "failed");
188
206
  });
189
207
  }, q ? 250 : 0));
190
208
  };
191
209
 
210
+ var pickedRow = function (box) {
211
+ var row = slotOf(box);
212
+ return row && !row.hidden ? row : null;
213
+ };
214
+
215
+ // A pick in a box under offered records answers nothing: the match shows in its row (its name,
216
+ // its detail, "Use this one", focused), replacing an earlier one, and the box empties.
217
+ var pickInline = function (box, button) {
218
+ var p = parts(box);
219
+ var row = slotOf(box);
220
+ close(box);
221
+ p.input.value = "";
222
+ p.value.value = "";
223
+ check(box, p);
224
+ if (!row) return;
225
+ var value = row.querySelector('input[type="hidden"][name^="data["]');
226
+ if (value) value.value = button.getAttribute("data-value");
227
+ // The row's text: its name, then its detail (the slot renders both, empty).
228
+ var submit = row.querySelector('button[type="submit"]');
229
+ var text = submit && submit.parentElement.firstElementChild; // the row's text, beside its button
230
+ if (!text || !text.children[0]) return;
231
+ text.children[0].textContent = button.getAttribute("data-label");
232
+ var detail = text.children[1];
233
+ var source = button.querySelector("[data-qube-search-detail]");
234
+ if (detail) {
235
+ detail.textContent = "";
236
+ if (source) each(source.childNodes, function (node) { detail.appendChild(node.cloneNode(true)); });
237
+ conceal(detail, !source);
238
+ }
239
+ conceal(row, false);
240
+ submit.focus();
241
+ declineFirst(box, false);
242
+ };
243
+
192
244
  var pick = function (button) {
193
245
  var box = button.closest("[data-qube-search]");
246
+ if (inline(box)) return pickInline(box, button);
194
247
  var p = parts(box);
195
248
  p.value.value = button.getAttribute("data-value");
196
249
  p.input.value = button.getAttribute("data-label");
@@ -233,9 +286,11 @@
233
286
  var primary = question.getAttribute("data-qube-button-class") || "";
234
287
  var secondary = question.getAttribute("data-qube-button-secondary-class") || "";
235
288
  var from = on ? "secondary" : "primary";
236
- each(question.querySelectorAll('button[data-qube-choice-role="decline"], form[data-qube-choice-role="decline"] button[type="submit"]'), function (button) {
289
+ // A decline's button, its exit button in another row, or its option row's action.
290
+ each(question.querySelectorAll('button[data-qube-choice-role="decline"], form[data-qube-choice-role="decline"] button[type="submit"], form[data-qube-choice-role="decline"] [data-qube-offered]'), function (button) {
237
291
  if (button.getAttribute("data-qube-button") !== from) return;
238
292
  swap(button, on ? secondary : primary, on ? primary : secondary, on ? "primary" : "secondary");
293
+ if (button.hasAttribute("data-qube-offered")) button.setAttribute("data-qube-offered", on ? "primary" : "secondary");
239
294
  });
240
295
  };
241
296
 
@@ -451,35 +506,6 @@
451
506
  offers(list);
452
507
  stepUp(list);
453
508
  });
454
- // A chain shows the branch the page has on show, with its buttons.
455
- each(document.querySelectorAll("[data-qube-question]"), function (question) {
456
- if (!question.querySelector("[data-qube-branch]")) return;
457
- var offered = question.querySelector('[data-qube-branch-part="offered"]');
458
- showBranch(question, offered && offered.hidden ? "search" : "offered", false);
459
- });
460
- };
461
-
462
- // ---- A chain: offered records with a search, a branch at a time. What belongs to each carries
463
- // data-qube-branch-part ("offered" or "search"); a [data-qube-branch="<branch>"] button moves to
464
- // one. "Not this one" sits with the offered records; the decline shows with the search only.
465
- var showBranch = function (question, branch, focus) {
466
- each(question.querySelectorAll("[data-qube-branch-part]"), function (part) {
467
- conceal(part, part.getAttribute("data-qube-branch-part") !== branch);
468
- });
469
- each(question.querySelectorAll('[data-qube-branch="search"]:not([data-qube-branch-part])'), function (button) {
470
- conceal(button, branch !== "offered");
471
- });
472
- each(question.querySelectorAll('form[data-qube-choice-role="decline"]'), function (form) {
473
- conceal(form, branch === "offered");
474
- });
475
- if (!focus) return;
476
- // A move the person made: the stylesheet brings the branch in from its side.
477
- question.setAttribute("data-qube-branch-moved", branch);
478
- // The search box browses as it's focused; Back lands on the first offered record.
479
- var target = branch === "search"
480
- ? question.querySelector('[data-qube-branch-part="search"] [data-qube-search-input]')
481
- : question.querySelector('[data-qube-branch-part="offered"] [data-qube-choice-kind="pick"] button');
482
- if (target) target.focus();
483
509
  };
484
510
 
485
511
  // ---- Steps: a rows field with more than one row shows one row at a time, in order. The script
@@ -766,13 +792,22 @@
766
792
  search(box);
767
793
  });
768
794
 
795
+ // Closed as the focus leaves, open or not yet: a list still on its way (the focus's browse)
796
+ // never opens for a box no longer in use.
769
797
  document.addEventListener("focusout", function (event) {
770
798
  var box = event.target.closest && event.target.closest("[data-qube-search]");
771
- if (box && box.hasAttribute("data-qube-open") && !(event.relatedTarget && box.contains(event.relatedTarget))) {
772
- close(box);
773
- }
799
+ if (box && !(event.relatedTarget && box.contains(event.relatedTarget))) close(box);
774
800
  });
775
801
 
802
+ // A form behind "or <its label>…": opening it puts the focus in its first field. (toggle
803
+ // doesn't bubble, so it's caught on the way down.)
804
+ document.addEventListener("toggle", function (event) {
805
+ var details = event.target;
806
+ if (!details.matches || !details.matches("details[data-qube-disclosure]") || !details.open) return;
807
+ var input = details.querySelector("input:not([type=hidden]), select, textarea");
808
+ if (input) input.focus();
809
+ }, true);
810
+
776
811
  document.addEventListener("click", function (event) {
777
812
  if (!event.target.closest) return;
778
813
  each(document.querySelectorAll("[data-qube-search][data-qube-open]"), function (box) {
@@ -780,9 +815,6 @@
780
815
  });
781
816
  var result = event.target.closest("[data-qube-search-result]");
782
817
  if (result) return pick(result);
783
- var branch = event.target.closest("[data-qube-branch]");
784
- var question = branch && branch.closest("[data-qube-question]");
785
- if (question) return showBranch(question, branch.getAttribute("data-qube-branch"), true);
786
818
  var setAllButton = event.target.closest("[data-qube-set-all]");
787
819
  if (setAllButton) {
788
820
  return setAll(setAllButton);
@@ -825,6 +857,13 @@
825
857
  }, 0);
826
858
  }, true);
827
859
 
860
+ // A box under offered records sends nothing itself (Enter with no match highlighted): its pick's
861
+ // row answers.
862
+ document.addEventListener("submit", function (event) {
863
+ var box = event.target.querySelector && event.target.querySelector("[data-qube-search]");
864
+ if (box && inline(box) && box.closest("form") === event.target) event.preventDefault();
865
+ }, true);
866
+
828
867
  // Enter in a step before the last: the next step, not the form's submit.
829
868
  document.addEventListener("submit", function (event) {
830
869
  var list = event.target.querySelector && event.target.querySelector("[data-qube-rows][data-qube-stepped]");
@@ -1407,3 +1407,146 @@
1407
1407
  display: none;
1408
1408
  }
1409
1409
  }
1410
+
1411
+ /* Ahead of the engine (priv/ahead/qube_sync.css, CHANGELOG): a form behind "or <its label>…"
1412
+ under a record that is the main action (a name clash's "Add it under another name"), a row
1413
+ about one record (title, lines, facts), option rows on a fill rather than a border, a
1414
+ decline beside offered records with their weight, and a search box inline under them */
1415
+ @layer components {
1416
+ :where(.qube-choice-disclosure) {
1417
+ display: grid;
1418
+ gap: 12px;
1419
+ }
1420
+
1421
+ :where(.qube-choice-disclosure > summary) {
1422
+ justify-self: start;
1423
+ list-style: none;
1424
+ }
1425
+
1426
+ :where(.qube-choice-disclosure > summary)::-webkit-details-marker {
1427
+ display: none;
1428
+ }
1429
+
1430
+ /* The form drops in under "or <its label>…" as it opens; closing is instant */
1431
+ :where(.qube-choice-disclosure[open] > .qube-choice) {
1432
+ animation: qube-disclosure-open 200ms var(--qube-ease-out);
1433
+ }
1434
+
1435
+ @keyframes qube-disclosure-open {
1436
+ from {
1437
+ opacity: 0;
1438
+ transform: translateY(-4px);
1439
+ }
1440
+ }
1441
+
1442
+ /* Open, its submit is the main action: the record's "Use this one" steps back to secondary */
1443
+ :where(.qube-choices:has(> .qube-choice-disclosure[open]) .qube-pick-action[data-qube-button="primary"]) {
1444
+ color: inherit;
1445
+ background: var(--qube-solid-color);
1446
+ border-color: var(--qube-control-border-color);
1447
+ }
1448
+
1449
+ @media (hover: hover) and (pointer: fine) {
1450
+ :where(.qube-choices:has(> .qube-choice-disclosure[open]) .qube-pick-action[data-qube-button="primary"]:hover) {
1451
+ background: color-mix(in srgb, currentColor 6%, var(--qube-solid-color));
1452
+ }
1453
+ }
1454
+
1455
+ /* An option row sits on a quiet fill, not in a box of its own inside the card */
1456
+ :where(.qube-pick) {
1457
+ background: color-mix(in srgb, currentColor 5%, var(--qube-solid-color));
1458
+ border-color: transparent;
1459
+ }
1460
+
1461
+ @media (hover: hover) and (pointer: fine) {
1462
+ :where(.qube-pick:hover) {
1463
+ background: color-mix(in srgb, currentColor 9%, var(--qube-solid-color));
1464
+ border-color: transparent;
1465
+ }
1466
+ }
1467
+
1468
+ /* An offered record: only its button is a target, so the row stays still under the pointer */
1469
+ :where(div.qube-pick) {
1470
+ cursor: auto;
1471
+ user-select: auto;
1472
+ }
1473
+
1474
+ :where(div.qube-pick:active) {
1475
+ transform: none;
1476
+ }
1477
+
1478
+ @media (hover: hover) and (pointer: fine) {
1479
+ :where(div.qube-pick:hover) {
1480
+ background: color-mix(in srgb, currentColor 5%, var(--qube-solid-color));
1481
+ }
1482
+ }
1483
+
1484
+ /* A row about one record: its name, then its lines, then its facts, each on a line of its own */
1485
+ :where(.qube-pick-line, .qube-pick-facts) {
1486
+ flex-basis: 100%;
1487
+ }
1488
+
1489
+ :where(.qube-pick-facts) {
1490
+ display: flex;
1491
+ flex-wrap: wrap;
1492
+ gap: 2px 16px;
1493
+ font-variant-numeric: tabular-nums;
1494
+ }
1495
+
1496
+ :where(.qube-pick-fact) {
1497
+ white-space: nowrap;
1498
+ }
1499
+
1500
+ :where(.qube-pick-fact-label) {
1501
+ color: var(--qube-muted-color);
1502
+ }
1503
+
1504
+ /* A decline beside offered records weighs the same as their buttons: just under them, at the
1505
+ same right edge, not in the footer by cancel */
1506
+ :where(.qube-choice-alongside) {
1507
+ display: flex;
1508
+ flex-wrap: wrap;
1509
+ justify-content: flex-end;
1510
+ gap: 8px;
1511
+ }
1512
+
1513
+ :where(.qube-choice-alongside > .qube-choice) {
1514
+ justify-items: end;
1515
+ }
1516
+
1517
+ :where(.qube-choice-alongside) + :where(.qube-choice-footer) {
1518
+ padding-top: 16px;
1519
+ }
1520
+
1521
+ @container qube-choices (max-width: 30rem) {
1522
+ :where(.qube-choice-alongside > .qube-choice) {
1523
+ flex-grow: 1;
1524
+ justify-items: stretch;
1525
+ }
1526
+
1527
+ :where(.qube-choice-alongside .qube-choice-button) {
1528
+ justify-content: center;
1529
+ }
1530
+ }
1531
+
1532
+ /* A search beside offered records is its box alone, under them; its label is for screen
1533
+ readers (the box says "Search QuickBooks…") */
1534
+ :where(.qube-search-inline) :where(.qube-search, .qube-search-input) {
1535
+ max-width: none;
1536
+ }
1537
+
1538
+ :where(.qube-search-inline .qube-label) {
1539
+ position: absolute;
1540
+ width: 1px;
1541
+ height: 1px;
1542
+ overflow: hidden;
1543
+ clip-path: inset(50%);
1544
+ white-space: nowrap;
1545
+ }
1546
+
1547
+ @media (prefers-reduced-motion: reduce) {
1548
+ :where(.qube-choice-disclosure[open] > .qube-choice) {
1549
+ animation-name: qube-fade-in;
1550
+ }
1551
+ }
1552
+ }
@@ -56,6 +56,20 @@ module QubeRails
56
56
  send_to_setup(connection)
57
57
  end
58
58
 
59
+ # POST /quickbooks/connections/:id/company_file: sends the customer to switch one of the
60
+ # signed-in account's connections to another company file (its links.company_file, renewed
61
+ # as onboarding's is). QuBe Sync then sends connection.company_file_changed. A simulated
62
+ # connection, or one QuBe Sync never answered, answers 404, as another account's does.
63
+ def company_file
64
+ account = signed_in_account
65
+ return head(:unauthorized) unless account
66
+
67
+ connection = QubeRails::Connection.owned_by(account).find_by(id: params[:id])
68
+ return head(:not_found) if connection.nil? || connection.qube_id.to_s.empty? || connection.simulated?
69
+
70
+ send_to_setup(connection, link: "company_file")
71
+ end
72
+
59
73
  # What the page says when it can't, as I18n keys under qube_rails (config/locales/en.yml).
60
74
  RUNS_GOING = :"errors.still_syncing"
61
75
  NO_ANSWER = :"errors.disconnect_unreachable"
@@ -138,8 +152,9 @@ module QubeRails
138
152
  name.empty? ? nil : name[0, 255]
139
153
  end
140
154
 
141
- # fresh: a row this request wrote, which no earlier create can have been made for.
142
- def send_to_setup(connection, fresh: false)
155
+ # fresh: a row this request wrote, which no earlier create can have been made for. link: the
156
+ # connection's link to send them to, else its onboarding (a page QuBe Sync has no link for).
157
+ def send_to_setup(connection, fresh: false, link: "onboarding")
143
158
  # An earlier create that landed before its id was saved: that connection, not a second one.
144
159
  connection.adopt_qube_connection if !fresh && connection.qube_id.to_s.empty?
145
160
  if connection.qube_id
@@ -152,7 +167,8 @@ module QubeRails
152
167
  end
153
168
 
154
169
  # 303, so the browser follows the POST with a GET.
155
- redirect_to made.dig("links", "onboarding"), allow_other_host: true, status: :see_other
170
+ target = made.dig("links", link) || made.dig("links", "onboarding")
171
+ redirect_to target, allow_other_host: true, status: :see_other
156
172
  end
157
173
  end
158
174
  end
@@ -28,7 +28,7 @@ module QubeRails
28
28
  end
29
29
 
30
30
  def search
31
- render json: Storyboard.matches(@screen, params[:field], params[:q])
31
+ render json: Storyboard.matches(@screen, params[:field], params[:q], params[:row].presence)
32
32
  end
33
33
 
34
34
  def answer
@@ -12,16 +12,20 @@ module QubeRails
12
12
  # qube_id is QuBe Sync's id for it, nil until QuBe Sync answers the create. status is
13
13
  # "pending" until the setup ends, then "connected", "setup_cancelled" or "stalled" (with
14
14
  # stall_reason), kept up to date by its own webhooks (QubeRails::ConnectionEvents). revision
15
- # is the last of those applied. connection_type is "realtime", "standard" or "simulated": as
15
+ # is the last of those applied. A switch to another company file (connection.company_file_changed)
16
+ # sets it back to "pending", marks its QuickBooks links stale and sets company_file_changed_at
17
+ # and company_file_revision (a late one forgets the file without the status).
18
+ # connection_type is "realtime", "standard" or "simulated": as
16
19
  # onboarding made it, then as its events (or refresh_type!) say. Workflows don't run on standard.
17
20
  #
18
21
  # The QuickBooks ids your app learns are kept against it, by list family (link, qb_id_for,
19
- # find_linked), since a ListID is unique only within one list family of one company file.
22
+ # find_linked), since a ListID is unique only within one list family of one company file. After a
23
+ # company file change they're stale (unused) until QubeRails::LinkCheck checks them.
20
24
  class Connection < ApplicationRecord
21
25
  self.table_name = "qube_connections"
22
26
 
23
- # The ones that have never connected, newest first: what Connect sends back through setup
24
- # rather than making another.
27
+ # The ones not connected yet (never set up, set up again after a company file change, or setup
28
+ # cancelled), newest first: what Connect sends back through setup rather than making another.
25
29
  scope :never_connected, -> { where(status: %w[pending setup_cancelled]).order(created_at: :desc, id: :desc) }
26
30
 
27
31
  STATUSES = %w[pending connected setup_cancelled stalled].freeze
@@ -68,6 +72,21 @@ module QubeRails
68
72
  status == "connected"
69
73
  end
70
74
 
75
+ # Set up, and not being set up again: connected, or stalled since.
76
+ def set_up?
77
+ %w[connected stalled].include?(status)
78
+ end
79
+
80
+ def simulated?
81
+ connection_type == "simulated"
82
+ end
83
+
84
+ # Whether to offer "Use a different company file": set up, and not simulated (a simulated
85
+ # connection has no file to switch).
86
+ def company_file_switchable?
87
+ set_up? && !qube_id.to_s.empty? && !simulated?
88
+ end
89
+
71
90
  # Workflows don't run on it: Operation.start raises RealtimeRequired.
72
91
  def standard?
73
92
  connection_type == "standard"
@@ -160,6 +179,11 @@ module QubeRails
160
179
  row.assign_attributes(kind: kind, qb_id: qb_id, record_type: type, record_id: id)
161
180
  row.edit_sequence = edit_sequence unless edit_sequence.nil?
162
181
  row.name = name unless name.nil?
182
+ # Learned on the file it opens now: no longer the old file's.
183
+ row.stale_since = nil
184
+ row.missing_since = nil
185
+ row.review_since = nil
186
+ row.review_name = nil
163
187
  row.save!
164
188
  row
165
189
  end
@@ -180,7 +204,7 @@ module QubeRails
180
204
  # kind.
181
205
  def qb_id_for(record, kind:)
182
206
  type, id = self.class.key_for(record)
183
- mine = quickbooks_records.where(record_type: type, record_id: id)
207
+ mine = quickbooks_records.current.where(record_type: type, record_id: id)
184
208
  exact = mine.where(kind: kind.to_s).limit(1).pluck(:qb_id).first
185
209
  family = QuickbooksRecord.family_for(kind)
186
210
  return exact if exact || family == "name"
@@ -189,6 +213,72 @@ module QubeRails
189
213
  row && row.qb_id
190
214
  end
191
215
 
216
+ # Its links still to be checked against the company file it opens now.
217
+ def stale_links
218
+ quickbooks_records.stale
219
+ end
220
+
221
+ # Its links the company file it opens now doesn't have: unused, checked again on the next change.
222
+ def missing_links
223
+ quickbooks_records.missing
224
+ end
225
+
226
+ # Its links the new company file has under another name: unused until confirm_link or reject_link.
227
+ def review_links
228
+ quickbooks_records.under_review
229
+ end
230
+
231
+ # While record's link here waits for its check, nothing is sent for it (Operation.start and
232
+ # Request.queue raise LinkBeingChecked), so a record already in QuickBooks isn't added again.
233
+ def checking_link?(record)
234
+ own_links(record).stale.exists?
235
+ end
236
+
237
+ # While record's link here waits for your decision (review_links), nothing is sent for it either
238
+ # (LinkNeedsReview).
239
+ def link_under_review?(record)
240
+ own_links(record).under_review.exists?
241
+ end
242
+
243
+ # A held record first makes sure its links' check is going (LinkCheck.revive), in case the last
244
+ # one failed or was lost.
245
+ def wait_for_link_check!(record, what)
246
+ stale_kinds = own_links(record).stale.distinct.pluck(:kind)
247
+ unless stale_kinds.empty?
248
+ LinkCheck.revive(self, stale_kinds)
249
+ stale_kinds = own_links(record).stale.distinct.pluck(:kind)
250
+ end
251
+ unless stale_kinds.empty?
252
+ raise LinkBeingChecked, "QuBe Sync: #{record.class.name} #{record.id}'s link on connection #{id} is being " \
253
+ "checked on its new company file; send #{what} again once it's settled"
254
+ end
255
+ return unless link_under_review?(record)
256
+
257
+ raise LinkNeedsReview, "QuBe Sync: #{record.class.name} #{record.id}'s link on connection #{id} has another " \
258
+ "name on its new company file; confirm_link or reject_link it, then send #{what} again"
259
+ end
260
+
261
+ # A link under review is the same record after all: current again, with the name QuickBooks has.
262
+ def confirm_link(link)
263
+ link = own_review_link(link)
264
+ link.update!(name: link.review_name.nil? ? link.name : link.review_name, review_since: nil, review_name: nil)
265
+ link
266
+ end
267
+
268
+ # A link under review isn't the same record: kept as missing, so the record is matched by name
269
+ # (or added) when it's next sent.
270
+ def reject_link(link)
271
+ link = own_review_link(link)
272
+ link.update!(review_since: nil, review_name: nil, missing_since: Time.now)
273
+ link
274
+ end
275
+
276
+ # Queues the checks of its stale links (QubeRails::LinkCheck), when it's connected and has any.
277
+ # The engine does this on its events; call it to start them again after a check failed.
278
+ def check_stale_links
279
+ LinkCheck.start(self)
280
+ end
281
+
192
282
  # Where record stands with QuickBooks on this connection for operation (its SLUG): a
193
283
  # QubeRails::SyncStatus, worked out from its runs and its link, never stored.
194
284
  def sync_status(record, operation:)
@@ -223,12 +313,12 @@ module QubeRails
223
313
  row && row.record
224
314
  end
225
315
 
226
- # The row for that QuickBooks id (with its kind, edit_sequence and name), or nil. With a kind,
316
+ # The current (not stale) row for that QuickBooks id (with its kind, edit_sequence and name), or nil. With a kind,
227
317
  # it's looked up in that kind's family; without one, in every family, and an id found in more
228
318
  # than one (the same counter value is a customer and an account) raises ArgumentError, so the
229
319
  # caller says which kind it means.
230
320
  def quickbooks_record(qb_id, kind: nil)
231
- rows = quickbooks_records.where(qb_id: qb_id.to_s)
321
+ rows = quickbooks_records.current.where(qb_id: qb_id.to_s)
232
322
  return rows.find_by(list: QuickbooksRecord.family_for(kind)) if kind
233
323
 
234
324
  only_one(rows, "QuickBooks id #{qb_id}")
@@ -236,6 +326,18 @@ module QubeRails
236
326
 
237
327
  private
238
328
 
329
+ def own_links(record)
330
+ type, id = self.class.key_for(record)
331
+ quickbooks_records.where(record_type: type, record_id: id)
332
+ end
333
+
334
+ def own_review_link(link)
335
+ row = review_links.find_by(id: link.respond_to?(:id) ? link.id : link)
336
+ raise ArgumentError, "QuBe Sync: link #{link.inspect} isn't under review on connection #{id}" unless row
337
+
338
+ row
339
+ end
340
+
239
341
  # The record's own link is a different row from the one holding this qb_id, so it gives way.
240
342
  def replaced?(mine, by:)
241
343
  !mine.nil? && !by.nil? && by.id != mine.id
@@ -277,7 +379,7 @@ module QubeRails
277
379
  # EXISTS a link on this connection for each row of scope's table.
278
380
  def linked_here(scope)
279
381
  model = scope.all.klass
280
- QuickbooksRecord.where(connection_id: id, record_type: model.base_class.name)
382
+ QuickbooksRecord.current.where(connection_id: id, record_type: model.base_class.name)
281
383
  .where("qube_quickbooks_records.record_id = #{id_as_text(model, QuickbooksRecord)}")
282
384
  .arel.exists
283
385
  end
@@ -11,6 +11,11 @@ module QubeRails
11
11
  # own, while the same counter value turns up in several families. So a row is unique by
12
12
  # connection, family and qb_id, and by connection, kind and your record (one contact can be
13
13
  # both a customer and a vendor, as two QuickBooks names).
14
+ #
15
+ # A row with stale_since is the old company file's, after connection.company_file_changed, until
16
+ # QubeRails::LinkCheck looks it up on the new file; one with missing_since wasn't there; one with
17
+ # review_since was, under another name (review_name), and waits for Connection#confirm_link or
18
+ # #reject_link. None of them is used: every reader takes current.
14
19
  class QuickbooksRecord < ApplicationRecord
15
20
  self.table_name = "qube_quickbooks_records"
16
21
 
@@ -21,6 +26,23 @@ module QubeRails
21
26
 
22
27
  before_validation { self.list = self.class.family_for(kind) }
23
28
 
29
+ scope :current, -> { where(stale_since: nil, missing_since: nil, review_since: nil) }
30
+ scope :stale, -> { where.not(stale_since: nil) }
31
+ scope :missing, -> { where.not(missing_since: nil) }
32
+ scope :under_review, -> { where.not(review_since: nil) }
33
+
34
+ def stale?
35
+ !stale_since.nil?
36
+ end
37
+
38
+ def missing?
39
+ !missing_since.nil?
40
+ end
41
+
42
+ def under_review?
43
+ !review_since.nil?
44
+ end
45
+
24
46
  # The list family a kind's ids are unique within: "name" for customers, vendors, employees
25
47
  # and other names; "item" for "item" and every item_* kind; for anything else (an account, a
26
48
  # class, a transaction, a kind this version doesn't know) the kind itself. TxnIDs are taken as
@@ -0,0 +1,19 @@
1
+ <%# A set-up connection's actions, for the page that shows its QuickBooks status:
2
+ render "qube_rails/manage_connection", connection: connection
3
+ "Set up QuickBooks again" (a moved computer, a removed Web Connector) and, unless it's
4
+ simulated, "Use a different company file". Nothing while it isn't set up: offer Connect then.
5
+ Both leave for QuBe Sync's setup, so their forms opt out of Turbo. The words are I18n's:
6
+ qube_rails.connection.set_up_again and .use_different_company_file. %>
7
+ <% connection = local_assigns.fetch(:connection) %>
8
+ <% if connection.set_up? %>
9
+ <div class="qube-manage-connection" id="qube-manage-connection-<%= connection.id %>">
10
+ <%= button_to QubeRails.t("connection.set_up_again"), qube_rails.connection_onboarding_path(connection),
11
+ class: qube_classes(:button_secondary, "qube-set-up-again"),
12
+ form: { class: "qube-set-up-again-form", data: { turbo: false } } %>
13
+ <% if connection.company_file_switchable? %>
14
+ <%= button_to QubeRails.t("connection.use_different_company_file"), qube_rails.connection_company_file_path(connection),
15
+ class: qube_classes(:button_secondary, "qube-use-different-company-file"),
16
+ form: { class: "qube-use-different-company-file-form", data: { turbo: false } } %>
17
+ <% end %>
18
+ </div>
19
+ <% end %>
@@ -4,14 +4,12 @@
4
4
  Sync's role for it, when the prompt has roles.
5
5
  A form choice ends in its button row (.qube-choice-actions): cancel on the left, its submit on
6
6
  the right, with the exits passed as exits: (their buttons name their own forms, form=). With
7
- detached: true it renders only the form, for a button elsewhere to submit. A form in a chain
8
- carries its branch (data-qube-branch-part), hidden when that isn't the one on show. %>
7
+ detached: true it renders only the form, for a button elsewhere to submit. %>
9
8
  <% exits = local_assigns[:exits] || [] %>
10
9
  <% detached = local_assigns[:detached] %>
11
10
  <% row = choice.kind == :form %>
12
- <% part = question.branch_part(choice) unless detached %>
13
11
  <form class="<%= qube_classes(:choice, "qube-choice", choice.kind == :form ? "qube-choice-form" : nil) %>" method="post"
14
- action="<%= choice.action_url %>" id="<%= choice.form_id %>" data-qube-choice="<%= choice.id %>" data-qube-choice-kind="<%= choice.kind %>"<% if choice.role %> data-qube-choice-role="<%= choice.role %>"<% end %><% if part %> data-qube-branch-part="<%= part %>"<% end %><%= " hidden" if detached || !question.shown_part?(part) %>>
12
+ action="<%= choice.action_url %>" id="<%= choice.form_id %>" data-qube-choice="<%= choice.id %>" data-qube-choice-kind="<%= choice.kind %>"<% if choice.role %> data-qube-choice-role="<%= choice.role %>"<% end %><%= " hidden" if detached %>>
15
13
  <%= hidden_field_tag request_forgery_protection_token, form_authenticity_token, id: nil %>
16
14
  <%= hidden_field_tag :choice, choice.id, id: nil %>
17
15
  <% unless detached %>
@@ -0,0 +1,7 @@
1
+ <%# A form after a promoted row (question.disclosed: a name clash's rename), behind "or <its
2
+ label>…". Opened, its submit is the main action and the row's button steps back (stylesheet);
3
+ a refused answer for it opens it, and the script puts the focus in its first field. %>
4
+ <details class="qube-choice-disclosure" data-qube-disclosure<%= " open" if choice.refused? %>>
5
+ <summary class="<%= qube_classes(:button_ghost, "qube-choice-button") %>" data-qube-button="ghost"><%= QubeRails.t("question.disclose_choice", label: question.mid_sentence(choice.label)) %></summary>
6
+ <%= render "qube_rails/question/choice", question: question, choice: choice %>
7
+ </details>