Class: Headmin::Form::AssociationView

Inherits:
ViewModel
  • Object
show all
Includes:
Hintable, InputGroupable, Labelable, Listable, Placeholderable, Validatable, Wrappable
Defined in:
app/models/headmin/form/association_view.rb

Defined Under Namespace

Classes: AssociationDoesNotExistError

Instance Method Summary collapse

Methods inherited from ViewModel

#attributes, #initialize, #to_hash

Constructor Details

This class inherits a constructor from ViewModel

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ViewModel

Instance Method Details

#attribute_with_idObject



39
40
41
42
43
44
45
46
47
# File 'app/models/headmin/form/association_view.rb', line 39

def attribute_with_id
  attribute_with_id = collection? ? "#{association_foreign_key}s" : foreign_key

  if attribute_with_id.nil?
    raise(AssociationDoesNotExistError, "Association attribute that was passed does not exist.")
  else
    attribute_with_id
  end
end

#collectionObject



49
50
51
# File 'app/models/headmin/form/association_view.rb', line 49

def collection
  association_class.all.map { |item| [item.to_s, item.id] }
end

#input_group_optionsObject



21
22
23
24
25
# File 'app/models/headmin/form/association_view.rb', line 21

def input_group_options
  default_input_group_options
    .deep_merge(label_input_group_options)
    .deep_merge(@input_group || {})
end

#input_optionsObject



12
13
14
15
16
17
18
19
# File 'app/models/headmin/form/association_view.rb', line 12

def input_options
  keys = attributes - %i[append attribute collection float form input_group include_blank label prepend validate selected tags wrapper]
  options = to_h.slice(*keys)
  def_input_options = default_input_options
  def_input_options = def_input_options.deep_merge(options)
  def_input_options = def_input_options.merge({multiple: true}) if collection?
  def_input_options
end

#select_optionsObject



33
34
35
36
37
# File 'app/models/headmin/form/association_view.rb', line 33

def select_options
  keys = %i[include_blank selected]
  options = to_h.slice(*keys)
  default_options.deep_merge(options)
end

#wrapper_optionsObject



27
28
29
30
31
# File 'app/models/headmin/form/association_view.rb', line 27

def wrapper_options
  default_wrapper_options.deep_merge({
    class: ["mb-3", ("form-floating" if float)]
  }).deep_merge(@wrapper || {})
end