Skip to content

Commit

Permalink
Ensure the admin locale is only set by available locales
Browse files Browse the repository at this point in the history
The available locales need to be Alchemy locales, not all locales
available to the I18n gem.
  • Loading branch information
tvdeyen committed Oct 22, 2019
1 parent 411fc9d commit bc08b36
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 30 deletions.
2 changes: 1 addition & 1 deletion lib/alchemy/admin/locale.rb
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def locale_change_needed?
#
def available_locale
locales = [params[:admin_locale], locale_from_user, locale_from_browser].compact.map(&:to_sym)
locales.detect { |locale| ::I18n.available_locales.include?(locale) }
locales.detect { |locale| Alchemy::I18n.available_locales.include?(locale) }
end

# Try to get the locale from user settings.
Expand Down
File renamed without changes.
26 changes: 26 additions & 0 deletions spec/dummy/config/locales/alchemy.en.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
en:
alchemy:
element_names:
gallery_picture: Gallery picture
content_names:
essence_boolean: EssenceBoolean
essence_date: EssenceDate
essence_file: EssenceFile
essence_html: EssenceHtml
essence_link: EssenceLink
essence_picture: EssencePicture
essence_richtext: EssenceRichtext
essence_select: EssenceSelect
essence_text: EssenceText
element_hints:
all_you_can_eat: This element shows all content types (Essences) and settings of an Element Alchemy comes with in a default installation.
content_hints:
essence_boolean: This content type (Essence) represents a simple true/false value
essence_date: This content type (Essence) represents a date value
essence_file: This content type (Essence) represents a downloadable file
essence_html: This content type (Essence) represents raw HTML output
essence_link: This content type (Essence) represents a link
essence_picture: This content type (Essence) represents a picture
essence_richtext: This content type (Essence) represents richtext
essence_select: This content type (Essence) represents values the editor can choose from
essence_text: This content type (Essence) represents a simple line of text
3 changes: 3 additions & 0 deletions spec/dummy/config/locales/alchemy.kl.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
kl:
alchemy:
welcome_back_note: majQa' Dub
26 changes: 0 additions & 26 deletions spec/dummy/config/locales/en.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,4 @@
en:
alchemy:
element_names:
gallery_picture: Gallery picture
content_names:
essence_boolean: EssenceBoolean
essence_date: EssenceDate
essence_file: EssenceFile
essence_html: EssenceHtml
essence_link: EssenceLink
essence_picture: EssencePicture
essence_richtext: EssenceRichtext
essence_select: EssenceSelect
essence_text: EssenceText
element_hints:
all_you_can_eat: This element shows all content types (Essences) and settings of an Element Alchemy comes with in a default installation.
content_hints:
essence_boolean: This content type (Essence) represents a simple true/false value
essence_date: This content type (Essence) represents a date value
essence_file: This content type (Essence) represents a downloadable file
essence_html: This content type (Essence) represents raw HTML output
essence_link: This content type (Essence) represents a link
essence_picture: This content type (Essence) represents a picture
essence_richtext: This content type (Essence) represents richtext
essence_select: This content type (Essence) represents values the editor can choose from
essence_text: This content type (Essence) represents a simple line of text

activerecord:
models:
event:
Expand Down
2 changes: 0 additions & 2 deletions spec/dummy/config/locales/kl.yml

This file was deleted.

23 changes: 22 additions & 1 deletion spec/features/admin/locale_select_feature_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

RSpec.describe 'Locale select', type: :system do
let(:a_page) { create(:alchemy_page, :public) }

before do
allow(Alchemy::I18n).to receive(:translation_files).and_return ['alchemy.kl.yml', 'alchemy.jp.yml', 'alchemy.cz.yml']
authorize_user(:as_admin)
Expand All @@ -19,9 +20,29 @@
allow(Alchemy::I18n).to receive(:available_locales).and_return [:jp, :cz]
end

it "provides only that locales" do
it "provides only these locales" do
visit admin_dashboard_path
expect(page).to have_select('change_locale', options: ['Jp', 'Cz'])
end
end

describe 'user selects locale' do
context 'that is available' do
before do
allow(Alchemy::I18n).to receive(:available_locales).and_return [:kl]
end

it 'switches the locale' do
visit admin_dashboard_path(admin_locale: 'kl')
expect(page).to have_content("majQa' Dub")
end
end

context 'that is not available' do
it 'does not switch the locale' do
visit admin_dashboard_path(admin_locale: 'de')
expect(page).to have_content("Welcome back")
end
end
end
end

0 comments on commit bc08b36

Please sign in to comment.