inline_forms 8.1.84 → 8.1.86

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: 1d75751950448c427cecf87e64fe3e4dc6c59b3801ebe8724a15312d26c2a34b
4
- data.tar.gz: 6c35978ef33487025fec7cfaa4cbc7d6b52e27c03098b2150d3e278c40d614f1
3
+ metadata.gz: 9a3ff73bf6f164c1a0cd0ba6ebf4f421a66e9c4f2bcbef55fda82a1af0503ef1
4
+ data.tar.gz: a594714d3d774683f6eeaa5b83a57e89caefee3e88859b3e2b793a7d0f2cf0cf
5
5
  SHA512:
6
- metadata.gz: 9afef54387ecf3129c658133c00efdd78a7c1ea35b22ff54c59dd19b26824a87c92d48f79061c9409f2b2abe3ab74ee75c471b6ad7ac3699732047afcf0503dc
7
- data.tar.gz: d8379842ec2f9e052ba7984921457598d45b05b596d779dd31f6df3883f3ccdfb2dbdf9859a9028364ea0c08b70421f1d30f66a33b8069b584b6f886fac84096
6
+ metadata.gz: 5568cb9e4741533c92e0066b8a02d2ec44f6e570bcefc0e46a594ffd2fd57c7be11b23f36949a98afdc0d950629379c66beb0846f91d926f9cfc8cdb8e96ba32
7
+ data.tar.gz: 1ca435ac4f92b5079176ceda91fd48374e25ba5f09324ba1df0d214c0ef6b877297fc3c05dc0138a0d309dadc3b60534ed72b2386529f840390d9b9e2cb5e349
data/CHANGELOG.md CHANGED
@@ -8,6 +8,51 @@ 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.86] - 2026-11-25
12
+
13
+ From the third RACVS / yavetracker ladder prompt (`ladder/inline_forms-PROMPT-3-2026-11-25.md`).
14
+
15
+ ### Added
16
+
17
+ - **`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.
18
+ - **`suggest_names … scope: :incoming`**: a named scope as a Symbol, besides a lambda. Either must give a relation (ArgumentError otherwise).
19
+
20
+ ### Fixed
21
+
22
+ - **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.
23
+ - **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.
24
+ - **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).
25
+ - **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.
26
+ - **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`.
27
+ - The display sanitizer is built once, not once per value.
28
+
29
+ ### Documentation
30
+
31
+ - 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`.
32
+
33
+ ## [8.1.85] - 2026-11-25
34
+
35
+ 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`).
36
+
37
+ ### Security
38
+
39
+ - **`form_element=rich_text` on a `:text_area` row is refused when the app overrides any of `text_area_show`, `_edit` or `_update`**, in its helpers or on the controller. 8.1.84 looked only at `text_area_show` in the view helpers, so an app that replaced only `text_area_update` (where its checks are) or included its override only into the controller was still sent around to the engine's `rich_text_update`, and the raw HTML was saved. The same holds for `:plain_text_area` delegating to `:plain_text`. This corrects 8.1.84's Security note, which said an app's own `text_area_*` helpers were covered. No host overrides these.
40
+
41
+ ### Fixed
42
+
43
+ - **The archived-element check runs after the app's own `to_prepare` blocks.** It was the engine's `config.to_prepare`, which runs before the application's, so on a reload in development it did not yet see a vendored element's helpers that the app includes into `InlineFormsHelper` in its own `to_prepare` (yavetracker's `geo_code_curacao`). It is now registered on the app's reloader from `after_initialize` (`InlineForms.validate_loaded_models!`), after the app's blocks. The boot's own first prepare, which ran before eager loading and saw almost no models, is no longer checked; every reload is, as before. This is what 8.1.84's "yavetracker's boot exception can go now" needed: the private-helper fix alone did not change the order.
44
+ - **A values list that mixes numbers and strings works again.** Since 8.1.83 kept numbers as numbers, `[1, 2, "many"]` (or a `nil` entry) raised `ArgumentError: comparison of Array with Array failed` in every element that lists the values (dropdown_with_values, radio_button, scale, slider, check_box, dropdown_with_integers). Numbers now sort as numbers and anything else as text.
45
+
46
+ ### Documentation
47
+
48
+ - **Vendoring an archived element:** `archived/README.md` and the boot check's hint said to put `<name>_update` in an app helper. The save calls it on the controller, which does not get the app's helpers, so such a field showed and edited but every save was refused (400; a 500 before 8.1.84). Include the module into both `InlineFormsHelper` and `InlineFormsController` in a `to_prepare`, as the README now shows (yavetracker already does).
49
+ - **8.1.83, not mentioned then:** a values list given as an integer Range (`0..12`) is in numeric order since 8.1.83 (0, 1, 2 … 12; it was 0, 1, 10, 11, 12, 2 …). yavetracker's `0..40`, `0..7` and `1..8` dropdowns change order.
50
+ - README: the tabs section's `+link_options+` renders as code again.
51
+
52
+ ### Tests
53
+
54
+ - The engine header's logout link is a Turbo DELETE; an `update` override written `super and return` stops after a refusal; `MissingAttributeListError` on a field's show and save, not only on edit.
55
+
11
56
  ## [8.1.84] - 2026-11-24
12
57
 
13
58
  From the second RACVS / yavetracker ladder prompt (`ladder/inline_forms-PROMPT-2-2026-11-24.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: +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.
266
266
 
267
267
  == Generated application +rails-i18n+
268
268
 
@@ -339,6 +339,40 @@ Note: generated apps also depend on ActiveStorage transitively because the +:ric
339
339
 
340
340
  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
341
 
342
+ == Search suggestions (+suggest_field_tag+, +suggest_names+)
343
+
344
+ 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):
345
+
346
+ <%= suggest_field_tag "search", params[:search], autocomplete_client_last_name_clients_path, placeholder: "last name" %>
347
+
348
+ 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).
349
+
350
+ class ClientsController < InlineFormsController
351
+ suggest_names :client, :last_name # the term anywhere, A-Z
352
+ suggest_names :client, :first_name, match: :start # :contains, :start or :word
353
+ suggest_names :document, :subject, scope: :incoming, # a named scope, or a lambda
354
+ order: :relation # the scope's order, not A-Z
355
+ end
356
+
357
+ 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:
358
+
359
+ resources :clients do
360
+ get :autocomplete_client_last_name, on: :collection
361
+ end
362
+ can :autocomplete_client_last_name, Client
363
+
364
+ Only values of records the user may read (+accessible_by+) and of a column the user may read (<tt>can?(:read, Model, :column)</tt>). +order: :relation+ (8.1.86) keeps the relation's own order (a +default_scope+, or a scope that orders documents newest first) instead of A-Z.
365
+
366
+ An action of your own uses the same private helpers, which are supported API:
367
+
368
+ def autocomplete_word_name
369
+ words = inline_forms_suggestion_scope(Word).where(language: current_language) # records the user may read
370
+ columns = inline_forms_readable_columns(Word, [ :name ]) # columns the user may read
371
+ render_inline_forms_suggestions InlineForms::Suggestions.values(words, columns, params[:term], match: :start)
372
+ end
373
+
374
+ 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>.
375
+
342
376
  == Per-user themes, colors and locale
343
377
 
344
378
  Generated apps let each user pick, on their own user panel:
@@ -46,6 +46,16 @@ 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.
52
+ document.addEventListener("trix-file-accept", function(event) {
53
+ var editor = event.target;
54
+ if (editor && editor.classList && editor.classList.contains("inline_forms-trix-no-files")) {
55
+ event.preventDefault();
56
+ }
57
+ });
58
+
49
59
  // get rid of translation_missing tooltips
50
60
  document.addEventListener("mouseover", function(event) {
51
61
  var el = event.target.closest && event.target.closest(".translation_missing");
@@ -1,12 +1,16 @@
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
@@ -21,21 +25,42 @@
21
25
  #
22
26
  # Only names of records the user may read (`accessible_by`), and only from a
23
27
  # 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.
28
+ # on; `scope:` narrows them further: a named scope (Symbol), or a lambda run
29
+ # in the controller (instance_exec) with the relation; either returns a
30
+ # relation. `order: :relation` keeps that relation's order (8.1.86).
31
+ #
32
+ # For an action of your own, three private helpers are supported API
33
+ # (Papiamentu, RACVS and yavetracker build on them):
34
+ #
35
+ # inline_forms_suggestion_scope(model) the records the user may read
36
+ # inline_forms_readable_columns(model, columns) the columns the user may read
37
+ # render_inline_forms_suggestions(values) the JSON answer
38
+ #
39
+ # def autocomplete_word_name
40
+ # names = inline_forms_suggestion_scope(Word).where(language: current_language)
41
+ # render_inline_forms_suggestions InlineForms::Suggestions.values(
42
+ # names, inline_forms_readable_columns(Word, [ :name ]), params[:term], match: :start)
43
+ # end
26
44
  module SuggestionsConcern
27
45
  extend ActiveSupport::Concern
28
46
 
29
47
  class_methods do
30
- def suggest_names(model_name, column, match: :contains, limit: InlineForms::Suggestions::LIMIT, scope: nil)
48
+ def suggest_names(model_name, column, match: :contains, limit: InlineForms::Suggestions::LIMIT, scope: nil,
49
+ order: :alphabetical)
31
50
  match = InlineForms::Suggestions.check_match!(match)
51
+ order = InlineForms::Suggestions.check_order!(order)
32
52
  InlineForms::Suggestions.check_limit!(limit)
53
+ unless scope.nil? || scope.respond_to?(:call) || scope.is_a?(Symbol)
54
+ raise ArgumentError, "scope: must be a Symbol (a named scope) or a lambda, not #{scope.inspect}"
55
+ end
56
+
33
57
  define_method("autocomplete_#{model_name}_#{column}") do
34
58
  model = model_name.to_s.camelize.constantize
35
59
  names = inline_forms_suggestion_scope(model)
36
- names = instance_exec(names, &scope) if scope
60
+ names = inline_forms_apply_suggestion_scope(names, scope) if scope
37
61
  render_inline_forms_suggestions InlineForms::Suggestions.values(
38
- names, inline_forms_readable_columns(model, [ column ]), params[:term], match: match, limit: limit
62
+ names, inline_forms_readable_columns(model, [ column ]), params[:term],
63
+ match: match, limit: limit, order: order
39
64
  )
40
65
  end
41
66
  end
@@ -43,6 +68,15 @@ module SuggestionsConcern
43
68
 
44
69
  private
45
70
 
71
+ # scope: a Symbol is a named scope of the model (`:incoming`), a lambda runs
72
+ # in the controller with the relation. Either must give a relation.
73
+ def inline_forms_apply_suggestion_scope(names, scope)
74
+ scoped = scope.is_a?(Symbol) ? names.public_send(scope) : instance_exec(names, &scope)
75
+ return scoped if scoped.is_a?(ActiveRecord::Relation)
76
+
77
+ raise ArgumentError, "suggest_names scope: #{scope.inspect} gave #{scoped.class}, not a relation"
78
+ end
79
+
46
80
  def inline_forms_suggestion_scope(model)
47
81
  cancan_enabled? ? model.accessible_by(current_ability) : model.all
48
82
  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
@@ -513,11 +514,24 @@ class InlineFormsController < ApplicationController
513
514
  suffix == "update" ? respond_to?(name, true) : helpers.respond_to?(name, true)
514
515
  end
515
516
 
516
- # The element's show helper is the engine's own, not an app's vendored copy
517
- # of the same name (InlineForms::FormElements::*).
517
+ # The element is the engine's own, not an app's copy of the same name:
518
+ # its _show, _edit and _update, in the view and on the controller, are all
519
+ # defined in the engine's lib/inline_forms/form_elements/. An app that
520
+ # overrides any one of them (only the _update with its own checks, say)
521
+ # keeps the delegate away. By file, not owner: `private` on an included
522
+ # helper (the controller makes them private, 8.1.83) gives a stub whose
523
+ # owner is InlineFormsController but whose source is still the engine's.
524
+ ENGINE_FORM_ELEMENTS_DIR = File.expand_path("../../lib/inline_forms/form_elements", __dir__) + File::SEPARATOR
525
+ private_constant :ENGINE_FORM_ELEMENTS_DIR
526
+
518
527
  def engine_form_element?(form_element)
519
- owner = helpers.method(:"#{form_element}_show").owner
520
- owner.name.to_s.start_with?("InlineForms::FormElements::")
528
+ %w[show edit update].all? do |suffix|
529
+ name = :"#{form_element}_#{suffix}"
530
+ [ helpers, self ].all? do |target|
531
+ !target.respond_to?(name, true) ||
532
+ target.method(name).source_location&.first.to_s.start_with?(ENGINE_FORM_ELEMENTS_DIR)
533
+ end
534
+ end
521
535
  rescue NameError
522
536
  false
523
537
  end
@@ -404,17 +404,24 @@ 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| a[0]<=>b[0] }
407
+ values = temp.sort { |a, b| inline_forms_compare_values(a[0], b[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| a[1]<=>b[1] }
413
+ values = temp.sort { |a, b| inline_forms_compare_values(a[1], b[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)
423
+ end
424
+
418
425
  # A value as text for the screen: a Date as dd-mm-yyyy (since Rails 7.1 a
419
426
  # bare Date prints ISO), a time as inline_forms_time_text, anything else
420
427
  # as it is. Used by the info elements and the versions panel.
data/archived/README.md CHANGED
@@ -43,11 +43,27 @@ Active form elements load from `app/helpers/form_elements/*.rb` only (top-level
43
43
 
44
44
  An app can keep using an archived element without changing the gem: put its
45
45
  helper methods (`<name>_show`, `<name>_edit`, `<name>_update`, and `_info` if it
46
- has one) in a module under the app's own `app/helpers/`, plus the models,
47
- controllers, views, routes and assets its README lists. Since 8.1.81 the boot
48
- check (`InlineForms.validate_no_archived_form_elements_for!`) skips an archived
46
+ has one) in a module, plus the models, controllers, views, routes and assets
47
+ its README lists. The views call `_show` and `_edit`, but the save calls
48
+ `_update` on the controller, which does not get the app's helpers. So include
49
+ the module into both, in a `to_prepare` (both are reloadable engine
50
+ constants):
51
+
52
+ ```ruby
53
+ # config/initializers/inline_forms_vendored_form_elements.rb
54
+ require Rails.root.join("lib/form_elements/geo_code_curacao").to_s
55
+
56
+ Rails.application.config.to_prepare do
57
+ [InlineFormsHelper, InlineFormsController].each { |target| target.include(GeoCodeCuracaoHelper) }
58
+ end
59
+ ```
60
+
61
+ A module only under `app/helpers/` shows and edits, but every save is refused
62
+ (400). Since 8.1.81 the boot check
63
+ (`InlineForms.validate_no_archived_form_elements_for!`) skips an archived
49
64
  symbol when the app's helpers (`ApplicationController.helpers`) answer
50
- `<name>_show`, so the element is then the app's. The archived files are
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
51
67
  written in the old top-level `def` style; wrap them in a helper module
52
68
  (`module GeoCodeCuracaoHelper … end`) and replace any jQuery/UJS they use.
53
69
 
@@ -85,7 +85,8 @@ module InlineForms
85
85
  meta = ARCHIVED_FORM_ELEMENTS[key]
86
86
  version = meta[:archived_in_version] || meta[:removed_in_version]
87
87
  hint = if meta[:archive_path]
88
- "Vendor it into your app: define #{key}_show (and _edit, _update) in an app helper " \
88
+ "Vendor it into your app: define #{key}_show, _edit and _update in a module and " \
89
+ "include it into InlineFormsHelper and InlineFormsController in a to_prepare " \
89
90
  "(#{meta[:archive_path]}/README.md, archived/README.md)."
90
91
  else
91
92
  "See archived/README.md and CHANGELOG #{version}."
@@ -7,19 +7,24 @@ 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 what is shown: a value the allowlist empties (only an
13
+ # <img>, a <script>, an empty <p>) gets the plus, or the link would be
14
+ # empty and the field could not be opened.
15
+ shown = rich.is_a?(String) ? InlineForms.rich_text_display_html(rich) : rich
16
+ is_blank =
17
+ shown.nil? ||
18
+ (shown.respond_to?(:blank?) && shown.blank?) ||
19
+ (shown.respond_to?(:to_plain_text) && shown.to_plain_text.to_s.strip.empty?) ||
20
+ (shown.is_a?(String) && strip_tags(shown).gsub(/[[:space:]]|&nbsp;/, "").empty?)
16
21
  display_value =
17
22
  if is_blank
18
23
  "<i class='fi-plus'></i>".html_safe
19
- elsif rich.is_a?(String)
20
- InlineForms.rich_text_display_html(rich)
24
+ elsif shown.is_a?(String)
25
+ shown
21
26
  else
22
- rich.to_s.html_safe
27
+ shown.to_s.html_safe
23
28
  end
24
29
  link_to_inline_edit object, attribute, display_value, from_callee: __callee__
25
30
  end
@@ -17,10 +17,16 @@ 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)
22
27
  module Suggestions
23
28
  MATCHES = %i[contains start word].freeze
29
+ ORDERS = %i[alphabetical relation].freeze
24
30
  # What starts a word for match: :word, besides the start of the value.
25
31
  WORD_STARTS = [ " ", "-", "'", "’" ].freeze
26
32
  LIMIT = 12
@@ -35,6 +41,14 @@ module InlineForms
35
41
  raise ArgumentError, "match: must be one of #{MATCHES.map(&:inspect).join(', ')}, not #{match.inspect}"
36
42
  end
37
43
 
44
+ # Raises ArgumentError for an unknown order; returns it as a Symbol.
45
+ def check_order!(order)
46
+ order = order.to_sym
47
+ return order if ORDERS.include?(order)
48
+
49
+ raise ArgumentError, "order: must be one of #{ORDERS.map(&:inspect).join(', ')}, not #{order.inspect}"
50
+ end
51
+
38
52
  def check_limit!(limit)
39
53
  return limit if limit.is_a?(Integer) && limit.positive?
40
54
 
@@ -42,13 +56,20 @@ module InlineForms
42
56
  end
43
57
 
44
58
  # relation: what the user may see (already `accessible_by`, scoped, …).
45
- def values(relation, columns, term, match: :contains, limit: LIMIT)
59
+ def values(relation, columns, term, match: :contains, limit: LIMIT, order: :alphabetical)
46
60
  match = check_match!(match)
47
61
  limit = check_limit!(limit)
62
+ order = check_order!(order)
48
63
  term = term.to_s.strip
49
64
  return [] if term.empty?
50
65
 
51
66
  columns = Array(columns)
67
+ if order == :relation
68
+ # Column after column, each in the relation's order.
69
+ found = columns.flat_map { |column| relation_ordered_values(relation, column, term, match, limit) }
70
+ return found.uniq(&:downcase).first(limit)
71
+ end
72
+
52
73
  found = columns.flat_map { |column| column_values(relation, column, term, match, limit) }
53
74
  found = found.map(&:to_s).reject(&:empty?)
54
75
  # One column: the database's order (its collation, e.g. accents with
@@ -57,6 +78,33 @@ module InlineForms
57
78
  found.uniq(&:downcase).first(limit)
58
79
  end
59
80
 
81
+ # The relation's own order kept, so no GROUP BY (its ORDER BY columns
82
+ # would have to be grouped or aggregated): rows in that order, LOWER(value)
83
+ # A-Z after it, a page at a time, each value once (stripped, any case),
84
+ # until `limit` values or PAGES pages.
85
+ PAGES = 10
86
+
87
+ def relation_ordered_values(relation, column, term, match, limit)
88
+ model = relation.model
89
+ attribute = model.arel_table[column.to_s]
90
+ lowered = Arel::Nodes::NamedFunction.new("LOWER", [ attribute ])
91
+ condition = patterns(model, term, match).map { |pattern|
92
+ lowered.matches(Arel::Nodes::NamedFunction.new("LOWER", [ Arel::Nodes.build_quoted(pattern) ]), "\\", true)
93
+ }.reduce(:or)
94
+ rows = relation.where(condition).unscope(:select).order(lowered.asc)
95
+ page_size = limit * 4
96
+ found = {}
97
+ PAGES.times do |page|
98
+ batch = rows.offset(page * page_size).limit(page_size).pluck(attribute)
99
+ batch.each do |value|
100
+ value = value.to_s.strip
101
+ found[value.downcase] ||= value unless value.empty?
102
+ end
103
+ break if found.size >= limit || batch.size < page_size
104
+ end
105
+ found.values.first(limit)
106
+ end
107
+
60
108
  # SELECT MIN(TRIM(value)) … GROUP BY LOWER(TRIM(value)) ORDER BY that:
61
109
  # one row per value as the user sees it, so the limit counts distinct
62
110
  # 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.84"
4
+ VERSION = "8.1.86"
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
 
@@ -339,6 +360,24 @@ module InlineForms
339
360
  attribute.to_s
340
361
  end
341
362
 
363
+ # Every loaded model's attribute list: no archived element the app has not
364
+ # vendored, and plain_text columns present. Run on every reload, after the
365
+ # app's own to_prepare blocks (see the engine).
366
+ def self.validate_loaded_models!
367
+ return unless defined?(ActiveRecord::Base)
368
+
369
+ ActiveRecord::Base.descendants.each do |klass|
370
+ validate_no_archived_form_elements_for!(klass)
371
+ validate_plain_text_configuration_for!(klass)
372
+ rescue PlainTextColumnMissingError, ArchivedFormElementError
373
+ raise
374
+ rescue StandardError
375
+ # Some descendants might be abstract or temporarily unresolved while
376
+ # the app boots/reloads; runtime checks in controllers still enforce
377
+ # plain_text column presence for active resources.
378
+ end
379
+ end
380
+
342
381
  def self.validate_plain_text_configuration_for!(klass)
343
382
  return unless klass.respond_to?(:table_exists?) &&
344
383
  klass.respond_to?(:column_names) &&
@@ -466,21 +505,18 @@ module InlineForms
466
505
  ]
467
506
  end
468
507
 
469
- config.to_prepare do
470
- next unless defined?(ActiveRecord::Base)
471
-
472
- ActiveRecord::Base.descendants.each do |klass|
473
- begin
474
- InlineForms.validate_no_archived_form_elements_for!(klass)
475
- InlineForms.validate_plain_text_configuration_for!(klass)
476
- rescue InlineForms::PlainTextColumnMissingError, InlineForms::ArchivedFormElementError
477
- raise
478
- rescue StandardError
479
- # Some descendants might be abstract or temporarily unresolved while
480
- # the app boots/reloads; runtime checks in controllers still enforce
481
- # plain_text column presence for active resources.
482
- end
483
- end
508
+ # The checks of the loaded models run after the app's own to_prepare
509
+ # blocks (8.1.85): an app that includes a vendored element's helpers into
510
+ # InlineFormsHelper in its config/initializers' to_prepare must have done
511
+ # so before the archived-element check asks for <name>_show. A plain
512
+ # config.to_prepare here ran first, before the app's (engine blocks are
513
+ # registered before the application's), so on a reload in development
514
+ # the check did not see the app's helper. Registered from after_initialize,
515
+ # so it lands after the app's blocks; it then runs on every reload, as
516
+ # before. (The boot's own first prepare, before eager loading, saw almost
517
+ # no models; it is no longer checked.)
518
+ config.after_initialize do |app|
519
+ app.reloader.to_prepare { InlineForms.validate_loaded_models! }
484
520
  end
485
521
 
486
522
  # .rb too: inline_forms_titles.rb holds the title keys as lambdas.
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.84
4
+ version: 8.1.86
5
5
  platform: ruby
6
6
  authors:
7
7
  - Ace Suares