-
Notifications
You must be signed in to change notification settings - Fork 993
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fixes #35274 - Make columns on host index page selectable (#9319)
- Loading branch information
Showing
17 changed files
with
585 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
module SelectableColumnsHelper | ||
def render_selected_column_ths | ||
result = "" | ||
@selected_columns.each do |column| | ||
result += render( | ||
'common/selectable_column_th', | ||
attributes: attributes(column[:th]), | ||
th_content: th_content(column), | ||
callback: column[:th][:callback] | ||
) | ||
end | ||
result.html_safe | ||
end | ||
|
||
def render_selected_column_tds(record) | ||
result = "" | ||
@selected_columns.each do |column| | ||
result += render( | ||
'common/selectable_column_td', | ||
attributes: attributes(column[:td]), | ||
attr_callbacks: column[:td][:attr_callbacks], | ||
subject: record, | ||
callback: column[:td][:callback] | ||
) | ||
end | ||
result.html_safe | ||
end | ||
|
||
def attr_from_callbacks(callbacks, subject) | ||
return unless callbacks | ||
|
||
callbacks.map { |(k, v)| "#{k}=\"#{instance_exec(subject, &v)}\"" } | ||
.join(' ') | ||
.html_safe | ||
end | ||
|
||
private | ||
|
||
def attributes(th_or_td) | ||
th_or_td.except(:callback, :sortable, :default_sort, :attr_callbacks, :label) | ||
.map { |(k, v)| "#{k}=\"#{v}\"" } | ||
.join(' ') | ||
.html_safe | ||
end | ||
|
||
def th_content(col) | ||
return if col[:th][:callback] | ||
return col[:th][:label] unless col[:th][:sortable] | ||
|
||
sort col[:key], as: col[:th][:label], default: col[:th][:default_sort] || 'ASC' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
module Foreman | ||
module SelectableColumns | ||
class Category < Array | ||
attr_reader :id, :label | ||
|
||
def initialize(id, label, table, default: false) | ||
@id = id | ||
@label = label | ||
@table = table | ||
@default = default | ||
@columns_to_use = HashWithIndifferentAccess.new | ||
super(0) | ||
end | ||
|
||
def column(key, th:, td:) | ||
self << { key: key.to_s, th: th, td: td }.with_indifferent_access | ||
end | ||
|
||
def use_column(key, from:) | ||
return if from.to_s == @id | ||
|
||
@columns_to_use[from] ||= [] | ||
@columns_to_use[from] << key.to_s | ||
end | ||
|
||
def common_th_class | ||
'hidden-tablet hidden-xs' | ||
end | ||
|
||
def common_td_class | ||
common_th_class + ' ellipsis' | ||
end | ||
|
||
# This instance can be updated after it was created by DSL, but | ||
# only at the initialization time. | ||
# This method is only for actual runtime usage, | ||
# after the Storage is fully defined/updated. | ||
# Thus, saving once computed result. | ||
def keys | ||
@keys ||= columns.map { |c| c[:key] }.sort | ||
end | ||
|
||
def default? | ||
@default | ||
end | ||
|
||
def columns | ||
self + foreign_columns | ||
end | ||
|
||
private | ||
|
||
# This is meant for actual runtime usage, | ||
# after the categories are fully defined. | ||
# Thus, saving once computed result. | ||
def foreign_columns | ||
@foreign_columns ||= @columns_to_use.keys.map do |category_id| | ||
@table.find { |cat| cat.id == category_id }&.select { |col| @columns_to_use[category_id].include?(col[:key]) } | ||
end.flatten.compact | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
module Foreman | ||
module SelectableColumns | ||
class Storage | ||
include Singleton | ||
|
||
class << self | ||
def tables | ||
@tables ||= ActiveSupport::HashWithIndifferentAccess.new | ||
end | ||
|
||
def define(name, &block) | ||
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) | ||
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) | ||
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.columns.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 | ||
result = if selected_keys | ||
tables[table].select { |category| (category.keys & selected_keys).any? } | ||
.map(&:columns) | ||
.flatten | ||
.select { |col| selected_keys.include?(col[:key]) } | ||
else | ||
tables[table].select { |category| category.default? } | ||
.map(&:columns) | ||
.flatten | ||
end | ||
result.uniq | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
module Foreman | ||
module SelectableColumns | ||
class Table < Array | ||
attr_reader :name | ||
|
||
def initialize(name) | ||
@name = name | ||
super(0) | ||
end | ||
|
||
def category(id, label: _('General'), default: false, &block) | ||
category = find_or_create(id.to_s, label, default) | ||
category.instance_eval(&block) | ||
end | ||
|
||
private | ||
|
||
def find_or_create(id, label, default) | ||
category = find { |c| c.id == id } | ||
unless category | ||
category = Category.new(id, label, self, default: default) | ||
self << category | ||
end | ||
category | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<td <%= attributes %> <%= attr_from_callbacks(attr_callbacks, subject) %>><%= instance_exec(subject, &callback) %></td> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
<th <%= attributes %>><%= th_content ? th_content : instance_eval(callback) %></th> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.