inline_forms 8.1.85 → 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 +4 -4
- data/CHANGELOG.md +22 -0
- data/README.rdoc +34 -0
- data/app/assets/javascripts/inline_forms/inline_forms.js +10 -0
- data/app/controllers/concerns/suggestions_concern.rb +41 -7
- data/app/controllers/inline_forms_controller.rb +8 -7
- data/lib/inline_forms/form_elements/rich_text_helper.rb +13 -8
- data/lib/inline_forms/suggestions.rb +51 -3
- data/lib/inline_forms/version.rb +1 -1
- data/lib/inline_forms.rb +25 -4
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 9a3ff73bf6f164c1a0cd0ba6ebf4f421a66e9c4f2bcbef55fda82a1af0503ef1
|
|
4
|
+
data.tar.gz: a594714d3d774683f6eeaa5b83a57e89caefee3e88859b3e2b793a7d0f2cf0cf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 5568cb9e4741533c92e0066b8a02d2ec44f6e570bcefc0e46a594ffd2fd57c7be11b23f36949a98afdc0d950629379c66beb0846f91d926f9cfc8cdb8e96ba32
|
|
7
|
+
data.tar.gz: 1ca435ac4f92b5079176ceda91fd48374e25ba5f09324ba1df0d214c0ef6b877297fc3c05dc0138a0d309dadc3b60534ed72b2386529f840390d9b9e2cb5e349
|
data/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,28 @@ 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
|
+
|
|
11
33
|
## [8.1.85] - 2026-11-25
|
|
12
34
|
|
|
13
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`).
|
data/README.rdoc
CHANGED
|
@@ -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).
|
|
5
|
-
#
|
|
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
|
|
25
|
-
# with the 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 =
|
|
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],
|
|
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
|
-
|
|
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,
|
|
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
|
|
@@ -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:]]| /, "").empty?)
|
|
16
21
|
display_value =
|
|
17
22
|
if is_blank
|
|
18
23
|
"<i class='fi-plus'></i>".html_safe
|
|
19
|
-
elsif
|
|
20
|
-
|
|
24
|
+
elsif shown.is_a?(String)
|
|
25
|
+
shown
|
|
21
26
|
else
|
|
22
|
-
|
|
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,
|
|
21
|
-
#
|
|
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
|
data/lib/inline_forms/version.rb
CHANGED
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
|
-
|
|
54
|
-
|
|
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
|
|