Module: ActiveAdminAddons::SelectHelpers

Includes:
InputMethods, InputOptionsHandler
Included in:
NestedLevelInput, SearchSelectInput, TagsInput
Defined in:
lib/activeadmin_addons/support/input_helpers/select_helpers.rb

Instance Method Summary collapse

Methods included from InputOptionsHandler

#attr_options, #control_attributes, #css_classes, #data_attr_options, #get_data_attr_value, #load_attr, #load_class, #load_data_attr

Methods included from InputMethods

#association_name, #build_virtual_attr, #input_association_value, #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

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'lib/activeadmin_addons/support/input_helpers/select_helpers.rb', line 52

def active_record_select?
  active_record_relation?(collection) &&
    active_record_relation?(selected_collection)
rescue NameError
  false
end

#array_to_select_optionsObject



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.is_a?(Array) ? input_value : 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_optionsObject



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_optionsObject



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]] # add blank option
  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
50
# 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_collectionObject



59
60
61
62
63
64
65
66
67
# File 'lib/activeadmin_addons/support/input_helpers/select_helpers.rb', line 59

def selected_collection
  @selected_collection ||= begin
    if active_record_relation?(collection)
      collection.model.where(id: input_value)
    else
      method_model.where(id: input_value)
    end
  end
end

#selected_itemObject



69
70
71
72
73
74
75
# File 'lib/activeadmin_addons/support/input_helpers/select_helpers.rb', line 69

def selected_item
  @selected_item ||= begin
                       input_association_value
                     rescue NoMethodError
                       selected_collection.first
                     end
end