diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 8137d00..fc77082 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -39,16 +39,9 @@ class UsersController < ApplicationController end def recover_password - user = User.find(params[:user_id]) - if user.recovery_password_digest && user.authenticate_recovery_password(params[:recovery_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 + @user = User.find(params[:user_id]) + if recovery_password_proper? + set_new_password else redirect_to '/welcome', notice: 'Recovery link expired or invalid' end @@ -63,4 +56,22 @@ class UsersController < ApplicationController User.find(params[:id]).update(status: :blocked) redirect_to '/users' 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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 15b06f0..292788a 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +# Application helper module ApplicationHelper end diff --git a/app/helpers/books_helper.rb b/app/helpers/books_helper.rb index 0e4b37c..84f1d25 100644 --- a/app/helpers/books_helper.rb +++ b/app/helpers/books_helper.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +# Books helper module BooksHelper def cart_summary number_to_currency(@books.map(&:price).reduce(:+)) diff --git a/app/helpers/sessions_helper.rb b/app/helpers/sessions_helper.rb index 97aeb4c..dc9b8a1 100644 --- a/app/helpers/sessions_helper.rb +++ b/app/helpers/sessions_helper.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +# Sessions helper module SessionsHelper end diff --git a/app/helpers/users_helper.rb b/app/helpers/users_helper.rb index 4dc909e..f9820e4 100644 --- a/app/helpers/users_helper.rb +++ b/app/helpers/users_helper.rb @@ -1,4 +1,5 @@ # frozen_string_literal: true +# Users helper module UsersHelper end diff --git a/app/mailers/user_mailer.rb b/app/mailers/user_mailer.rb index 51d0f22..169ef04 100644 --- a/app/mailers/user_mailer.rb +++ b/app/mailers/user_mailer.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +# User mailer class UserMailer < ApplicationMailer def password_recovery @user = params[:user] diff --git a/app/models/application_record.rb b/app/models/application_record.rb index e2e8915..536baf0 100644 --- a/app/models/application_record.rb +++ b/app/models/application_record.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +# Application record class ApplicationRecord < ActiveRecord::Base self.abstract_class = true def update(*args) diff --git a/app/presenters/books_presenter.rb b/app/presenters/books_presenter.rb index f382006..b5762ba 100644 --- a/app/presenters/books_presenter.rb +++ b/app/presenters/books_presenter.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +# Books presenter class BooksPresenter < SimpleDelegator include ActiveSupport::NumberHelper diff --git a/config/application.rb b/config/application.rb index c526332..a55fbe9 100644 --- a/config/application.rb +++ b/config/application.rb @@ -8,6 +8,7 @@ require 'rails/all' # you've limited to :test, :development, or :production. Bundler.require(*Rails.groups) +# App module App class Application < Rails::Application # Initialize configuration defaults for originally generated Rails version. diff --git a/db/migrate/20210319142051_create_authors.rb b/db/migrate/20210319142051_create_authors.rb index d76d005..100b87c 100644 --- a/db/migrate/20210319142051_create_authors.rb +++ b/db/migrate/20210319142051_create_authors.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +# Create authors class CreateAuthors < ActiveRecord::Migration[6.1] def change create_table :authors do |t| diff --git a/db/migrate/20210319142054_create_books.rb b/db/migrate/20210319142054_create_books.rb index e819f45..f87d7cc 100644 --- a/db/migrate/20210319142054_create_books.rb +++ b/db/migrate/20210319142054_create_books.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +# Create books class CreateBooks < ActiveRecord::Migration[6.1] def change create_table :books do |t| diff --git a/db/migrate/20210319142059_create_authors_books.rb b/db/migrate/20210319142059_create_authors_books.rb index 3bbd6e7..d0fe027 100644 --- a/db/migrate/20210319142059_create_authors_books.rb +++ b/db/migrate/20210319142059_create_authors_books.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +# Create association table between authors and books class CreateAuthorsBooks < ActiveRecord::Migration[6.1] def change create_table :authors_books do |t| diff --git a/db/migrate/20210320130542_create_users.rb b/db/migrate/20210320130542_create_users.rb index c5b5a4d..d264856 100644 --- a/db/migrate/20210320130542_create_users.rb +++ b/db/migrate/20210320130542_create_users.rb @@ -1,5 +1,6 @@ # frozen_string_literal: true +# Create users class CreateUsers < ActiveRecord::Migration[6.1] def change create_table :users do |t|