Class: Mensa::Base

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

Instance Attribute 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



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

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.



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

def component
  @component
end

#nameObject

Returns the value of attribute name.



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

def name
  @name
end

#original_view_contextObject



108
109
110
# File 'app/tables/mensa/base.rb', line 108

def original_view_context
  @original_view_context || component.original_view_context
end

#paramsObject (readonly)

Returns the value of attribute params.



11
12
13
# File 'app/tables/mensa/base.rb', line 11

def params
  @params
end

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

#table_viewObject

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

#actionsObject



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

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



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

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

#active_filtersObject

Returns the active filters



70
71
72
# File 'app/tables/mensa/base.rb', line 70

def active_filters
  (config[:filters] || {}).map { |column_name, filter_config| Mensa::Filter.new(column: column(column_name), config: filter_config, table: self) }
end

#all_viewsObject



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

def all_views
  views = system_views
  views += TableView.where(table_name: name).where(user: [nil, Current.user])
  views
end

#column(name) ⇒ Object

Returns a column by name



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



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

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

#columnsObject

Returns all columns



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

def 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 true if the table has filters



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

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


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

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



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

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

#system_viewsObject



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

def system_views
  config[:views]&.key?(:default) ? [] : [Mensa::SystemView.new(:default, config: {name: I18n.t("mensa.views.default")}, table: self)] +
    (config[:views] || {}).keys.map { |view_name| Mensa::SystemView.new(view_name, config: config.dig(:views, view_name), table: self) }
end

#table_idObject



102
103
104
105
106
# File 'app/tables/mensa/base.rb', line 102

def table_id
  return @table_id if @table_id

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