Class: Mensa::Base

Inherits:
Object
  • Object
show all
Includes:
ConfigReaders, Scope, Pagy::Method
Defined in:
app/tables/mensa/base.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Scope

#filtered_scope, #ordered_scope, #paged_scope, #pagy_details, #selected_scope

Constructor Details

#initialize(config = {}) ⇒ Base

Returns a new instance of Base.



22
23
24
25
26
27
28
29
30
31
32
# File 'app/tables/mensa/base.rb', line 22

def initialize(config = {})
  normalized_config = config.to_h.deep_symbolize_keys
  @config = self.class.definition.merge(normalized_config)
  @params = (@config[:params].presence || {}).deep_symbolize_keys
  @config[:params] = @params

  current_hidden_columns&.each do |column_name|
    c = columns.find { |c| c.name == column_name.to_sym }
    c.config[:visible] = false
  end
end

Instance Attribute Details

#componentObject

Returns the value of attribute component.



10
11
12
# File 'app/tables/mensa/base.rb', line 10

def component
  @component
end

#nameObject

Returns the value of attribute name.



10
11
12
# File 'app/tables/mensa/base.rb', line 10

def name
  @name
end

#original_view_contextObject



162
163
164
# File 'app/tables/mensa/base.rb', line 162

def original_view_context
  @original_view_context || component.original_view_context
end

#requestObject

Returns the value of attribute request.



10
11
12
# File 'app/tables/mensa/base.rb', line 10

def request
  @request
end

#table_viewObject

Returns the value of attribute table_view.



10
11
12
# File 'app/tables/mensa/base.rb', line 10

def table_view
  @table_view
end

Instance Method Details

#actionsObject



106
107
108
109
110
# File 'app/tables/mensa/base.rb', line 106

def actions
  return @actions if @actions

  @actions ||= config[:actions].keys.map { |action_name| Mensa::Action.new(action_name, config: config.dig(:actions, action_name), table: self) }
end

#actions?Boolean

Returns:

  • (Boolean)


102
103
104
# File 'app/tables/mensa/base.rb', line 102

def actions?
  config[:actions].present?
end

#active_filtersObject

Returns the active filters, skipping any whose column no longer exists.



94
95
96
97
98
99
100
# File 'app/tables/mensa/base.rb', line 94

def active_filters
  (config[:filters] || {}).filter_map do |column_name, filter_config|
    col = column(column_name)
    next unless col
    Mensa::Filter.new(column: col, config: filter_config, table: self)
  end
end

#all_viewsObject



138
139
140
141
142
# File 'app/tables/mensa/base.rb', line 138

def all_views
  views = system_views
  views += TableView.where(table_name: name).where(user: [nil, current_user])
  views
end

#batch_actionsObject



116
117
118
119
120
# File 'app/tables/mensa/base.rb', line 116

def batch_actions
  return @batch_actions if @batch_actions

  @batch_actions ||= config[:batches].keys.map { |batch_name| Mensa::BatchAction.new(batch_name, config: config.dig(:batches, batch_name), table: self) }
end

#batch_actions?Boolean

Returns:

  • (Boolean)


112
113
114
# File 'app/tables/mensa/base.rb', line 112

def batch_actions?
  config[:batches].present?
end

#column(name) ⇒ Object

Returns a column by name

Parameters:

  • name (String)


55
56
57
58
59
60
61
# File 'app/tables/mensa/base.rb', line 55

def column(name)
  found_column = columns.find { |c| c.name == name.to_sym }
  return found_column if found_column || @internal_columns_ensured

  @columns = nil
  columns.find { |c| c.name == name.to_sym }
end

#column_orderObject



34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/tables/mensa/base.rb', line 34

def column_order
  order = config[:column_order].presence || config[:columns]&.keys
  order = order&.map(&:to_sym)
  return order if order.nil?

  # Internal columns are never shown in the column customizer UI, so they
  # are absent from any URL-supplied column_order. Always append them so
  # that columns and selected_scope include their attributes.
  all_keys = (config[:columns]&.keys || []).map(&:to_sym)
  internal_keys = all_keys.select { |key| config.dig(:columns, key, :internal) }
  (order | internal_keys)
end

#columnsObject

Returns all columns



48
49
50
51
# File 'app/tables/mensa/base.rb', line 48

def columns
  ensure_internal_columns_for_joined_associations
  @columns ||= column_order.map { |column_name| Mensa::Column.new(column_name, config: config.dig(:columns, column_name), table: self) }
end

#current_column_orderObject



182
183
184
# File 'app/tables/mensa/base.rb', line 182

def current_column_order
  config[:column_order]
end

#current_hidden_columnsObject



186
187
188
# File 'app/tables/mensa/base.rb', line 186

def current_hidden_columns
  config[:hidden_columns]
end

#current_orderObject



170
171
172
# File 'app/tables/mensa/base.rb', line 170

def current_order
  config[:order]
end

#current_order_provided?Boolean

Returns:

  • (Boolean)


174
175
176
# File 'app/tables/mensa/base.rb', line 174

def current_order_provided?
  config.key?(:order)
end

#current_queryObject



166
167
168
# File 'app/tables/mensa/base.rb', line 166

def current_query
  config[:query]
end

#current_table_view_idObject



178
179
180
# File 'app/tables/mensa/base.rb', line 178

def current_table_view_id
  config[:table_view_id]
end

#current_turbo_frame_idObject



190
191
192
# File 'app/tables/mensa/base.rb', line 190

def current_turbo_frame_id
  config[:turbo_frame_id]
end

#current_userObject

The user that owns custom views. Returns nil when the host application has no current user, in which case views cannot be saved.



150
151
152
153
154
# File 'app/tables/mensa/base.rb', line 150

def current_user
  return Current.user if defined?(Current) && Current.respond_to?(:user)

  nil
end

#display_columnsObject

Returns the columns to be displayed, ordered by column_order.



64
65
66
67
68
69
70
71
72
# File 'app/tables/mensa/base.rb', line 64

def display_columns
  @display_columns ||= begin
    order = column_order || []
    columns
      .select(&:visible?)
      .reject(&:internal?)
      .sort_by { |col| order.index(col.name) || Float::INFINITY }
  end
end

#export_rowsObject



79
80
81
# File 'app/tables/mensa/base.rb', line 79

def export_rows
  ordered_scope.map { |row| Mensa::Row.new(self, row) }
end

#filters?Boolean

Returns true if the table has filters

Returns:

  • (Boolean)


89
90
91
# File 'app/tables/mensa/base.rb', line 89

def filters?
  columns.any?(&:filter?)
end

#path(order: {}, turbo_frame_id: current_turbo_frame_id, table_view_id: current_table_view_id, column_order: current_column_order, hidden_columns: current_hidden_columns, user_params: nil) ⇒ Object

Returns the current path with configuration



123
124
125
126
127
128
129
130
131
132
133
134
135
136
# File 'app/tables/mensa/base.rb', line 123

def path(order: {}, turbo_frame_id: current_turbo_frame_id, table_view_id: current_table_view_id, column_order: current_column_order, hidden_columns: current_hidden_columns, user_params: nil)
  # FIXME: if someone doesn't use as: :mensa in the routes, it breaks
  path = original_view_context.mensa.table_path(name)
  query = {
    params: user_params || params,
    order: order_hash(order),
    turbo_frame_id: turbo_frame_id,
    table_view_id: table_view_id,
    column_order: column_order,
    hidden_columns: hidden_columns
  }.compact.to_query

  query.present? ? "#{path}?#{query}" : path
end

#rowsObject

Returns the rows to be displayed



75
76
77
# File 'app/tables/mensa/base.rb', line 75

def rows
  paged_scope.map { |row| Mensa::Row.new(self, row) }
end

#system_viewsObject



83
84
85
86
# File 'app/tables/mensa/base.rb', line 83

def system_views
  views = config[:views]&.key?(:default) ? [] : [Mensa::SystemView.new(:default, config: {name: I18n.t("mensa.views.default")}, table: self)]
  views + (config[:views] || {}).keys.map { |view_name| Mensa::SystemView.new(view_name, config: config.dig(:views, view_name), table: self) }
end

#table_idObject



156
157
158
159
160
# File 'app/tables/mensa/base.rb', line 156

def table_id
  return @table_id if @table_id

  @table_id = current_turbo_frame_id || "#{name.to_s.gsub("/", "__")}-#{SecureRandom.base36}"
end

#views?Boolean

Returns:

  • (Boolean)


144
145
146
# File 'app/tables/mensa/base.rb', line 144

def views?
  all_views.reject{_1.id == :default}.present?
end