Class: Compony::ModelFields::Anchormodel
- 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
-
.collect(flat_array, label_method: :label, key_method: :key) ⇒ Object
Takes an array of objects implementing the methods
label
andkey
and returns an array suitable for simple_form select fields.
Instance Method Summary collapse
- #simpleform_input(form, _component, name: nil, **input_opts) ⇒ Object
- #simpleform_input_hidden(form, _component, name: nil, **input_opts) ⇒ Object
- #value_for(data, controller: nil, **_) ⇒ Object
Methods inherited from Base
#association?, #initialize, #label, #multi?, #schema_line, #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
#simpleform_input(form, _component, name: nil, **input_opts) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/compony/model_fields/anchormodel.rb', line 13 def simpleform_input(form, _component, name: nil, **input_opts) anchormodel_attribute = @model_class.anchormodel_attributes[@name] anchormodel_class = anchormodel_attribute.anchormodel_class input_opts[:input_html] ||= {} # Attempt to read selected key from html input options "value", as the caller might not know that this is a select. selected_key = input_opts[:input_html].delete(:value) # can also be both nil or blank if selected_key.blank? && form.object # No selected key override present and a model is present, use the model to find out what to select selected_cst = form.object.send(@name) selected_key = selected_cst&.key || anchormodel_class.all.first end opts = { collection: self.class.collect(anchormodel_class.all), label_method: :first, value_method: :second, selected: selected_key, # if used in select checked: selected_key, # if used in radio buttons include_blank: anchormodel_attribute.optional }.merge(input_opts) return form.input name || @name, **opts end |
#simpleform_input_hidden(form, _component, name: nil, **input_opts) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/compony/model_fields/anchormodel.rb', line 35 def simpleform_input_hidden(form, _component, name: nil, **input_opts) if form.object selected_cst = form.object.send(@name) input_opts[:input_html] ||= {} input_opts[:input_html][:value] = selected_cst.is_a?(::Anchormodel) ? selected_cst.key : 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 |