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.

API:

  • description

Constant Summary collapse

INT_OR_UUID_REGEX =

API:

  • description

/(?:\d+|[0-9a-fA-F]{8}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{4}-[0-9a-fA-F]{12})/

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?, #store_data

Methods inherited from Compony::Component

#before_render, comp_name, #content, #exposed_intents, family_name, #id, #id_path, #id_path_hash, #inspect, #param_name, #path, #remove_content, #remove_content!, #render, #resourceful?, #root_comp, #root_comp?, setup, #sub_comp

Constructor Details

#initialize(skip_columns: []) ⇒ Show

Returns a new instance of Show.

Parameters:

  • (defaults to: [])

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

API:

  • description



58
59
60
61
62
# File 'lib/compony/components/show.rb', line 58

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:

  • Compony-enriched model that will be queried for fields.

API:

  • description



106
107
108
# File 'lib/compony/components/show.rb', line 106

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:

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

  • (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.

  • (defaults to: nil)

    Extra CSS classes for the column's value.

  • (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).

  • (defaults to: :show)

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

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

API:

  • description



71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/compony/components/show.rb', line 71

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:

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

API:

  • description



91
92
93
# File 'lib/compony/components/show.rb', line 91

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 of the column to be skipped.

API:

  • description



99
100
101
# File 'lib/compony/components/show.rb', line 99

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