rubocop corrections
This commit is contained in:
parent
c63c6bc448
commit
de29815686
72 changed files with 468 additions and 311 deletions
|
@ -1,3 +1,6 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Base for application controllers
|
||||
class ApplicationController < ActionController::Base
|
||||
helper_method :current_user
|
||||
helper_method :logged_in?
|
||||
|
@ -5,7 +8,7 @@ class ApplicationController < ActionController::Base
|
|||
def current_user
|
||||
User.find_by(id: session[:user_id])
|
||||
end
|
||||
|
||||
|
||||
def logged_in?
|
||||
!current_user.nil?
|
||||
end
|
||||
|
@ -13,15 +16,13 @@ class ApplicationController < ActionController::Base
|
|||
protected
|
||||
|
||||
def notices_from_errors(record)
|
||||
messages = record.errors.messages.map do |attribute, messages|
|
||||
errors = record.errors.messages.map do |attribute, messages|
|
||||
messages.map { |message| "#{attribute} #{message}".capitalize }
|
||||
end
|
||||
messages.flatten
|
||||
errors.flatten
|
||||
end
|
||||
|
||||
def ensure_admin
|
||||
unless current_user&.admin?
|
||||
redirect_to '/welcome', notice: 'You are not allowed to perform this action'
|
||||
end
|
||||
redirect_to '/welcome', notice: 'You are not allowed to perform this action' unless current_user&.admin?
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Authors controller
|
||||
class AuthorsController < ApplicationController
|
||||
before_action :ensure_admin
|
||||
before_action :set_author, only: [:edit, :update]
|
||||
before_action :set_author, only: %i[edit update]
|
||||
|
||||
def index
|
||||
@authors = Author.all
|
||||
|
@ -11,9 +14,7 @@ class AuthorsController < ApplicationController
|
|||
end
|
||||
|
||||
def update
|
||||
if @author.update(author_params)
|
||||
redirect_to '/authors'
|
||||
end
|
||||
redirect_to '/authors' if @author.update(author_params)
|
||||
end
|
||||
|
||||
private
|
||||
|
|
|
@ -1,26 +1,25 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Books controller
|
||||
class BooksController < ApplicationController
|
||||
before_action :set_book, only: [:show, :edit, :update, :add_to_cart]
|
||||
before_action :ensure_admin, only: [:edit, :update]
|
||||
before_action :set_book, only: %i[show edit update add_to_cart]
|
||||
before_action :ensure_admin, only: %i[edit update]
|
||||
|
||||
def index
|
||||
if current_user&.admin?
|
||||
books = Book.all
|
||||
else
|
||||
books = Book.published
|
||||
end
|
||||
books = if current_user&.admin?
|
||||
Book.all
|
||||
else
|
||||
Book.published
|
||||
end
|
||||
@books = books.map { |book| BooksPresenter.new(book) }
|
||||
end
|
||||
|
||||
def show
|
||||
end
|
||||
def show; end
|
||||
|
||||
def edit
|
||||
end
|
||||
def edit; end
|
||||
|
||||
def update
|
||||
if @book.update(book_params)
|
||||
redirect_to '/books'
|
||||
end
|
||||
redirect_to '/books' if @book.update(book_params)
|
||||
end
|
||||
|
||||
def add_to_cart
|
||||
|
|
|
@ -1,23 +1,18 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Sessions controller
|
||||
class SessionsController < ApplicationController
|
||||
def new
|
||||
end
|
||||
def new; end
|
||||
|
||||
def create
|
||||
@user = User.find_by(email: params[:email])
|
||||
unless @user
|
||||
redirect_to '/welcome', notice: 'Wrong email address'
|
||||
return
|
||||
problem = problem_with_login
|
||||
if problem
|
||||
redirect_to '/welcome', notice: problem
|
||||
else
|
||||
session[:user_id] = @user.id
|
||||
redirect_to '/welcome'
|
||||
end
|
||||
unless @user.authenticate(params[:password])
|
||||
redirect_to '/welcome', notice: 'Wrong password'
|
||||
return
|
||||
end
|
||||
if @user.blocked?
|
||||
redirect_to '/welcome', notice: 'You are blocked, please contact support'
|
||||
return
|
||||
end
|
||||
session[:user_id] = @user.id
|
||||
redirect_to '/welcome'
|
||||
end
|
||||
|
||||
def delete
|
||||
|
@ -25,6 +20,17 @@ class SessionsController < ApplicationController
|
|||
redirect_to '/welcome', notice: 'Logged out properly'
|
||||
end
|
||||
|
||||
def welcome
|
||||
def welcome; end
|
||||
|
||||
private
|
||||
|
||||
def problem_with_login
|
||||
if !@user
|
||||
'Wrong email address'
|
||||
elsif !@user.authenticate(params[:password])
|
||||
'Wrong password'
|
||||
elsif @user.blocked?
|
||||
'You are blocked, please contact support'
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,5 +1,8 @@
|
|||
# frozen_string_literal: true
|
||||
|
||||
# Users controller
|
||||
class UsersController < ApplicationController
|
||||
before_action :ensure_admin, only: [:destroy, :block]
|
||||
before_action :ensure_admin, only: %i[destroy block]
|
||||
|
||||
def index
|
||||
@users = User.all
|
||||
|
@ -21,15 +24,14 @@ class UsersController < ApplicationController
|
|||
|
||||
def password_recovery_request
|
||||
@user = User.where(email: params['email']).first
|
||||
recovery_password = ('a'..'z').to_a.shuffle[0,8].join
|
||||
recovery_password = ('a'..'z').to_a.sample(8).join
|
||||
@user.recovery_password = recovery_password
|
||||
@user.save
|
||||
UserMailer.with(user: @user, recovery_password: recovery_password).password_recovery.deliver_now
|
||||
redirect_to '/welcome', notice: "Recovery email sent to #{params['email']}"
|
||||
end
|
||||
|
||||
def password_recovery_request_form
|
||||
end
|
||||
def password_recovery_request_form; end
|
||||
|
||||
def recover_password_form
|
||||
@recovery_password = params[:recovery_password]
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue