Class: Compony::Components::Show
- Inherits:
-
Compony::Component
- Object
- Compony::Component
- Compony::Components::Show
- 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
-
#all_field_columns(data) ⇒ Object
DSL method Goes through the fields of the given data and adds a field column for every field found.
-
#column(name, label: nil, class: nil, link_opts: {}, link_to_component: :show, &block) ⇒ Object
Adds a column.
-
#columns(*col_names) ⇒ Object
DSL method Adds multiple columns that have identical kwargs, e.g.
-
#initialize(skip_columns: []) ⇒ Show
constructor
A new instance of Show.
-
#skip_column(name) ⇒ Object
DSL method Marks a column as skipped.
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.
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.
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.
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.
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.
104 105 106 |
# File 'lib/compony/components/show.rb', line 104 def skip_column(name) @skipped_columns << name.to_sym end |