Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Service Modules

Service Modules

Lightning talk about refactoring Rails services from classes/objects to stateless modules.

Presented at the Ruby on Rails Oceania meetup on 11 July 2017.

Presentation slide deck markdown and speaker notes (useable in Deckset):
https://github.com/paulfioravanti/presentations/tree/master/service_modules

Paul Fioravanti

July 11, 2017
Tweet

More Decks by Paul Fioravanti

Other Decks in Programming

Transcript

  1. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if user && BCrypt::Password.new(user.password_digest) == params[:password] sign_in user redirect_to dashboard_path else flash[:alert] = "Login failed." render "new" end end end @paulfioravanti 4
  2. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if user && BCrypt::Password.new(user.password_digest) == params[:password] sign_in user redirect_to dashboard_path else flash[:alert] = "Login failed." render "new" end end end @paulfioravanti 5
  3. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if user && BCrypt::Password.new(user.password_digest) == params[:password] sign_in user redirect_to dashboard_path else flash[:alert] = "Login failed." render "new" end end end @paulfioravanti 6
  4. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if user && BCrypt::Password.new(user.password_digest) == params[:password] sign_in user redirect_to dashboard_path else flash[:alert] = "Login failed." render "new" end end end @paulfioravanti 7
  5. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if user && BCrypt::Password.new(user.password_digest) == params[:password] sign_in user redirect_to dashboard_path else flash[:alert] = "Login failed." render "new" end end end @paulfioravanti 8
  6. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if user && BCrypt::Password.new(user.password_digest) == params[:password] sign_in user redirect_to dashboard_path else flash[:alert] = "Login failed." render "new" end end end @paulfioravanti 9
  7. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if user && BCrypt::Password.new(user.password_digest) == params[:password] # ... else # ... end end end @paulfioravanti 10
  8. class UserAuthenticator def initialize(user) @user = user end def authenticated?(unencrypted_password)

    return false unless @user BCrypt::Password.new(@user.password_digest) == unencrypted_password end end @paulfioravanti 11
  9. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if UserAuthenticator.new(user).authenticated?(params[:password]) # ... else # ... end end end @paulfioravanti 12
  10. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if UserAuthenticator.new(user).authenticated?(params[:password]) sign_in user redirect_to dashboard_path else flash[:alert] = "Login failed." render "new" end end end @paulfioravanti 13
  11. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if UserAuthenticator.new(user).authenticated?(params[:password]) # ... else # ... end end end @paulfioravanti 16
  12. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if UserAuthenticator.new(user).authenticated?(params[:password]) # ... else # ... end end end @paulfioravanti 20
  13. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if UserAuthenticator.authenticated?(user, params[:password]) # ... else # ... end end end @paulfioravanti 21
  14. class UserAuthenticator def initialize(user) @user = user end def authenticated?(unencrypted_password)

    return false unless @user BCrypt::Password.new(@user.password_digest) == unencrypted_password end end @paulfioravanti 22
  15. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if UserAuthenticator.authenticated?(user, params[:password]) # ... else # ... end end end @paulfioravanti 29
  16. module UserAuthenticator module_function def self.authenticated?(user, unencrypted_password) return false unless user

    BCrypt::Password.new(user.password_digest) == unencrypted_password end end @paulfioravanti 35
  17. module UserAuthenticator module_function def authenticated?(user, unencrypted_password) return false unless user

    BCrypt::Password.new(user.password_digest) == unencrypted_password end end @paulfioravanti 36
  18. module UserAuthenticator module_function def authenticated?(user, unencrypted_password) return false unless user

    BCrypt::Password.new(user.password_digest) == unencrypted_password end end @paulfioravanti 37
  19. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if UserAuthenticator.authenticated?(user, params[:password]) sign_in user redirect_to dashboard_path else flash[:alert] = "Login failed." render "new" end end end @paulfioravanti 38
  20. class SessionsController < ApplicationController def create user = User.find_by(email: params[:email])

    if UserAuthenticator.authenticated?(user, params[:password]) sign_in user redirect_to dashboard_path else flash[:alert] = "Login failed." render "new" end end end @paulfioravanti 39