Class: Mensa::Column

Inherits:
Object
  • Object
show all
Includes:
ConfigReaders
Defined in:
app/tables/mensa/column.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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



59
60
61
# File 'app/tables/mensa/column.rb', line 59

def active_record_column
  @active_record_column ||= table.model&.columns&.find { |column| column.name == name.to_s }
end

#active_record_column_type ⇒ Object



63
64
65
# File 'app/tables/mensa/column.rb', line 63

def active_record_column_type
  active_record_column&.type
end

#attribute ⇒ Object



39
40
41
42
43
44
45
46
47
# File 'app/tables/mensa/column.rb', line 39

def attribute
  return @attribute if @attribute

  @attribute = if config[:attribute].present?
    "#{config[:attribute]} AS #{name}"
  elsif table.model.column_names.include? name.to_s
    "#{table.model.table_name}.#{name}"
  end
end

#attribute_for_condition ⇒ Object



71
72
73
74
75
76
77
78
79
# File 'app/tables/mensa/column.rb', line 71

def attribute_for_condition
  return @attribute_for_condition if @attribute_for_condition

  @attribute_for_condition = if config[:attribute].present?
    Arel.sql(raw_attribute)
  elsif table.model.column_names.include? name.to_s
    Arel.sql("\"#{table.model.table_name}\".\"#{name}\"")
  end
end

#filter ⇒ Object



86
87
88
89
90
# File 'app/tables/mensa/column.rb', line 86

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

Returns:

  • (Boolean)


82
83
84
# File 'app/tables/mensa/column.rb', line 82

def filter?
  config[:filter] != false
end

#format ⇒ Object



92
93
94
# File 'app/tables/mensa/column.rb', line 92

def format
  @format ||= Mensa::Format.new(config: config.dig(:format).presence || {}, column: self)
end

#human_name ⇒ Object



96
97
98
99
100
101
102
# File 'app/tables/mensa/column.rb', line 96

def human_name
  if table.model < ActiveRecord::Base
    table.model.human_attribute_name name
  else
    name.to_s.humanize
  end
end

#next_sort_direction ⇒ Object



29
30
31
32
33
34
35
36
37
# File 'app/tables/mensa/column.rb', line 29

def next_sort_direction
  if sort_direction == :asc
    :desc
  elsif sort_direction == :desc
    nil
  else
    :asc
  end
end

#raw_attribute ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'app/tables/mensa/column.rb', line 49

def raw_attribute
  return @raw_attribute if @raw_attribute

  @raw_attribute = if config[:attribute].present?
    config[:attribute]
  elsif table.model.column_names.include? name.to_s
    "#{table.model.table_name}.#{name}"
  end
end

#sort_direction ⇒ Object



24
25
26
27
# File 'app/tables/mensa/column.rb', line 24

def sort_direction
  value = table.config.dig(:order, name)
  value.presence&.to_sym
end

#sort_indicator_icon_key ⇒ Object

Determines the sort indicator icon key based on column type



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
# File 'app/tables/mensa/column.rb', line 105

def sort_indicator_icon_key
  numeric_types = [:integer, :decimal, :float, :bigint]
  date_types = [:date, :datetime, :time, :timestamp]
  text_types = [:string, :text]

  column_type = type
  direction = sort_direction.to_s.presence || ""
  direction_suffix = direction.present? ? "_#{direction}" : ""

  case column_type
  when *numeric_types
    "header_order_indicator_numeric#{direction_suffix}"
  when *date_types
    "header_order_indicator_date#{direction_suffix}"
  when *text_types
    "header_order_indicator_text#{direction_suffix}"
  else
    "header_order_indicator#{direction_suffix}"
  end
end

#type ⇒ Object



67
68
69
# File 'app/tables/mensa/column.rb', line 67

def type
  config[:type] || active_record_column_type
end