Class: Compony::Components::Show

Inherits:
Compony::Component show all
Includes:
Compony::ComponentMixins::Resourceful
Defined in:
lib/compony/components/show.rb

Overview

This component is used for the Rails show paradigm.

Instance Attribute Summary

Attributes included from Compony::ComponentMixins::Resourceful

#data, #global_after_assign_attributes_block, #global_after_load_data_block, #global_assign_attributes_block, #global_load_data_block, #global_store_data_block

Attributes inherited from Compony::Component

#comp_opts, #content_blocks, #parent_comp

Instance Method Summary collapse

Methods included from Compony::ComponentMixins::Resourceful

#after_assign_attributes, #after_load_data, #assign_attributes, #data_class, #load_data, #resourceful?, #resourceful_sub_comp, #store_data

Methods inherited from Compony::Component

#action, #before_render, comp_cst, comp_name, #content, family_cst, family_name, #id, #inspect, #param_name, #path, #path_hash, #remove_content, #remove_content!, #render, #render_actions, #resourceful?, #root_comp, #root_comp?, setup, #skip_action, #sub_comp

Constructor Details

#initialize(skip_columns: []) ⇒ Show

Returns a new instance of Show.

Parameters:

  • skip_columns (Array) (defaults to: [])

    Column names to be skipped in the case where this component is nested and therefore instanciated by a parent comp.



63
64
65
66
67
# File 'lib/compony/components/show.rb', line 63

def initialize(*, skip_columns: [], **)
  @columns = NaturalOrdering.new
  @skipped_columns = skip_columns.map(&:to_sym)
  super(*, **)
end

Instance Method Details

#all_field_columns(data) ⇒ Object

DSL method Goes through the fields of the given data and adds a field column for every field found.

Parameters:

  • data (ApplicationModel)

    Compony-enriched model that will be queried for fields.



111
112
113
# File 'lib/compony/components/show.rb', line 111

def all_field_columns(data)
  data.fields.each_key { |field_name| column(field_name) }
end

#column(name, label: nil, class: nil, link_opts: {}, link_to_component: :show, &block) ⇒ Object

Adds a column. The term column is for consistency with the List component and columns are typically model fields / attributes.

Parameters:

  • name (String, Symbol)

    The technical name of the attribute this column will be for.

  • label (nil, String) (defaults to: nil)

    The human displayed label for this attribte. If nil, will consider name to be a field name and load the field's label.

  • class (nil, String) (defaults to: nil)

    Extra CSS classes for the column's value.

  • link_opts (Hash) (defaults to: {})

    Options to pass to the link_to helper. Only used in the case of a field column that will produce a link (e.g. accociation).

  • link_to_component (Symbol) (defaults to: :show)

    In the case a link is produced (e.g. association), defines the component the link points to. Detaults to :show.

  • block (Block)

    Custom code to run in order to provide the displayed value. Will be given the current record.



76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/compony/components/show.rb', line 76

def column(name, label: nil, class: nil, link_opts: {}, link_to_component: :show, **, &block)
  name = name.to_sym
  unless block_given?
    # Assume field column
    field = data_class.fields[name] || fail("Field #{name.inspect} was not found for data class #{data_class}")
    block = proc do |record|
      if controller.current_ability.permitted_attributes(:show, record).include?(field.name.to_sym)
        next field.value_for(record, link_to_component:, controller:, link_opts:).to_s
      else
        Rails.logger.debug { "Skipping show col #{field.name.inspect} because the current user is not allowed to perform show on #{data}." }
        nil
      end
    end
  end
  @columns.natural_push(name, block, label: label || field.label, class:, **)
end

#columns(*col_names) ⇒ Object

DSL method Adds multiple columns that have identical kwargs, e.g. class (see column). Typically only used for bulk-adding model fields.

Parameters:

  • col_names (String)

    Names of the fields in @data that are to be added as attributes.



96
97
98
# File 'lib/compony/components/show.rb', line 96

def columns(*col_names, **)
  col_names.each { |col_name| column(col_name, **) }
end

#skip_column(name) ⇒ Object

DSL method Marks a column as skipped. Useful only when inheriting from a component that provides too many columns. When nesting components and a column of a child Show component is to be skipped, use the constructor's skip_columns argument instead.

Parameters:

  • name (String)

    Name of the column to be skipped.



104
105
106
# File 'lib/compony/components/show.rb', line 104

def skip_column(name)
  @skipped_columns << name.to_sym
end