inline_forms 8.1.85 → 8.1.88

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bfea9f6d48988ef6cb10aa6631e6b35b61ab2c1833bdd3c574f1ece144c72b3e
4
- data.tar.gz: 8b7d53c69dd60028833cf4b7112162baee37b4fdf18ea441d8f5cda0592bee39
3
+ metadata.gz: f557f3a3da96a06b4c9e0bc5a2ac3608aab2a131466baddf804a59888db906e5
4
+ data.tar.gz: 1a15811f4cac5dca91e00ebeb04bbe301c3e8a9d6b8c189fe0e3eba93d73b271
5
5
  SHA512:
6
- metadata.gz: dba83a8ec7081f256cc836f45bd05bbc64a39683f2937f5ea99ec9421ff70c7a19979119db93dd2bd8c54e1c5b22d29a30423514965ac6194c19a8c266de9c82
7
- data.tar.gz: db81917c887f949c399cfd4c2a217fd90073142f260802b956e8bb830563a5963e72a24fbc08a3f0714ccf90733d6837676eebb5b00604f14b99f4b64400038d
6
+ metadata.gz: 24e0258caf2408e17b54d09bccd0dec27969728061275abd6bc5e949e12b22a323198d20af2ce91dd2f1799bb2eabbaacb0d0cac3634449fc3afe526561fe26b
7
+ data.tar.gz: cf33ec74101cc24ae8a37b42579cd433b01561013eb9d448ca087d297fd84a5ab8e3eb351f929b97fbaef8b222f9cd7e824beffb6f66dae25fefe4e76fe43944
data/CHANGELOG.md CHANGED
@@ -8,6 +8,63 @@ All notable changes to this project are documented in this file.
8
8
 
9
9
  - Known issues, parked (local note `stuff/2026-10-31-dropdown-rule-3-parked.md`) with dropdown order rule 3: `includes`, `group` or `select` in `inline_forms_list` make `dropdown` and `dropdown_with_other` raise while `/index` works, so 8.1.65's fix for a list order on a joined table holds only for `joins`, `left_joins` and `eager_load`; a has_many join gives duplicate options. Present since 8.1.60. No host's list scope has these today. Parked; not changed.
10
10
 
11
+ ## [8.1.88] - 2026-11-25
12
+
13
+ From the fourth ladder note from RACVS and yavetracker (`ladder/inline_forms-PROMPT-4-2026-11-25.md`).
14
+
15
+ ### Changed
16
+
17
+ - **A file dropped or pasted into a `:rich_text` editor on a plain column now gives a message.** Since 8.1.86 the file was refused without a word. The editor now carries the translated `inline_forms.files.not_in_this_field` ("Files cannot be added to this field.") in `data-no-files-message`, and `inline_forms.js` shows it under the editor when it refuses a file, once however many files were dropped. The message goes away when the text changes. Styled by `.inline_forms-trix-refused`. New locale key in en and nl.
18
+
19
+ ### Fixed
20
+
21
+ - **`order: :relation` reads its pages in one fixed order.** Rows equal on the relation's order and on `LOWER(value)` had no fixed order between the page queries (MySQL and PostgreSQL may order ties differently each time), so a row could be read twice or skipped at a page boundary. The primary key is now the last `ORDER BY` term. The visible effect was at most which spelling of an equal value ("José", "Jose") was kept.
22
+
23
+ ### Documentation
24
+
25
+ - **The plus on a plain-column `:rich_text` is decided by text, not by what the allowlist removes.** A value with no text in what is shown gets the plus, which includes markup that shows something but holds no text (a lone `<hr>`, a table of empty cells), not only what the allowlist empties. The code is unchanged since 8.1.86; the comment in `rich_text_show`, the README and this entry now say so. The plus opens the editor with that markup in it.
26
+
27
+ ## [8.1.87] - 2026-11-25
28
+
29
+ From a review of 8.1.85 and 8.1.86 (local note `stuff/reviews/2026-11-25-review-8.1.85-8.1.86.md`).
30
+
31
+ ### Fixed
32
+
33
+ - **A values list that mixes numbers and strings sorts the same way every time.** 8.1.85 compared two numbers as numbers and anything else as text, and the two rules contradicted each other: `[10, 9, "1a"]` gave 10, "1a", 9, and a `0..12` list with "12+" added put "12+" between 1 and 2. Now `nil` comes first (a blank option, as before), then the numbers in numeric order, then everything else as text; equal values keep their order. This corrects 8.1.85's "Numbers now sort as numbers", which held only for lists of numbers alone.
34
+
35
+ ### Documentation
36
+
37
+ - **`order: :relation` reads a limited window** (README, `InlineForms::Suggestions`): at most 10 pages of 4 × `limit` matching rows per column (480 at the default 12), one query each, and stops there. A value that fills those rows hides the values after it (480 × "Ada" hide "Adam", which A-Z finds). 8.1.86 did not say so.
38
+ - **A suggestion action outside `InlineFormsController` is not authorized by the engine.** `load_and_authorize_resource` checks the action's ability only in `InlineFormsController`; elsewhere the app authorizes the action itself. Records and columns are still limited to what the user may read (README, `SuggestionsConcern`).
39
+ - **Vendoring an archived element (`archived/README.md`): make the module's methods private.** Included into `InlineFormsController`, a public module made `<name>_show`, `_edit` and `_update` controller actions again, which 8.1.83 stopped for the engine's own. The example now shows the module. The page no longer calls the archived-element check a boot check: since 8.1.85 it runs on each code reload in development.
40
+ - README: the tabs section's code (`link_options: …`, `<a>`, `<turbo-frame …>`) and the suggestions section's `order: :relation` render as code. 8.1.85 said the first did; RDoc's `+…+` does not mark text with spaces or markup, so it showed the plus signs.
41
+
42
+ ### Tests
43
+
44
+ - The Trix file refusal's listener, dropping `<template>` and `<noscript>`, `order: :relation` with a CanCan rule that hides records, and the `order: :relation` window.
45
+
46
+ ## [8.1.86] - 2026-11-25
47
+
48
+ From the third RACVS / yavetracker ladder prompt (`ladder/inline_forms-PROMPT-3-2026-11-25.md`).
49
+
50
+ ### Added
51
+
52
+ - **`suggest_names … order: :relation`** keeps the relation's own order (a `default_scope`'s, a scope's: documents newest first) instead of A-Z, with `LOWER(value)` A-Z after it and each value once. Default stays `:alphabetical`. `InlineForms::Suggestions.values` takes the same `order:`. RACVS's `ListOrderSuggestions` concern can go.
53
+ - **`suggest_names … scope: :incoming`**: a named scope as a Symbol, besides a lambda. Either must give a relation (ArgumentError otherwise).
54
+
55
+ ### Fixed
56
+
57
+ - **CKEditor's `<font>` keeps its colour, face and size** on a plain column under `:rich_text`: `font` with `color`, `face` and `size` is in the display allowlist, and so are `kbd`, `samp`, `tt` and `var`. RACVS's own `rich_text_show` can go.
58
+ - **A plain column holding only what the allowlist drops shows the plus.** Blank was decided on the stored value, before the allowlist: a value of only an `<img>`, an empty `<p>` or a `<script>` gave a link with no text (40 blanks) instead of the plus.
59
+ - **The contents of `<script>`, `<style>`, `<template>` and `<noscript>` are not shown.** The allowlist removed the tag and kept its text, so a stored `<script>alert(1)</script>` showed as the text `alert(1)` (escaped; it never ran).
60
+ - **A file dropped or pasted into a plain-column Trix editor is refused** (`trix-file-accept`), not inserted with nowhere to go. 8.1.84 only hid the attach button. RACVS's own listener can go.
61
+ - **The field request check asks for the helper of the engine action that runs** (`show`, `edit`, `update` pass it), not one read from `action_name`: an app action of another name that hands over to `edit` was checked for `_show`.
62
+ - The display sanitizer is built once, not once per value.
63
+
64
+ ### Documentation
65
+
66
+ - README: a section on search suggestions (`suggest_field_tag`, `suggest_names`, an action of your own). `inline_forms_suggestion_scope`, `inline_forms_readable_columns` and `render_inline_forms_suggestions` are named as supported API (private helpers), and any controller may include `SuggestionsConcern`.
67
+
11
68
  ## [8.1.85] - 2026-11-25
12
69
 
13
70
  From a review of 8.1.81 to 8.1.84 (local note `stuff/reviews/2026-11-25-review-8.1.81-8.1.84.md`).
data/README.rdoc CHANGED
@@ -262,7 +262,7 @@ Two rules hold for every pattern:
262
262
  - *The model keeps an instance +inline_forms_attribute_list+ with the rows of every tab.* Each field request (edit, update, a field's show) is checked against the record's list since 8.1.76, not against the tab's +@inline_forms_attribute_list+ of the page that linked to it. A model whose rows live only in a class-level structure raises +InlineForms::MissingAttributeListError+, naming the model; give it e.g. <tt>def inline_forms_attribute_list = @inline_forms_attribute_list ||= self.class.tab_fields.values.flatten(1)</tt>.
263
263
  - *A +show+ override hands field requests to the engine with <tt>return super if params[:form_element] || …</tt>*, as above. Since 8.1.84 +show+ returns true after it has answered, a refusal included, so the older <tt>super and return if …</tt> works too; before, a refused request (400) fell through to the override's own render (DoubleRenderError).
264
264
 
265
- In every case the Turbo wiring is the same: +link_options: { data: { turbo_frame: @update_span } }+ on the +<a>+, surrounding +<turbo-frame id="<%= @update_span %>">+ in the show view, and a controller that picks the active tab via +set_tab+ + +params[:tab]+. The +InlineForms::TurboTabsBuilder+ choice is independent of which partial layout you adopt.
265
+ In every case the Turbo wiring is the same: <tt>link_options: { data: { turbo_frame: @update_span } }</tt> on the <tt><a></tt>, surrounding <tt><turbo-frame id="<%= @update_span %>"></tt> in the show view, and a controller that picks the active tab via +set_tab+ + +params[:tab]+. The +InlineForms::TurboTabsBuilder+ choice is independent of which partial layout you adopt.
266
266
 
267
267
  == Generated application +rails-i18n+
268
268
 
@@ -333,12 +333,50 @@ Trade-off: files accumulate on disk; periodic sweeping is out of scope of the ge
333
333
 
334
334
  For long text fields, use +:plain_text+ for a plain textarea backed by a DB +text+ column, or +:rich_text+ for ActionText/Trix content. +:plain_text+ requires an actual column on the model table; if the column is missing, inline_forms now raises +InlineForms::PlainTextColumnMissingError+ during controller boot/runtime checks.
335
335
 
336
+ +:rich_text+ also works on a plain text column, without +has_rich_text+. The stored HTML is shown through the display allowlist (<tt>InlineForms.rich_text_display_html</tt>). A value with no text in what is shown gets the plus, so the field can still be opened: that covers what the allowlist empties (only an +<img>+ or a +<script>+) and also markup that shows something but holds no text, such as a lone +<hr>+ or a table of empty cells. Such a column cannot store a file: the editor has no attach button, and a file dropped or pasted into it is refused with the notice <tt>inline_forms.files.not_in_this_field</tt> under the editor (8.1.88).
337
+
336
338
  Note: generated apps also depend on ActiveStorage transitively because the +:rich_text+ form element uses ActionText (+active_storage:install+ runs during +inline_forms create+). Image uploads still go through CarrierWave; ActiveStorage is only there to back ActionText embeds.
337
339
 
338
340
  == Native inputs (no jQuery UI)
339
341
 
340
342
  Since 8.1.25/8.1.26 the date/time widgets are native HTML inputs — +:date_select+ renders +<input type="date">+, +:time_select+ +<input type="time">+, +:month_year_picker+ +<input type="month">+, +:slider_with_values+ +<input type="range">+, +:dropdown_with_other+ an +<input list>+ + +<datalist>+ combobox, and the new +:color_field+ (8.1.32) +<input type="color">+. jQuery UI, its themes and the +jquery-ui-rails+ / +rails-jquery-autocomplete+ / +jquery-timepicker-rails+ gems are gone; jQuery itself remains only because Foundation 6's JS requires it.
341
343
 
344
+ == Search suggestions (+suggest_field_tag+, +suggest_names+)
345
+
346
+ A search field with suggestions is a text field with the browser's own <tt><datalist></tt>, filled while the user types from a JSON action (no jQuery UI; it replaces rails-jquery-autocomplete's +autocomplete_field_tag+ and +autocomplete+ macro):
347
+
348
+ <%= suggest_field_tag "search", params[:search], autocomplete_client_last_name_clients_path, placeholder: "last name" %>
349
+
350
+ The action comes from +suggest_names+ in the controller. +InlineFormsController+ includes +SuggestionsConcern+; any other controller may include it too, with +CancanConcern+ for +suggest_names+ and the scope and column helpers below (+render_inline_forms_suggestions+ alone needs nothing).
351
+
352
+ class ClientsController < InlineFormsController
353
+ suggest_names :client, :last_name # the term anywhere, A-Z
354
+ suggest_names :client, :first_name, match: :start # :contains, :start or :word
355
+ suggest_names :document, :subject, scope: :incoming, # a named scope, or a lambda
356
+ order: :relation # the scope's order, not A-Z
357
+ end
358
+
359
+ Each defines <tt>autocomplete_<model>_<column></tt>, answering <tt>GET …?term=…</tt> with at most 12 values (+limit:+), each once. The app adds the route and the ability:
360
+
361
+ resources :clients do
362
+ get :autocomplete_client_last_name, on: :collection
363
+ end
364
+ can :autocomplete_client_last_name, Client
365
+
366
+ Only values of records the user may read (+accessible_by+) and of a column the user may read (<tt>can?(:read, Model, :column)</tt>). <tt>order: :relation</tt> (8.1.86) keeps the relation's own order (a +default_scope+, or a scope that orders documents newest first) instead of A-Z. It reads the matching rows in that order, at most 10 pages of 4 × +limit+ rows (480 at the default 12) per column, and stops there: a column where one value fills those rows (500 documents with the same subject) answers fewer values than A-Z would. Each page is a query. Rows equal on the relation's order and the value are read in primary-key order, so none is read twice or skipped between pages (8.1.88).
367
+
368
+ Outside +InlineFormsController+ nothing checks the action's ability: +InlineFormsController+'s +load_and_authorize_resource+ does that. Records and columns are still limited to what the user may read, but the app authorizes the action itself (+authorize!+ in a +before_action+, or its own +load_and_authorize_resource+).
369
+
370
+ An action of your own uses the same private helpers, which are supported API:
371
+
372
+ def autocomplete_word_name
373
+ words = inline_forms_suggestion_scope(Word).where(language: current_language) # records the user may read
374
+ columns = inline_forms_readable_columns(Word, [ :name ]) # columns the user may read
375
+ render_inline_forms_suggestions InlineForms::Suggestions.values(words, columns, params[:term], match: :start)
376
+ end
377
+
378
+ The engine's own search box suggests too, for a model that declares it: <tt>inline_forms_search_on :name, :email, suggest: { on: :name }</tt>.
379
+
342
380
  == Per-user themes, colors and locale
343
381
 
344
382
  Generated apps let each user pick, on their own user panel:
@@ -46,6 +46,32 @@ if (!window.inlineFormsBooted) {
46
46
  initInlineFormsWidgets(root);
47
47
  });
48
48
 
49
+ // A :rich_text editor on a plain column (no has_rich_text) cannot store a
50
+ // file: the attach button is hidden (CSS), and a dropped or pasted file is
51
+ // refused here, so nothing is inserted. The editor's data-no-files-message
52
+ // (translated by rich_text_edit) is shown under it, once however many files
53
+ // were dropped, and goes away when the text changes.
54
+ document.addEventListener("trix-file-accept", function(event) {
55
+ var editor = event.target;
56
+ if (editor && editor.classList && editor.classList.contains("inline_forms-trix-no-files")) {
57
+ event.preventDefault();
58
+ var message = editor.getAttribute("data-no-files-message");
59
+ if (!message) { return; }
60
+ var notice = editor.nextElementSibling;
61
+ if (!notice || !notice.classList.contains("inline_forms-trix-refused")) {
62
+ notice = document.createElement("p");
63
+ notice.className = "inline_forms-trix-refused";
64
+ notice.setAttribute("role", "status");
65
+ editor.insertAdjacentElement("afterend", notice);
66
+ }
67
+ notice.textContent = message;
68
+ }
69
+ });
70
+ document.addEventListener("trix-change", function(event) {
71
+ var notice = event.target && event.target.nextElementSibling;
72
+ if (notice && notice.classList.contains("inline_forms-trix-refused")) { notice.remove(); }
73
+ });
74
+
49
75
  // get rid of translation_missing tooltips
50
76
  document.addEventListener("mouseover", function(event) {
51
77
  var el = event.target.closest && event.target.closest(".translation_missing");
@@ -588,3 +588,9 @@ trix-toolbar .trix-button-row.trix-button-row {
588
588
  trix-toolbar:has(+ trix-editor.inline_forms-trix-no-files) .trix-button-group--file-tools {
589
589
  display: none;
590
590
  }
591
+ // The notice inline_forms.js shows under it when a file is dropped or pasted.
592
+ .inline_forms-trix-refused {
593
+ margin: 0.25rem 0 0;
594
+ font-size: 0.875rem;
595
+ font-style: italic;
596
+ }
@@ -1,18 +1,24 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # The JSON behind a search field's suggestions (suggest_field_tag +
4
- # inline_forms/datalist_suggest.js). In a controller that inherits from
5
- # InlineFormsController:
4
+ # inline_forms/datalist_suggest.js). InlineFormsController includes it; any
5
+ # other controller may include it too. suggest_names and
6
+ # inline_forms_suggestion_scope / inline_forms_readable_columns ask
7
+ # `cancan_enabled?`: outside InlineFormsController include CancanConcern
8
+ # as well (render_inline_forms_suggestions alone needs nothing):
6
9
  #
7
10
  # suggest_names :client, :last_name # term anywhere in the name
8
11
  # suggest_names :client, :first_name, match: :start # only at the start
9
12
  # suggest_names :member, :last_name, scope: ->(members) { members.where(active: true) }
13
+ # suggest_names :document, :subject, scope: :incoming, order: :relation # newest first
10
14
  #
11
15
  # defines the action `autocomplete_<model>_<column>` (the name
12
16
  # rails-jquery-autocomplete's `autocomplete` macro used), which answers
13
17
  # GET …?term=… with a JSON array of at most `limit` (12) names: see
14
18
  # InlineForms::Suggestions for `match:` (:contains, :start, :word) and the
15
- # order. The app adds the route and the ability, as for any other action:
19
+ # order. The app adds the route and the ability, as for any other action
20
+ # (outside InlineFormsController, whose load_and_authorize_resource checks
21
+ # it, the app authorizes the action itself):
16
22
  #
17
23
  # resources :clients do
18
24
  # get :autocomplete_client_last_name, on: :collection
@@ -21,21 +27,42 @@
21
27
  #
22
28
  # Only names of records the user may read (`accessible_by`), and only from a
23
29
  # column the user may read (`can?(:read, Model, :column)`), when CanCan is
24
- # on; `scope:` narrows them further, run in the controller (instance_exec)
25
- # with the relation, and returns a relation.
30
+ # on; `scope:` narrows them further: a named scope (Symbol), or a lambda run
31
+ # in the controller (instance_exec) with the relation; either returns a
32
+ # relation. `order: :relation` keeps that relation's order (8.1.86).
33
+ #
34
+ # For an action of your own, three private helpers are supported API
35
+ # (Papiamentu, RACVS and yavetracker build on them):
36
+ #
37
+ # inline_forms_suggestion_scope(model) the records the user may read
38
+ # inline_forms_readable_columns(model, columns) the columns the user may read
39
+ # render_inline_forms_suggestions(values) the JSON answer
40
+ #
41
+ # def autocomplete_word_name
42
+ # names = inline_forms_suggestion_scope(Word).where(language: current_language)
43
+ # render_inline_forms_suggestions InlineForms::Suggestions.values(
44
+ # names, inline_forms_readable_columns(Word, [ :name ]), params[:term], match: :start)
45
+ # end
26
46
  module SuggestionsConcern
27
47
  extend ActiveSupport::Concern
28
48
 
29
49
  class_methods do
30
- def suggest_names(model_name, column, match: :contains, limit: InlineForms::Suggestions::LIMIT, scope: nil)
50
+ def suggest_names(model_name, column, match: :contains, limit: InlineForms::Suggestions::LIMIT, scope: nil,
51
+ order: :alphabetical)
31
52
  match = InlineForms::Suggestions.check_match!(match)
53
+ order = InlineForms::Suggestions.check_order!(order)
32
54
  InlineForms::Suggestions.check_limit!(limit)
55
+ unless scope.nil? || scope.respond_to?(:call) || scope.is_a?(Symbol)
56
+ raise ArgumentError, "scope: must be a Symbol (a named scope) or a lambda, not #{scope.inspect}"
57
+ end
58
+
33
59
  define_method("autocomplete_#{model_name}_#{column}") do
34
60
  model = model_name.to_s.camelize.constantize
35
61
  names = inline_forms_suggestion_scope(model)
36
- names = instance_exec(names, &scope) if scope
62
+ names = inline_forms_apply_suggestion_scope(names, scope) if scope
37
63
  render_inline_forms_suggestions InlineForms::Suggestions.values(
38
- names, inline_forms_readable_columns(model, [ column ]), params[:term], match: match, limit: limit
64
+ names, inline_forms_readable_columns(model, [ column ]), params[:term],
65
+ match: match, limit: limit, order: order
39
66
  )
40
67
  end
41
68
  end
@@ -43,6 +70,15 @@ module SuggestionsConcern
43
70
 
44
71
  private
45
72
 
73
+ # scope: a Symbol is a named scope of the model (`:incoming`), a lambda runs
74
+ # in the controller with the relation. Either must give a relation.
75
+ def inline_forms_apply_suggestion_scope(names, scope)
76
+ scoped = scope.is_a?(Symbol) ? names.public_send(scope) : instance_exec(names, &scope)
77
+ return scoped if scoped.is_a?(ActiveRecord::Relation)
78
+
79
+ raise ArgumentError, "suggest_names scope: #{scope.inspect} gave #{scoped.class}, not a relation"
80
+ end
81
+
46
82
  def inline_forms_suggestion_scope(model)
47
83
  cancan_enabled? ? model.accessible_by(current_ability) : model.all
48
84
  end
@@ -150,7 +150,7 @@ class InlineFormsController < ApplicationController
150
150
  @form_element = params[:form_element]
151
151
  @sub_id = params[:sub_id]
152
152
  @update_span = params[:update]
153
- return true unless field_request_permitted?(:update)
153
+ return true unless field_request_permitted?(:update, "edit")
154
154
  return true unless file_replace_permitted?
155
155
  respond_to do |format|
156
156
  format.html { render_turbo_field(:field_edit) }
@@ -229,7 +229,7 @@ class InlineFormsController < ApplicationController
229
229
  @form_element = params[:form_element]
230
230
  @sub_id = params[:sub_id]
231
231
  @update_span = params[:update]
232
- return true unless field_request_permitted?(:update)
232
+ return true unless field_request_permitted?(:update, "update")
233
233
  return true unless file_replace_permitted?
234
234
  # Defense-in-depth: the UI renders a pending field as a read-only
235
235
  # placeholder (never links to edit/update), but a hand-crafted request
@@ -282,7 +282,7 @@ class InlineFormsController < ApplicationController
282
282
  close = params[:close] || false
283
283
  # Single-field show (cancel link, post-update render): same whitelist as
284
284
  # edit/update.
285
- return true if @attribute && !field_request_permitted?(:read)
285
+ return true if @attribute && !field_request_permitted?(:read, "show")
286
286
  @update_span = params[:update]
287
287
  if @attribute.nil?
288
288
  respond_to do |format|
@@ -478,11 +478,14 @@ class InlineFormsController < ApplicationController
478
478
  # are lists, not fields, and have no `<element>_show` (a listed :associated
479
479
  # row requested through show raised NoMethodError, a 500). A delegated name (form_element=rich_text on a
480
480
  # :text_area row) only counts while the row's element is the engine's own.
481
- def field_request_permitted?(action)
481
+ # +helper+ is the helper the calling engine action uses (show, edit,
482
+ # update), passed by that action rather than read from action_name, so an
483
+ # app action of another name that calls super is checked for the right one.
484
+ def field_request_permitted?(action, helper)
482
485
  attributes = @inline_forms_attribute_list || inline_forms_attribute_list_of!(@object)
483
486
  row = InlineForms.attribute_list_row_for(attributes, @attribute, @form_element,
484
487
  delegate_allowed: ->(declared) { engine_form_element?(declared) })
485
- unless row && form_element_helper?(@form_element, FIELD_REQUEST_HELPER_SUFFIX.fetch(action_name, "show"))
488
+ unless row && form_element_helper?(@form_element, helper)
486
489
  head :bad_request
487
490
  return false
488
491
  end
@@ -490,8 +493,6 @@ class InlineFormsController < ApplicationController
490
493
  true
491
494
  end
492
495
 
493
- FIELD_REQUEST_HELPER_SUFFIX = { "edit" => "edit", "update" => "update", "show" => "show" }.freeze
494
- private_constant :FIELD_REQUEST_HELPER_SUFFIX
495
496
 
496
497
  # The record's attribute list, or an error that names the model. Field
497
498
  # requests are checked against it (8.1.76); a model whose rows live only in
@@ -404,22 +404,32 @@ module InlineFormsHelper
404
404
  values.to_a.each do |k, v|
405
405
  temp << [ k, inline_forms_value_label(v) ]
406
406
  end
407
- values = temp.sort { |a, b| inline_forms_compare_values(a[0], b[0]) }
407
+ values = inline_forms_sort_values(temp) { |pair| pair[0] }
408
408
  else
409
409
  temp = Array.new
410
410
  values.to_a.each_index do |i|
411
411
  temp << [ i, inline_forms_value_label(values.to_a[i]) ]
412
412
  end
413
- values = temp.sort { |a, b| inline_forms_compare_values(a[1], b[1]) }
413
+ values = inline_forms_sort_values(temp) { |pair| pair[1] }
414
414
  end
415
415
  values
416
416
  end
417
417
 
418
- # Numbers in numeric order, anything else as text: a list that mixes
419
- # numbers and strings ([1, 2, "many"]) raised ArgumentError in 8.1.83,
420
- # once labels stopped being the strings t() returned.
421
- def inline_forms_compare_values(a, b)
422
- (a <=> b) || (a.to_s <=> b.to_s)
418
+ # nil first (a blank option), then numbers in numeric order, then anything
419
+ # else as text, equal ones in their given order. One key for every value: a pairwise fallback
420
+ # ((a <=> b) || (a.to_s <=> b.to_s), 8.1.85) contradicted itself on a mixed
421
+ # list ([10, 9, "1a"] put 10 before 9). A list that mixes numbers and
422
+ # strings ([1, 2, "many"]) raised ArgumentError in 8.1.83, once labels
423
+ # stopped being the strings t() returned.
424
+ def inline_forms_sort_values(pairs)
425
+ pairs.each_with_index.sort_by { |pair, index| [ inline_forms_value_sort_key(yield(pair)), index ] }.map(&:first)
426
+ end
427
+
428
+ def inline_forms_value_sort_key(value)
429
+ if value.nil? then [ 0, 0, "" ]
430
+ elsif value.is_a?(Numeric) && value.real? then [ 1, value, "" ]
431
+ else [ 2, 0, value.to_s ]
432
+ end
423
433
  end
424
434
 
425
435
  # A value as text for the screen: a Date as dd-mm-yyyy (since Rails 7.1 a
data/archived/README.md CHANGED
@@ -15,7 +15,7 @@ This directory is the **versioned archive** for inline_forms capabilities that a
15
15
  | `:question_list` | **Archived** | **8.1.30** | [`form_elements/question_list/`](form_elements/question_list/) — hardcoded host `Question` model |
16
16
  | `:dnsrecords` | **Archived** | **8.1.30** | [`form_elements/dns_records/`](form_elements/dns_records/) — host `a_records`/`djbdns_line` APIs |
17
17
 
18
- Programmatic registry: `InlineForms::ARCHIVED_FORM_ELEMENTS` in `lib/inline_forms.rb` (boot-time check if a model still declares an archived symbol).
18
+ Programmatic registry: `InlineForms::ARCHIVED_FORM_ELEMENTS` in `lib/inline_forms.rb` (checked on every code reload in development, after the app's own `to_prepare` blocks, if a model still declares an archived symbol; since 8.1.85 not at boot).
19
19
 
20
20
  ## Layout
21
21
 
@@ -47,9 +47,20 @@ has one) in a module, plus the models, controllers, views, routes and assets
47
47
  its README lists. The views call `_show` and `_edit`, but the save calls
48
48
  `_update` on the controller, which does not get the app's helpers. So include
49
49
  the module into both, in a `to_prepare` (both are reloadable engine
50
- constants):
50
+ constants). Make its methods **private**: a public method of a module
51
+ included into a controller is an action, and since 8.1.83 the engine's own
52
+ form element helpers are not.
51
53
 
52
54
  ```ruby
55
+ # lib/form_elements/geo_code_curacao.rb
56
+ module GeoCodeCuracaoHelper
57
+ private
58
+
59
+ def geo_code_curacao_show(object, attribute) = …
60
+ def geo_code_curacao_edit(object, attribute) = …
61
+ def geo_code_curacao_update(object, attribute) = …
62
+ end
63
+
53
64
  # config/initializers/inline_forms_vendored_form_elements.rb
54
65
  require Rails.root.join("lib/form_elements/geo_code_curacao").to_s
55
66
 
@@ -59,11 +70,13 @@ end
59
70
  ```
60
71
 
61
72
  A module only under `app/helpers/` shows and edits, but every save is refused
62
- (400). Since 8.1.81 the boot check
73
+ (400). Since 8.1.81 the archived-element check
63
74
  (`InlineForms.validate_no_archived_form_elements_for!`) skips an archived
64
75
  symbol when the app's helpers (`ApplicationController.helpers`) answer
65
- `<name>_show`, so the element is then the app's. Since 8.1.85 that check runs
66
- after the app's own `to_prepare` blocks, so the include above is seen. The archived files are
76
+ `<name>_show`, private or not (8.1.84), so the element is then the app's.
77
+ Since 8.1.85 that check runs on each code reload in development, after the
78
+ app's own `to_prepare` blocks, so the include above is seen; it no longer
79
+ runs at boot. The archived files are
67
80
  written in the old top-level `def` style; wrap them in a helper module
68
81
  (`module GeoCodeCuracaoHelper … end`) and replace any jQuery/UJS they use.
69
82
 
@@ -7,19 +7,27 @@ module InlineForms
7
7
 
8
8
  def rich_text_show(object, attribute)
9
9
  rich = object.respond_to?(attribute) ? object.public_send(attribute) : nil
10
- is_blank =
11
- rich.nil? ||
12
- (rich.respond_to?(:blank?) && rich.blank?) ||
13
- (rich.respond_to?(:to_plain_text) && rich.to_plain_text.to_s.strip.empty?)
14
10
  # A plain column's HTML goes through the display allowlist; Action Text
15
- # sanitizes its own (InlineForms.rich_text_display_html).
11
+ # sanitizes its own (InlineForms.rich_text_display_html). Blank is
12
+ # decided on the TEXT of what is shown: a value without any text gets
13
+ # the plus, or the link could be empty and the field could not be
14
+ # opened. That is wider than what the allowlist empties (only an <img>,
15
+ # a <script>, an empty <p>): markup that shows something but holds no
16
+ # text, such as a lone <hr> or a table of empty cells, gets the plus
17
+ # too, and the plus opens the editor with that markup in it.
18
+ shown = rich.is_a?(String) ? InlineForms.rich_text_display_html(rich) : rich
19
+ is_blank =
20
+ shown.nil? ||
21
+ (shown.respond_to?(:blank?) && shown.blank?) ||
22
+ (shown.respond_to?(:to_plain_text) && shown.to_plain_text.to_s.strip.empty?) ||
23
+ (shown.is_a?(String) && strip_tags(shown).gsub(/[[:space:]]|&nbsp;/, "").empty?)
16
24
  display_value =
17
25
  if is_blank
18
26
  "<i class='fi-plus'></i>".html_safe
19
- elsif rich.is_a?(String)
20
- InlineForms.rich_text_display_html(rich)
27
+ elsif shown.is_a?(String)
28
+ shown
21
29
  else
22
- rich.to_s.html_safe
30
+ shown.to_s.html_safe
23
31
  end
24
32
  link_to_inline_edit object, attribute, display_value, from_callee: __callee__
25
33
  end
@@ -36,11 +44,16 @@ module InlineForms
36
44
  end
37
45
  input_id = "trix_input_#{object.class.name.underscore}_#{object.id || 'new'}_#{attribute}"
38
46
  # A plain column (no has_rich_text) cannot hold attachments: the class
39
- # hides Trix's attach button there.
47
+ # hides Trix's attach button there, and inline_forms.js refuses a
48
+ # dropped or pasted file and shows the message in the data attribute.
40
49
  classes = 'trix-content attribute_rich_text_area'
41
- classes += ' inline_forms-trix-no-files' unless rich.respond_to?(:body)
50
+ data = {}
51
+ unless rich.respond_to?(:body)
52
+ classes += ' inline_forms-trix-no-files'
53
+ data[:no_files_message] = t('inline_forms.files.not_in_this_field')
54
+ end
42
55
  hidden_field_tag(attribute, body, id: input_id) +
43
- content_tag(:'trix-editor', ''.html_safe, input: input_id, class: classes)
56
+ content_tag(:'trix-editor', ''.html_safe, input: input_id, class: classes, data: data)
44
57
  end
45
58
 
46
59
  def rich_text_update(object, attribute)
@@ -17,10 +17,19 @@ module InlineForms
17
17
  # ("marc" finds Jean-Marc, "brien" O'Brien and O’Brien)
18
18
  #
19
19
  # The answer: at most `limit` values, each once (compared stripped and in any
20
- # case, the first in A-Z order kept), in case-insensitive A-Z order. An empty
21
- # term gives [].
20
+ # case), in the order `order:` names. An empty term gives [].
21
+ #
22
+ # :alphabetical case-insensitive A-Z, the first spelling in that order kept
23
+ # (the default)
24
+ # :relation the relation's own order (a default_scope's, a scope's:
25
+ # documents newest first), LOWER(value) A-Z after it; the
26
+ # first record's spelling kept (8.1.86). It reads at most
27
+ # PAGES pages of 4 × limit matching rows per column (480 at
28
+ # the default limit), one query each, and stops there: a
29
+ # value that fills them hides the values after it.
22
30
  module Suggestions
23
31
  MATCHES = %i[contains start word].freeze
32
+ ORDERS = %i[alphabetical relation].freeze
24
33
  # What starts a word for match: :word, besides the start of the value.
25
34
  WORD_STARTS = [ " ", "-", "'", "’" ].freeze
26
35
  LIMIT = 12
@@ -35,6 +44,14 @@ module InlineForms
35
44
  raise ArgumentError, "match: must be one of #{MATCHES.map(&:inspect).join(', ')}, not #{match.inspect}"
36
45
  end
37
46
 
47
+ # Raises ArgumentError for an unknown order; returns it as a Symbol.
48
+ def check_order!(order)
49
+ order = order.to_sym
50
+ return order if ORDERS.include?(order)
51
+
52
+ raise ArgumentError, "order: must be one of #{ORDERS.map(&:inspect).join(', ')}, not #{order.inspect}"
53
+ end
54
+
38
55
  def check_limit!(limit)
39
56
  return limit if limit.is_a?(Integer) && limit.positive?
40
57
 
@@ -42,13 +59,20 @@ module InlineForms
42
59
  end
43
60
 
44
61
  # relation: what the user may see (already `accessible_by`, scoped, …).
45
- def values(relation, columns, term, match: :contains, limit: LIMIT)
62
+ def values(relation, columns, term, match: :contains, limit: LIMIT, order: :alphabetical)
46
63
  match = check_match!(match)
47
64
  limit = check_limit!(limit)
65
+ order = check_order!(order)
48
66
  term = term.to_s.strip
49
67
  return [] if term.empty?
50
68
 
51
69
  columns = Array(columns)
70
+ if order == :relation
71
+ # Column after column, each in the relation's order.
72
+ found = columns.flat_map { |column| relation_ordered_values(relation, column, term, match, limit) }
73
+ return found.uniq(&:downcase).first(limit)
74
+ end
75
+
52
76
  found = columns.flat_map { |column| column_values(relation, column, term, match, limit) }
53
77
  found = found.map(&:to_s).reject(&:empty?)
54
78
  # One column: the database's order (its collation, e.g. accents with
@@ -57,6 +81,38 @@ module InlineForms
57
81
  found.uniq(&:downcase).first(limit)
58
82
  end
59
83
 
84
+ # The relation's own order kept, so no GROUP BY (its ORDER BY columns
85
+ # would have to be grouped or aggregated): rows in that order, LOWER(value)
86
+ # A-Z and the primary key after it, a page at a time, each value once (stripped, any case),
87
+ # until `limit` values or PAGES pages.
88
+ PAGES = 10
89
+
90
+ def relation_ordered_values(relation, column, term, match, limit)
91
+ model = relation.model
92
+ attribute = model.arel_table[column.to_s]
93
+ lowered = Arel::Nodes::NamedFunction.new("LOWER", [ attribute ])
94
+ condition = patterns(model, term, match).map { |pattern|
95
+ lowered.matches(Arel::Nodes::NamedFunction.new("LOWER", [ Arel::Nodes.build_quoted(pattern) ]), "\\", true)
96
+ }.reduce(:or)
97
+ # The primary key last, so rows equal on everything before it keep one
98
+ # order from page to page (none would be read twice or skipped at a
99
+ # page boundary: MySQL and PostgreSQL may order ties differently in
100
+ # each query).
101
+ tiebreak = Array(model.primary_key).map { |key| model.arel_table[key].asc }
102
+ rows = relation.where(condition).unscope(:select).order(lowered.asc, *tiebreak)
103
+ page_size = limit * 4
104
+ found = {}
105
+ PAGES.times do |page|
106
+ batch = rows.offset(page * page_size).limit(page_size).pluck(attribute)
107
+ batch.each do |value|
108
+ value = value.to_s.strip
109
+ found[value.downcase] ||= value unless value.empty?
110
+ end
111
+ break if found.size >= limit || batch.size < page_size
112
+ end
113
+ found.values.first(limit)
114
+ end
115
+
60
116
  # SELECT MIN(TRIM(value)) … GROUP BY LOWER(TRIM(value)) ORDER BY that:
61
117
  # one row per value as the user sees it, so the limit counts distinct
62
118
  # names ('Adriana', 'adriana ' and 'Adriana' are one). A plain DISTINCT
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
3
  module InlineForms
4
- VERSION = "8.1.85"
4
+ VERSION = "8.1.88"
5
5
  end
data/lib/inline_forms.rb CHANGED
@@ -38,20 +38,41 @@ module InlineForms
38
38
  # expression()); href keeps only safe protocols. The stored value is not
39
39
  # changed. An Action Text value is not passed through here: Action Text
40
40
  # sanitizes it itself.
41
+ #
42
+ # Since 8.1.86 also CKEditor's <font> with color, face and size (RACVS has
43
+ # tens of thousands of values with it), and kbd, samp, tt, var.
41
44
  RICH_TEXT_DISPLAY_TAGS = %w[
42
- a b strong i em u s strike del ins sub sup small big
43
- p div span br hr blockquote pre code
45
+ a b strong i em u s strike del ins sub sup small big font
46
+ p div span br hr blockquote pre code kbd samp tt var
44
47
  h1 h2 h3 h4 h5 h6 ul ol li dl dt dd
45
48
  table thead tbody tfoot tr th td caption colgroup col
46
49
  figure figcaption
47
50
  ].freeze
48
51
  RICH_TEXT_DISPLAY_ATTRIBUTES = %w[
49
52
  href title style align valign width border cellpadding cellspacing colspan rowspan
53
+ color face size
50
54
  ].freeze
51
55
 
56
+ # One sanitizer for every call (a list page calls this once per row); it
57
+ # keeps no state between calls.
58
+ def self.rich_text_display_sanitizer
59
+ @rich_text_display_sanitizer ||= Rails::HTML::Sanitizer.best_supported_vendor.safe_list_sanitizer.new
60
+ end
61
+
62
+ # Elements whose content is not text for the reader: dropped with what is
63
+ # in them. The allowlist alone strips a tag and keeps its content, so a
64
+ # stored <script>alert(1)</script> showed as the text "alert(1)".
65
+ RICH_TEXT_DISPLAY_DROPPED = %w[script style template noscript].freeze
66
+
52
67
  def self.rich_text_display_html(html)
53
- Rails::HTML::Sanitizer.best_supported_vendor.safe_list_sanitizer.new.sanitize(
54
- html.to_s, tags: RICH_TEXT_DISPLAY_TAGS, attributes: RICH_TEXT_DISPLAY_ATTRIBUTES
68
+ html = html.to_s
69
+ if RICH_TEXT_DISPLAY_DROPPED.any? { |tag| html.match?(/<#{tag}\b/i) }
70
+ fragment = Rails::HTML::Sanitizer.html5_support? ? Loofah.html5_fragment(html) : Loofah.fragment(html)
71
+ fragment.css(RICH_TEXT_DISPLAY_DROPPED.join(",")).each(&:remove)
72
+ html = fragment.to_html
73
+ end
74
+ rich_text_display_sanitizer.sanitize(
75
+ html, tags: RICH_TEXT_DISPLAY_TAGS, attributes: RICH_TEXT_DISPLAY_ATTRIBUTES
55
76
  ).to_s.html_safe
56
77
  end
57
78
 
@@ -68,6 +68,7 @@ en:
68
68
  undo: undo
69
69
  restore_previous: "restore removed file %{filename}"
70
70
  replace_hint: "The current file %{filename} goes to the trash."
71
+ not_in_this_field: "Files cannot be added to this field."
71
72
  confirm_remove:
72
73
  one: "Remove %{filename}? It goes to the trash and can be restored for 1 day."
73
74
  other: "Remove %{filename}? It goes to the trash and can be restored for %{count} days."
@@ -68,6 +68,7 @@ nl:
68
68
  undo: ongedaan maken
69
69
  restore_previous: "verwijderd bestand %{filename} terugzetten"
70
70
  replace_hint: "Het huidige bestand %{filename} gaat naar de prullenbak."
71
+ not_in_this_field: "In dit veld kunnen geen bestanden worden opgenomen."
71
72
  confirm_remove:
72
73
  one: "%{filename} verwijderen? Het gaat naar de prullenbak en kan 1 dag worden teruggezet."
73
74
  other: "%{filename} verwijderen? Het gaat naar de prullenbak en kan %{count} dagen worden teruggezet."
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: inline_forms
3
3
  version: !ruby/object:Gem::Version
4
- version: 8.1.85
4
+ version: 8.1.88
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ace Suares