Class: Mensa::Column
- Inherits:
-
Object
- Object
- Mensa::Column
- Includes:
- ConfigReaders
- Defined in:
- app/tables/mensa/column.rb
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #active_record_column ⇒ Object
- #active_record_column_type ⇒ Object
- #attribute ⇒ Object
- #attribute_for_condition ⇒ Object
- #filter ⇒ Object
-
#filter? ⇒ Boolean
Returns true if the column supports filtering.
- #human_name ⇒ Object
-
#initialize(name, config:, table:) ⇒ Column
constructor
A new instance of Column.
- #menu ⇒ Object
- #next_sort_direction ⇒ Object
- #sort_direction ⇒ Object
- #type ⇒ Object
Constructor Details
#initialize(name, config:, table:) ⇒ Column
Returns a new instance of Column.
11 12 13 14 15 |
# File 'app/tables/mensa/column.rb', line 11 def initialize(name, config:, table:) @name = name @config = self.class.definition.merge(config || {}) @table = table end |
Instance Attribute Details
#name ⇒ Object (readonly)
Returns the value of attribute name.
9 10 11 |
# File 'app/tables/mensa/column.rb', line 9 def name @name end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
9 10 11 |
# File 'app/tables/mensa/column.rb', line 9 def table @table end |
Instance Method Details
#active_record_column ⇒ Object
48 49 50 |
# File 'app/tables/mensa/column.rb', line 48 def active_record_column @active_record_column ||= table.model&.columns&.find { _1.name == name.to_s } end |
#active_record_column_type ⇒ Object
52 53 54 |
# File 'app/tables/mensa/column.rb', line 52 def active_record_column_type active_record_column&.type end |
#attribute ⇒ Object
38 39 40 41 42 43 44 45 46 |
# File 'app/tables/mensa/column.rb', line 38 def attribute return @attribute if @attribute @attribute = if config[:attribute].present? "#{config[:attribute]} AS #{name}" elsif table.model.column_names.include? name.to_s name.to_s end end |
#attribute_for_condition ⇒ Object
60 61 62 63 64 65 66 67 68 |
# File 'app/tables/mensa/column.rb', line 60 def attribute_for_condition return @attribute_for_condition if @attribute_for_condition @attribute_for_condition = if config[:attribute].present? config[:attribute] elsif table.model.column_names.include? name.to_s name.to_s end end |
#filter ⇒ Object
75 76 77 78 79 |
# File 'app/tables/mensa/column.rb', line 75 def filter return unless filter? @filter ||= Mensa::Filter.new(column: self, config: table.config.dig(:columns, name, :filter) || {}, table: table) end |
#filter? ⇒ Boolean
Returns true if the column supports filtering
71 72 73 |
# File 'app/tables/mensa/column.rb', line 71 def filter? config.key?(:filter) end |
#human_name ⇒ Object
81 82 83 84 85 86 87 |
# File 'app/tables/mensa/column.rb', line 81 def human_name if table.model < ActiveRecord::Base table.model.human_attribute_name name else name.to_s.humanize end end |
#menu ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'app/tables/mensa/column.rb', line 89 def Satis::Menus::Builder.build(:filter_menu, event: "click") do |m| if sortable? m.item :sort_ascending, icon: "fa-solid fa-arrow-up-short-wide", link: table.path(order: {name => :asc}), link_attributes: {"data-turbo-frame": "_self"} m.item :sort_descending, icon: "fa-solid fa-arrow-down-wide-short", link: table.path(order: {name => :desc}), link_attributes: {"data-turbo-frame": "_self"} end end end |
#next_sort_direction ⇒ Object
28 29 30 31 32 33 34 35 36 |
# File 'app/tables/mensa/column.rb', line 28 def next_sort_direction if sort_direction == :asc :desc elsif sort_direction == :desc nil else :asc end end |
#sort_direction ⇒ Object
23 24 25 26 |
# File 'app/tables/mensa/column.rb', line 23 def sort_direction value = table.config.dig(:order, name) value.present? ? value.to_sym : nil end |
#type ⇒ Object
56 57 58 |
# File 'app/tables/mensa/column.rb', line 56 def type config[:type] || active_record_column_type end |