inline_forms 8.1.78 → 8.1.79
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 +16 -0
- data/app/assets/javascripts/inline_forms/datalist_suggest.js +81 -0
- data/app/assets/javascripts/inline_forms/inline_forms.js +1 -0
- data/app/controllers/concerns/suggestions_concern.rb +52 -0
- data/app/controllers/inline_forms_controller.rb +15 -0
- data/app/helpers/datalist_suggest_helper.rb +20 -0
- data/app/views/inline_forms/_generic_search.html.erb +7 -1
- data/lib/inline_forms/searchable.rb +40 -1
- data/lib/inline_forms/suggestions.rb +84 -0
- data/lib/inline_forms/version.rb +1 -1
- data/lib/inline_forms.rb +1 -0
- metadata +5 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e5e23dce20574f758b57ab81d8a344f0d74b8e969c13e305ab426b6270623b9d
|
|
4
|
+
data.tar.gz: 49eba2b2136e79bd27dbc51c258681c98cbcf718679d22ceced338e0f252bd3d
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 8d40d0a5af35ef3889706d15b667cffd1eb12165b5a87f397a200cdd044a9ac28ecaadcb0639632b317b837a85d8eade09fe0545e28848334d1bc88e30f88721
|
|
7
|
+
data.tar.gz: 239613687ee6ee98a42b9a0997c63af81b0c9e28f326c152363e2edb6cb918b9b0ac430894d0f2c1d440d93e98e23ec23df8129e8754220a371ed06a32ac0287
|
data/CHANGELOG.md
CHANGED
|
@@ -8,6 +8,22 @@ 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.79] - 2026-11-23
|
|
12
|
+
|
|
13
|
+
### Added
|
|
14
|
+
|
|
15
|
+
- **Search suggestions, in the engine.** The pieces Scouting, StProject and Papiamentu each had, so an app can drop its own copies on an upgrade:
|
|
16
|
+
- `inline_forms/datalist_suggest.js`, in the engine's script bundle: while the user types, it fills the browser's own suggestion list (`<datalist>`) from a JSON URL. It is the apps' file unchanged; its once-guard (`window.datalistSuggestLoaded`) keeps it from running twice when an app still loads its own copy. No library, no inline script; it passes terser on mini_racer.
|
|
17
|
+
- The helper `suggest_field_tag(name, value, url, options)`: a text field with `list`, `autocomplete="off"` and `data-suggest-url`, and an empty `<datalist>`. It has the apps' module name, `DatalistSuggestHelper`, so an app's own copy wins.
|
|
18
|
+
- The controller macro `suggest_names :client, :last_name`, in every controller that inherits from `InlineFormsController`. It defines the action `autocomplete_client_last_name`, which answers `?term=` with at most 12 names (`limit:`), each once (case and surrounding spaces ignored), in A-Z order, only from records the user may read (`accessible_by`). `scope:` narrows them further, run in the controller. The app adds the route and the ability, as before.
|
|
19
|
+
- **`match:` says where the term must be:** `:contains` (anywhere, the default: "ma" finds Martina, tinaMart and tinartma), `:start` (only at the start: Martina) or `:word` (at the start of any word: Martina, Anna Maria). Case is ignored, and `%` and `_` in the term are taken literally.
|
|
20
|
+
- **The engine's search box suggests,** for a model that asks for it: `inline_forms_search_on :name, :email, suggest: true`, or `suggest: { on: :name, match: :start, limit: 8 }`. The list answers the suggestions itself (`?suggest=1&term=…`), from the records the user may see there, so they need no route or ability of their own. Without `suggest:` the box is as before.
|
|
21
|
+
- Installer: a new app's users list suggests names (`inline_forms_search_on :name, :email, suggest: { on: :name }`), with an example-app test.
|
|
22
|
+
|
|
23
|
+
### Changed
|
|
24
|
+
|
|
25
|
+
- Full gate (CI, run by hand or weekly): the Brakeman ignore entry that 8.1.77 made obsolete is gone, so the gate is green again for the code released as 8.1.78.
|
|
26
|
+
|
|
11
27
|
## [8.1.78] - 2026-11-22
|
|
12
28
|
|
|
13
29
|
### Changed
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
// Search suggestions with the browser's own <datalist>. No library.
|
|
2
|
+
//
|
|
3
|
+
// <input type="text" name="search" list="search_suggestions" autocomplete="off"
|
|
4
|
+
// data-suggest-url="/members/autocomplete_member_last_name">
|
|
5
|
+
// <datalist id="search_suggestions"></datalist>
|
|
6
|
+
//
|
|
7
|
+
// While the user types, GET <url>?term=<text> and put the answers in the
|
|
8
|
+
// datalist. The answer is JSON: an array of strings, or of objects with
|
|
9
|
+
// "value" or "label". The browser shows the list and does the picking; a pick
|
|
10
|
+
// only fills the text, and the form is submitted as before.
|
|
11
|
+
//
|
|
12
|
+
// `data-suggest-min` on the input sets the number of characters before the
|
|
13
|
+
// first request (default 2).
|
|
14
|
+
(function () {
|
|
15
|
+
"use strict";
|
|
16
|
+
|
|
17
|
+
// A layout that loads this file in <body> runs it again on every Turbo visit.
|
|
18
|
+
if (window.datalistSuggestLoaded) { return; }
|
|
19
|
+
window.datalistSuggestLoaded = true;
|
|
20
|
+
|
|
21
|
+
var DELAY = 200; // ms after the last keystroke
|
|
22
|
+
|
|
23
|
+
function textOf(item) {
|
|
24
|
+
if (typeof item === "string") { return item; }
|
|
25
|
+
if (item && typeof item === "object") { return String(item.value || item.label || ""); }
|
|
26
|
+
return "";
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
function fill(list, items) {
|
|
30
|
+
var seen = {};
|
|
31
|
+
list.textContent = "";
|
|
32
|
+
items.forEach(function (item) {
|
|
33
|
+
var text = textOf(item);
|
|
34
|
+
if (text === "" || seen[text]) { return; }
|
|
35
|
+
seen[text] = true;
|
|
36
|
+
var option = document.createElement("option");
|
|
37
|
+
option.value = text;
|
|
38
|
+
list.appendChild(option);
|
|
39
|
+
});
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
function isOption(list, text) {
|
|
43
|
+
for (var i = 0; i < list.options.length; i++) {
|
|
44
|
+
if (list.options[i].value === text) { return true; }
|
|
45
|
+
}
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function suggest(input) {
|
|
50
|
+
var list = document.getElementById(input.getAttribute("list"));
|
|
51
|
+
var url = input.getAttribute("data-suggest-url");
|
|
52
|
+
var min = parseInt(input.getAttribute("data-suggest-min"), 10);
|
|
53
|
+
var term = input.value.trim();
|
|
54
|
+
if (!list || !url) { return; }
|
|
55
|
+
if (input.suggestRequest) { input.suggestRequest.abort(); }
|
|
56
|
+
if (term.length < (isNaN(min) ? 2 : min)) { list.textContent = ""; return; }
|
|
57
|
+
|
|
58
|
+
var request = new AbortController();
|
|
59
|
+
var address = new URL(url, window.location.href);
|
|
60
|
+
address.searchParams.set("term", term);
|
|
61
|
+
input.suggestRequest = request;
|
|
62
|
+
fetch(address, { headers: { "Accept": "application/json" }, credentials: "same-origin", signal: request.signal })
|
|
63
|
+
.then(function (response) { return response.ok ? response.json() : []; })
|
|
64
|
+
.then(function (items) {
|
|
65
|
+
if (input.suggestRequest === request) { fill(list, Array.isArray(items) ? items : []); }
|
|
66
|
+
})
|
|
67
|
+
.catch(function () { /* aborted or offline: keep what is shown */ });
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
document.addEventListener("input", function (event) {
|
|
71
|
+
var input = event.target;
|
|
72
|
+
if (!input.matches || !input.matches("input[list][data-suggest-url]")) { return; }
|
|
73
|
+
clearTimeout(input.suggestTimer);
|
|
74
|
+
// Picking a suggestion fires "input" too. Nothing to ask then, and filling
|
|
75
|
+
// the list again would open it again.
|
|
76
|
+
var list = document.getElementById(input.getAttribute("list"));
|
|
77
|
+
if (event.inputType === "insertReplacementText") { return; }
|
|
78
|
+
if (event.inputType === undefined && list && isOption(list, input.value)) { return; }
|
|
79
|
+
input.suggestTimer = setTimeout(function () { suggest(input); }, DELAY);
|
|
80
|
+
});
|
|
81
|
+
})();
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
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:
|
|
6
|
+
#
|
|
7
|
+
# suggest_names :client, :last_name # term anywhere in the name
|
|
8
|
+
# suggest_names :client, :first_name, match: :start # only at the start
|
|
9
|
+
# suggest_names :member, :last_name, scope: ->(members) { members.where(active: true) }
|
|
10
|
+
#
|
|
11
|
+
# defines the action `autocomplete_<model>_<column>` (the name
|
|
12
|
+
# rails-jquery-autocomplete's `autocomplete` macro used), which answers
|
|
13
|
+
# GET …?term=… with a JSON array of at most `limit` (12) names: see
|
|
14
|
+
# InlineForms::Suggestions for `match:` (:contains, :start, :word) and the
|
|
15
|
+
# order. The app adds the route and the ability, as for any other action:
|
|
16
|
+
#
|
|
17
|
+
# resources :clients do
|
|
18
|
+
# get :autocomplete_client_last_name, on: :collection
|
|
19
|
+
# end
|
|
20
|
+
# can :autocomplete_client_last_name, Client
|
|
21
|
+
#
|
|
22
|
+
# Only names of records the user may read (`accessible_by`) when CanCan is
|
|
23
|
+
# on; `scope:` narrows them further, run in the controller (instance_exec)
|
|
24
|
+
# with the relation, and returns a relation.
|
|
25
|
+
module SuggestionsConcern
|
|
26
|
+
extend ActiveSupport::Concern
|
|
27
|
+
|
|
28
|
+
class_methods do
|
|
29
|
+
def suggest_names(model_name, column, match: :contains, limit: InlineForms::Suggestions::LIMIT, scope: nil)
|
|
30
|
+
match = InlineForms::Suggestions.check_match!(match)
|
|
31
|
+
InlineForms::Suggestions.check_limit!(limit)
|
|
32
|
+
define_method("autocomplete_#{model_name}_#{column}") do
|
|
33
|
+
model = model_name.to_s.camelize.constantize
|
|
34
|
+
names = inline_forms_suggestion_scope(model)
|
|
35
|
+
names = instance_exec(names, &scope) if scope
|
|
36
|
+
render_inline_forms_suggestions InlineForms::Suggestions.values(names, column, params[:term], match: match, limit: limit)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
private
|
|
42
|
+
|
|
43
|
+
def inline_forms_suggestion_scope(model)
|
|
44
|
+
cancan_enabled? ? model.accessible_by(current_ability) : model.all
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
# A JSON string, so a serializer gem (active_model_serializers 0.9) does
|
|
48
|
+
# not wrap the array.
|
|
49
|
+
def render_inline_forms_suggestions(values)
|
|
50
|
+
render json: values.to_json
|
|
51
|
+
end
|
|
52
|
+
end
|
|
@@ -27,6 +27,7 @@ class InlineFormsController < ApplicationController
|
|
|
27
27
|
include CancanConcern
|
|
28
28
|
include VersionsConcern
|
|
29
29
|
include FileSlotsConcern
|
|
30
|
+
include SuggestionsConcern
|
|
30
31
|
|
|
31
32
|
before_action :getKlass
|
|
32
33
|
|
|
@@ -58,6 +59,9 @@ class InlineFormsController < ApplicationController
|
|
|
58
59
|
@objects ||= @Klass.accessible_by(current_ability) if cancan_enabled?
|
|
59
60
|
@objects ||= @Klass.all
|
|
60
61
|
@objects = @objects.merge(@Klass.inline_forms_list) if @Klass.respond_to?(:inline_forms_list)
|
|
62
|
+
# The search box's suggestions (?suggest=1&term=…): from the same records
|
|
63
|
+
# as the list, so they need no route or ability of their own.
|
|
64
|
+
return render_search_suggestions if params.key?(:suggest) && fk_conditions.nil?
|
|
61
65
|
if fk_conditions.nil? && params[:search].present? && @Klass.respond_to?(:inline_forms_search)
|
|
62
66
|
@objects = @objects.merge(@Klass.inline_forms_search(params[:search]))
|
|
63
67
|
end
|
|
@@ -780,6 +784,17 @@ class InlineFormsController < ApplicationController
|
|
|
780
784
|
render "inline_forms/new_record", layout: associated_list_frame_layout
|
|
781
785
|
end
|
|
782
786
|
|
|
787
|
+
# The engine's search box asks the list for suggestions when the model
|
|
788
|
+
# declares `inline_forms_search_on …, suggest: …`; else there are none (404).
|
|
789
|
+
def render_search_suggestions
|
|
790
|
+
options = @Klass.respond_to?(:inline_forms_suggestions) && @Klass.inline_forms_suggestions
|
|
791
|
+
return head(:not_found) unless options
|
|
792
|
+
|
|
793
|
+
render_inline_forms_suggestions InlineForms::Suggestions.values(
|
|
794
|
+
@objects, options[:on], params[:term], match: options[:match], limit: options[:limit]
|
|
795
|
+
)
|
|
796
|
+
end
|
|
797
|
+
|
|
783
798
|
# Nested +index+ / cancel / +create+ HTML inside a parent-associated +<turbo-frame>+.
|
|
784
799
|
def render_nested_associated_list_html
|
|
785
800
|
if turbo_frame_request? && nested_list_frame_id?(params[:update])
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
# A text field with the browser's own suggestion list (<datalist>), filled by
|
|
2
|
+
# app/assets/javascripts/inline_forms/datalist_suggest.js from `url` (JSON)
|
|
3
|
+
# while the user types. Replaces rails-jquery-autocomplete's
|
|
4
|
+
# `autocomplete_field_tag`:
|
|
5
|
+
#
|
|
6
|
+
# <%= suggest_field_tag "search", params[:search], autocomplete_member_last_name_members_path, placeholder: "achternaam" %>
|
|
7
|
+
#
|
|
8
|
+
# Two fields with the same name on one page: give each its own `list:` id.
|
|
9
|
+
#
|
|
10
|
+
# The same module, with the same name, as the copies some apps have in their
|
|
11
|
+
# own app/helpers: an app's file wins over this one.
|
|
12
|
+
module DatalistSuggestHelper
|
|
13
|
+
def suggest_field_tag(name, value, url, options = {})
|
|
14
|
+
options = options.dup
|
|
15
|
+
list_id = options.delete(:list) || "#{name.to_s.gsub(/[^-a-zA-Z0-9:.]/, '_')}_suggestions"
|
|
16
|
+
data = (options.delete(:data) || {}).merge(suggest_url: url)
|
|
17
|
+
field = text_field_tag(name, value, options.merge(list: list_id, autocomplete: "off", data: data))
|
|
18
|
+
safe_join([ field, content_tag(:datalist, "", id: list_id) ])
|
|
19
|
+
end
|
|
20
|
+
end
|
|
@@ -4,7 +4,13 @@
|
|
|
4
4
|
<%= link_to tag.i(class: "fi-x"), request.path, title: t("inline_forms.search.reset"), class: "button expanded inline_forms_model_top_bar_buttons" %>
|
|
5
5
|
</div>
|
|
6
6
|
<div class="large-6 small-6 columns">
|
|
7
|
-
|
|
7
|
+
<%# With `inline_forms_search_on …, suggest: …` the list itself answers %>
|
|
8
|
+
<%# the suggestions (index with ?suggest=1&term=…). %>
|
|
9
|
+
<% if @Klass.respond_to?(:inline_forms_suggestions) && @Klass.inline_forms_suggestions %>
|
|
10
|
+
<%= suggest_field_tag :search, params[:search], "#{request.path}?suggest=1", placeholder: t("inline_forms.search.placeholder"), id: "input_search", list: "input_search_suggestions" %>
|
|
11
|
+
<% else %>
|
|
12
|
+
<%= text_field_tag :search, params[:search], placeholder: t("inline_forms.search.placeholder"), id: "input_search" %>
|
|
13
|
+
<% end %>
|
|
8
14
|
</div>
|
|
9
15
|
<div class="large-3 small-3 columns">
|
|
10
16
|
<%= submit_tag t("inline_forms.search.submit"), class: "button expanded inline_forms_model_top_bar_buttons" %>
|
|
@@ -4,6 +4,17 @@ module InlineForms
|
|
|
4
4
|
# Opt-in list search for inline_forms index views. Models declare searchable
|
|
5
5
|
# columns with `inline_forms_search_on :col, …`; only those models get the
|
|
6
6
|
# engine's generic search box (or a bespoke `_<model>_search` partial).
|
|
7
|
+
#
|
|
8
|
+
# `suggest:` gives that search box suggestions while the user types (the
|
|
9
|
+
# browser's own list, see InlineForms::Suggestions), from the records the
|
|
10
|
+
# user may see in the list:
|
|
11
|
+
#
|
|
12
|
+
# inline_forms_search_on :name, :email, suggest: true # from both columns
|
|
13
|
+
# inline_forms_search_on :name, :email, suggest: { on: :name, match: :start, limit: 8 }
|
|
14
|
+
#
|
|
15
|
+
# `on:` (default: the search columns), `match:` (:contains, the default,
|
|
16
|
+
# :start or :word) and `limit:` (12). The search itself still finds the
|
|
17
|
+
# text anywhere in any search column.
|
|
7
18
|
module Searchable
|
|
8
19
|
extend ActiveSupport::Concern
|
|
9
20
|
|
|
@@ -12,7 +23,12 @@ module InlineForms
|
|
|
12
23
|
@inline_forms_searchable == true
|
|
13
24
|
end
|
|
14
25
|
|
|
15
|
-
|
|
26
|
+
# nil, or { on: [columns], match:, limit: }.
|
|
27
|
+
def inline_forms_suggestions
|
|
28
|
+
@inline_forms_suggestions
|
|
29
|
+
end
|
|
30
|
+
|
|
31
|
+
def inline_forms_search_on(*columns, suggest: nil)
|
|
16
32
|
cols = columns.flatten.map(&:to_s)
|
|
17
33
|
if cols.empty?
|
|
18
34
|
raise ArgumentError, "inline_forms_search_on requires at least one column"
|
|
@@ -20,12 +36,35 @@ module InlineForms
|
|
|
20
36
|
|
|
21
37
|
@inline_forms_searchable = true
|
|
22
38
|
@inline_forms_search_columns = cols
|
|
39
|
+
@inline_forms_suggestions = inline_forms_suggest_options(suggest, cols)
|
|
23
40
|
model = self
|
|
24
41
|
scope :inline_forms_search, ->(q) {
|
|
25
42
|
where(model.inline_forms_search_clause, q: "%#{sanitize_sql_like(q.to_s)}%")
|
|
26
43
|
}
|
|
27
44
|
end
|
|
28
45
|
|
|
46
|
+
def inline_forms_suggest_options(suggest, search_columns)
|
|
47
|
+
return nil if suggest.nil? || suggest == false
|
|
48
|
+
|
|
49
|
+
options = suggest == true ? {} : suggest
|
|
50
|
+
unless options.is_a?(Hash)
|
|
51
|
+
raise ArgumentError, "inline_forms_search_on suggest: takes true or a Hash (on:, match:, limit:), not #{suggest.inspect}"
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
unknown = options.keys.map(&:to_sym) - %i[on match limit]
|
|
55
|
+
raise ArgumentError, "inline_forms_search_on suggest: unknown option #{unknown.first.inspect}" if unknown.any?
|
|
56
|
+
|
|
57
|
+
options = options.transform_keys(&:to_sym)
|
|
58
|
+
on = Array(options.fetch(:on, search_columns)).map(&:to_s)
|
|
59
|
+
raise ArgumentError, "inline_forms_search_on suggest: on: needs at least one column" if on.empty?
|
|
60
|
+
|
|
61
|
+
{
|
|
62
|
+
on: on.freeze,
|
|
63
|
+
match: Suggestions.check_match!(options.fetch(:match, :contains)),
|
|
64
|
+
limit: Suggestions.check_limit!(options.fetch(:limit, Suggestions::LIMIT))
|
|
65
|
+
}.freeze
|
|
66
|
+
end
|
|
67
|
+
|
|
29
68
|
# Built on the first search, not in inline_forms_search_on: quoting
|
|
30
69
|
# needs a database connection, and the class body runs whenever the
|
|
31
70
|
# model loads (eager loading, assets:precompile without a database).
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module InlineForms
|
|
4
|
+
# Text suggestions for a search field: the values of one or more columns
|
|
5
|
+
# that match what the user typed, for the browser's own <datalist>
|
|
6
|
+
# (suggest_field_tag + inline_forms/datalist_suggest.js). Used by the
|
|
7
|
+
# controller macro `suggest_names` (SuggestionsConcern) and by the engine's
|
|
8
|
+
# search box for a model with `inline_forms_search_on …, suggest: …`.
|
|
9
|
+
#
|
|
10
|
+
# How a value matches the term (`match:`), case ignored, % and _ in the term
|
|
11
|
+
# taken literally:
|
|
12
|
+
#
|
|
13
|
+
# :contains anywhere in the value ("ma" finds Martina, Tinamart, Tima)
|
|
14
|
+
# :start at the start of the value ("ma" finds Martina only)
|
|
15
|
+
# :word at the start of any word ("ma" finds Martina, Anna Maria)
|
|
16
|
+
#
|
|
17
|
+
# The answer: at most `limit` values, each once (compared stripped and in any
|
|
18
|
+
# case, the first in A-Z order kept), in case-insensitive A-Z order. An empty
|
|
19
|
+
# term gives [].
|
|
20
|
+
module Suggestions
|
|
21
|
+
MATCHES = %i[contains start word].freeze
|
|
22
|
+
LIMIT = 12
|
|
23
|
+
|
|
24
|
+
module_function
|
|
25
|
+
|
|
26
|
+
# Raises ArgumentError for an unknown match; returns it as a Symbol.
|
|
27
|
+
def check_match!(match)
|
|
28
|
+
match = match.to_sym
|
|
29
|
+
return match if MATCHES.include?(match)
|
|
30
|
+
|
|
31
|
+
raise ArgumentError, "match: must be one of #{MATCHES.map(&:inspect).join(', ')}, not #{match.inspect}"
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
def check_limit!(limit)
|
|
35
|
+
return limit if limit.is_a?(Integer) && limit.positive?
|
|
36
|
+
|
|
37
|
+
raise ArgumentError, "limit: must be a positive Integer, not #{limit.inspect}"
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
# relation: what the user may see (already `accessible_by`, scoped, …).
|
|
41
|
+
def values(relation, columns, term, match: :contains, limit: LIMIT)
|
|
42
|
+
match = check_match!(match)
|
|
43
|
+
limit = check_limit!(limit)
|
|
44
|
+
term = term.to_s.strip
|
|
45
|
+
return [] if term.empty?
|
|
46
|
+
|
|
47
|
+
columns = Array(columns)
|
|
48
|
+
found = columns.flat_map { |column| column_values(relation, column, term, match, limit) }
|
|
49
|
+
found = found.map(&:to_s).reject(&:empty?)
|
|
50
|
+
# One column: the database's order (its collation, e.g. accents with
|
|
51
|
+
# their letter). Several: merged here.
|
|
52
|
+
found = found.sort_by { |value| [ value.downcase, value ] } if columns.size > 1
|
|
53
|
+
found.uniq(&:downcase).first(limit)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
# SELECT MIN(TRIM(value)) … GROUP BY LOWER(TRIM(value)) ORDER BY that:
|
|
57
|
+
# one row per value as the user sees it, so the limit counts distinct
|
|
58
|
+
# names ('Adriana', 'adriana ' and 'Adriana' are one). A plain DISTINCT
|
|
59
|
+
# would keep them apart (case, and MySQL's NO PAD collations keep a
|
|
60
|
+
# trailing space) and fill the limit with them. Valid under MySQL's
|
|
61
|
+
# ONLY_FULL_GROUP_BY and in PostgreSQL. reorder drops a default or list
|
|
62
|
+
# order the relation brings.
|
|
63
|
+
def column_values(relation, column, term, match, limit)
|
|
64
|
+
model = relation.model
|
|
65
|
+
attribute = model.arel_table[column.to_s]
|
|
66
|
+
lowered = Arel::Nodes::NamedFunction.new("LOWER", [ attribute ])
|
|
67
|
+
key = Arel::Nodes::NamedFunction.new("LOWER", [ Arel::Nodes::NamedFunction.new("TRIM", [ attribute ]) ])
|
|
68
|
+
condition = patterns(model, term, match).map { |pattern|
|
|
69
|
+
lowered.matches(Arel::Nodes::NamedFunction.new("LOWER", [ Arel::Nodes.build_quoted(pattern) ]), "\\", true)
|
|
70
|
+
}.reduce(:or)
|
|
71
|
+
relation.where(condition).unscope(:select).group(key).reorder(key.asc).limit(limit)
|
|
72
|
+
.pluck(Arel::Nodes::Min.new([ Arel::Nodes::NamedFunction.new("TRIM", [ attribute ]) ]))
|
|
73
|
+
end
|
|
74
|
+
|
|
75
|
+
def patterns(model, term, match)
|
|
76
|
+
escaped = model.sanitize_sql_like(term, "\\")
|
|
77
|
+
case match
|
|
78
|
+
when :contains then [ "%#{escaped}%" ]
|
|
79
|
+
when :start then [ "#{escaped}%" ]
|
|
80
|
+
when :word then [ "#{escaped}%", "% #{escaped}%" ]
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
84
|
+
end
|
data/lib/inline_forms/version.rb
CHANGED
data/lib/inline_forms.rb
CHANGED
|
@@ -9,6 +9,7 @@ require "inline_forms/schema_preview"
|
|
|
9
9
|
require "inline_forms/schema_apply"
|
|
10
10
|
require "inline_forms/schema_label"
|
|
11
11
|
require "inline_forms/form_element_from_callee"
|
|
12
|
+
require "inline_forms/suggestions"
|
|
12
13
|
require "inline_forms/searchable"
|
|
13
14
|
require "inline_forms/dropdown_order"
|
|
14
15
|
require "inline_forms/refused_values"
|
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.
|
|
4
|
+
version: 8.1.79
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Ace Suares
|
|
@@ -103,6 +103,7 @@ files:
|
|
|
103
103
|
- app/assets/fonts/opensans-v44-latin-italic.woff2
|
|
104
104
|
- app/assets/fonts/opensans-v44-latin-regular.woff2
|
|
105
105
|
- app/assets/images/inline_forms/glass_plate.gif
|
|
106
|
+
- app/assets/javascripts/inline_forms/datalist_suggest.js
|
|
106
107
|
- app/assets/javascripts/inline_forms/inline_forms.js
|
|
107
108
|
- app/assets/stylesheets/inline_forms/_foundation_icons.scss
|
|
108
109
|
- app/assets/stylesheets/inline_forms/_theme.scss
|
|
@@ -112,10 +113,12 @@ files:
|
|
|
112
113
|
- app/assets/stylesheets/inline_forms_fonts.css.erb
|
|
113
114
|
- app/controllers/concerns/cancan_concern.rb
|
|
114
115
|
- app/controllers/concerns/file_slots_concern.rb
|
|
116
|
+
- app/controllers/concerns/suggestions_concern.rb
|
|
115
117
|
- app/controllers/concerns/versions_concern.rb
|
|
116
118
|
- app/controllers/inline_forms/file_trash_controller.rb
|
|
117
119
|
- app/controllers/inline_forms_application_controller.rb
|
|
118
120
|
- app/controllers/inline_forms_controller.rb
|
|
121
|
+
- app/helpers/datalist_suggest_helper.rb
|
|
119
122
|
- app/helpers/inline_forms_file_trash_helper.rb
|
|
120
123
|
- app/helpers/inline_forms_helper.rb
|
|
121
124
|
- app/models/concerns/inline_forms/soft_deletable.rb
|
|
@@ -261,6 +264,7 @@ files:
|
|
|
261
264
|
- lib/inline_forms/schema_label.rb
|
|
262
265
|
- lib/inline_forms/schema_preview.rb
|
|
263
266
|
- lib/inline_forms/searchable.rb
|
|
267
|
+
- lib/inline_forms/suggestions.rb
|
|
264
268
|
- lib/inline_forms/tabs.rb
|
|
265
269
|
- lib/inline_forms/turbo_tabs_builder.rb
|
|
266
270
|
- lib/inline_forms/version.rb
|