Class: Mensa::Base
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
#component ⇒ Object
Returns the value of attribute component.
10
11
12
|
# File 'app/tables/mensa/base.rb', line 10
def component
@component
end
|
#name ⇒ Object
Returns the value of attribute name.
10
11
12
|
# File 'app/tables/mensa/base.rb', line 10
def name
@name
end
|
#original_view_context ⇒ Object
167
168
169
|
# File 'app/tables/mensa/base.rb', line 167
def original_view_context
@original_view_context || component.original_view_context
end
|
#request ⇒ Object
Returns the value of attribute request.
10
11
12
|
# File 'app/tables/mensa/base.rb', line 10
def request
@request
end
|
#table_view ⇒ Object
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
#actions ⇒ Object
110
111
112
113
114
|
# File 'app/tables/mensa/base.rb', line 110
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
106
107
108
|
# File 'app/tables/mensa/base.rb', line 106
def actions?
config[:actions].present?
end
|
#active_filters ⇒ Object
Returns the active filters, skipping any whose column no longer exists.
98
99
100
101
102
103
104
|
# File 'app/tables/mensa/base.rb', line 98
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_views ⇒ Object
143
144
145
146
147
|
# File 'app/tables/mensa/base.rb', line 143
def all_views
views = system_views
views += TableView.where(table_name: name).where(user: [nil, current_user])
views
end
|
#batch_actions ⇒ Object
120
121
122
123
124
|
# File 'app/tables/mensa/base.rb', line 120
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
116
117
118
|
# File 'app/tables/mensa/base.rb', line 116
def batch_actions?
config[:batches].present?
end
|
#column(name) ⇒ Object
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_order ⇒ Object
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?
all_keys = (config[:columns]&.keys || []).map(&:to_sym)
internal_keys = all_keys.select { |key| config.dig(:columns, key, :internal) }
(order | internal_keys)
end
|
#columns ⇒ Object
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_order ⇒ Object
191
192
193
|
# File 'app/tables/mensa/base.rb', line 191
def current_column_order
config[:column_order]
end
|
#current_hidden_columns ⇒ Object
195
196
197
|
# File 'app/tables/mensa/base.rb', line 195
def current_hidden_columns
config[:hidden_columns]
end
|
#current_order ⇒ Object
175
176
177
|
# File 'app/tables/mensa/base.rb', line 175
def current_order
config[:order]
end
|
#current_order_provided? ⇒ Boolean
179
180
181
|
# File 'app/tables/mensa/base.rb', line 179
def current_order_provided?
config.key?(:order)
end
|
#current_page ⇒ Object
187
188
189
|
# File 'app/tables/mensa/base.rb', line 187
def current_page
config[:page]
end
|
#current_query ⇒ Object
171
172
173
|
# File 'app/tables/mensa/base.rb', line 171
def current_query
config[:query]
end
|
#current_table_view_id ⇒ Object
183
184
185
|
# File 'app/tables/mensa/base.rb', line 183
def current_table_view_id
config[:table_view_id]
end
|
#current_turbo_frame_id ⇒ Object
199
200
201
|
# File 'app/tables/mensa/base.rb', line 199
def current_turbo_frame_id
config[:turbo_frame_id]
end
|
#current_user ⇒ Object
The user that owns custom views. Returns nil when the host application has no current user, in which case views cannot be saved.
155
156
157
158
159
|
# File 'app/tables/mensa/base.rb', line 155
def current_user
return Current.user if defined?(Current) && Current.respond_to?(:user)
nil
end
|
#default_system_view ⇒ Object
88
89
90
|
# File 'app/tables/mensa/base.rb', line 88
def default_system_view
system_views.find { |view| view.id == :default }
end
|
#display_columns ⇒ Object
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_rows ⇒ Object
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
93
94
95
|
# File 'app/tables/mensa/base.rb', line 93
def filters?
columns.any?(&:filter?)
end
|
#path(order: {}, turbo_frame_id: current_turbo_frame_id, table_view_id: current_table_view_id, page: current_page, column_order: current_column_order, hidden_columns: current_hidden_columns, user_params: nil) ⇒ Object
Returns the current path with configuration
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
|
# File 'app/tables/mensa/base.rb', line 127
def path(order: {}, turbo_frame_id: current_turbo_frame_id, table_view_id: current_table_view_id, page: current_page, column_order: current_column_order, hidden_columns: current_hidden_columns, user_params: nil)
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,
page: page,
column_order: column_order,
hidden_columns: hidden_columns
}.compact.to_query
query.present? ? "#{path}?#{query}" : path
end
|
#rows ⇒ Object
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_views ⇒ Object
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_id ⇒ Object
161
162
163
164
165
|
# File 'app/tables/mensa/base.rb', line 161
def table_id
return @table_id if @table_id
@table_id = current_turbo_frame_id || "#{name.to_s.gsub("/", "__")}-#{SecureRandom.base36}"
end
|
#views? ⇒ Boolean
149
150
151
|
# File 'app/tables/mensa/base.rb', line 149
def views?
all_views.reject { it.id == :default }.present?
end
|