Class: CrudComponents::Fields::HasManyField

Inherits:
Base
  • Object
show all
Defined in:
lib/crud_components/fields/has_many_field.rb

Overview

has_many / habtm: truncated list of links (“a, b +3 more”). No derived filter or sort; opt in with ‘filter like: :assoc` (delegation).

Constant Summary

Constants inherited from Base

Base::NON_EDITABLE_COLUMNS

Instance Attribute Summary

Attributes inherited from Base

#facets, #model, #name, #options

Instance Method Summary collapse

Methods inherited from Base

#apply_derived_filter, #apply_filter, #apply_filter_facet, #apply_sort, #column, #custom_header?, #declared_preloads, #derived_filter_control, #derived_filterable?, #derived_sortable?, #editable?, #editable_permitted?, #filter_choices, #filter_control, #filter_facet, #filter_includes_null?, #filterable?, #form_partial, #group_label, #header, #header_actions, #human_name, #initialize, #nullable?, #permitted?, #picker_label, #range_filter?, #render_block, #renderer, #renderer_options, #sort_facet, #sortable?, #value

Constructor Details

This class inherits a constructor from CrudComponents::Fields::Base

Instance Method Details

The index a “+n more” / list link points at: the nested route under the owner if it resolves, else the target’s index filtered by the owner. Resolved in the view (RouteResolver); here we just expose the association so the renderer can build it.



31
# File 'lib/crud_components/fields/has_many_field.rb', line 31

def collection_link_target = target

#default_editable?Boolean

Returns:

  • (Boolean)


37
# File 'lib/crud_components/fields/has_many_field.rb', line 37

def default_editable? = habtm?

#default_rendererObject



6
# File 'lib/crud_components/fields/has_many_field.rb', line 6

def default_renderer = :association_list

#eager_loadObject

Load the association, nesting each target’s identity_preloads (+ this column’s ‘preload:`) so rendering the list’s labels never N+1s.



22
23
24
25
# File 'lib/crud_components/fields/has_many_field.rb', line 22

def eager_load
  nested = (target_structure.identity_preloads + declared_preloads).uniq
  [nested.empty? ? name : { name => nested }]
end

#form_choicesObject



42
43
44
45
# File 'lib/crud_components/fields/has_many_field.rb', line 42

def form_choices
  structure = target_structure
  target.all.map { |record| [structure.label_for(record).to_s, record.id] }.sort_by(&:first)
end

#form_controlObject



38
# File 'lib/crud_components/fields/has_many_field.rb', line 38

def form_control = :habtm

#group_modelObject

Picker grouping: a has_many/habtm column anchors its target’s group, so ‘authors`, `authors.name` and `authors.email` all sit under “Author”.



18
# File 'lib/crud_components/fields/has_many_field.rb', line 18

def group_model = target

#habtm?Boolean

── forms ────────────────────────────────────────────────────────────Editable only for habtm (and simple has_many) via the *_ids setter —reassigning ids is safe; nested attributes are out of scope.

Returns:

  • (Boolean)


36
# File 'lib/crud_components/fields/has_many_field.rb', line 36

def habtm? = reflection.macro == :has_and_belongs_to_many

#ids_methodObject



39
# File 'lib/crud_components/fields/has_many_field.rb', line 39

def ids_method = "#{name.to_s.singularize}_ids".to_sym

#permit_paramObject



40
# File 'lib/crud_components/fields/has_many_field.rb', line 40

def permit_param = { ids_method => [] }

#reflectionObject



8
9
10
# File 'lib/crud_components/fields/has_many_field.rb', line 8

def reflection
  @reflection ||= model.reflect_on_association(name)
end

#targetObject



12
13
14
# File 'lib/crud_components/fields/has_many_field.rb', line 12

def target
  reflection.klass
end

#target_structureObject



47
# File 'lib/crud_components/fields/has_many_field.rb', line 47

def target_structure = Structure.for(target)