qube_rails 0.2.0 → 0.2.1

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 (54) hide show
  1. checksums.yaml +4 -4
  2. data/CHANGELOG.md +216 -0
  3. data/README.md +242 -69
  4. data/app/assets/javascripts/qube_rails/question.js +425 -38
  5. data/app/assets/stylesheets/qube_rails.css +1201 -907
  6. data/app/controllers/qube_rails/connections_controller.rb +23 -6
  7. data/app/controllers/qube_rails/questions_controller.rb +39 -7
  8. data/app/controllers/qube_rails/runs_controller.rb +4 -3
  9. data/app/models/qube_rails/connection.rb +22 -2
  10. data/app/models/qube_rails/run.rb +28 -10
  11. data/app/views/qube_rails/_error.html.erb +2 -2
  12. data/app/views/qube_rails/_question.html.erb +4 -1
  13. data/app/views/qube_rails/question/_choice.html.erb +36 -11
  14. data/app/views/qube_rails/question/_exit_button.html.erb +8 -0
  15. data/app/views/qube_rails/question/_field.html.erb +4 -3
  16. data/app/views/qube_rails/question/_footer.html.erb +19 -5
  17. data/app/views/qube_rails/question/_pick.html.erb +8 -3
  18. data/app/views/qube_rails/question/_picks.html.erb +4 -2
  19. data/app/views/qube_rails/question/_question.html.erb +22 -7
  20. data/app/views/qube_rails/question/_submit.html.erb +9 -0
  21. data/app/views/qube_rails/question/fields/_boolean.html.erb +2 -2
  22. data/app/views/qube_rails/question/fields/_number.html.erb +2 -2
  23. data/app/views/qube_rails/question/fields/_rows.html.erb +17 -4
  24. data/app/views/qube_rails/question/fields/_search.html.erb +3 -1
  25. data/app/views/qube_rails/question/fields/_select.html.erb +6 -5
  26. data/app/views/qube_rails/question/fields/_text.html.erb +2 -2
  27. data/app/views/qube_rails/question/rows/_segmented.html.erb +3 -1
  28. data/app/views/qube_rails/run_status/_detail.html.erb +5 -1
  29. data/app/views/qube_rails/run_status/_status.html.erb +5 -2
  30. data/config/locales/en.yml +23 -1
  31. data/db/migrate/20261031000000_add_qube_connection_type_and_run_headline.rb +12 -0
  32. data/lib/generators/qube_rails/templates/initializer.rb.tt +12 -4
  33. data/lib/qube_rails/configuration.rb +3 -2
  34. data/lib/qube_rails/connection_events.rb +3 -0
  35. data/lib/qube_rails/operation.rb +115 -32
  36. data/lib/qube_rails/question_view.rb +235 -29
  37. data/lib/qube_rails/rows_view.rb +18 -5
  38. data/lib/qube_rails/run_status_view.rb +12 -7
  39. data/lib/qube_rails/storyboard/ask_about_add.json +8 -10
  40. data/lib/qube_rails/storyboard/choose.json +8 -5
  41. data/lib/qube_rails/storyboard/enum_select.json +4 -3
  42. data/lib/qube_rails/storyboard/missing_record.json +10 -13
  43. data/lib/qube_rails/storyboard/name_clash.json +10 -18
  44. data/lib/qube_rails/storyboard/near_match.json +12 -14
  45. data/lib/qube_rails/storyboard/number_boolean.json +4 -3
  46. data/lib/qube_rails/storyboard/record_search.json +14 -15
  47. data/lib/qube_rails/storyboard/refusal.json +10 -18
  48. data/lib/qube_rails/storyboard/rows.json +11 -14
  49. data/lib/qube_rails/storyboard.rb +8 -3
  50. data/lib/qube_rails/sync_status.rb +4 -2
  51. data/lib/qube_rails/version.rb +1 -1
  52. data/lib/qube_rails/view_helpers.rb +4 -2
  53. data/lib/qube_rails.rb +2 -2
  54. metadata +5 -2
@@ -19,6 +19,9 @@ module QubeRails
19
19
  # row is left without a qube_id and the next click picks it up. Until connection.connected
20
20
  # comes, show it as waiting for QuickBooks: the setup doesn't come back here.
21
21
  #
22
+ # type is "realtime" or "standard" (default config.connection_type); any other answers 422.
23
+ # Only a never-connected connection of that type is sent back through setup.
24
+ #
22
25
  # With simulated=1, outside production, it connects a simulated QuickBooks company file
23
26
  # instead, for testing with no Windows computer: a new row whose QuBe Sync connection is
24
27
  # connected as it's made, so connection.connected comes at once, and back to the page the
@@ -28,11 +31,16 @@ module QubeRails
28
31
  return head(:unauthorized) unless account
29
32
  return connect_simulated(account) if simulated_requested?
30
33
 
31
- connection = QubeRails::Connection.owned_by(account).never_connected.first unless params[:new].to_s == "1"
34
+ type = requested_type
35
+ return head(:unprocessable_entity) unless type
36
+
37
+ unless params[:new].to_s == "1"
38
+ connection = QubeRails::Connection.owned_by(account).never_connected.find_by(connection_type: type)
39
+ end
32
40
  return send_to_setup(connection) if connection
33
41
 
34
- send_to_setup(QubeRails::Connection.create!(owner: account, name: connection_name, status: "pending"),
35
- fresh: true)
42
+ send_to_setup(QubeRails::Connection.create!(owner: account, name: connection_name, status: "pending",
43
+ connection_type: type), fresh: true)
36
44
  end
37
45
 
38
46
  # POST /quickbooks/connections/:id/onboarding: sends one of the signed-in account's
@@ -74,7 +82,7 @@ module QubeRails
74
82
  private
75
83
 
76
84
  def work_going?(connection)
77
- connection.runs.where.not(state: QubeRails::Run::ENDED_STATES).exists? ||
85
+ connection.runs.where.not(state: QubeRails::Run::ENDED_STATES + %w[realtime_required]).exists? ||
78
86
  connection.queued_requests.where(ended_at: nil).exists?
79
87
  end
80
88
 
@@ -101,6 +109,14 @@ module QubeRails
101
109
  account
102
110
  end
103
111
 
112
+ # The type param, else config.connection_type; nil for anything but realtime or standard.
113
+ def requested_type
114
+ type = params[:type].to_s
115
+ return QubeRails.config.connection_type if type.empty?
116
+
117
+ %w[realtime standard].include?(type) ? type : nil
118
+ end
119
+
104
120
  def simulated_requested?
105
121
  params[:simulated].to_s == "1"
106
122
  end
@@ -109,7 +125,8 @@ module QubeRails
109
125
  def connect_simulated(account)
110
126
  return head(:not_found) if Rails.env.production?
111
127
 
112
- connection = QubeRails::Connection.create!(owner: account, name: connection_name, status: "pending")
128
+ connection = QubeRails::Connection.create!(owner: account, name: connection_name, status: "pending",
129
+ connection_type: "simulated")
113
130
  made = QubeRails.client.create_connection(webhook_url: connection.webhook_url, name: connection.name,
114
131
  type: "simulated")
115
132
  connection.update!(qube_id: made["id"])
@@ -130,7 +147,7 @@ module QubeRails
130
147
  made = QubeRails.client.regenerate_onboarding(connection.qube_id)
131
148
  else
132
149
  made = QubeRails.client.create_connection(webhook_url: connection.webhook_url, name: connection.name,
133
- type: QubeRails.config.connection_type)
150
+ type: connection.connection_type)
134
151
  connection.update!(qube_id: made["id"])
135
152
  end
136
153
 
@@ -17,6 +17,11 @@ module QubeRails
17
17
  class QuestionsController < QubeRails.config.parent_controller.constantize
18
18
  # What the page says when the answer wasn't sent, as an I18n key under qube_rails.
19
19
  CHANGED = :"errors.question_changed"
20
+ # ... and when QuBe Sync says the question was answered or asked again since.
21
+ MOVED_ON = :"errors.question_moved_on"
22
+ # A refused answer's fields kept for the page to fill again: these types, this many characters.
23
+ KEPT_TYPES = %w[text number boolean select].freeze
24
+ KEPT_CHARACTERS = 1000
20
25
 
21
26
  include RunAuthorization
22
27
 
@@ -26,22 +31,24 @@ module QubeRails
26
31
  # links.decisions, and sends them back to the page; when it can't, back with why (the
27
32
  # partial shows it). The question is forgotten once it's
28
33
  # answered, or once QuBe Sync says it was answered or asked again since (a 409, or a 400 such
29
- # as not_awaiting_input); the run's next webhook says what came of it.
34
+ # as not_awaiting_input): either way the run has moved past it, so it reads as running until
35
+ # its next webhook says what came of it. A refusal (422) goes back to the question, with what
36
+ # was typed (flash[:qube_rails_refusal]).
30
37
  def create
31
38
  choices = Array(@question && @question["choices"])
32
39
  choice = choices.find { |c| c["id"] == params[:choice] }
33
40
  return back_with_error(CHANGED) unless choice && @links["decisions"]
34
41
 
35
42
  QubeRails.client.answer(@links["decisions"], choice: choice["id"], data: answer_data(choice))
36
- @operation.answered(@question["token"], taken: true)
43
+ @operation.answered(@question["token"], taken: true, cancelling: cancel?(choice))
37
44
  redirect_back fallback_location: "/", status: :see_other
38
45
  rescue Backend::APIError => e
39
46
  raise unless [400, 409, 422].include?(e.status)
40
47
  # A field QuBe Sync couldn't take: its errors.detail says which.
41
- return back_with_error(refusal(e)) if e.status == 422
48
+ return back_with_refusal(choice, refusal(e)) if e.status == 422
42
49
 
43
- @operation.answered(@question["token"])
44
- back_with_error(CHANGED)
50
+ @operation.answered(@question["token"], taken: true)
51
+ back_with_error(MOVED_ON)
45
52
  end
46
53
 
47
54
  # The matches for a record field, for what your user typed: [{value, label, detail}], at most
@@ -64,7 +71,6 @@ module QubeRails
64
71
 
65
72
  private
66
73
 
67
- # What a search is asked about: q of 2 to 100 characters, a field name of 1 to 100.
68
74
  # A rows field's answer: each row's filled columns, by row key. QuBe Sync drops a column the
69
75
  # row's other answers say doesn't apply, and names any required one left out.
70
76
  def rows_data(field, form)
@@ -74,13 +80,18 @@ module QubeRails
74
80
  columns = field["columns"].to_a.select { |column| row["columns"].to_a.include?(column["name"]) }
75
81
  rows[row["key"]] = columns.each_with_object({}) do |column, values|
76
82
  text = given[column["name"]].to_s.strip
83
+ # A checkbox, as a choice's: true or false (QuBe Sync drops one whose "when" doesn't hold).
84
+ next values[column["name"]] = text == "true" if column["type"] == "boolean"
85
+
77
86
  values[column["name"]] = column["type"] == "number" ? number(text) : text unless text.empty?
78
87
  end
79
88
  end
80
89
  end
81
90
 
91
+ # What a search is asked about: q of 2 to 100 characters, a field name of 1 to 100.
92
+ # Empty browses (the list's first records, as a box is focused); one letter isn't a search.
82
93
  def searchable?(q, field)
83
- q.length.between?(2, 100) && field.length.between?(1, 100)
94
+ (q.empty? || q.length.between?(2, 100)) && field.length.between?(1, 100)
84
95
  end
85
96
 
86
97
  def find_question
@@ -117,10 +128,31 @@ module QubeRails
117
128
  end
118
129
  end
119
130
 
131
+ # A cancel: its role, or without roles its id (as ChoiceView#cancel? reads it).
132
+ def cancel?(choice)
133
+ choice.key?("role") ? choice["role"] == "cancel" : choice["id"] == "cancel"
134
+ end
135
+
120
136
  def number(text)
121
137
  text.match?(/\A-?\d+\z/) ? text.to_i : Float(text, exception: false) || text
122
138
  end
123
139
 
140
+ # Back to the question with why it was refused and what was typed in its own simple fields
141
+ # (not a search's pick or a rows field), when that fits a cookie session.
142
+ def back_with_refusal(choice, message)
143
+ form = params.fetch(:data, {})
144
+ values = choice["fields"].to_a.each_with_object({}) do |field, kept|
145
+ next unless KEPT_TYPES.include?(FieldView.type_of(field, record_type: "search"))
146
+
147
+ text = form[field["name"]]
148
+ kept[field["name"].to_s] = text if text.is_a?(String) && !text.empty?
149
+ end
150
+ values = {} if values.sum { |name, text| name.length + text.length } > KEPT_CHARACTERS
151
+ flash[:qube_rails_refusal] = { "token" => @question["token"].to_s, "choice" => choice["id"].to_s,
152
+ "message" => message, "values" => values }
153
+ redirect_back fallback_location: "/", status: :see_other
154
+ end
155
+
124
156
  # What QuBe Sync said was wrong with an answer, in words (its errors.detail).
125
157
  def refusal(error)
126
158
  detail = JSON.parse(error.body.to_s).dig("errors", "detail")
@@ -2,7 +2,8 @@
2
2
 
3
3
  module QubeRails
4
4
  # POST /qube/runs/<run id>/retry_start, from the run status partial's "Try sending again"
5
- # button: sends a start QuBe Sync never confirmed (a run still "starting") again, with the same
5
+ # button: sends a start QuBe Sync never confirmed (a run still "starting", or one refused as
6
+ # "realtime_required") again, with the same
6
7
  # reference as its Idempotency-Key (QubeRails::Run#retry_start!), so a start that did land
7
8
  # answers with its run rather than making another. Behind your sign-in and CSRF, as your
8
9
  # ApplicationController (config.parent_controller) is, and config.authorize_question decides
@@ -19,11 +20,11 @@ module QubeRails
19
20
  def retry_start
20
21
  run, = authorized_run("a start retry")
21
22
  return head(:not_found) unless run
22
- return back_with_error(NOT_STARTING) unless run.state == "starting"
23
+ return back_with_error(NOT_STARTING) unless Operation::RETRYABLE_STATES.include?(run.state)
23
24
 
24
25
  begin
25
26
  run.retry_start!
26
- rescue Backend::APIError, *Backend::NETWORK_ERRORS => e
27
+ rescue Backend::APIError, RealtimeRequired, *Backend::NETWORK_ERRORS => e
27
28
  # Refused (the run is now failed) or no answer again (still unconfirmed): the status on
28
29
  # the page says which.
29
30
  QubeRails.logger.warn("QuBe Sync: run #{run.id}'s start retry didn't go through (#{e.class})")
@@ -12,7 +12,8 @@ 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.
15
+ # is the last of those applied. connection_type is "realtime", "standard" or "simulated": as
16
+ # onboarding made it, then as its events (or refresh_type!) say. Workflows don't run on standard.
16
17
  #
17
18
  # The QuickBooks ids your app learns are kept against it, by list family (link, qb_id_for,
18
19
  # find_linked), since a ListID is unique only within one list family of one company file.
@@ -30,7 +31,10 @@ module QubeRails
30
31
  has_many :queued_requests, class_name: "QubeRails::QueuedRequest", inverse_of: :connection
31
32
  has_many :quickbooks_records, class_name: "QubeRails::QuickbooksRecord", inverse_of: :connection
32
33
 
34
+ TYPES = %w[realtime standard simulated].freeze
35
+
33
36
  validates :status, inclusion: { in: STATUSES }
37
+ validates :connection_type, inclusion: { in: TYPES }
34
38
 
35
39
  # The connections owner has, whatever its key's type.
36
40
  # The row, with its runs (and their notifications), its requests and its QuickBooks links: a connection
@@ -64,6 +68,21 @@ module QubeRails
64
68
  status == "connected"
65
69
  end
66
70
 
71
+ # Workflows don't run on it: Operation.start raises RealtimeRequired.
72
+ def standard?
73
+ connection_type == "standard"
74
+ end
75
+
76
+ # Reads its type from QuBe Sync again (GET /api/v2/connections/:id) and saves it: after it's
77
+ # upgraded to realtime in the dashboard, before its next event says so. Returns the type.
78
+ def refresh_type!
79
+ raise NotConnected, "QuBe Sync: connection #{id} has no qube_id yet" if qube_id.to_s.empty?
80
+
81
+ type = QubeRails.client.get_connection(qube_id)["type"].to_s
82
+ update!(connection_type: type) if TYPES.include?(type) && type != connection_type
83
+ connection_type
84
+ end
85
+
67
86
  # Stalled because the Web Connector isn't checking in (the QuickBooks computer is off, or
68
87
  # the Web Connector closed), rather than because it can't open QuickBooks.
69
88
  def not_checking_in?
@@ -102,7 +121,8 @@ module QubeRails
102
121
 
103
122
  QubeRails.logger.info("QuBe Sync: connection #{id} adopted QuBe Sync's connection #{found["id"]}, " \
104
123
  "made for it before its id was saved")
105
- update!(qube_id: found["id"])
124
+ type = found["type"].to_s
125
+ update!({ qube_id: found["id"] }.merge(TYPES.include?(type) ? { connection_type: type } : {}))
106
126
  qube_id
107
127
  end
108
128
 
@@ -9,11 +9,14 @@ module QubeRails
9
9
  #
10
10
  # reference is new for every run and sent with it, and every event is checked against it and
11
11
  # the connection's qube_id. revision is the last event applied; state ("starting", then the
12
- # run's own: "running", "awaiting_input", "completed", "failed", "cancelled") and outcome are
12
+ # run's own: "running", "awaiting_input", "completed", "failed", "cancelled"; "cancelling" after
13
+ # your user's cancel, until the next event) and outcome are
13
14
  # the run's as of that event. question and links are the question it's asking, if any, for the
14
- # question partial. input is what the run was started with. A start QuBe Sync refused (a 4xx),
15
+ # question partial. input is what the run was started with. headline is the last event's
16
+ # sentence for a person (shown, never parsed). A start QuBe Sync refused (a 4xx),
15
17
  # or whose input raised, leaves state "failed"; one that got no answer (a timeout, a 5xx, a
16
- # dropped connection) stays "starting", for retry_start!.
18
+ # dropped connection) stays "starting", for retry_start!. One refused as realtime_required is
19
+ # "realtime_required", with that error, and isn't ended: retry_start! sends it once upgraded.
17
20
  class Run < ApplicationRecord
18
21
  self.table_name = "qube_runs"
19
22
 
@@ -80,6 +83,11 @@ module QubeRails
80
83
  state == "failed"
81
84
  end
82
85
 
86
+ # Refused because its connection is standard; retry_start! once it's upgraded.
87
+ def realtime_required?
88
+ state == "realtime_required"
89
+ end
90
+
83
91
  # Cancelled by your app (state "cancelled"), or a chart that ended on its own "cancelled"
84
92
  # outcome (the app chose to stop): neither is a success.
85
93
  def cancelled?
@@ -97,7 +105,7 @@ module QubeRails
97
105
  # or waiting on a question this app never received. Its webhook may have been lost (the app
98
106
  # was down longer than QuBe Sync's retries), so the status offers to send it again.
99
107
  def looks_stuck?
100
- quiet_since?(Time.now - QUIET_AFTER_SECONDS) && (state == "running" || question_missing?)
108
+ quiet_since?(Time.now - QUIET_AFTER_SECONDS) && (%w[running cancelling].include?(state) || question_missing?)
101
109
  end
102
110
 
103
111
  # A start QuBe Sync hasn't confirmed for a minute: send it again with retry_start!.
@@ -117,7 +125,8 @@ module QubeRails
117
125
 
118
126
  # Where it stands, in a few words for a person: "Added to QuickBooks", "Done", "Couldn't
119
127
  # sync", "Cancelled", "Needs your answer", "Sending to QuickBooks", "Still waiting for
120
- # QuickBooks" (a start unconfirmed for a minute) or "Syncing with QuickBooks". Why it failed
128
+ # QuickBooks" (a start unconfirmed for a minute), "Cancelling…" (a cancel posted, no event
129
+ # since) or "Syncing with QuickBooks". Why it failed
121
130
  # or was cancelled is reason_text. The words are I18n's, under qube_rails.status.
122
131
  def summary_text
123
132
  return QubeRails.t("status.cancelled") if cancelled?
@@ -129,10 +138,13 @@ module QubeRails
129
138
  case outcome.to_s
130
139
  when "added" then QubeRails.t("status.added")
131
140
  when "", "completed" then QubeRails.t("status.done")
141
+ when /_with_errors\z/ then QubeRails.t("status.done_with_errors")
132
142
  else QubeRails.t("status.done_with", outcome: humanize(outcome))
133
143
  end
134
144
  when "failed" then QubeRails.t("status.failed")
145
+ when "realtime_required" then error_message.to_s.empty? ? QubeRails.t("status.realtime_required") : error_message
135
146
  when "awaiting_input" then QubeRails.t("status.needs_answer")
147
+ when "cancelling" then QubeRails.t("status.cancelling")
136
148
  else QubeRails.t("status.running")
137
149
  end
138
150
  end
@@ -155,8 +167,14 @@ module QubeRails
155
167
  reason.empty? ? QubeRails.t("status.cancel_reason") : reason
156
168
  end
157
169
 
158
- # A failed run's message: its error's own (a sentence, or the message of a code, message and
159
- # detail), else its outcome in words, or nil.
170
+ # A completed run that ended on an outcome with errors (push_records' pushed_with_errors): some
171
+ # of its records failed, so it needs a look.
172
+ def with_errors?
173
+ state == "completed" && outcome.to_s.end_with?("_with_errors")
174
+ end
175
+
176
+ # A failed run's message: its error's own (a sentence, or the message of {code, type, message,
177
+ # quickbooks_message, hint}, or of 0.2.0's code, message and detail), else its outcome in words, or nil.
160
178
  def error_message
161
179
  text =
162
180
  case error
@@ -170,12 +188,12 @@ module QubeRails
170
188
  outcome.to_s.empty? || outcome == "failed" ? nil : humanize(outcome)
171
189
  end
172
190
 
173
- # The error's detail, when it carries one apart from its message.
191
+ # What to do about the error: its hint, else its detail, when apart from its message.
174
192
  def error_detail
175
193
  return nil unless error.is_a?(Hash)
176
194
 
177
- detail = error["detail"]
178
- detail = detail.to_s.strip
195
+ detail = error["hint"].to_s.strip
196
+ detail = error["detail"].to_s.strip if detail.empty?
179
197
  detail.empty? || detail == error_message ? nil : detail
180
198
  end
181
199
 
@@ -1,7 +1,7 @@
1
1
  <%# A message from one of the engine's routes, shown once. Without a message local it takes
2
2
  flash[:qube_rails_error], for a page without the run partials:
3
3
  render "qube_rails/error"
4
- The question and run status pieces pass theirs (message: nil shows nothing). %>
4
+ The question and run status pieces pass theirs (message: nil shows nothing); id: is optional. %>
5
5
  <% if local_assigns.key?(:message) %>
6
6
  <% message = local_assigns[:message] %>
7
7
  <% else %>
@@ -9,5 +9,5 @@
9
9
  <% flash.delete(:qube_rails_error) %>
10
10
  <% end %>
11
11
  <% if message.present? %>
12
- <p class="<%= qube_classes(:error, "qube-error") %>" role="alert"><%= message %></p>
12
+ <%= tag.p(message, class: qube_classes(:error, "qube-error"), role: "alert", id: local_assigns[:id]) %>
13
13
  <% end %>
@@ -13,7 +13,10 @@
13
13
  <%= turbo_stream_from(record, :qube_rails) if respond_to?(:turbo_stream_from) %>
14
14
  <% error = flash[:qube_rails_error] %>
15
15
  <% flash.delete(:qube_rails_error) # shown once, by whichever partial comes first %>
16
- <% question = QubeRails::QuestionView.from_run(run, qube_rails, error: error) %>
16
+ <%# A refused answer (422): shown in its question, with what was typed. %>
17
+ <% refusal = flash[:qube_rails_refusal] %>
18
+ <% flash.delete(:qube_rails_refusal) %>
19
+ <% question = QubeRails::QuestionView.from_run(run, qube_rails, error: error, refusal: refusal) %>
17
20
  <% if question %>
18
21
  <%= render "qube_rails/question/question", question: question %>
19
22
  <% else %>
@@ -1,20 +1,45 @@
1
1
  <%# One choice (a QubeRails::ChoiceView): a form posting choice=<id> and data[...] for its fields
2
2
  to choice.action_url, with your CSRF token. Its button is choice.style's (:primary, :secondary
3
- or :ghost), with that ui_classes piece (choice.button_piece). %>
3
+ or :ghost), with that ui_classes piece (choice.button_piece). data-qube-choice-role is QuBe
4
+ Sync's role for it, when the prompt has roles.
5
+ A form choice ends in its button row (.qube-choice-actions): cancel on the left, its submit on
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. %>
9
+ <% exits = local_assigns[:exits] || [] %>
10
+ <% detached = local_assigns[:detached] %>
11
+ <% row = choice.kind == :form %>
12
+ <% part = question.branch_part(choice) unless detached %>
4
13
  <form class="<%= qube_classes(:choice, "qube-choice", choice.kind == :form ? "qube-choice-form" : nil) %>" method="post"
5
- action="<%= choice.action_url %>" data-qube-choice="<%= choice.id %>" data-qube-choice-kind="<%= choice.kind %>">
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) %>>
6
15
  <%= hidden_field_tag request_forgery_protection_token, form_authenticity_token, id: nil %>
7
16
  <%= hidden_field_tag :choice, choice.id, id: nil %>
8
- <% choice.fields.each do |field| %>
17
+ <% unless detached %>
18
+ <% choice.fields.each do |field| %>
9
19
  <%= render field.partial, field: field %>
10
- <% end %>
11
- <button type="submit" class="<%= qube_classes(choice.button_piece, "qube-choice-button") %>" data-qube-button="<%= choice.style %>">
12
- <%= choice.label %>
13
- <% if choice.detail %>
14
- <span class="<%= qube_classes(:choice_detail, "qube-choice-detail") %>"><%= choice.detail %></span>
15
- <% end %>
16
- </button>
17
- <% if choice.description %>
20
+ <% end %>
21
+ <% if row %>
22
+ <% if choice.description %>
23
+ <p class="<%= qube_classes(:choice_description, "qube-choice-description") %>"><%= choice.description %></p>
24
+ <% end %>
25
+ <div class="<%= qube_classes(:choice_actions, "qube-choice-actions") %>" data-qube-actions>
26
+ <div class="qube-choice-actions-start">
27
+ <% exits.select(&:cancel?).each do |exit_choice| %>
28
+ <%= render "qube_rails/question/exit_button", choice: exit_choice %>
29
+ <% end %>
30
+ </div>
31
+ <div class="qube-choice-actions-end" data-qube-actions-end>
32
+ <% exits.reject(&:cancel?).each do |exit_choice| %>
33
+ <%= render "qube_rails/question/exit_button", choice: exit_choice %>
34
+ <% end %>
35
+ <%= render "qube_rails/question/submit", choice: choice %>
36
+ </div>
37
+ </div>
38
+ <% else %>
39
+ <%= render "qube_rails/question/submit", choice: choice %>
40
+ <% if choice.description %>
18
41
  <p class="<%= qube_classes(:choice_description, "qube-choice-description") %>"><%= choice.description %></p>
42
+ <% end %>
43
+ <% end %>
19
44
  <% end %>
20
45
  </form>
@@ -0,0 +1,8 @@
1
+ <%# An exit's button in a form choice's row: it submits the exit's own form (form=), rendered
2
+ detached after the question's forms. %>
3
+ <button type="submit" form="<%= choice.form_id %>" class="<%= qube_classes(choice.button_piece, "qube-choice-button") %>" data-qube-button="<%= choice.style %>" data-qube-exit="<%= choice.id %>"<% if choice.role %> data-qube-choice-role="<%= choice.role %>"<% end %>>
4
+ <%= choice.label %>
5
+ <% if choice.detail %>
6
+ <span class="<%= qube_classes(:choice_detail, "qube-choice-detail") %>"><%= choice.detail %></span>
7
+ <% end %>
8
+ </button>
@@ -1,12 +1,13 @@
1
- <%# The wrapper every field piece renders its control (control, HTML) in: a choice's field gets
2
- its label; inside a rows table, the control stands alone. %>
1
+ <%# The wrapper every field piece renders its control (control, HTML) in, with its label; a field
2
+ that isn't required says "(optional)" (question.optional) after it (not a checkbox). %>
3
3
  <% if field.labelled? %>
4
4
  <div class="<%= qube_classes(:field, "qube-field", "qube-field-#{field.type}") %>">
5
5
  <% if field.type == "boolean" %>
6
6
  <%= control %>
7
7
  <%= label_tag field.dom_id, field.label, class: qube_classes(:label, "qube-label") %>
8
8
  <% else %>
9
- <%= label_tag field.dom_id, field.label, class: qube_classes(:label, "qube-label") %>
9
+ <% optional = field.optional? ? tag.span(QubeRails.t("question.optional"), class: "qube-optional") : nil %>
10
+ <%= label_tag field.dom_id, safe_join([field.label, optional].compact, " "), class: qube_classes(:label, "qube-label") %>
10
11
  <%= control %>
11
12
  <% end %>
12
13
  </div>
@@ -1,7 +1,21 @@
1
- <%# The exits (question.exits: none of these, cancel and the like) in a row; in a plain question
2
- (no picks, no forms) every choice, the first primary. %>
3
- <div class="<%= qube_classes(:choice_footer, "qube-choice-footer") %>"<%= " data-qube-plain" if question.plain? %>>
4
- <% question.exits.each do |choice| %>
5
- <%= render "qube_rails/question/choice", question: question, choice: choice %>
1
+ <%# The exits (question.exits) in one row: cancel on the left, the rest (decline and the like)
2
+ on the right. In a plain question (no picks, no forms) every choice, the first primary. With
3
+ descriptions they stack, cancel last. In a chain it ends in "Not this one" / "None of these"
4
+ (data-qube-branch="search", shown by the script, which hides the decline: that's on the search
5
+ branch); when the search's row holds the exits, this row is the offered branch's. %>
6
+ <% part = question.branch_part(:footer) %>
7
+ <div class="<%= qube_classes(:choice_footer, "qube-choice-footer") %>"<%= " data-qube-plain" if question.plain? %><% if part %> data-qube-branch-part="<%= part %>"<% end %><%= " hidden" unless question.shown_part?(part) %>>
8
+ <div class="qube-choice-footer-start">
9
+ <% question.exits.select(&:cancel?).each do |choice| %>
10
+ <%= render "qube_rails/question/choice", question: question, choice: choice %>
6
11
  <% end %>
12
+ </div>
13
+ <div class="qube-choice-footer-end">
14
+ <% question.exits.reject(&:cancel?).each do |choice| %>
15
+ <%= render "qube_rails/question/choice", question: question, choice: choice %>
16
+ <% end %>
17
+ <% if question.chain? %>
18
+ <button type="button" class="<%= qube_classes(:button_secondary, "qube-choice-button") %>" data-qube-button="secondary" data-qube-branch="search" hidden><%= question.ui_text(:none_of_these, count: question.offered_count) %></button>
19
+ <% end %>
20
+ </div>
7
21
  </div>
@@ -1,19 +1,24 @@
1
1
  <%# One pick (a QubeRails::ChoiceView of kind :pick): an option row, a form posting choice=<id>
2
- to choice.action_url with your CSRF token. %>
2
+ to choice.action_url with your CSRF token. An offered record (choice.style set) ends in a
3
+ "Use this one" button: the first primary, the rest secondary. %>
3
4
  <form class="qube-choice qube-pick-form" method="post" action="<%= choice.action_url %>" role="listitem"
4
- data-qube-choice="<%= choice.id %>" data-qube-choice-kind="pick">
5
+ data-qube-choice="<%= choice.id %>" data-qube-choice-kind="pick"<% if choice.role %> data-qube-choice-role="<%= choice.role %>"<% end %>>
5
6
  <%= hidden_field_tag request_forgery_protection_token, form_authenticity_token, id: nil %>
6
7
  <%= hidden_field_tag :choice, choice.id, id: nil %>
7
8
  <button type="submit" class="<%= qube_classes(:pick, "qube-pick") %>">
8
9
  <span class="qube-pick-text">
9
10
  <span class="qube-pick-label"><%= choice.label %></span>
10
11
  <% if choice.detail %>
11
- <span class="<%= qube_classes(:pick_detail, "qube-pick-detail") %>"><%= choice.detail %></span>
12
+ <span class="<%= qube_classes(:pick_detail, "qube-pick-detail") %>"><%= safe_join(choice.detail_parts.map { |part| tag.span(part, class: "qube-detail-part") }, " ") %></span>
12
13
  <% end %>
13
14
  <% if choice.description %>
14
15
  <span class="qube-pick-description"><%= choice.description %></span>
15
16
  <% end %>
16
17
  </span>
18
+ <% if choice.style %>
19
+ <span class="<%= qube_classes(choice.button_piece, "qube-pick-action", "qube-choice-button") %>" data-qube-button="<%= choice.style %>" data-qube-offered="<%= choice.style %>"><%= QubeRails.t("question.use_pick") %></span>
20
+ <% else %>
17
21
  <span class="<%= qube_classes(:pick_action, "qube-pick-action") %>"><%= QubeRails.t("question.use_pick") %></span>
22
+ <% end %>
18
23
  </button>
19
24
  </form>
@@ -1,5 +1,7 @@
1
- <%# The question's picks (question.picks): option rows, one form each. %>
2
- <div class="<%= qube_classes(:picks, "qube-picks") %>" role="list">
1
+ <%# The question's picks (question.picks): option rows, one form each. The question's message says
2
+ what they are, so they have no heading. In a chain they're its offered branch. %>
3
+ <% part = question.branch_part(:picks) %>
4
+ <div class="<%= qube_classes(:picks, "qube-picks") %>" role="list"<% if part %> data-qube-branch-part="<%= part %>"<% end %><%= " hidden" unless question.shown_part?(part) %>>
3
5
  <% question.picks.each do |choice| %>
4
6
  <%= render "qube_rails/question/pick", question: question, choice: choice %>
5
7
  <% end %>
@@ -1,23 +1,38 @@
1
1
  <%# A question (a QubeRails::QuestionView): a refusal, its message, what it's about, then its
2
2
  choices by kind: the picks (option rows), each form choice ("or" between), and the exits in a
3
- footer. A question with neither picks nor forms shows its choices in one row. %>
4
- <section class="<%= qube_classes(:question, "qube-question") %>" data-qube-question="<%= question.token %>">
5
- <%= render "qube_rails/error", message: question.error %>
3
+ footer. With one form and no picks, the exits share that form's button row instead (question.
4
+ joined_form). A question with neither picks nor forms shows its choices in one row.
5
+ Offered records with a search (question.chain?) show a branch at a time: each part carries
6
+ data-qube-branch-part, and the one not on show is hidden; the script moves between them.
7
+ data-qube-button-*-class are the script's, for swapping a button's weight. %>
8
+ <section class="<%= qube_classes(:question, "qube-question") %>" data-qube-question="<%= question.token %>"
9
+ data-qube-button-class="<%= qube_classes(:button) %>" data-qube-button-secondary-class="<%= qube_classes(:button_secondary) %>">
10
+ <%= render "qube_rails/error", message: question.error, id: question.error_id %>
6
11
  <%= render "qube_rails/question/message", question: question %>
7
12
  <% if question.details.any? %>
8
13
  <%= render "qube_rails/question/details", question: question %>
9
14
  <% end %>
10
15
  <div class="qube-choices">
16
+ <% if question.chain? %>
17
+ <button type="button" class="<%= qube_classes(:button_ghost, "qube-choice-button", "qube-branch-back") %>" data-qube-button="ghost" data-qube-branch="offered" data-qube-branch-part="search" hidden><%= question.ui_text(:back_to_offered, count: question.offered_count) %></button>
18
+ <% end %>
11
19
  <% if question.picks.any? %>
12
20
  <%= render "qube_rails/question/picks", question: question %>
13
21
  <% end %>
22
+ <% joined = question.joined_form %>
14
23
  <% question.forms.each_with_index do |choice, n| %>
15
- <% if n.positive? || question.picks.any? %>
16
- <div class="<%= qube_classes(:choice_divider, "qube-choice-divider") %>" role="separator"><span><%= QubeRails.t("question.or") %></span></div>
24
+ <% if question.divided?(choice, n) %>
25
+ <% part = question.branch_part(choice) %>
26
+ <div class="<%= qube_classes(:choice_divider, "qube-choice-divider") %>" role="separator"<% if part %> data-qube-branch-part="<%= part %>"<% end %><%= " hidden" unless question.shown_part?(part) %>><span><%= QubeRails.t("question.or") %></span></div>
17
27
  <% end %>
18
- <%= render "qube_rails/question/choice", question: question, choice: choice %>
28
+ <%= render "qube_rails/question/choice", question: question, choice: choice,
29
+ exits: (choice.equal?(joined) ? question.joined_exits : []) %>
19
30
  <% end %>
20
- <% if question.exits.any? %>
31
+ <% if joined && !question.chain? %>
32
+ <% question.exits.each do |choice| %>
33
+ <%= render "qube_rails/question/choice", question: question, choice: choice, detached: true %>
34
+ <% end %>
35
+ <% elsif question.exits.any? || question.chain? %>
21
36
  <%= render "qube_rails/question/footer", question: question %>
22
37
  <% end %>
23
38
  </div>
@@ -0,0 +1,9 @@
1
+ <%# A choice's own submit, in its form. A rows form's submit waits for the behaviour script
2
+ (hidden until it runs: a rows field can't be answered without it); a search's ("Use this
3
+ one") until a match is picked (choice.waits_for_pick?), which the script shows. %>
4
+ <button type="submit" class="<%= qube_classes(choice.button_piece, "qube-choice-button") %>" data-qube-button="<%= choice.style %>"<% if choice.rows? %> data-qube-needs-script hidden<% elsif choice.waits_for_pick? %> hidden<% end %>>
5
+ <%= choice.label %>
6
+ <% if choice.detail %>
7
+ <span class="<%= qube_classes(:choice_detail, "qube-choice-detail") %>"><%= choice.detail %></span>
8
+ <% end %>
9
+ </button>
@@ -1,7 +1,7 @@
1
1
  <%# A checkbox: ticked posts "true", sent as true; unticked, false. %>
2
2
  <% control = capture do %>
3
- <%= check_box_tag field.input_name, "true", false, id: field.dom_id, disabled: field.disabled,
3
+ <%= check_box_tag field.input_name, "true", field.value.to_s == "true", id: field.dom_id, disabled: field.disabled,
4
4
  class: qube_classes(:checkbox, "qube-checkbox", field.cell? ? "qube-rows-input" : nil),
5
- "aria-label": (field.labelled? ? nil : field.label), data: field.data %>
5
+ "aria-label": (field.labelled? ? nil : field.label), data: field.data, **field.aria_invalid %>
6
6
  <% end %>
7
7
  <%= render "qube_rails/question/field", field: field, control: control %>
@@ -1,7 +1,7 @@
1
1
  <%# A number field: posted as typed, sent as a number. %>
2
2
  <% control = capture do %>
3
- <%= number_field_tag field.input_name, nil, id: field.dom_id, step: "any", required: field.required,
3
+ <%= number_field_tag field.input_name, field.value, id: field.dom_id, step: "any", required: field.required,
4
4
  disabled: field.disabled, class: qube_classes(:input, "qube-input", field.cell? ? "qube-rows-input" : nil),
5
- "aria-label": (field.labelled? ? nil : field.label), data: field.data %>
5
+ "aria-label": (field.labelled? ? nil : field.label), data: field.data, **field.aria_invalid %>
6
6
  <% end %>
7
7
  <%= render "qube_rails/question/field", field: field, control: control %>