Class: TagsInput

Inherits:
TomSelectInput
  • Object
show all
Defined in:
app/inputs/tags_input.rb

Overview

Custom SimpleForm input for centralized tag selection

This input provides a TomSelect-powered multiselect that:

  • Shows all available tags from the Tag model
  • Allows creating new tags on the fly
  • Works with the Taggable concern's tags= setter

Usage in forms:
<%= f.input :tags, as: :tags %>
<%= f.input :tags, as: :tags, placeholder: 'Select or create tags...' %>
<%= f.input :tags, as: :tags, create: false %> # Disable new tag creation

Instance Method Summary collapse

Methods inherited from TomSelectInput

#input_html_classes

Instance Method Details

#collectionObject



33
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
# File 'app/inputs/tags_input.rb', line 33

def collection
  # If a custom collection is provided, use it
  return options[:collection] if options[:collection].present?

  # Get all available tags from the centralized Tag model
  available_tags = Tag.order(:name).pluck(:name)

  # Get currently selected tags from the object
  current_tags = Array(selected_value || []).compact

  # Build a tag→path map for all CMS page tags so users can discover them
  # even before they have ever been applied, and see the associated URL path.
  page_tag_map = defined?(DigitalAsset) ? DigitalAsset.available_page_tags_with_paths : {}

  all_tag_names = (available_tags + current_tags + page_tag_map.keys).uniq.sort

  # Return [label, value] pairs so TomSelect can search by path while the submitted
  # value remains the plain tag name.  Page tags get "tag — /path" as their label;
  # regular tags use the name for both to keep SimpleForm's detect_collection_methods happy.
  all_tag_names.map do |tag_name|
    if (path = page_tag_map[tag_name])
      ["#{tag_name} \u2014 #{path}", tag_name]
    else
      [tag_name, tag_name]
    end
  end
end

#input(wrapper_options = nil) ⇒ Object



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'app/inputs/tags_input.rb', line 16

def input(wrapper_options = nil)
  # Enable multiple selection by default for tags
  input_html_options[:multiple] = true

  # Enable creation of new tags by default (can be overridden with create: false)
  options[:create] = true unless options.key?(:create) && options[:create] == false

  # Set default placeholder
  options[:placeholder] ||= 'Select or create tags...'

  # Add tag-specific data attributes
  input_html_options[:data] ||= {}
  input_html_options[:data]['tom-select-allow-empty-option-value'] = true

  super
end