Add buttons to check all/none available languages

Although most Consul Democracy installations will only have a few
available languages using `config.i18n.available_locales`, there's a
chance some installation will keep every language as available and will
enable the desired ones using the admin interface. In these cases,
enabling (or disabling) every language would be tedious, particularly
when casually experimenting in a staging environment or while using the
official Consul Democracy demo.

So we're adding buttons to simplify the process. Since some
installations might have only a couple of available languages, and in
this case these buttons would be pretty much useless, we're only showing
them when there are many languages available.
This commit is contained in:
Javi Martín
2024-04-12 03:34:22 +02:00
parent a911b0dec7
commit c367f21705
8 changed files with 118 additions and 13 deletions

View File

@@ -5,10 +5,9 @@ describe Admin::Locales::FormComponent do
let(:enabled_locales) { %i[en nl] }
let(:locales_settings) { Setting::LocalesSettings.new(default: default_locale, enabled: enabled_locales) }
let(:component) { Admin::Locales::FormComponent.new(locales_settings) }
before { allow(I18n).to receive(:available_locales).and_return(%i[de en es nl]) }
describe "default language selector" do
before { allow(I18n).to receive(:available_locales).and_return(%i[de en es nl]) }
it "renders radio buttons when there are only a few locales" do
render_inline component
@@ -34,4 +33,24 @@ describe Admin::Locales::FormComponent do
selected: "Nederlands"
end
end
describe "buttons to check all/none" do
it "is not rendered when there are only a few locales" do
render_inline component
expect(page).not_to have_button "Select all"
expect(page).not_to have_button "Select none"
end
it "is rendered when there are many locales" do
allow(component).to receive(:select_field_threshold).and_return(3)
render_inline component
page.find(:fieldset, "Enabled languages") do |fieldset|
expect(fieldset).to have_button "Select all"
expect(fieldset).to have_button "Select none"
end
end
end
end

View File

@@ -0,0 +1,41 @@
require "rails_helper"
describe Shared::CheckAllNoneComponent do
it "generates a data-field-name attribute when a field name is given" do
render_inline Shared::CheckAllNoneComponent.new("ids[]")
expect(page).to have_button count: 2
page.find("li:first-child") do |check_all|
expect(check_all).to have_button "Select all"
expect(check_all).to have_css "button[type='button'][data-field-name='ids[]'][data-check-all]"
expect(check_all).not_to have_css "[data-check-none]"
end
page.find("li:last-child") do |check_none|
expect(check_none).to have_button "Select none"
expect(check_none).to have_css "button[type='button'][data-field-name='ids[]'][data-check-none]"
expect(check_none).not_to have_css "[data-check-all]"
end
end
it "does not generate a data-field-name attribute when no field name is given" do
render_inline Shared::CheckAllNoneComponent.new
expect(page).to have_button count: 2
page.find("li:first-child") do |check_all|
expect(check_all).to have_button "Select all"
expect(check_all).to have_css "button[type='button'][data-check-all]"
expect(check_all).not_to have_css "[data-check-none]"
expect(check_all).not_to have_css "[data-field-name]"
end
page.find("li:last-child") do |check_none|
expect(check_none).to have_button "Select none"
expect(check_none).to have_css "button[type='button'][data-check-none]"
expect(check_none).not_to have_css "[data-check-all]"
expect(check_none).not_to have_css "[data-field-name]"
end
end
end