diff --git a/app/components/enumerations/table_component.rb b/app/components/enumerations/table_component.rb index 177f8510186..ffde9e733b7 100644 --- a/app/components/enumerations/table_component.rb +++ b/app/components/enumerations/table_component.rb @@ -30,12 +30,15 @@ module Enumerations class TableComponent < ::TableComponent + attr_reader :enumeration + + def initialize(enumeration:, rows: [], **) + super(rows: rows, **) + @enumeration = enumeration + end + def columns - %i[name is_default active sort].tap do |default| - if with_colors - default.insert 3, :color - end - end + headers.map(&:first) end def sortable? @@ -43,16 +46,13 @@ module Enumerations end def headers - [ + @headers ||= [ ["name", { caption: Enumeration.human_attribute_name(:name) }], - ["is_default", { caption: Enumeration.human_attribute_name(:is_default) }], + enumeration.can_have_default_value? ? ["is_default", { caption: Enumeration.human_attribute_name(:is_default) }] : nil, ["active", { caption: Enumeration.human_attribute_name(:active) }], + with_colors ? ["color", { caption: Enumeration.human_attribute_name(:color) }] : nil, ["sort", { caption: I18n.t(:label_sort) }] - ].tap do |default| - if with_colors - default.insert 3, ["color", { caption: Enumeration.human_attribute_name(:color) }] - end - end + ].compact end def with_colors diff --git a/app/models/enumeration.rb b/app/models/enumeration.rb index da80df87146..c12c715822a 100644 --- a/app/models/enumeration.rb +++ b/app/models/enumeration.rb @@ -75,6 +75,11 @@ class Enumeration < ApplicationRecord end end + # boolean to define if the enumeration can set a default + def self.can_have_default_value? + true + end + # Destroys enumerations in a single transaction # It ensures, that the transactions can be safely transferred to each # entry's parent diff --git a/app/views/enumerations/_form.html.erb b/app/views/enumerations/_form.html.erb index db1be44dbc1..8e0fe7566e9 100644 --- a/app/views/enumerations/_form.html.erb +++ b/app/views/enumerations/_form.html.erb @@ -37,7 +37,10 @@ See COPYRIGHT and LICENSE files for more details. <%= f.hidden_field 'type' %>