refactoring and rubocop changes
This commit is contained in:
parent
f7ec73f85e
commit
5475086622
13 changed files with 33 additions and 10 deletions
|
@ -39,16 +39,9 @@ class UsersController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def recover_password
|
def recover_password
|
||||||
user = User.find(params[:user_id])
|
@user = User.find(params[:user_id])
|
||||||
if user.recovery_password_digest && user.authenticate_recovery_password(params[:recovery_password])
|
if recovery_password_proper?
|
||||||
user.password = params[:password]
|
set_new_password
|
||||||
user.password_confirmation = params[:password_confirmation]
|
|
||||||
if user.save
|
|
||||||
user.update(recovery_password: nil)
|
|
||||||
redirect_to '/welcome', notice: 'Password changed'
|
|
||||||
else
|
|
||||||
redirect_to '/welcome', notice: 'Passwords don\'t match'
|
|
||||||
end
|
|
||||||
else
|
else
|
||||||
redirect_to '/welcome', notice: 'Recovery link expired or invalid'
|
redirect_to '/welcome', notice: 'Recovery link expired or invalid'
|
||||||
end
|
end
|
||||||
|
@ -63,4 +56,22 @@ class UsersController < ApplicationController
|
||||||
User.find(params[:id]).update(status: :blocked)
|
User.find(params[:id]).update(status: :blocked)
|
||||||
redirect_to '/users'
|
redirect_to '/users'
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
|
||||||
|
def recovery_password_proper?
|
||||||
|
@user.recovery_password_digest &&
|
||||||
|
@user.authenticate_recovery_password(params[:recovery_password])
|
||||||
|
end
|
||||||
|
|
||||||
|
def set_new_password
|
||||||
|
@user.password = params[:password]
|
||||||
|
@user.password_confirmation = params[:password_confirmation]
|
||||||
|
if @user.save
|
||||||
|
@user.update(recovery_password: nil)
|
||||||
|
redirect_to '/welcome', notice: 'Password changed'
|
||||||
|
else
|
||||||
|
redirect_to '/welcome', notice: 'Passwords don\'t match'
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Application helper
|
||||||
module ApplicationHelper
|
module ApplicationHelper
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Books helper
|
||||||
module BooksHelper
|
module BooksHelper
|
||||||
def cart_summary
|
def cart_summary
|
||||||
number_to_currency(@books.map(&:price).reduce(:+))
|
number_to_currency(@books.map(&:price).reduce(:+))
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Sessions helper
|
||||||
module SessionsHelper
|
module SessionsHelper
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Users helper
|
||||||
module UsersHelper
|
module UsersHelper
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# User mailer
|
||||||
class UserMailer < ApplicationMailer
|
class UserMailer < ApplicationMailer
|
||||||
def password_recovery
|
def password_recovery
|
||||||
@user = params[:user]
|
@user = params[:user]
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Application record
|
||||||
class ApplicationRecord < ActiveRecord::Base
|
class ApplicationRecord < ActiveRecord::Base
|
||||||
self.abstract_class = true
|
self.abstract_class = true
|
||||||
def update(*args)
|
def update(*args)
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Books presenter
|
||||||
class BooksPresenter < SimpleDelegator
|
class BooksPresenter < SimpleDelegator
|
||||||
include ActiveSupport::NumberHelper
|
include ActiveSupport::NumberHelper
|
||||||
|
|
||||||
|
|
|
@ -8,6 +8,7 @@ require 'rails/all'
|
||||||
# you've limited to :test, :development, or :production.
|
# you've limited to :test, :development, or :production.
|
||||||
Bundler.require(*Rails.groups)
|
Bundler.require(*Rails.groups)
|
||||||
|
|
||||||
|
# App
|
||||||
module App
|
module App
|
||||||
class Application < Rails::Application
|
class Application < Rails::Application
|
||||||
# Initialize configuration defaults for originally generated Rails version.
|
# Initialize configuration defaults for originally generated Rails version.
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Create authors
|
||||||
class CreateAuthors < ActiveRecord::Migration[6.1]
|
class CreateAuthors < ActiveRecord::Migration[6.1]
|
||||||
def change
|
def change
|
||||||
create_table :authors do |t|
|
create_table :authors do |t|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Create books
|
||||||
class CreateBooks < ActiveRecord::Migration[6.1]
|
class CreateBooks < ActiveRecord::Migration[6.1]
|
||||||
def change
|
def change
|
||||||
create_table :books do |t|
|
create_table :books do |t|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Create association table between authors and books
|
||||||
class CreateAuthorsBooks < ActiveRecord::Migration[6.1]
|
class CreateAuthorsBooks < ActiveRecord::Migration[6.1]
|
||||||
def change
|
def change
|
||||||
create_table :authors_books do |t|
|
create_table :authors_books do |t|
|
||||||
|
|
|
@ -1,5 +1,6 @@
|
||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
|
# Create users
|
||||||
class CreateUsers < ActiveRecord::Migration[6.1]
|
class CreateUsers < ActiveRecord::Migration[6.1]
|
||||||
def change
|
def change
|
||||||
create_table :users do |t|
|
create_table :users do |t|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue