Module: CrudComponents::Presenters::ColumnSelection

Included in:
Collection, Record
Defined in:
lib/crud_components/presenters/column_selection.rb

Overview

Shared "which columns are shown" logic for any presenter that exposes available_fields (the permitted universe) and a param_prefix. Two knobs drive it (set as @picker and @picked_columns by the including presenter):

@picker         false → no picking (the fieldset governs); true → the view
              participates (a collection also renders the gear).
@picked_columns :auto → read the `?cols=` submit; an Array → that exact
              selection, **without ever reading the param** (the backend
              already resolved it — from a persisted pref, or from the
              param via {CrudComponents.selected_columns}).

The chosen selection is always intersected with available_fields — so a forged or stale selection can only hide or reorder columns, never reveal one the if: gate forbids. Mixed into both the collection and the record presenter, so a column picker drives a table and a detail view alike.

Instance Method Summary collapse

Instance Method Details

#column_visible?(field) ⇒ Boolean

Is this column part of the current view (ticked in the picker)?

Returns:

  • (Boolean)


26
# File 'lib/crud_components/presenters/column_selection.rb', line 26

def column_visible?(field) = fields.include?(field)

#field_groupsObject

The column-picker universe grouped by source model (Pipedrive-style): [[model, fields], …] with this collection's own model first, then each associated model in first-appearance order. So publisher, publisher.name and publisher.founded_on cluster under Publisher.



32
33
34
35
36
# File 'lib/crud_components/presenters/column_selection.rb', line 32

def field_groups
  by_model = available_fields.group_by(&:group_model)
  ordered = [model, *(by_model.keys - [model])]
  ordered.filter_map { |m| [m, by_model[m]] if by_model[m] }
end

#fieldsObject

The columns actually rendered: the permitted set, narrowed and ordered by the user's selection when there is one.



21
22
23
# File 'lib/crud_components/presenters/column_selection.rb', line 21

def fields
  @fields ||= select_visible(available_fields)
end

#group_heading(group_model) ⇒ Object

A picker group's heading text and icon (no prefix), for a grouped model.



39
# File 'lib/crud_components/presenters/column_selection.rb', line 39

def group_heading(group_model) = group_model.model_name.human

#group_icon(group_model) ⇒ Object



40
# File 'lib/crud_components/presenters/column_selection.rb', line 40

def group_icon(group_model) = Structure.for(group_model).icon

#visible_columnsObject

The ordered column names to show, or nil for "all permitted". The selection is independent of the gear: a resolved Array always applies (the backend decided it — the gear may live elsewhere, e.g. a standalone picker or a detail view), verbatim and without reading the param. :auto reads the ?cols= submit only when a gear is rendered here (picker: true); with no gear here, :auto means "don't narrow" (a stray ?cols= is ignored).



48
49
50
51
52
53
54
55
# File 'lib/crud_components/presenters/column_selection.rb', line 48

def visible_columns
  return @visible_columns if defined?(@visible_columns)

  @visible_columns =
    if @picked_columns.is_a?(Array) then @picked_columns
    elsif @picker then cols_param
    end
end