Class: Mensa::Base
- Inherits:
-
Object
- Object
- Mensa::Base
- Includes:
- ConfigReaders, Scope, Pagy::Method
- Defined in:
- app/tables/mensa/base.rb
Instance Attribute Summary collapse
-
#component ⇒ Object
Returns the value of attribute component.
-
#name ⇒ Object
Returns the value of attribute name.
- #original_view_context ⇒ Object
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#request ⇒ Object
Returns the value of attribute request.
-
#table_view ⇒ Object
Returns the value of attribute table_view.
Instance Method Summary collapse
- #actions ⇒ Object
- #actions? ⇒ Boolean
-
#active_filters ⇒ Object
Returns the active filters, skipping any whose column no longer exists.
- #all_views ⇒ Object
- #batch_actions ⇒ Object
- #batch_actions? ⇒ Boolean
-
#column(name) ⇒ Object
Returns a column by name.
- #column_order ⇒ Object
-
#columns ⇒ Object
Returns all columns.
-
#current_user ⇒ Object
The user that owns custom views.
-
#display_columns ⇒ Object
Returns the columns to be displayed, ordered by column_order.
- #export_rows ⇒ Object
-
#filters? ⇒ Boolean
Returns true if the table has filters.
-
#initialize(config = {}) ⇒ Base
constructor
A new instance of Base.
- #menu ⇒ Object
-
#path(order: {}, turbo_frame_id: nil, table_view_id: , column_order: , hidden_columns: ) ⇒ Object
Returns the current path with configuration.
-
#rows ⇒ Object
Returns the rows to be displayed.
- #system_views ⇒ Object
- #table_id ⇒ Object
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 = {}) @params = config.to_h.deep_symbolize_keys @config = self.class.definition.merge(@params || {}) ensure_internal_columns_for_joined_associations params[: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
158 159 160 |
# File 'app/tables/mensa/base.rb', line 158 def original_view_context @original_view_context || component.original_view_context end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
11 12 13 |
# File 'app/tables/mensa/base.rb', line 11 def params @params 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
103 104 105 106 107 |
# File 'app/tables/mensa/base.rb', line 103 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
99 100 101 |
# File 'app/tables/mensa/base.rb', line 99 def actions? config[:actions].present? end |
#active_filters ⇒ Object
Returns the active filters, skipping any whose column no longer exists.
91 92 93 94 95 96 97 |
# File 'app/tables/mensa/base.rb', line 91 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
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_actions ⇒ Object
113 114 115 116 117 |
# File 'app/tables/mensa/base.rb', line 113 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
109 110 111 |
# File 'app/tables/mensa/base.rb', line 109 def batch_actions? config[:batches].present? end |
#column(name) ⇒ Object
Returns a column by name
56 57 58 |
# File 'app/tables/mensa/base.rb', line 56 def column(name) 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? # 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 |
#columns ⇒ Object
Returns all columns
50 51 52 |
# File 'app/tables/mensa/base.rb', line 50 def columns @columns ||= column_order.map { |column_name| Mensa::Column.new(column_name, config: config.dig(:columns, column_name), table: self) } 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.
146 147 148 149 150 |
# File 'app/tables/mensa/base.rb', line 146 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.
61 62 63 64 65 66 67 68 69 |
# File 'app/tables/mensa/base.rb', line 61 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
76 77 78 |
# File 'app/tables/mensa/base.rb', line 76 def export_rows ordered_scope.map { |row| Mensa::Row.new(self, row) } end |
#filters? ⇒ Boolean
Returns true if the table has filters
86 87 88 |
# File 'app/tables/mensa/base.rb', line 86 def filters? columns.any?(&:filter?) end |
#menu ⇒ Object
132 133 134 135 136 |
# File 'app/tables/mensa/base.rb', line 132 def Satis::Menus::Builder.build([:table, :view_menu]) do |m| m.item :export, icon: "fal fa-file-export", link: nil end end |
#path(order: {}, turbo_frame_id: nil, table_view_id: , column_order: , hidden_columns: ) ⇒ Object
Returns the current path with configuration
120 121 122 123 124 125 126 127 128 129 130 |
# File 'app/tables/mensa/base.rb', line 120 def path(order: {}, turbo_frame_id: nil, table_view_id: params[:table_view_id], column_order: params[:column_order], hidden_columns: params[:hidden_columns]) # FIXME: if someone doesn't use as: :mensa in the routes, it breaks original_view_context.mensa.table_path( name, order: order_hash(order), turbo_frame_id: turbo_frame_id, table_view_id: table_view_id, column_order: column_order, hidden_columns: hidden_columns ) end |
#rows ⇒ Object
Returns the rows to be displayed
72 73 74 |
# File 'app/tables/mensa/base.rb', line 72 def rows paged_scope.map { |row| Mensa::Row.new(self, row) } end |
#system_views ⇒ Object
80 81 82 83 |
# File 'app/tables/mensa/base.rb', line 80 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
152 153 154 155 156 |
# File 'app/tables/mensa/base.rb', line 152 def table_id return @table_id if @table_id @table_id = params[:turbo_frame_id] || "#{name.to_s.gsub("/", "__")}-#{SecureRandom.base36}" end |