Module: Workbench::AutoselectHelper

Defined in:
app/helpers/workbench/autoselect_helper.rb

Instance Method Summary collapse

Instance Method Details

#autoselect_field(model:, param:, method:, object: nil, tag_id: nil, current: nil, current_label: nil, placeholder: nil, disabled: false, level_delay: nil, new_record_component: nil, preferences_options_component: nil) ⇒ Object

Renders the mount point for the Vue AutoselectField initializer (app/javascript/vue/initializers/Autoselect/). The element is inert until the initializer picks it up on turbolinks:load.

Parameters:

  • model (String) —

    the autoselect resource's model key, e.g. 'taxon_names'; drives the fetch url (/<model>/autoselect) and the !n create-component lookup (see AUTOSELECT_NEW_RECORD_COMPONENTS)

  • param (String, Symbol) —

    the key read from the selected item's response_values, e.g. :taxon_name_id

  • object (String, Symbol, nil) (defaults to: nil) —

    the Rails form object name, e.g. :otu. When nil the hidden input is named after method alone.

  • method (String, Symbol) —

    the attribute written by the hidden input, e.g. :taxon_name_id

  • tag_id (String, nil) (defaults to: nil) —

    a globally unique id for the instance; it is the DOM id and the key user preferences are stored under. A UUID is generated client-side when absent.

  • current (ApplicationRecord, nil) (defaults to: nil) —

    the presently assigned record, if any

  • current_label (String, nil) (defaults to: nil) —

    HTML label for current; defaults to the model's <model>_autoselect_tag

  • new_record_component (String, nil) (defaults to: nil) —

    key of the component opened by !n, e.g. 'TaxonNameNewModal'. When nil (or not a known key) the model's default applies (AUTOSELECT_NEW_RECORD_COMPONENTS); there's no way to turn !n off from here.

  • preferences_options_component (String, nil) (defaults to: nil) —

    key of the component rendered inside the !p modal, e.g. 'ColDatasetPicker'. When nil (or not a known key) the default, ColDatasetPicker, applies.



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
# File 'app/helpers/workbench/autoselect_helper.rb', line 34

def autoselect_field(
  model:,
  param:,
  method:,
  object: nil,
  tag_id: nil,
  current: nil,
  current_label: nil,
  placeholder: nil,
  disabled: false,
  level_delay: nil,
  new_record_component: nil,
  preferences_options_component: nil
)
  (:div, '', data: {
    'autoselect' => true,
    'autoselect-model' => model,
    'autoselect-param' => param,
    'autoselect-field-object' => object,
    'autoselect-field-property' => method,
    'autoselect-id' => tag_id,
    'autoselect-placeholder' => placeholder,
    'autoselect-disabled' => disabled,
    'autoselect-level-delay' => level_delay,
    'autoselect-current-value' => current&.id,
    'autoselect-current-label' => current_label || autoselect_label_for(current),
    'autoselect-new-record-component' => new_record_component,
    'autoselect-preferences-options-component' => preferences_options_component
  })
end

#autoselect_label_for(record) ⇒ String?

Returns the HTML label a model renders in the autoselect dropdown.

Returns:

  • (String, nil) —

    the HTML label a model renders in the autoselect dropdown



67
68
69
70
71
72
73
# File 'app/helpers/workbench/autoselect_helper.rb', line 67

def autoselect_label_for(record)
  return nil if record.nil?

  tag_method = "#{record.class.base_class.name.underscore}_autoselect_tag"

  respond_to?(tag_method) ? send(tag_method, record) : label_for(record)
end