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.



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

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



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

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



106
107
108
# File 'app/tables/mensa/base.rb', line 106

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

Instance Method Details

#actionsObject



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

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)


63
64
65
# File 'app/tables/mensa/base.rb', line 63

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

#active_filtersObject



89
90
91
# File 'app/tables/mensa/base.rb', line 89

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



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

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)


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

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

#column_orderObject



28
29
30
# File 'app/tables/mensa/base.rb', line 28

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

#columnsObject

Returns all columns



33
34
35
36
37
# File 'app/tables/mensa/base.rb', line 33

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



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

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

#export_rowsObject



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

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

#filters?Boolean

Returns:

  • (Boolean)


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

def filters?
 columns.any?(&:filter?)
end


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

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



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

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



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

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

#table_idObject



93
94
95
96
97
# File 'app/tables/mensa/base.rb', line 93

def table_id
  return @table_id if @table_id

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