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.
24
25
26
27
28
29
30
31
32
33
34
|
# File 'app/tables/mensa/base.rb', line 24
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
160
161
162
|
# File 'app/tables/mensa/base.rb', line 160
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
108
109
110
111
112
|
# File 'app/tables/mensa/base.rb', line 108
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
104
105
106
|
# File 'app/tables/mensa/base.rb', line 104
def actions?
config[:actions].present?
end
|
#active_filters ⇒ Object
Returns the active filters, skipping any whose column no longer exists.
96
97
98
99
100
101
102
|
# File 'app/tables/mensa/base.rb', line 96
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
140
141
142
143
144
|
# File 'app/tables/mensa/base.rb', line 140
def all_views
views = system_views
views += TableView.where(table_name: name).where(user: [nil, current_user])
views
end
|
#batch_actions ⇒ Object
118
119
120
121
122
|
# File 'app/tables/mensa/base.rb', line 118
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
114
115
116
|
# File 'app/tables/mensa/base.rb', line 114
def batch_actions?
config[:batches].present?
end
|
#column(name) ⇒ Object
57
58
59
60
61
62
63
|
# File 'app/tables/mensa/base.rb', line 57
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
36
37
38
39
40
41
42
43
44
45
46
47
|
# File 'app/tables/mensa/base.rb', line 36
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
50
51
52
53
|
# File 'app/tables/mensa/base.rb', line 50
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
180
181
182
|
# File 'app/tables/mensa/base.rb', line 180
def current_column_order
config[:column_order]
end
|
#current_hidden_columns ⇒ Object
184
185
186
|
# File 'app/tables/mensa/base.rb', line 184
def current_hidden_columns
config[:hidden_columns]
end
|
#current_order ⇒ Object
168
169
170
|
# File 'app/tables/mensa/base.rb', line 168
def current_order
config[:order]
end
|
#current_order_provided? ⇒ Boolean
172
173
174
|
# File 'app/tables/mensa/base.rb', line 172
def current_order_provided?
config.key?(:order)
end
|
#current_query ⇒ Object
164
165
166
|
# File 'app/tables/mensa/base.rb', line 164
def current_query
config[:query]
end
|
#current_table_view_id ⇒ Object
176
177
178
|
# File 'app/tables/mensa/base.rb', line 176
def current_table_view_id
config[:table_view_id]
end
|
#current_turbo_frame_id ⇒ Object
188
189
190
|
# File 'app/tables/mensa/base.rb', line 188
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.
148
149
150
151
152
|
# File 'app/tables/mensa/base.rb', line 148
def current_user
return Current.user if defined?(Current) && Current.respond_to?(:user)
nil
end
|
#display_columns ⇒ Object
Returns the columns to be displayed, ordered by column_order.
66
67
68
69
70
71
72
73
74
|
# File 'app/tables/mensa/base.rb', line 66
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
81
82
83
|
# File 'app/tables/mensa/base.rb', line 81
def export_rows
ordered_scope.map { |row| Mensa::Row.new(self, row) }
end
|
#filters? ⇒ Boolean
Returns true if the table has filters
91
92
93
|
# File 'app/tables/mensa/base.rb', line 91
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
|
# File 'app/tables/mensa/base.rb', line 125
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)
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
|
#rows ⇒ Object
Returns the rows to be displayed
77
78
79
|
# File 'app/tables/mensa/base.rb', line 77
def rows
paged_scope.map { |row| Mensa::Row.new(self, row) }
end
|
#system_views ⇒ Object
85
86
87
88
|
# File 'app/tables/mensa/base.rb', line 85
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
154
155
156
157
158
|
# File 'app/tables/mensa/base.rb', line 154
def table_id
return @table_id if @table_id
@table_id = current_turbo_frame_id || "#{name.to_s.gsub("/", "__")}-#{SecureRandom.base36}"
end
|