inline_forms 8.1.78 → 8.1.80

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: e67cd418b32c04cdfab71c7194f85cd74d29ab2a72318c73b625fc8e358d7ca2
4
- data.tar.gz: a4f42e8e906151206666250f4ab9d53c6de4cac35fa8c81e95c9b3acc242d342
3
+ metadata.gz: 50bb6fa49e77c07b640995f0385e6c4194ac115a99c81b622983171ae8cb68e9
4
+ data.tar.gz: e4496274fbf81968e133425f365d0cd5b3cd93921f00f98dc034111184e4d3f1
5
5
  SHA512:
6
- metadata.gz: 01a5bdea6611e9010034ac25d4fdfa33a2d4c0e662cb26c84651c1d93a5b8fe12f66f73c5beba71069363958186c3bf9e66b3498c6f65f6ff51b42a8cc9cd554
7
- data.tar.gz: 616387f11d98b8be25d5c2628c7c636502b6328ecf3ae2d5f721321138c6300b37efe21ed382353631c3fa5bc8ca649959ddc03941ecc3ee92d046fd5a9b343a
6
+ metadata.gz: 530bd5a53c304dbea6c620d331de5bdf5ca7bf37f46ec5c05e75b90ef46b294f2c9ae6128f2ecf001bf70676a597a4fabe684bcd8c8a3e278cd651b83a19e71e
7
+ data.tar.gz: 26b3c77195556e13c3de8ceada7322c9dd8bad3373903a17752e074f0d40319918e598a9a8784e1287d9a34d75f59dcd8a50305e1e1b7f80d29990f4838ed922
data/CHANGELOG.md CHANGED
@@ -8,6 +8,38 @@ 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.80] - 2026-11-24
12
+
13
+ From the notes of the apps' move to 8.1.79's suggestions.
14
+
15
+ ### Changed
16
+
17
+ - **`match: :word` also starts a word after a hyphen or an apostrophe** (`'` and `’`), not only after a space: "marc" finds Jean-Marc, "brien" O'Brien, "abreu" D’Abreu. Still not inside a word ("marc" does not find Lamarck). Apps that pin the old space-only behaviour in a spec (Scouting's `members_autocomplete_spec.rb`) see that spec fail on the upgrade, on purpose.
18
+
19
+ ### Added
20
+
21
+ - **`inline_forms_suggestion_request?`** (concern `SuggestionRequestConcern`), in `InlineFormsController` and in `InlineFormsApplicationController`, so a generated app's `ApplicationController` has it for every controller, and in views: true when the request is the search box's suggestions (`index` with `?suggest=1`), which answer JSON and are not a list view; false outside the inline_forms lists. For an app's own callbacks, e.g. statistics that count list views only: `skip_after_action :track_action, if: :inline_forms_suggestion_request?`.
22
+
23
+ ### Fixed
24
+
25
+ - **The generic search box has accessible names.** The form has `role="search"`, the field an `aria-label` (new key `inline_forms.search.label`: "search", "zoeken"; without it, the placeholder text), the reset link an `aria-label` as well as its `title`, and its `fi-x` icon `aria-hidden="true"`.
26
+
27
+ ## [8.1.79] - 2026-11-23
28
+
29
+ ### Added
30
+
31
+ - **Search suggestions, in the engine.** The pieces Scouting, StProject and Papiamentu each had, so an app can drop its own copies on an upgrade:
32
+ - `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.
33
+ - 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.
34
+ - 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.
35
+ - **`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.
36
+ - **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.
37
+ - Installer: a new app's users list suggests names (`inline_forms_search_on :name, :email, suggest: { on: :name }`), with an example-app test.
38
+
39
+ ### Changed
40
+
41
+ - 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.
42
+
11
43
  ## [8.1.78] - 2026-11-22
12
44
 
13
45
  ### 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
+ })();
@@ -1,5 +1,6 @@
1
1
  //= require jquery
2
2
  //= require foundation
3
+ //= require inline_forms/datalist_suggest
3
4
 
4
5
  // Turbo / Hotwire is intentionally NOT required into this Sprockets bundle.
5
6
  // `turbo-rails` ships only an ES-module build of turbo (`app/assets/javascripts/turbo.js`
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ # `inline_forms_suggestion_request?`: true when this request is an
4
+ # inline_forms list's search-box suggestions (index with ?suggest=1&term=…,
5
+ # which answers a JSON array), not a list view. For an app's own callbacks,
6
+ # e.g. statistics that count list views only:
7
+ #
8
+ # skip_after_action :track_action, if: :inline_forms_suggestion_request?
9
+ #
10
+ # Included in InlineFormsController and in InlineFormsApplicationController,
11
+ # so an ApplicationController that inherits from the latter has it for every
12
+ # controller (false outside the inline_forms lists).
13
+ module SuggestionRequestConcern
14
+ extend ActiveSupport::Concern
15
+
16
+ included do
17
+ helper_method :inline_forms_suggestion_request?
18
+ end
19
+
20
+ protected
21
+
22
+ def inline_forms_suggestion_request?
23
+ is_a?(InlineFormsController) && action_name == "index" &&
24
+ params.key?(:suggest) && params[:parent_class].blank?
25
+ end
26
+ end
@@ -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
@@ -2,6 +2,7 @@
2
2
 
3
3
  class InlineFormsApplicationController < ActionController::Base
4
4
  protect_from_forgery
5
+ include SuggestionRequestConcern
5
6
  # `layout 'devise' if :devise_controller?` was wrong: `:devise_controller?` is a
6
7
  # Symbol (always truthy), so every controller used the Devise layout. Use a
7
8
  # callable so only Devise controllers get `layouts/devise`; everything else
@@ -27,6 +27,8 @@ class InlineFormsController < ApplicationController
27
27
  include CancanConcern
28
28
  include VersionsConcern
29
29
  include FileSlotsConcern
30
+ include SuggestionsConcern
31
+ include SuggestionRequestConcern
30
32
 
31
33
  before_action :getKlass
32
34
 
@@ -58,6 +60,9 @@ class InlineFormsController < ApplicationController
58
60
  @objects ||= @Klass.accessible_by(current_ability) if cancan_enabled?
59
61
  @objects ||= @Klass.all
60
62
  @objects = @objects.merge(@Klass.inline_forms_list) if @Klass.respond_to?(:inline_forms_list)
63
+ # The search box's suggestions (?suggest=1&term=…): from the same records
64
+ # as the list, so they need no route or ability of their own.
65
+ return render_search_suggestions if params.key?(:suggest) && fk_conditions.nil?
61
66
  if fk_conditions.nil? && params[:search].present? && @Klass.respond_to?(:inline_forms_search)
62
67
  @objects = @objects.merge(@Klass.inline_forms_search(params[:search]))
63
68
  end
@@ -780,6 +785,17 @@ class InlineFormsController < ApplicationController
780
785
  render "inline_forms/new_record", layout: associated_list_frame_layout
781
786
  end
782
787
 
788
+ # The engine's search box asks the list for suggestions when the model
789
+ # declares `inline_forms_search_on …, suggest: …`; else there are none (404).
790
+ def render_search_suggestions
791
+ options = @Klass.respond_to?(:inline_forms_suggestions) && @Klass.inline_forms_suggestions
792
+ return head(:not_found) unless options
793
+
794
+ render_inline_forms_suggestions InlineForms::Suggestions.values(
795
+ @objects, options[:on], params[:term], match: options[:match], limit: options[:limit]
796
+ )
797
+ end
798
+
783
799
  # Nested +index+ / cancel / +create+ HTML inside a parent-associated +<turbo-frame>+.
784
800
  def render_nested_associated_list_html
785
801
  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
@@ -1,10 +1,17 @@
1
- <%= form_tag request.path, method: :get do %>
1
+ <% search_label = t("inline_forms.search.label", default: t("inline_forms.search.placeholder")) %>
2
+ <%= form_tag request.path, method: :get, role: "search" do %>
2
3
  <div class="row collapse">
3
4
  <div class="large-1 small-1 columns">
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
+ <%= link_to tag.i(class: "fi-x", "aria-hidden": "true"), request.path, title: t("inline_forms.search.reset"), "aria-label": t("inline_forms.search.reset"), class: "button expanded inline_forms_model_top_bar_buttons" %>
5
6
  </div>
6
7
  <div class="large-6 small-6 columns">
7
- <%= text_field_tag :search, params[:search], placeholder: t("inline_forms.search.placeholder"), id: "input_search" %>
8
+ <%# With `inline_forms_search_on …, suggest: …` the list itself answers %>
9
+ <%# the suggestions (index with ?suggest=1&term=…). %>
10
+ <% if @Klass.respond_to?(:inline_forms_suggestions) && @Klass.inline_forms_suggestions %>
11
+ <%= suggest_field_tag :search, params[:search], "#{request.path}?suggest=1", placeholder: t("inline_forms.search.placeholder"), "aria-label": search_label, id: "input_search", list: "input_search_suggestions" %>
12
+ <% else %>
13
+ <%= text_field_tag :search, params[:search], placeholder: t("inline_forms.search.placeholder"), "aria-label": search_label, id: "input_search" %>
14
+ <% end %>
8
15
  </div>
9
16
  <div class="large-3 small-3 columns">
10
17
  <%= 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
- def inline_forms_search_on(*columns)
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,88 @@
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
+ # a word starts after a space, a hyphen or an apostrophe
17
+ # ("marc" finds Jean-Marc, "brien" O'Brien and O’Brien)
18
+ #
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 [].
22
+ module Suggestions
23
+ MATCHES = %i[contains start word].freeze
24
+ # What starts a word for match: :word, besides the start of the value.
25
+ WORD_STARTS = [ " ", "-", "'", "’" ].freeze
26
+ LIMIT = 12
27
+
28
+ module_function
29
+
30
+ # Raises ArgumentError for an unknown match; returns it as a Symbol.
31
+ def check_match!(match)
32
+ match = match.to_sym
33
+ return match if MATCHES.include?(match)
34
+
35
+ raise ArgumentError, "match: must be one of #{MATCHES.map(&:inspect).join(', ')}, not #{match.inspect}"
36
+ end
37
+
38
+ def check_limit!(limit)
39
+ return limit if limit.is_a?(Integer) && limit.positive?
40
+
41
+ raise ArgumentError, "limit: must be a positive Integer, not #{limit.inspect}"
42
+ end
43
+
44
+ # relation: what the user may see (already `accessible_by`, scoped, …).
45
+ def values(relation, columns, term, match: :contains, limit: LIMIT)
46
+ match = check_match!(match)
47
+ limit = check_limit!(limit)
48
+ term = term.to_s.strip
49
+ return [] if term.empty?
50
+
51
+ columns = Array(columns)
52
+ found = columns.flat_map { |column| column_values(relation, column, term, match, limit) }
53
+ found = found.map(&:to_s).reject(&:empty?)
54
+ # One column: the database's order (its collation, e.g. accents with
55
+ # their letter). Several: merged here.
56
+ found = found.sort_by { |value| [ value.downcase, value ] } if columns.size > 1
57
+ found.uniq(&:downcase).first(limit)
58
+ end
59
+
60
+ # SELECT MIN(TRIM(value)) … GROUP BY LOWER(TRIM(value)) ORDER BY that:
61
+ # one row per value as the user sees it, so the limit counts distinct
62
+ # names ('Adriana', 'adriana ' and 'Adriana' are one). A plain DISTINCT
63
+ # would keep them apart (case, and MySQL's NO PAD collations keep a
64
+ # trailing space) and fill the limit with them. Valid under MySQL's
65
+ # ONLY_FULL_GROUP_BY and in PostgreSQL. reorder drops a default or list
66
+ # order the relation brings.
67
+ def column_values(relation, column, term, match, limit)
68
+ model = relation.model
69
+ attribute = model.arel_table[column.to_s]
70
+ lowered = Arel::Nodes::NamedFunction.new("LOWER", [ attribute ])
71
+ key = Arel::Nodes::NamedFunction.new("LOWER", [ Arel::Nodes::NamedFunction.new("TRIM", [ attribute ]) ])
72
+ condition = patterns(model, term, match).map { |pattern|
73
+ lowered.matches(Arel::Nodes::NamedFunction.new("LOWER", [ Arel::Nodes.build_quoted(pattern) ]), "\\", true)
74
+ }.reduce(:or)
75
+ relation.where(condition).unscope(:select).group(key).reorder(key.asc).limit(limit)
76
+ .pluck(Arel::Nodes::Min.new([ Arel::Nodes::NamedFunction.new("TRIM", [ attribute ]) ]))
77
+ end
78
+
79
+ def patterns(model, term, match)
80
+ escaped = model.sanitize_sql_like(term, "\\")
81
+ case match
82
+ when :contains then [ "%#{escaped}%" ]
83
+ when :start then [ "#{escaped}%" ]
84
+ when :word then [ "#{escaped}%", *WORD_STARTS.map { |start| "%#{start}#{escaped}%" } ]
85
+ end
86
+ end
87
+ end
88
+ end
@@ -1,5 +1,5 @@
1
1
  # -*- encoding : utf-8 -*-
2
2
 
3
3
  module InlineForms
4
- VERSION = "8.1.78"
4
+ VERSION = "8.1.80"
5
5
  end
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"
@@ -4,6 +4,7 @@ en:
4
4
  # models that declare inline_forms_search_on.
5
5
  search:
6
6
  placeholder: "search..."
7
+ label: search
7
8
  submit: search
8
9
  reset: reset
9
10
  # general.application_title, devise.title_for_devise and devise.welcome
@@ -4,6 +4,7 @@ nl:
4
4
  # models that declare inline_forms_search_on.
5
5
  search:
6
6
  placeholder: "zoeken..."
7
+ label: zoeken
7
8
  submit: zoek
8
9
  reset: wissen
9
10
  # general.application_title, devise.title_for_devise and devise.welcome
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.78
4
+ version: 8.1.80
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,13 @@ 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/suggestion_request_concern.rb
117
+ - app/controllers/concerns/suggestions_concern.rb
115
118
  - app/controllers/concerns/versions_concern.rb
116
119
  - app/controllers/inline_forms/file_trash_controller.rb
117
120
  - app/controllers/inline_forms_application_controller.rb
118
121
  - app/controllers/inline_forms_controller.rb
122
+ - app/helpers/datalist_suggest_helper.rb
119
123
  - app/helpers/inline_forms_file_trash_helper.rb
120
124
  - app/helpers/inline_forms_helper.rb
121
125
  - app/models/concerns/inline_forms/soft_deletable.rb
@@ -261,6 +265,7 @@ files:
261
265
  - lib/inline_forms/schema_label.rb
262
266
  - lib/inline_forms/schema_preview.rb
263
267
  - lib/inline_forms/searchable.rb
268
+ - lib/inline_forms/suggestions.rb
264
269
  - lib/inline_forms/tabs.rb
265
270
  - lib/inline_forms/turbo_tabs_builder.rb
266
271
  - lib/inline_forms/version.rb