From 6a64f38d178e136e87fc0164854798d38f92761c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= <15726+Senen@users.noreply.github.com> Date: Wed, 22 Nov 2023 21:32:37 +0100 Subject: [PATCH] Use admin table settings component to render featured settings Now, with the same template we can render all kind of settings. --- .../settings/featured_settings_table.scss | 6 -- .../stylesheets/admin/settings/table.scss | 13 +++++ .../admin/settings/table_component.html.erb | 14 +++-- .../admin/settings/table_component.rb | 22 +++++++- app/models/setting.rb | 14 ++--- .../_featured_settings_table.html.erb | 25 --------- .../admin/settings/_features_tab.html.erb | 2 +- .../_participation_processes_tab.html.erb | 2 +- .../settings/_sdg_configuration_tab.html.erb | 2 +- app/views/admin/settings/update.js.erb | 2 +- .../content_blocks/index.html.erb | 2 +- .../admin/settings/table_component_spec.rb | 42 +++++++++++++- spec/models/setting_spec.rb | 56 ++----------------- 13 files changed, 96 insertions(+), 106 deletions(-) delete mode 100644 app/assets/stylesheets/admin/settings/featured_settings_table.scss create mode 100644 app/assets/stylesheets/admin/settings/table.scss delete mode 100644 app/views/admin/settings/_featured_settings_table.html.erb diff --git a/app/assets/stylesheets/admin/settings/featured_settings_table.scss b/app/assets/stylesheets/admin/settings/featured_settings_table.scss deleted file mode 100644 index ef0df6709..000000000 --- a/app/assets/stylesheets/admin/settings/featured_settings_table.scss +++ /dev/null @@ -1,6 +0,0 @@ -.admin .featured-settings-table { - - td { - max-width: $global-width / 3; - } -} diff --git a/app/assets/stylesheets/admin/settings/table.scss b/app/assets/stylesheets/admin/settings/table.scss new file mode 100644 index 000000000..e41467509 --- /dev/null +++ b/app/assets/stylesheets/admin/settings/table.scss @@ -0,0 +1,13 @@ +.admin { + .featured-settings-table { + td { + max-width: $global-width / 3; + } + } + + .mixed-settings-table { + td { + width: 50%; + } + } +} diff --git a/app/components/admin/settings/table_component.html.erb b/app/components/admin/settings/table_component.html.erb index cf486f499..b3b088d43 100644 --- a/app/components/admin/settings/table_component.html.erb +++ b/app/components/admin/settings/table_component.html.erb @@ -1,23 +1,25 @@ - +
- - + + <% settings.each do |setting| %> - -
<%= display_setting_name(setting_name) %><%= t("admin.settings.setting_value") %><%= key_header %><%= value_header %>
- <%= t("settings.#{setting.key}") %> + + <%= t("settings.#{setting.key}") %>
<%= t("settings.#{setting.key}_description", default: t("admin.settings.no_description")) %>
+ <% if setting.content_type? %> <%= render Admin::Settings::ContentTypesFormComponent.new(setting) %> + <% elsif setting.feature? %> + <%= render Admin::Settings::FeaturedSettingsFormComponent.new(setting, tab: tab) %> <% else %> <%= render Admin::Settings::TextFormComponent.new(setting, tab: tab) %> <% end %> diff --git a/app/components/admin/settings/table_component.rb b/app/components/admin/settings/table_component.rb index 23f7997d3..3c7e3fc6e 100644 --- a/app/components/admin/settings/table_component.rb +++ b/app/components/admin/settings/table_component.rb @@ -8,11 +8,29 @@ class Admin::Settings::TableComponent < ApplicationComponent @tab = tab end - def display_setting_name(setting_name) - if setting_name == "setting" + def key_header + if setting_name == "feature" + t("admin.settings.setting") + elsif setting_name == "setting" t("admin.settings.setting_name") else t("admin.settings.#{setting_name}") 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 diff --git a/app/models/setting.rb b/app/models/setting.rb index fb2219066..626091f66 100644 --- a/app/models/setting.rb +++ b/app/models/setting.rb @@ -7,16 +7,6 @@ class Setting < ApplicationRecord key.split(".").first 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? value.present? end @@ -25,6 +15,10 @@ class Setting < ApplicationRecord key.split(".").last == "content_types" end + def feature? + %w[feature process sdg].include?(prefix) + end + def content_type_group key.split(".").second end diff --git a/app/views/admin/settings/_featured_settings_table.html.erb b/app/views/admin/settings/_featured_settings_table.html.erb deleted file mode 100644 index 649d43cec..000000000 --- a/app/views/admin/settings/_featured_settings_table.html.erb +++ /dev/null @@ -1,25 +0,0 @@ - - - - - - - - - <% features.each do |feature| %> - - - - - - <% end %> - - diff --git a/app/views/admin/settings/_features_tab.html.erb b/app/views/admin/settings/_features_tab.html.erb index 52fdae2e9..2043b33cf 100644 --- a/app/views/admin/settings/_features_tab.html.erb +++ b/app/views/admin/settings/_features_tab.html.erb @@ -1,3 +1,3 @@

<%= t("admin.settings.index.feature_flags") %>

-<%= 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") %> diff --git a/app/views/admin/settings/_participation_processes_tab.html.erb b/app/views/admin/settings/_participation_processes_tab.html.erb index 01878c51d..7ff7f1b45 100644 --- a/app/views/admin/settings/_participation_processes_tab.html.erb +++ b/app/views/admin/settings/_participation_processes_tab.html.erb @@ -1,3 +1,3 @@

<%= t("admin.settings.index.participation_processes") %>

-<%= 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") %> diff --git a/app/views/admin/settings/_sdg_configuration_tab.html.erb b/app/views/admin/settings/_sdg_configuration_tab.html.erb index e931b67e1..149908bd3 100644 --- a/app/views/admin/settings/_sdg_configuration_tab.html.erb +++ b/app/views/admin/settings/_sdg_configuration_tab.html.erb @@ -1,7 +1,7 @@ <% if feature?(:sdg) %>

<%= t("admin.settings.index.sdg.title") %>

- <%= 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 %>
<%= t("admin.settings.index.sdg.how_to_enable") %> diff --git a/app/views/admin/settings/update.js.erb b/app/views/admin/settings/update.js.erb index 503ef165d..a18526d08 100644 --- a/app/views/admin/settings/update.js.erb +++ b/app/views/admin/settings/update.js.erb @@ -6,6 +6,6 @@ var form = $("<%= j render Admin::Settings::FeaturedSettingsFormComponent.new( $("#" + 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(); <% end %> diff --git a/app/views/admin/site_customization/content_blocks/index.html.erb b/app/views/admin/site_customization/content_blocks/index.html.erb index 11c3a8493..8f6ffae3f 100644 --- a/app/views/admin/site_customization/content_blocks/index.html.erb +++ b/app/views/admin/site_customization/content_blocks/index.html.erb @@ -6,7 +6,7 @@

<%= t("admin.site_customization.content_blocks.index.title") %>

-<%= render "admin/settings/settings_table", settings: @html_settings, setting_name: "setting" %> +<%= render Admin::Settings::TableComponent.new(settings: @html_settings, setting_name: "setting") %>

<%= t("admin.site_customization.content_blocks.information") %>

diff --git a/spec/components/admin/settings/table_component_spec.rb b/spec/components/admin/settings/table_component_spec.rb index f30ed2416..8f7b7e3ac 100644 --- a/spec/components/admin/settings/table_component_spec.rb +++ b/spec/components/admin/settings/table_component_spec.rb @@ -1,10 +1,14 @@ require "rails_helper" describe Admin::Settings::TableComponent do - describe "#display_setting_name" do - it "returns correct table header" do + describe "#key_header" do + it "returns correct table header for the setting name colums" do 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" expect(page).to have_content("Setting") @@ -25,4 +29,38 @@ describe Admin::Settings::TableComponent do expect(page).to have_content("Response Data") 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 diff --git a/spec/models/setting_spec.rb b/spec/models/setting_spec.rb index f573505bf..655ade457 100644 --- a/spec/models/setting_spec.rb +++ b/spec/models/setting_spec.rb @@ -27,56 +27,12 @@ describe Setting do end end - describe "#type" do - it "returns the key prefix for 'process' settings" do - process_setting = Setting.create!(key: "process.whatever") - expect(process_setting.type).to eq "process" - end - - 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" + describe "#feature?" do + it "returns true if the key prefix is process, feature or sdg" do + expect(Setting.find_by!(key: "process.debates").feature?).to be true + expect(Setting.find_by!(key: "feature.map").feature?).to be true + 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 end end