Class: TagsInput

Inherits:
Formtastic::Inputs::StringInput
  • Object
show all
Defined in:
app/inputs/tags_input.rb

Instance Method Summary collapse

Instance Method Details

#build_virtual_attrObject



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_optionsObject



2
3
4
5
6
# File 'app/inputs/tags_input.rb', line 2

def input_html_options
  opts = {}
  opts[:class] = "select2-tags"
  super.merge(opts)
end

#model_nameObject



80
81
82
# File 'app/inputs/tags_input.rb', line 80

def model_name
  @object.class.to_s.underscore
end

#relation?Boolean

Returns:

  • (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_optionsObject



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# File 'app/inputs/tags_input.rb', line 44

def relation_tag_options
  opts = input_html_options
  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_itemsObject



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.(: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_tagsObject



17
18
19
20
21
22
23
24
# File 'app/inputs/tags_input.rb', line 17

def render_tags
  input_wrapping do
    [
      label_html,
      builder.text_field(method, tag_options)
    ].join("\n").html_safe
  end
end

#render_tags_from_relationObject



26
27
28
29
30
31
32
33
34
35
36
# File 'app/inputs/tags_input.rb', line 26

def render_tags_from_relation
  virtual_input_name = build_virtual_attr

  input_wrapping do
    [
      label_html,
      render_hidden_items,
      builder.text_field(virtual_input_name, relation_tag_options)
    ].flatten.join("\n").html_safe
  end
end

#tag_optionsObject



38
39
40
41
42
# File 'app/inputs/tags_input.rb', line 38

def tag_options
  opts = input_html_options
  opts["data-collection"] = (@options[:collection] || []).to_json
  opts
end

#to_htmlObject



8
9
10
# File 'app/inputs/tags_input.rb', line 8

def to_html
  relation? ? render_tags_from_relation : render_tags
end