Class: Formstrap::AssociationView

Inherits:
ViewModel
  • Object
show all
Includes:
Hintable, InputGroupable, Labelable, Listable, Placeholderable, Validatable, Wrappable
Defined in:
app/models/formstrap/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



38
39
40
41
42
43
44
45
46
# File 'app/models/formstrap/association_view.rb', line 38

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



48
49
50
# File 'app/models/formstrap/association_view.rb', line 48

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

#input_group_optionsObject



20
21
22
23
24
# File 'app/models/formstrap/association_view.rb', line 20

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

#input_optionsObject



11
12
13
14
15
16
17
18
# File 'app/models/formstrap/association_view.rb', line 11

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



32
33
34
35
36
# File 'app/models/formstrap/association_view.rb', line 32

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

#wrapper_optionsObject



26
27
28
29
30
# File 'app/models/formstrap/association_view.rb', line 26

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