Class: Compony::ModelFields::Anchormodel

Inherits:
Base
  • Object
show all
Defined in:
lib/compony/model_fields/anchormodel.rb

Instance Attribute Summary

Attributes inherited from Base

#extra_attrs, #model_class, #name, #schema_key

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#association?, #initialize, #label, #multi?, #schema_line, #simpleform_input, #transform_and_join

Constructor Details

This class inherits a constructor from Compony::ModelFields::Base

Class Method Details

.collect(flat_array, label_method: :label, key_method: :key) ⇒ Object

Takes an array of objects implementing the methods label and key and returns an array suitable for simple_form select fields.



5
6
7
# File 'lib/compony/model_fields/anchormodel.rb', line 5

def self.collect(flat_array, label_method: :label, key_method: :key)
  return flat_array.map { |entry| [entry.send(label_method), entry.send(key_method)] }
end

Instance Method Details

#ransack_filter_input(form, **input_opts) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/compony/model_fields/anchormodel.rb', line 28

def ransack_filter_input(form, **input_opts)
  form.select(
    ransack_filter_name,
    self.class.collect(@model_class.anchormodel_attributes[@name].anchormodel_class.all),
    { include_blank: true },
    { class: input_opts[:filter_select_class] }
  )
end

#ransack_filter_nameObject



24
25
26
# File 'lib/compony/model_fields/anchormodel.rb', line 24

def ransack_filter_name
  :"#{@name}_eq"
end

#simpleform_input_hidden(form, _component, name: nil, **input_opts) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/compony/model_fields/anchormodel.rb', line 13

def simpleform_input_hidden(form, _component, name: nil, **input_opts)
  if form.object
    selected_cst = form.object.send(@name)
    am_attr = form.object.class.anchormodel_attributes[@name]
    am_serializer = (am_attr.multiple? ? ::Anchormodel::ActiveModelTypeValueMulti : ::Anchormodel::ActiveModelTypeValueSingle).new(am_attr)
    input_opts[:input_html] ||= {}
    input_opts[:input_html][:value] = am_serializer.serialize(selected_cst)
  end
  return form.input name || @name, as: :hidden, **input_opts
end

#value_for(data, controller: nil, **_) ⇒ Object



9
10
11
# File 'lib/compony/model_fields/anchormodel.rb', line 9

def value_for(data, controller: nil, **_)
  return transform_and_join(data.send(@name), controller:) { |el| el&.label }
end