Module: Workbench::AutoselectHelper

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

Instance Method Summary collapse

Instance Method Details

#autoselect_field(url:, 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:

  • url (String)

    the autoselect resource, e.g. /taxon_names/autoselect

  • 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'

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

    key of the component rendered inside the !p modal, e.g. 'ColDatasetPicker'



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'app/helpers/workbench/autoselect_helper.rb', line 27

def autoselect_field(
  url:,
  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-url' => url,
    '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



60
61
62
63
64
65
66
# File 'app/helpers/workbench/autoselect_helper.rb', line 60

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