Class: UiBibzInputs::CollectionInput

Inherits:
SimpleForm::Inputs::CollectionInput
  • Object
show all
Includes:
ActionView::Helpers::FormOptionsHelper, UiBibz::Ui::Core::Forms::Selects
Defined in:
lib/ui_bibz/inputs/ui_bibz_inputs/collection_input.rb

Instance Method Summary collapse

Instance Method Details

#collectionObject



62
63
64
65
66
67
68
69
# File 'lib/ui_bibz/inputs/ui_bibz_inputs/collection_input.rb', line 62

def collection
  @collection ||= if options[:grouped]
                    grouped_collection.map { |collection| collection.try(:send, group_method) }.detect(&:present?) || []
                  else
                    collection = options.delete(:collection) || self.class.boolean_collection
                    collection.respond_to?(:call) ? collection.call : collection.to_a
                  end
end

#group_label_methodObject



75
76
77
78
79
80
81
82
83
84
# File 'lib/ui_bibz/inputs/ui_bibz_inputs/collection_input.rb', line 75

def group_label_method
  label = options.delete(:group_label_method)

  unless label
    common_method_for = detect_common_display_methods(detect_collection_classes(grouped_collection))
    label = common_method_for[:label]
  end

  label
end

#group_methodObject



71
72
73
# File 'lib/ui_bibz/inputs/ui_bibz_inputs/collection_input.rb', line 71

def group_method
  @group_method ||= options.delete(:group_method)
end

#grouped_collectionObject



51
52
53
54
55
56
# File 'lib/ui_bibz/inputs/ui_bibz_inputs/collection_input.rb', line 51

def grouped_collection
  @grouped_collection ||= begin
    grouped_collection = options.delete(:collection)
    grouped_collection.respond_to?(:call) ? grouped_collection.call : grouped_collection.to_a
  end
end

#input_attribute_nameObject



58
59
60
# File 'lib/ui_bibz/inputs/ui_bibz_inputs/collection_input.rb', line 58

def input_attribute_name
  "#{@builder.object_name}[#{attribute_name}]"
end

#input_html_optionsObject



8
9
10
11
12
13
14
15
16
# File 'lib/ui_bibz/inputs/ui_bibz_inputs/collection_input.rb', line 8

def input_html_options
  opts = super
  input_classes = UiBibz::Utils::Screwdriver.join_classes(@builder.options[:input_html].try(:[], :class), options[:input_html].try(:[], :class), options[:class])
  opts = opts.merge({ prompt: options[:prompt] })               if options[:prompt].present?
  opts = opts.merge({ disabled: options[:disabled] })           if options[:disabled].present?
  opts = opts.merge({ include_blank: options[:include_blank] }) if options[:include_blank].present?
  opts = opts.merge({ multiple: options[:multiple] })           if options[:multiple].present?
  (@builder.options[:input_html] || {}).merge(options[:input_html] || {}).merge(opts || {}).merge({ class: input_classes })
end

#new_optionsObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ui_bibz/inputs/ui_bibz_inputs/collection_input.rb', line 18

def new_options
  label_method, value_method = detect_collection_methods

  if options[:option_tags].nil?
    if options[:grouped] == true
      options.merge({ option_tags: option_groups_from_collection_for_select(
        grouped_collection,
        group_method, group_label_method,
        value_method, label_method,
        new_value
      ) })
    else
      options.merge({ option_tags: options_from_collection_for_select(
        collection, value_method, label_method, new_value
      ) })
    end
  else
    options
  end
end

#new_valueObject



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/ui_bibz/inputs/ui_bibz_inputs/collection_input.rb', line 39

def new_value
  if @builder.object.send(attribute_name).blank?
    @builder.object.send(attribute_name)
  else
    begin
      ActiveSupport::JSON.decode(@builder.object.send(attribute_name).to_s)
    rescue StandardError
      @builder.object.send(attribute_name)
    end
  end
end