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.



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

#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



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

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



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

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

Instance Method Details

#actionsObject



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

Returns:

  • (Boolean)


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

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

#active_filtersObject



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_viewsObject



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

Parameters:

  • name (String)


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

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

#column_orderObject



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

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

#columnsObject

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_columnsObject

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_rowsObject



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

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


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

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



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

#rowsObject

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_idObject



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