rubocop corrections

This commit is contained in:
Karol Selak 2021-03-22 03:16:29 +01:00
parent c63c6bc448
commit de29815686
72 changed files with 468 additions and 311 deletions

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ApplicationCable
class Channel < ActionCable::Channel::Base
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
module ApplicationCable
class Connection < ActionCable::Connection::Base
end

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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

View file

@ -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]

View file

@ -1,2 +1,4 @@
# frozen_string_literal: true
module ApplicationHelper
end

View file

@ -1,7 +1,10 @@
# frozen_string_literal: true
module BooksHelper
def cart_summary
number_to_currency(@books.map{ |b| b.price }.reduce(:+))
number_to_currency(@books.map(&:price).reduce(:+))
end
def can_book_be_added?(book)
logged_in? && book.quantity.positive? && !current_user.books.exists?(book.id)
end

View file

@ -1,2 +1,4 @@
# frozen_string_literal: true
module SessionsHelper
end

View file

@ -1,2 +1,4 @@
# frozen_string_literal: true
module UsersHelper
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class ApplicationJob < ActiveJob::Base
# Automatically retry jobs that encountered a deadlock
# retry_on ActiveRecord::Deadlocked

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class ApplicationMailer < ActionMailer::Base
default from: 'from@example.com'
layout 'mailer'

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class UserMailer < ApplicationMailer
def password_recovery
@user = params[:user]

View file

@ -1,23 +1,28 @@
# frozen_string_literal: true
class ApplicationRecord < ActiveRecord::Base
self.abstract_class = true
def update(*args)
result = super(*args)
AuditRecord.create(model: self.class, action: 'update', params: self.to_json)
AuditRecord.create(model: self.class, action: 'update', params: to_json)
result
end
def save(*args)
result = super(*args)
AuditRecord.create(model: self.class, action: 'save', params: self.to_json)
AuditRecord.create(model: self.class, action: 'save', params: to_json)
result
end
def self.create(*args)
result = super(*args)
AuditRecord.create(model: self.class, action: 'create', params: result.to_json)
result
end
def decrement!(*args)
result = super(*args)
AuditRecord.create(model: self.class, action: 'decrement!', params: self.to_json)
AuditRecord.create(model: self.class, action: 'decrement!', params: to_json)
result
end
end

View file

@ -1,2 +1,4 @@
# frozen_string_literal: true
class AuditRecord < ActiveRecord::Base
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Author < ApplicationRecord
validates :first_name, presence: true
validates :last_name, presence: true

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class Book < ApplicationRecord
has_and_belongs_to_many :authors

View file

@ -1,10 +1,12 @@
# frozen_string_literal: true
class User < ApplicationRecord
has_and_belongs_to_many :books
has_secure_password
has_secure_password :recovery_password, validations: false
enum role: [:customer, :admin], _default: :customer
enum status: [:ready, :blocked], _default: :ready
enum role: %i[customer admin], _default: :customer
enum status: %i[ready blocked], _default: :ready
validates :email, presence: true, uniqueness: true, format: { with: URI::MailTo::EMAIL_REGEXP }
validates :role, presence: true
@ -12,6 +14,6 @@ class User < ApplicationRecord
validates :password, {
presence: true,
length: { minimum: 8 },
if: lambda{ new_record? || !password.nil? }
if: -> { new_record? || !password.nil? }
}
end

View file

@ -1,3 +1,5 @@
# frozen_string_literal: true
class BooksPresenter < SimpleDelegator
include ActiveSupport::NumberHelper