Module: ActiveAdminAddons::SelectHelpers
  
  
  
  
    
      Instance Method Summary
      collapse
    
    
  
  
  
  
  
  
  
  
  
  
  #attr_options, #control_attributes, #css_classes, #data_attr_options, #get_data_attr_value, #load_attr, #load_class, #load_data_attr
  
  
  
  
  
  
  
  
  
  #association_name, #build_virtual_attr, #input_related_items, #input_value, #method_model, #model_name, #object_class, #tableize_method, #translated_method, #url_from_method, #valid_method, #valid_object
  
    Instance Method Details
    
      
  
  
    #active_record_select?  ⇒ Boolean 
  
  
  
  
    | 
51
52
53
54
55
56 | # File 'lib/activeadmin_addons/support/input_helpers/select_helpers.rb', line 51
def active_record_select?
  active_record_relation?(collection) &&
    active_record_relation?(selected_collection)
rescue NameError
  false
end
 | 
 
    
      
  
  
    #array_to_select_options  ⇒ Object 
  
  
  
  
    | 
6
7
8
9
10
11
12
13
14 | # File 'lib/activeadmin_addons/support/input_helpers/select_helpers.rb', line 6
def array_to_select_options
  selected_values = input_value.to_s.split(",")
  array = collection.map(&:to_s) + selected_values
  array.sort.map do |value|
    option = { id: value, text: value }
    option[:selected] = "selected" if selected_values.include?(value)
    option
  end.uniq
end
 | 
 
    
      
  
  
    #collection_to_select_options  ⇒ Object 
  
  
  
  
    | 
28
29
30
31
32
33
34
35
36
37
38
39
40
41 | # File 'lib/activeadmin_addons/support/input_helpers/select_helpers.rb', line 28
def collection_to_select_options
  complete_collection = collection + selected_collection
  complete_collection.map do |item|
    option = item_to_select_option(item)
    yield(item, option) if block_given?
    if selected_collection.include?(item)
      load_data_attr(:selected, value: option.dup, formatter: :to_json)
      option[:selected] = "selected"
    end
    option
  end.uniq
end
 | 
 
    
      
  
  
    #initial_collection_to_select_options  ⇒ Object 
  
  
  
  
    | 
16
17
18
19
20
21
22
23
24
25
26 | # File 'lib/activeadmin_addons/support/input_helpers/select_helpers.rb', line 16
def initial_collection_to_select_options
  initial_options = [[nil]] 
  selected = selected_item
  if selected
    selected_option = item_to_select_option(selected)
    initial_options << [selected_option[:text], selected_option[:id]]
  end
  initial_options
end
 | 
 
    
      
  
  
    #item_to_select_option(item)  ⇒ Object 
  
  
  
  
    | 
43
44
45
46
47
48
49 | # File 'lib/activeadmin_addons/support/input_helpers/select_helpers.rb', line 43
def item_to_select_option(item)
  return unless item
  {
    id: item.send((valid_options[:value] || :id)),
    text: item.send((valid_options[:display_name] || :name))
  }
end
 | 
 
    
      
  
  
    #selected_collection  ⇒ Object 
  
  
  
  
    | 
58
59
60 | # File 'lib/activeadmin_addons/support/input_helpers/select_helpers.rb', line 58
def selected_collection
  method_model.where(id: input_value)
end
 | 
 
    
      
  
  
    #selected_item  ⇒ Object 
  
  
  
  
    | 
62
63
64 | # File 'lib/activeadmin_addons/support/input_helpers/select_helpers.rb', line 62
def selected_item
  selected_collection.first
end
 |