Class: CrudComponents::Fields::HasManyField
- 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
Instance Attribute Summary
Attributes inherited from Base
#facets, #model, #name, #options
Instance Method Summary collapse
- #apply_derived_filter(scope, value: nil) ⇒ Object
-
#collection_link_target ⇒ Object
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.
- #default_editable? ⇒ Boolean
- #default_renderer ⇒ Object
-
#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.
-
#eager_load ⇒ Object
Load the association, nesting each target's identity_preloads (+ this column's
preload:) so rendering the list's labels never N+1s. - #form_choices ⇒ Object
- #form_control ⇒ Object
-
#group_model ⇒ Object
Picker grouping: a has_many/habtm column anchors its target's group, so
authors,authors.nameandauthors.emailall sit under "Author". -
#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.
- #ids_method ⇒ Object
- #permit_param ⇒ Object
- #reflection ⇒ Object
-
#search_spec_entry ⇒ Object
Default ?q= reaches the children's label (the names shown in the list).
- #target ⇒ Object
- #target_structure ⇒ Object
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 |
#collection_link_target ⇒ Object
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
37 |
# File 'lib/crud_components/fields/has_many_field.rb', line 37 def default_editable? = habtm? |
#default_renderer ⇒ Object
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.
61 |
# File 'lib/crud_components/fields/has_many_field.rb', line 61 def derived_filterable? = target_structure.label_field_name.present? |
#eager_load ⇒ Object
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_choices ⇒ Object
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_control ⇒ Object
38 |
# File 'lib/crud_components/fields/has_many_field.rb', line 38 def form_control = :habtm |
#group_model ⇒ Object
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.
36 |
# File 'lib/crud_components/fields/has_many_field.rb', line 36 def habtm? = reflection.macro == :has_and_belongs_to_many |
#ids_method ⇒ Object
39 |
# File 'lib/crud_components/fields/has_many_field.rb', line 39 def ids_method = "#{name.to_s.singularize}_ids".to_sym |
#permit_param ⇒ Object
40 |
# File 'lib/crud_components/fields/has_many_field.rb', line 40 def permit_param = { ids_method => [] } |
#reflection ⇒ Object
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_entry ⇒ Object
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 |
#target ⇒ Object
12 13 14 |
# File 'lib/crud_components/fields/has_many_field.rb', line 12 def target reflection.klass end |