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"). Filters by the children's label (the names shown in the list); no derived sort.

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_filter, #apply_filter_facet, #apply_sort, #column, #custom_header?, #declared_preloads, #derived_filter_control, #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?, #typed_filter, #value

Constructor Details

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

Instance Method Details

#apply_derived_filter(scope, value: nil) ⇒ Object



63
64
65
66
67
# File 'lib/crud_components/fields/has_many_field.rb', line 63

def apply_derived_filter(scope, value: nil, **)
  return scope unless value

  LikeSpec.apply(scope, name, value)
end

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

#derived_filterable?Boolean

── filter ───────────────────────────────────────────────────────────── "Filter what you see": a free-text contains-match against the children's label — the names shown in the list — so an owner matches when at least one of its children does. Skipped when the target's label is a block/columnless (no single column to match). Mirrors belongs_to, which filters by its target's label the same way.

Returns:

  • (Boolean)


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

def derived_filterable? = target_structure.label_field_name.present?

#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

#search_spec_entryObject

Default ?q= reaches the children's label (the names shown in the list). Skipped when the target's label is a block/columnless.



51
52
53
# File 'lib/crud_components/fields/has_many_field.rb', line 51

def search_spec_entry
  name if target_structure.label_field_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)