Class: Mensa::Base
- Inherits:
-
Object
- Object
- Mensa::Base
- Includes:
- ConfigReaders, Scope, Pagy::Backend
- Defined in:
- app/tables/mensa/base.rb
Instance Attribute Summary collapse
-
#component ⇒ Object
Returns the value of attribute component.
-
#config ⇒ Object
readonly
Returns the value of attribute config.
-
#name ⇒ Object
Returns the value of attribute name.
- #original_view_context ⇒ Object
-
#params ⇒ Object
readonly
Returns the value of attribute params.
-
#table_view ⇒ Object
Returns the value of attribute table_view.
Class Method Summary collapse
Instance Method Summary collapse
- #actions ⇒ Object
- #actions? ⇒ Boolean
- #active_filters ⇒ Object
- #all_views ⇒ Object
-
#column(name) ⇒ Object
Returns a column by name.
- #column_order ⇒ Object
-
#columns ⇒ Object
Returns all columns.
-
#display_columns ⇒ Object
Returns the columns to be displayed.
- #export_rows ⇒ Object
-
#initialize(config = {}) ⇒ Base
constructor
A new instance of Base.
- #menu ⇒ Object
-
#path(order: {}, turbo_frame_id: nil, table_view_id: nil) ⇒ Object
Returns the current path with configuration.
-
#rows ⇒ Object
Returns the rows to be displayed.
- #table_id ⇒ Object
Methods included from Scope
#filtered_scope, #ordered_scope, #paged_scope, #pagy_details, #scope, #selected_scope
Constructor Details
#initialize(config = {}) ⇒ Base
Returns a new instance of Base.
20 21 22 23 |
# File 'app/tables/mensa/base.rb', line 20 def initialize(config = {}) @params = config.to_h.deep_symbolize_keys @config = self.class.definition.merge(@params || {}) end |
Instance Attribute Details
#component ⇒ Object
Returns the value of attribute component.
8 9 10 |
# File 'app/tables/mensa/base.rb', line 8 def component @component end |
#config ⇒ Object (readonly)
Returns the value of attribute config.
9 10 11 |
# File 'app/tables/mensa/base.rb', line 9 def config @config end |
#name ⇒ Object
Returns the value of attribute name.
8 9 10 |
# File 'app/tables/mensa/base.rb', line 8 def name @name end |
#original_view_context ⇒ Object
92 93 94 |
# File 'app/tables/mensa/base.rb', line 92 def original_view_context @original_view_context || component.original_view_context end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
9 10 11 |
# File 'app/tables/mensa/base.rb', line 9 def params @params end |
#table_view ⇒ Object
Returns the value of attribute table_view.
8 9 10 |
# File 'app/tables/mensa/base.rb', line 8 def table_view @table_view end |
Class Method Details
Instance Method Details
#actions ⇒ Object
60 61 62 63 64 |
# File 'app/tables/mensa/base.rb', line 60 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
56 57 58 |
# File 'app/tables/mensa/base.rb', line 56 def actions? config[:actions].present? end |
#active_filters ⇒ Object
82 83 84 |
# File 'app/tables/mensa/base.rb', line 82 def active_filters (config[:filters] || {}).map { |column_name, value| Mensa::Filter.new(value, column: column(column_name), config: config.dig(:filters, column_name), table: self) } end |
#all_views ⇒ Object
78 79 80 |
# File 'app/tables/mensa/base.rb', line 78 def all_views [Mensa::TableView.new(name: I18n.t('.mensa.views.all'))] + TableView.where(table_name: name).where(user: [nil, Current.user]) end |
#column(name) ⇒ Object
Returns a column by name
38 39 40 |
# File 'app/tables/mensa/base.rb', line 38 def column(name) columns.find { |c| c.name == name.to_sym } end |
#column_order ⇒ Object
25 26 27 |
# File 'app/tables/mensa/base.rb', line 25 def column_order config[:column_order] || config[:columns]&.keys end |
#columns ⇒ Object
Returns all columns
30 31 32 33 34 |
# File 'app/tables/mensa/base.rb', line 30 def columns return @columns if @columns @columns ||= column_order.map { |column_name| Mensa::Column.new(column_name, config: config.dig(:columns, column_name), table: self) } end |
#display_columns ⇒ Object
Returns the columns to be displayed
43 44 45 |
# File 'app/tables/mensa/base.rb', line 43 def display_columns @display_columns ||= columns.select(&:visible?).reject(&:internal?) end |
#export_rows ⇒ Object
52 53 54 |
# File 'app/tables/mensa/base.rb', line 52 def export_rows ordered_scope.map { |row| Mensa::Row.new(self, row) } end |
#menu ⇒ Object
72 73 74 75 76 |
# File 'app/tables/mensa/base.rb', line 72 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: nil) ⇒ Object
Returns the current path with configuration
67 68 69 70 |
# File 'app/tables/mensa/base.rb', line 67 def path(order: {}, turbo_frame_id: nil, table_view_id: nil) # 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) end |
#rows ⇒ Object
Returns the rows to be displayed
48 49 50 |
# File 'app/tables/mensa/base.rb', line 48 def rows paged_scope.map { |row| Mensa::Row.new(self, row) } end |
#table_id ⇒ Object
86 87 88 89 90 |
# File 'app/tables/mensa/base.rb', line 86 def table_id return @table_id if @table_id @table_id = params[:turbo_frame_id] || "#{name.to_s.gsub("/", "__")}-#{SecureRandom.base36}" end |