Skip to content

Commit

Permalink
Rubocop corrections.
Browse files Browse the repository at this point in the history
  • Loading branch information
xet7 committed Nov 2, 2024
1 parent 94e6ca3 commit 6b1ffc8
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 22 deletions.
15 changes: 6 additions & 9 deletions app/controllers/languages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class LanguagesController < ApplicationController
before_action :set_locale
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
Expand All @@ -15,18 +17,13 @@ def edit
end

def set_locale
if params[:locale]
I18n.locale = params[:locale]
else
I18n.locale = I18n.default_locale
end
I18n.locale = params[:locale] || I18n.default_locale
end

# Add other actions (new, create, edit, update, destroy) as needed

private

def record_not_found
redirect_to languages_path, alert: 'Language not found.'
end
def record_not_found
redirect_to languages_path, alert: "Language not found."
end
end
6 changes: 3 additions & 3 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ def create
if user&.authenticate(params[:password])
session[:user_id] = user.id
Current.user = user
redirect_to workspaces_path, notice: 'Logged in successfully.'
redirect_to workspaces_path, notice: "Logged in successfully."
else
render :new, alert: 'Invalid email or password.'
render :new, alert: "Invalid email or password."
end
end

def destroy
session[:user_id] = nil
Current.user = nil
redirect_to login_path, notice: 'Logged out successfully.'
redirect_to login_path, notice: "Logged out successfully."
end

private
Expand Down
11 changes: 5 additions & 6 deletions app/controllers/settings_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,10 @@ def index
end

private

def change_locale
if I18n.available_locales.map(&:to_s).include?(params[:locale])
session[:locale] = params[:locale]
def change_locale
if I18n.available_locales.map(&:to_s).include?(params[:locale])
session[:locale] = params[:locale]
end
redirect_back fallback_location: root_path
end
redirect_back fallback_location: root_path
end
end
2 changes: 2 additions & 0 deletions app/models/language.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class Language < ApplicationRecord
validates :name, presence: true
validates :code, presence: true, uniqueness: true
Expand Down
5 changes: 4 additions & 1 deletion app/views/languages/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
<h1>Current Locale</h1>

<p><%= I18n.locale %></p>

<h1>Available Languages</h1>
<p>Current Locale: <%= I18n.locale %></p>
<ul>
<% @languages.each do |locale| %>
<li><%= link_to locale, url_for(locale: locale) %></li>
Expand Down
4 changes: 2 additions & 2 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
get "settings", to: "settings#index"
get "help", to: "help#index"

get 'login', to: 'sessions#new'
get "login", to: "sessions#new"

root "home#index"

Expand All @@ -35,7 +35,7 @@
get "up" => "rails/health#show", as: :rails_health_check

# Route to change locale
patch 'change_locale', to: 'settings#change_locale'
patch "change_locale", to: "settings#change_locale"

# Defines the root path route ("/")
# root "posts#index"
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20240101000000_create_languages.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class CreateLanguages < ActiveRecord::Migration[7.0]
def change
create_table :languages do |t|
Expand Down
2 changes: 2 additions & 0 deletions db/migrate/20240101000001_add_locale_to_languages.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

class AddLocaleToLanguages < ActiveRecord::Migration[7.2]
def change
add_column :languages, :locale, :string
Expand Down
4 changes: 3 additions & 1 deletion db/migrate/20241102040438_add_language_to_users.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

class AddLanguageToUsers < ActiveRecord::Migration[7.0]
def change
add_column :users, :language, :string, default: 'en'
add_column :users, :language, :string, default: "en"
end
end
2 changes: 2 additions & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 6b1ffc8

Please sign in to comment.