Class: TagsInput
- Inherits:
-
Formtastic::Inputs::StringInput
- Object
- Formtastic::Inputs::StringInput
- TagsInput
- Defined in:
- app/inputs/tags_input.rb
Instance Method Summary collapse
- #build_virtual_attr ⇒ Object
- #input_html_options ⇒ Object
- #model_name ⇒ Object
- #relation? ⇒ Boolean
- #relation_tag_options ⇒ Object
- #render_hidden_items ⇒ Object
- #render_tags ⇒ Object
- #render_tags_from_relation ⇒ Object
- #tag_options ⇒ Object
- #to_html ⇒ Object
Instance Method Details
#build_virtual_attr ⇒ Object
60 61 62 63 64 65 |
# File 'app/inputs/tags_input.rb', line 60 def build_virtual_attr attr_name = "#{method}_str" @object.singleton_class.send(:attr_accessor, attr_name) unless @object.respond_to?(attr_name) @object.send("#{attr_name}=", @object.send(method).join(",")) attr_name.to_sym end |
#input_html_options ⇒ Object
2 3 4 5 6 |
# File 'app/inputs/tags_input.rb', line 2 def opts = {} opts[:class] = "select2-tags" super.merge(opts) end |
#model_name ⇒ Object
80 81 82 |
# File 'app/inputs/tags_input.rb', line 80 def model_name @object.class.to_s.underscore end |
#relation? ⇒ Boolean
12 13 14 15 |
# File 'app/inputs/tags_input.rb', line 12 def relation? @object.send(attributized_method_name).respond_to?(:each) && @options[:collection].class.name == "ActiveRecord::Relation" end |
#relation_tag_options ⇒ Object
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'app/inputs/tags_input.rb', line 44 def opts = opts["data-model"] = model_name opts["data-method"] = method if @options[:collection].class.name != "ActiveRecord::Relation" fail "collection must be an ActiveRecord::Relation instance" end opts["data-collection"] = @options[:collection].map do |item| { id: item.id, text: item.send((@options[:display_name] || "name")) } end.to_json opts end |
#render_hidden_items ⇒ Object
67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'app/inputs/tags_input.rb', line 67 def render_hidden_items prefix = "#{model_name}_#{method}" attr_name = "#{model_name}[#{method}][]" template.content_tag(:div, id: "#{prefix}_selected_values") do template.concat( builder.hidden_field(method, id: "#{prefix}_empty", name: attr_name, value: nil)) @object.send(method).each do |item_id| attr_id = "#{prefix}_#{item_id}" template.concat(builder.hidden_field(method, id: attr_id, name: attr_name, value: item_id)) end end end |
#render_tags ⇒ Object
17 18 19 20 21 22 23 24 |
# File 'app/inputs/tags_input.rb', line 17 def input_wrapping do [ label_html, builder.text_field(method, ) ].join("\n").html_safe end end |
#render_tags_from_relation ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 |
# File 'app/inputs/tags_input.rb', line 26 def virtual_input_name = build_virtual_attr input_wrapping do [ label_html, render_hidden_items, builder.text_field(virtual_input_name, ) ].flatten.join("\n").html_safe end end |
#tag_options ⇒ Object
38 39 40 41 42 |
# File 'app/inputs/tags_input.rb', line 38 def opts = opts["data-collection"] = (@options[:collection] || []).to_json opts end |
#to_html ⇒ Object
8 9 10 |
# File 'app/inputs/tags_input.rb', line 8 def to_html relation? ? : end |