Class: Formtastic::Inputs::SelectManyInput

Inherits:
SelectInput
  • Object
show all
Defined in:
lib/formtastic/inputs/select_many_input.rb

Instance Method Summary collapse

Instance Method Details

#buttons_htmlObject



33
34
35
36
37
38
39
40
# File 'lib/formtastic/inputs/select_many_input.rb', line 33

def buttons_html
  template.( :div, class: 'buttons' ) do
    template.link_to( '→'.html_safe, 'Javascript:void(0)', class: 'add' ) +
    template.link_to( '←'.html_safe, 'Javascript:void(0)', class: 'remove' ) +
    template.link_to( '↑'.html_safe, 'Javascript:void(0)', class: 'move_up' ) +
    template.link_to( '↓'.html_safe, 'Javascript:void(0)', class: 'move_down' )
  end
end

#hidden_inputObject



23
24
25
26
27
28
29
30
31
# File 'lib/formtastic/inputs/select_many_input.rb', line 23

def hidden_input
  template.( :div, class: 'values', 'data-name': input_html_options[:name] ) do
    values = object.send( input_name )
    values = [values] if values.is_a? Fixnum
    values.each do |value|
      template.concat template.hidden_field_tag( input_html_options[:name], value, {id: nil} )
    end if values
  end
end

#search_box_htmlObject



42
43
44
45
# File 'lib/formtastic/inputs/select_many_input.rb', line 42

def search_box_html
  @opts ||= {id: nil, class: 'search-select', placeholder: options.delete( :placeholder ), 'data-remote-collection': options[:'data-remote-collection'], 'data-search': options[:search_param] ? options[:search_param] : 'name_contains', 'data-text': options[:text_key] ? options[:text_key] : 'name', 'data-value': options[:value_key] ? options[:value_key] : 'id'}
  template.text_field_tag( nil, '', @opts )
end

#select_dst_htmlObject



60
61
62
63
64
65
66
# File 'lib/formtastic/inputs/select_many_input.rb', line 60

def select_dst_html
  selected = object.send( input_name )
  selected = [selected] if selected.is_a? Fixnum
  coll = selected ? collection.select { |option| selected.include?( option[1] ) } : collection
  opts = input_options.merge( name: nil, id: nil, multiple: true, 'data-select': 'dst', size: options[:size] ? options[:size] : 4 )
  template.select_tag nil, template.options_for_select( coll ), opts
end

#select_src_htmlObject



47
48
49
50
51
52
53
54
55
56
57
58
# File 'lib/formtastic/inputs/select_many_input.rb', line 47

def select_src_html
  coll = if options[:'data-remote-collection']
    []
  else
    # TODO: add option unique ?
    selected = object.send( input_name )
    selected = [selected] if selected.is_a? Fixnum
    selected ? collection.select { |option| !selected.include?( option[1] ) } : collection
  end
  opts = input_options.merge( name: nil, id: nil, multiple: true, 'data-select': 'src', size: options[:size] ? options[:size] : 4 )
  template.select_tag nil, template.options_for_select( coll ), opts
end

#to_htmlObject



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# File 'lib/formtastic/inputs/select_many_input.rb', line 4

def to_html
  options[:'data-remote-collection'] = options.delete( :remote_collection )
  opts = { class: 'select-many-inputs' }
  opts[:sortable] = options.delete( :sortable ) if options[:sortable]
  input_wrapping do
    label_html <<
    template.( :div, opts ) do
      hidden_input <<
      search_box_html <<
      template.( :span, '', class: 'empty'  ) <<
      template.( :span, template.t( 'inputs.select_many.available' ), class: 'available' ) <<
      template.( :span, template.t( 'inputs.select_many.selected'  ), class: 'selected' ) <<
      select_src_html <<
      buttons_html <<
      select_dst_html
    end
  end
end