Skip to content

Commit

Permalink
Refs #35274 - Reduce DB calls, update UI data
Browse files Browse the repository at this point in the history
  • Loading branch information
ofedoren committed Jul 27, 2022
1 parent 8923e74 commit 9196997
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class ApplicationController < ActionController::Base
before_action :session_expiry, :update_activity_time, :unless => proc { |c| c.remote_user_provided? || c.api_request? }
before_action :set_taxonomy, :require_mail, :check_empty_taxonomy
before_action :authorize
before_action :welcome, :only => :index, :unless => :api_request?
before_action :welcome, :find_selected_columns, :only => :index, :unless => :api_request?
prepend_before_action :allow_webpack, if: -> { Rails.configuration.webpack.dev_server.enabled }
around_action :set_timezone

Expand Down
7 changes: 7 additions & 0 deletions app/controllers/concerns/application_shared.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,11 @@ def find_session_taxonomy(taxonomy, user)
end
determined_taxonomy
end

def find_selected_columns
instance_variable_set(
'@selected_columns',
Foreman::SelectableColumns::Storage.selected_by(User.current, controller_name)
)
end
end
8 changes: 4 additions & 4 deletions app/helpers/selectable_columns_helper.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module SelectableColumnsHelper
def render_selected_column_ths(table: controller_name)
def render_selected_column_ths
result = ""
Foreman::SelectableColumns::Storage.selected_by(User.current, table.to_s).each do |column|
@selected_columns.each do |column|
result += render(
'common/selectable_column_th',
attributes: attributes(column[:th]),
Expand All @@ -12,9 +12,9 @@ def render_selected_column_ths(table: controller_name)
result.html_safe
end

def render_selected_column_tds(record, table: controller_name)
def render_selected_column_tds(record)
result = ""
Foreman::SelectableColumns::Storage.selected_by(User.current, table.to_s).each do |column|
@selected_columns.each do |column|
result += render(
'common/selectable_column_td',
attributes: attributes(column[:td]),
Expand Down
17 changes: 13 additions & 4 deletions app/registries/foreman/selectable_columns/storage.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,35 @@ def tables
end

def define(name, &block)
Foreman::Logging.logger('app').warn _('Table %s is already defined, ignoring.') % name if tables[name]
return Foreman::Logging.logger('app').warn _('Table %s is already defined, ignoring.') % name if tables[name]

table = SelectableColumns::Table.new(name)
table.instance_eval(&block)
tables[name] = table
end

def register(name, &block)
Foreman::Logging.logger('app').warn _('Table %s is not defined, ignoring.') % name unless tables[name]
return Foreman::Logging.logger('app').warn _('Table %s is not defined, ignoring.') % name unless tables[name]

tables[name].instance_eval(&block)
end

# This is for UI data mostly
def defined_for(table)
tables[table].reduce({}) do |defined, category|
defined.update(category.label => category.map { |c| { c[:key] => c[:th][:label] } })
return Foreman::Logging.logger('app').warn _('Table %s is not defined, ignoring.') % table unless tables[table]

tables[table].map do |category|
{
id: category.id,
name: category.label,
columns: category.map { |c| { id: c[:key], name: c[:th][:label] } },
}
end
end

def selected_by(user, table)
return unless tables[table]

selected_keys = user.table_preferences.find_by(name: table)&.columns&.sort
if selected_keys
tables[table].select { |category| (category.keys & selected_keys).any? }
Expand Down
4 changes: 2 additions & 2 deletions developer_docs/how_to_create_a_plugin.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -522,8 +522,8 @@ end

To use your columns in views, please use

* `render_selected_column_ths(table: controller_name)`: Renders all selectable column <th> tags for table.
* `render_selected_column_tds(record, table: controller_name)`: Renders all selectable column <td> tags for table.
* `render_selected_column_ths`: Renders all selectable column <th> tags for table.
* `render_selected_column_tds(record)`: Renders all selectable column <td> tags for table.

[[adding-a-pagelet]]
=== Adding a Pagelet
Expand Down

0 comments on commit 9196997

Please sign in to comment.