Class: TagsInput
- Inherits:
-
TomSelectInput
- Object
- SimpleForm::Inputs::CollectionSelectInput
- TomSelectInput
- TagsInput
- 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
Instance Method Details
#collection ⇒ Object
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 [:collection] if [:collection].present? # Get all available tags from the centralized Tag model = Tag.order(:name).pluck(:name) # Get currently selected tags from the object = 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. : {} all_tag_names = ( + + 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( = nil) # Enable multiple selection by default for tags [:multiple] = true # Enable creation of new tags by default (can be overridden with create: false) [:create] = true unless .key?(:create) && [:create] == false # Set default placeholder [:placeholder] ||= 'Select or create tags...' # Add tag-specific data attributes [:data] ||= {} [:data]['tom-select-allow-empty-option-value'] = true super end |