Class: Mensa::Base

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



19
20
21
22
# File 'app/tables/mensa/base.rb', line 19

def initialize(config = {})
  @params = config.deep_symbolize_keys
  @config = self.class.definition.merge(@params || {})
end

Instance Attribute Details

#componentObject

Returns the value of attribute component.



8
9
10
# File 'app/tables/mensa/base.rb', line 8

def component
  @component
end

#configObject (readonly)

Returns the value of attribute config.



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

def config
  @config
end

#nameObject

Returns the value of attribute name.



8
9
10
# File 'app/tables/mensa/base.rb', line 8

def name
  @name
end

#original_view_contextObject



91
92
93
# File 'app/tables/mensa/base.rb', line 91

def original_view_context
  @original_view_context || component.original_view_context
end

#paramsObject (readonly)

Returns the value of attribute params.



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

def params
  @params
end

#table_viewObject

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

.definitionObject



98
99
100
# File 'app/tables/mensa/base.rb', line 98

def definition(&)
  @definition ||= Mensa::Config::TableDsl.new(self.name, &).config
end

Instance Method Details

#actionsObject



59
60
61
62
63
# File 'app/tables/mensa/base.rb', line 59

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)


55
56
57
# File 'app/tables/mensa/base.rb', line 55

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

#active_filtersObject



81
82
83
# File 'app/tables/mensa/base.rb', line 81

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_viewsObject



77
78
79
# File 'app/tables/mensa/base.rb', line 77

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

Parameters:

  • name (String)


37
38
39
# File 'app/tables/mensa/base.rb', line 37

def column(name)
  columns.find { |c| c.name == name.to_sym }
end

#column_orderObject



24
25
26
# File 'app/tables/mensa/base.rb', line 24

def column_order
  config[:column_order] || config[:columns]&.keys
end

#columnsObject

Returns all columns



29
30
31
32
33
# File 'app/tables/mensa/base.rb', line 29

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_columnsObject

Returns the columns to be displayed



42
43
44
# File 'app/tables/mensa/base.rb', line 42

def display_columns
  @display_columns ||= columns.select(&:visible?).reject(&:internal?)
end

#export_rowsObject



51
52
53
# File 'app/tables/mensa/base.rb', line 51

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


71
72
73
74
75
# File 'app/tables/mensa/base.rb', line 71

def menu
  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



66
67
68
69
# File 'app/tables/mensa/base.rb', line 66

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

#rowsObject

Returns the rows to be displayed



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

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

#table_idObject



85
86
87
88
89
# File 'app/tables/mensa/base.rb', line 85

def table_id
  return @table_id if @table_id

  @table_id = params[:turbo_frame_id] || "#{name.to_s.gsub("/", "__")}-#{SecureRandom.base36}"
end