Class: Mensa::Column
- Inherits:
-
Object
- Object
- Mensa::Column
- Includes:
- ConfigReaders
- Defined in:
- app/tables/mensa/column.rb
Instance Attribute Summary collapse
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#table ⇒ Object
readonly
Returns the value of attribute table.
Instance Method Summary collapse
- #attribute ⇒ Object
- #attribute_for_condition ⇒ Object
- #filter ⇒ Object
- #filter? ⇒ Boolean
- #human_name ⇒ Object
-
#initialize(name, config:, table:) ⇒ Column
constructor
A new instance of Column.
- #menu ⇒ Object
- #next_sort_direction ⇒ Object
- #sort_direction ⇒ Object
Constructor Details
#initialize(name, config:, table:) ⇒ Column
Returns a new instance of Column.
8 9 10 11 12 |
# File 'app/tables/mensa/column.rb', line 8 def initialize(name, config:, table:) @name = name @table = table @config = config end |
Instance Attribute Details
#config ⇒ Object (readonly)
Returns the value of attribute config.
6 7 8 |
# File 'app/tables/mensa/column.rb', line 6 def config @config end |
#name ⇒ Object (readonly)
Returns the value of attribute name.
6 7 8 |
# File 'app/tables/mensa/column.rb', line 6 def name @name end |
#table ⇒ Object (readonly)
Returns the value of attribute table.
6 7 8 |
# File 'app/tables/mensa/column.rb', line 6 def table @table end |
Instance Method Details
#attribute ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 |
# File 'app/tables/mensa/column.rb', line 34 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 else nil end end |
#attribute_for_condition ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 |
# File 'app/tables/mensa/column.rb', line 46 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 else nil end end |
#filter ⇒ Object
62 63 64 65 66 |
# File 'app/tables/mensa/column.rb', line 62 def filter return unless filter? @filter ||= Mensa::Filter.new(nil, column: self, config: config[:filter] || {}, table: table) end |
#filter? ⇒ Boolean
58 59 60 |
# File 'app/tables/mensa/column.rb', line 58 def filter? config.key?(:filter) end |
#human_name ⇒ Object
68 69 70 71 72 73 74 |
# File 'app/tables/mensa/column.rb', line 68 def human_name if table.model < ActiveRecord::Base table.model.human_attribute_name name else name.to_s.humanize end end |
#menu ⇒ Object
76 77 78 79 80 81 82 83 |
# File 'app/tables/mensa/column.rb', line 76 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'.freeze, link: table.path(order: { name => :asc }), link_attributes: { "data-turbo-frame": "_self" } m.item :sort_descending, icon: 'fa-solid fa-arrow-down-wide-short'.freeze, link: table.path(order: { name => :asc }), link_attributes: { "data-turbo-frame": "_self" } end end end |
#next_sort_direction ⇒ Object
24 25 26 27 28 29 30 31 32 |
# File 'app/tables/mensa/column.rb', line 24 def next_sort_direction if sort_direction == :asc :desc elsif sort_direction == :desc nil else :asc end end |
#sort_direction ⇒ Object
20 21 22 |
# File 'app/tables/mensa/column.rb', line 20 def sort_direction table.config.dig(:order, name)&.to_sym end |