diff --git a/app/controllers/active_language_controller.rb b/app/controllers/active_language_controller.rb
new file mode 100644
index 0000000..9041eb1
--- /dev/null
+++ b/app/controllers/active_language_controller.rb
@@ -0,0 +1,10 @@
+class ActiveLanguageController < ApplicationController
+
+ def set_active_language
+ if Language.find_by_id(params[:active_language_id]) != nil
+ cookies[:active_language_id] = params[:active_language_id]
+ else
+ redirect_to root_path, status: :unprocessable_entity
+ end
+ end
+end
diff --git a/app/helpers/active_language_helper.rb b/app/helpers/active_language_helper.rb
new file mode 100644
index 0000000..6602109
--- /dev/null
+++ b/app/helpers/active_language_helper.rb
@@ -0,0 +1,8 @@
+module ActiveLanguageHelper
+
+ def active_language_select_tag
+ languages = Language.all.to_a.delete_if {|language| language.name == "English" }
+ options = options_from_collection_for_select(languages, "id", "name", cookies[:active_language_id] || 1)
+ select_tag "active_language_id", options
+ end
+end
diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb
index 1825218..4bdc0e1 100644
--- a/app/views/layouts/application.html.erb
+++ b/app/views/layouts/application.html.erb
@@ -18,6 +18,11 @@
<% if user_signed_in? %>
|
<%= link_to "Languages", admin_languages_path %>
+ |
+ <%= form_tag '/set_active_language', class: "d-inline" do %>
+ <%= active_language_select_tag %>
+ <%= submit_tag "Switch Language" %>
+ <% end %>
<% end %>
diff --git a/config/routes.rb b/config/routes.rb
index 24361b3..f0e8a4c 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -3,6 +3,9 @@ Rails.application.routes.draw do
#get 'dictionary/index'
resources :dictionary, only: [:index, :show, :create]
resources :words, only: [:index, :show]
+
+ post '/set_active_language', action: :set_active_language, controller: :active_language
+
namespace :admin do
resources :languages
end
diff --git a/spec/factories/languages.rb b/spec/factories/languages.rb
index 24c2329..f29dcf5 100644
--- a/spec/factories/languages.rb
+++ b/spec/factories/languages.rb
@@ -1,5 +1,9 @@
FactoryBot.define do
factory :language do
- name { "MyString" }
+ sequence(:name) { |n| "MyString#{n.to_s}" }
+ end
+
+ factory :language_english, class: :language do
+ name { "English" }
end
end
diff --git a/spec/helpers/active_language_helper_spec.rb b/spec/helpers/active_language_helper_spec.rb
new file mode 100644
index 0000000..a4f36ce
--- /dev/null
+++ b/spec/helpers/active_language_helper_spec.rb
@@ -0,0 +1,31 @@
+require 'rails_helper'
+
+# Specs in this file have access to a helper object that includes
+# the ActiveLanguageHelper. For example:
+#
+# describe ActiveLanguageHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# expect(helper.concat_strings("this","that")).to eq("this that")
+# end
+# end
+# end
+RSpec.describe ActiveLanguageHelper, type: :helper do
+
+ describe "active_language_helper_tag" do
+ before :each do
+ english = FactoryBot.create(:language_english)
+ 2.times { FactoryBot.create(:language) }
+ @tag = active_language_select_tag
+ end
+
+ it "should return a select tag" do
+ expect(@tag).to have_selector(%(select))
+ end
+
+ it "should not contain an English option" do
+ expect(@tag).to_not include("English")
+
+ end
+ end
+end
diff --git a/spec/requests/active_language_spec.rb b/spec/requests/active_language_spec.rb
new file mode 100644
index 0000000..39096da
--- /dev/null
+++ b/spec/requests/active_language_spec.rb
@@ -0,0 +1,20 @@
+require 'rails_helper'
+
+RSpec.describe "ActiveLanguages", type: :request do
+
+ describe "POST :set_active_language" do
+
+ it "should set the cookie" do
+ language = FactoryBot.create(:language)
+ post "/set_active_language/", params: { active_language_id: language.id }
+ expect(cookies[:active_language_id]).to_not be_nil
+ end
+
+ it "should not set the cookie if language does not exist" do
+ language = FactoryBot.create(:language)
+ id = language.id + 1
+ post "/set_active_language/", params: { active_language_id: id }
+ expect(response).to have_http_status(:unprocessable_entity)
+ end
+ end
+end
diff --git a/spec/requests/root_spec.rb b/spec/requests/root_spec.rb
index 6963d48..a9b810c 100644
--- a/spec/requests/root_spec.rb
+++ b/spec/requests/root_spec.rb
@@ -39,5 +39,12 @@ RSpec.describe "Root path", type: :request do
get root_path
expect(response.body).to include(user.username)
end
+
+ it "should have a language drop-down" do
+ user = FactoryBot.create(:user)
+ sign_in user
+ get root_path
+ expect(response.body).to have_field("active_language_id")
+ end
end
end