Use admin table settings component to render featured settings

Now, with the same template we can render all kind of settings.
This commit is contained in:
Senén Rodero Rodríguez
2023-11-22 21:32:37 +01:00
committed by Javi Martín
parent e7223ba865
commit 6a64f38d17
13 changed files with 96 additions and 106 deletions

View File

@@ -1,6 +0,0 @@
.admin .featured-settings-table {
td {
max-width: $global-width / 3;
}
}

View File

@@ -0,0 +1,13 @@
.admin {
.featured-settings-table {
td {
max-width: $global-width / 3;
}
}
.mixed-settings-table {
td {
width: 50%;
}
}
}

View File

@@ -1,23 +1,25 @@
<table> <table class="<%= table_class %>">
<thead> <thead>
<tr> <tr>
<th><%= display_setting_name(setting_name) %></th> <th><%= key_header %></th>
<th><%= t("admin.settings.setting_value") %></th> <th><%= value_header %></th>
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
<% settings.each do |setting| %> <% settings.each do |setting| %>
<tr> <tr>
<td class="small-6"> <td>
<strong><%= t("settings.#{setting.key}") %></strong> <strong id="<%= dom_id(setting, :title) %>"><%= t("settings.#{setting.key}") %></strong>
<br> <br>
<span id="<%= dom_id(setting, :description) %>" class="small"> <span id="<%= dom_id(setting, :description) %>" class="small">
<%= t("settings.#{setting.key}_description", default: t("admin.settings.no_description")) %> <%= t("settings.#{setting.key}_description", default: t("admin.settings.no_description")) %>
</span> </span>
</td> </td>
<td class="small-6"> <td>
<% if setting.content_type? %> <% if setting.content_type? %>
<%= render Admin::Settings::ContentTypesFormComponent.new(setting) %> <%= render Admin::Settings::ContentTypesFormComponent.new(setting) %>
<% elsif setting.feature? %>
<%= render Admin::Settings::FeaturedSettingsFormComponent.new(setting, tab: tab) %>
<% else %> <% else %>
<%= render Admin::Settings::TextFormComponent.new(setting, tab: tab) %> <%= render Admin::Settings::TextFormComponent.new(setting, tab: tab) %>
<% end %> <% end %>

View File

@@ -8,11 +8,29 @@ class Admin::Settings::TableComponent < ApplicationComponent
@tab = tab @tab = tab
end end
def display_setting_name(setting_name) def key_header
if setting_name == "setting" if setting_name == "feature"
t("admin.settings.setting")
elsif setting_name == "setting"
t("admin.settings.setting_name") t("admin.settings.setting_name")
else else
t("admin.settings.#{setting_name}") t("admin.settings.#{setting_name}")
end end
end end
def value_header
if setting_name == "feature"
t("admin.settings.index.features.enabled")
else
t("admin.settings.setting_value")
end
end
def table_class
if settings.all?(&:feature?)
"featured-settings-table"
else
"mixed-settings-table"
end
end
end end

View File

@@ -7,16 +7,6 @@ class Setting < ApplicationRecord
key.split(".").first key.split(".").first
end end
def type
if %w[feature process proposals map html homepage uploads sdg machine_learning].include? prefix
prefix
elsif %w[remote_census].include? prefix
key.rpartition(".").first
else
"configuration"
end
end
def enabled? def enabled?
value.present? value.present?
end end
@@ -25,6 +15,10 @@ class Setting < ApplicationRecord
key.split(".").last == "content_types" key.split(".").last == "content_types"
end end
def feature?
%w[feature process sdg].include?(prefix)
end
def content_type_group def content_type_group
key.split(".").second key.split(".").second
end end

View File

@@ -1,25 +0,0 @@
<table class="featured-settings-table">
<thead>
<tr>
<th><%= t("admin.settings.setting") %></th>
<th><%= t("admin.settings.index.features.enabled") %></th>
</tr>
</thead>
<tbody>
<% features.each do |feature| %>
<tr>
<td>
<strong id="<%= dom_id(feature, :title) %>"><%= t("settings.#{feature.key}") %></strong>
<br>
<span class="small" id="<%= dom_id(feature, :description) %>">
<%= t("settings.#{feature.key}_description", default: t("admin.settings.no_description")) %>
</span>
</td>
<td>
<%= render Admin::Settings::FeaturedSettingsFormComponent.new(feature, tab: tab) %>
</td>
</tr>
<% end %>
</tbody>
</table>

View File

@@ -1,3 +1,3 @@
<h2><%= t("admin.settings.index.feature_flags") %></h2> <h2><%= t("admin.settings.index.feature_flags") %></h2>
<%= render "featured_settings_table", features: @feature_settings, tab: "#tab-feature-flags" %> <%= render Admin::Settings::TableComponent.new(settings: @feature_settings, setting_name: "feature", tab: "#tab-feature-flags") %>

View File

@@ -1,3 +1,3 @@
<h2><%= t("admin.settings.index.participation_processes") %></h2> <h2><%= t("admin.settings.index.participation_processes") %></h2>
<%= render "featured_settings_table", features: @participation_processes_settings, tab: "#tab-participation-processes" %> <%= render Admin::Settings::TableComponent.new(settings: @participation_processes_settings, setting_name: "feature", tab: "#tab-participation-processes") %>

View File

@@ -1,7 +1,7 @@
<% if feature?(:sdg) %> <% if feature?(:sdg) %>
<h2><%= t("admin.settings.index.sdg.title") %></h2> <h2><%= t("admin.settings.index.sdg.title") %></h2>
<%= render "featured_settings_table", features: @sdg_settings, tab: "#tab-sdg-configuration" %> <%= render Admin::Settings::TableComponent.new(settings: @sdg_settings, setting_name: "feature", tab: "#tab-sdg-configuration") %>
<% else %> <% else %>
<div class="callout primary"> <div class="callout primary">
<%= t("admin.settings.index.sdg.how_to_enable") %> <%= t("admin.settings.index.sdg.how_to_enable") %>

View File

@@ -6,6 +6,6 @@ var form = $("<%= j render Admin::Settings::FeaturedSettingsFormComponent.new(
$("#" + form.attr("id")).html(form.html()).find("[type='submit']").focus(); $("#" + form.attr("id")).html(form.html()).find("[type='submit']").focus();
<% if @setting.type == "feature" || @setting.type == "process" %> <% if @setting.prefix == "feature" || @setting.prefix == "process" %>
$("#side_menu").html("<%= j render Admin::MenuComponent.new %>").foundation(); $("#side_menu").html("<%= j render Admin::MenuComponent.new %>").foundation();
<% end %> <% end %>

View File

@@ -6,7 +6,7 @@
<h2 class="inline-block"><%= t("admin.site_customization.content_blocks.index.title") %></h2> <h2 class="inline-block"><%= t("admin.site_customization.content_blocks.index.title") %></h2>
<%= render "admin/settings/settings_table", settings: @html_settings, setting_name: "setting" %> <%= render Admin::Settings::TableComponent.new(settings: @html_settings, setting_name: "setting") %>
<h3><%= t("admin.site_customization.content_blocks.information") %></h3> <h3><%= t("admin.site_customization.content_blocks.information") %></h3>

View File

@@ -1,10 +1,14 @@
require "rails_helper" require "rails_helper"
describe Admin::Settings::TableComponent do describe Admin::Settings::TableComponent do
describe "#display_setting_name" do describe "#key_header" do
it "returns correct table header" do it "returns correct table header for the setting name colums" do
settings = Setting.limit(2) settings = Setting.limit(2)
render_inline Admin::Settings::TableComponent.new settings: settings, setting_name: "feature"
expect(page).to have_content("Feature")
render_inline Admin::Settings::TableComponent.new settings: settings, setting_name: "setting" render_inline Admin::Settings::TableComponent.new settings: settings, setting_name: "setting"
expect(page).to have_content("Setting") expect(page).to have_content("Setting")
@@ -25,4 +29,38 @@ describe Admin::Settings::TableComponent do
expect(page).to have_content("Response Data") expect(page).to have_content("Response Data")
end end
end end
describe "#value_header" do
it "returns correct table header for the setting interface column" do
settings = Setting.limit(2)
render_inline Admin::Settings::TableComponent.new settings: settings, setting_name: "feature"
expect(page).to have_content("Enabled")
render_inline Admin::Settings::TableComponent.new settings: settings, setting_name: "setting"
expect(page).to have_content("Value")
end
end
describe "#table_class" do
it "returns a CSS class when all given settings are features, otherwise returns a mixed class" do
settings = [Setting.find_by(key: "feature.map"), Setting.find_by(key: "process.debates")]
render_inline Admin::Settings::TableComponent.new settings: settings, setting_name: "feature"
expect(page).to have_css(".featured-settings-table")
expect(page).not_to have_css(".mixed-settings-table")
end
it "returns a CSS class when all given settings are features, otherwise returns a mixed class" do
settings = [Setting.find_by(key: "feature.map"), Setting.find_by(key: "mailer_from_name")]
render_inline Admin::Settings::TableComponent.new settings: settings, setting_name: "feature"
expect(page).not_to have_css(".featured-settings-table")
expect(page).to have_css(".mixed-settings-table")
end
end
end end

View File

@@ -27,56 +27,12 @@ describe Setting do
end end
end end
describe "#type" do describe "#feature?" do
it "returns the key prefix for 'process' settings" do it "returns true if the key prefix is process, feature or sdg" do
process_setting = Setting.create!(key: "process.whatever") expect(Setting.find_by!(key: "process.debates").feature?).to be true
expect(process_setting.type).to eq "process" expect(Setting.find_by!(key: "feature.map").feature?).to be true
end expect(Setting.find_by!(key: "sdg.process.debates").feature?).to be true
expect(Setting.find_by!(key: "uploads.documents.max_size").feature?).to be false
it "returns the key prefix for 'feature' settings" do
feature_setting = Setting.create!(key: "feature.whatever")
expect(feature_setting.type).to eq "feature"
end
it "returns the key prefix for 'map' settings" do
map_setting = Setting.create!(key: "map.whatever")
expect(map_setting.type).to eq "map"
end
it "returns the key prefix for 'html' settings" do
html_setting = Setting.create!(key: "html.whatever")
expect(html_setting.type).to eq "html"
end
it "returns the key prefix for 'homepage' settings" do
homepage_setting = Setting.create!(key: "homepage.whatever")
expect(homepage_setting.type).to eq "homepage"
end
it "returns the key prefix for 'sdg' settings" do
sdg_setting = Setting.create!(key: "sdg.whatever")
expect(sdg_setting.type).to eq "sdg"
end
it "returns the key prefix for 'remote_census.general' settings" do
remote_census_general_setting = Setting.create!(key: "remote_census.general.whatever")
expect(remote_census_general_setting.type).to eq "remote_census.general"
end
it "returns the key prefix for 'remote_census_request' settings" do
remote_census_request_setting = Setting.create!(key: "remote_census.request.whatever")
expect(remote_census_request_setting.type).to eq "remote_census.request"
end
it "returns the key prefix for 'remote_census_response' settings" do
remote_census_response_setting = Setting.create!(key: "remote_census.response.whatever")
expect(remote_census_response_setting.type).to eq "remote_census.response"
end
it "returns 'configuration' for the rest of the settings" do
configuration_setting = Setting.create!(key: "whatever")
expect(configuration_setting.type).to eq "configuration"
end end
end end