Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

I18n #30

Merged
merged 14 commits into from
Jan 31, 2021
20 changes: 13 additions & 7 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@ class ApplicationController < ActionController::Base
protect_from_forgery with: :exception
include SessionsHelper

around_action :switch_locale

def switch_locale(&action)
locale = current_user.try(:locale) || I18n.default_locale
I18n.with_locale(locale, &action)
end

private

# Confirms a logged in user
def logged_in_user
unless logged_in?
flash[:danger] = "Please log in."
redirect_to login_url
end
# Confirms a logged in user
def logged_in_user
unless logged_in?
flash[:danger] = t 'controllers.application.log-in'
redirect_to login_url
end

end
end
3 changes: 2 additions & 1 deletion app/controllers/comments_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class CommentsController < ApplicationController
# frozen_string_literal: true

class CommentsController < ApplicationController
def create
info = Info.find(params[:info_id])
@comment = info.comments.create(comment_params)
Expand Down
36 changes: 21 additions & 15 deletions app/controllers/infos_controller.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# frozen_string_literal: true

class InfosController < ApplicationController
before_action :logged_in_user, only: [:index, :create]
before_action :set_info, only: [:show, :edit, :update]
before_action :logged_in_user, only: %i[index show edit update archived]
before_action :set_info, only: %i[show edit update]

def index
@infos = Info.not_archived
@archived_count = Info.archived.count
@archived_count = Info.archived.size
end

def show
def show
@info = Info.find(params[:id])
end

Expand All @@ -16,14 +18,20 @@ def new
end

def edit
@info = Info.find(params[:id])
if @info.user_id == current_user.id
render :edit
else
render :show
end
end

def create
@info = current_user.infos.build(info_params)
@info.archived = false

if @info.save
flash[:success] = 'Info successfully created.'
flash[:success] = t 'controllers.infos.create.created'
redirect_to infos_url
else
render :new
Expand All @@ -32,7 +40,7 @@ def create

def update
if @info.update(info_params)
flash[:success] = 'Info updated.'
flash[:success] = t 'controllers.infos.update.updated'
redirect_to infos_url
else
render :edit
Expand All @@ -46,15 +54,13 @@ def archived

private

def info_params
params.require(:info).permit(:title, :content, :archived)
end

# Before actions

def set_info
@info = Info.find(params[:id])
end
def info_params
params.require(:info).permit(:title, :content, :archived)
end

# Before actions

def set_info
@info = Info.find(params[:id])
end
end
11 changes: 6 additions & 5 deletions app/controllers/listitems_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

class ListitemsController < ApplicationController
before_action :logged_in_user, only: [:index, :edit, :update, :destroy]
before_action :logged_in_user, only: %i[index edit update destroy]

def index
@shopping_items = Listitem.all
Expand All @@ -11,14 +13,14 @@ def create
@shopping_item.done = false

if @shopping_item.save
flash[:success] = 'Shopping list item added.'
flash[:success] = t 'controllers.listitems.create.created'
redirect_to listitems_url
end
end

def update
@shopping_item = Listitem.find(params[:id])

if @shopping_item.update(list_params)
redirect_to listitems_url
end
Expand All @@ -29,7 +31,7 @@ def destroy

if @shopping_items.count > 0
if @shopping_items.destroy_all
flash[:success] = 'Shopping list items, which are already bought, deleted.'
flash[:success] = t 'controllers.listitems.destroy.destroyed'
redirect_to listitems_url
end
end
Expand All @@ -40,5 +42,4 @@ def destroy
def list_params
params.require(:listitem).permit(:item, :done)
end

end
11 changes: 6 additions & 5 deletions app/controllers/qandas_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

class QandasController < ApplicationController
before_action :logged_in_user, only: [:index]
before_action :logged_in_user, only: %i[index edit update]

def index
@qandas = Qanda.all
Expand All @@ -12,12 +14,12 @@ def new
def edit
@qanda = Qanda.find(params[:id])
end

def create
@qanda = Qanda.new(qanda_params)

if @qanda.save
flash[:success] = 'Question successfully created.'
flash[:success] = t 'controllers.qandas.create.created'
redirect_to qandas_url
else
render :new
Expand All @@ -28,7 +30,7 @@ def update
@qanda = Qanda.find(params[:id])

if @qanda.update(qanda_params)
flash[:success] = 'Question/Answer updated.'
flash[:success] = t 'controllers.qandas.update.updated.'
redirect_to qandas_url
else
render :edit
Expand All @@ -40,5 +42,4 @@ def update
def qanda_params
params.require(:qanda).permit(:question, :answer)
end

end
5 changes: 3 additions & 2 deletions app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

class SessionsController < ApplicationController

def new
end

Expand All @@ -10,7 +11,7 @@ def create
log_in user
redirect_to infos_path
else
flash.now[:danger] = 'Invalid email/password combination'
flash.now[:danger] = t 'controllers.sessions.create.danger'
render :new
end
end
Expand Down
4 changes: 3 additions & 1 deletion app/controllers/static_pages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

class StaticPagesController < ApplicationController
before_action :logged_in_user, only: [:rules, :housework]
before_action :logged_in_user, only: %i[rules housework]

def rules
end
Expand Down
26 changes: 7 additions & 19 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# frozen_string_literal: true

class UsersController < ApplicationController
before_action :logged_in_user, only: [:index, :edit]
before_action :logged_in_user, only: %i[index edit update]

def index
@users = User.all
Expand All @@ -16,7 +18,7 @@ def edit
def create
@user = User.new(user_params)
if @user.save
flash[:success] = "User #{@user.name} created!"
flash[:success] = "#{ t 'controllers.users.create.created', name: @user.name }"
redirect_to users_url
else
render :new
Expand All @@ -26,7 +28,7 @@ def create
def update
@user = User.find(params[:id])
if @user.update_attributes(user_params)
flash[:success] = "Update successful!"
flash[:success] = t 'controllers.users.update.updated'
redirect_to edit_user_path(@user)
else
render :edit
Expand All @@ -39,21 +41,7 @@ def user_params
params.require(:user).permit(:name, :email, :phone, :birthday,
:moved_in, :moved_out,
:description, :password,
:password_confirmation, :picture)
:password_confirmation, :picture,
:locale)
end

=begin
# Before filters

# Confirms the correct user.
def correct_user
@user = User.find(params[:id])
redirect_to(root_url) unless current_user?(@user)
end

# Confirms an admin user.
def admin_user
redirect_to(root_url) unless current_user.admin?
=end

end
12 changes: 9 additions & 3 deletions app/models/info.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,15 @@ class Info < ApplicationRecord

validates :user_id, presence: true

default_scope -> { order(created_at: :desc) }
scope :not_archived, -> { where(archived: false).order(created_at: :desc) }
scope :archived, -> { where(archived: true).order(created_at: :desc) }
default_scope -> { includes(:user).order(created_at: :desc) }
scope :not_archived, -> { includes(:user, :comments)
.where(archived: false)
.order(created_at: :desc)
}
scope :archived, -> { includes(:user, :comments)
.where(archived: true)
.order(created_at: :desc)
}

validates :title, presence: true
validates :content, presence: true
Expand Down
25 changes: 14 additions & 11 deletions app/views/admin/admin/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<% provide(:title, 'Admin') %>
<h1>Administration Dashboard</h1>
<h1><%= t 'admin.admin.index.title' %></h1>

<div class="row">
<div class="col-md-4 col-sm-6">
Expand All @@ -11,10 +11,10 @@
<tr>
<td><%= user.id %></td>
<td><%= user.name %></td>
<td><%= link_to "edit", edit_admin_user_path(user) %></td>
<td><%= link_to "delete", [:admin, user], method: :delete,
data: { confirm: "Are you sure want to delete #{user.name}? Think about again!" },
class: 'text-danger' %></td>
<td><%= link_to "...", edit_admin_user_path(user) %></td>
<td><%= link_to "x", [:admin, user], method: :delete,
data: { confirm: "#{ t 'admin.admin.index.user.delete.confirm', name: user.name }" },
class: 'text-danger' %></td>
</tr>
<% end %>
</tbody>
Expand All @@ -35,11 +35,12 @@
<% @infos_narch.each do |info| %>
<tr>
<td><%= l info.created_at, format: :short %></td>
<td><%= info.user ? info.user.name : content_tag(:span, "Deleted User", class: "text-danger") %></td>
<td><%= info.user ? info.user.name : content_tag(:span, "#{ t 'admin.admin.index.user.deleted-user' }",
class: "text-danger") %></td>

<td><%= info.title %></td>
<td><%= link_to "delete", [:admin, info], method: :delete,
data: { confirm: "Are you sure want to delete #{info.title}?" },
<td><%= link_to "x", [:admin, info], method: :delete,
data: { confirm: "#{ t 'admin.admin.index.info.delete.confirm', title: info.title }" },
class: 'text-danger' %></td>
</tr>
<% end %>
Expand All @@ -49,10 +50,12 @@
<% @infos_arch.each do |info| %>
<tr>
<td><%= l info.created_at, format: :short %></td>
<td><%= info.user ? info.user.name : content_tag(:span, "Deleted User", class: "text-danger") %></td>
<td><%= info.user ? info.user.name : content_tag(:span, "#{ t 'admin.admin.index.user.deleted-user' }",
class: "text-danger") %></td>
<td><%= info.title %></td>
<td><%= link_to "delete", [:admin, info], method: :delete,
data: { confirm: "Are you sure want to delete #{info.title}?" } %></td>
<td><%= link_to "x", [:admin, info], method: :delete,
data: { confirm: "Are you sure want to delete #{info.title}?" },
class: 'text-danger' %></td>
</tr>
<% end %>

Expand Down
Loading