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.

Constant Summary collapse

INT_OR_UUID_REGEX =
/(?:\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.



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.



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.



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.



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.



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

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