Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Interactor in Rails

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 379
    Comment on it

    Ruby on Rails Interactor


    Before interactor we use to write complex business logic in some class in the ActiveRecord /models directory and that class can have too many responsibilities so while testing we could stuck between tediously slow test-suites as these objects are often fetched from the database.


    The solution is a concept called “Interactors”. Rails interactor is like an object that has a single purpose and each interactor represent one thing that your application does. It encapsulates complex business logic and make your ActiveRecord classes skinny.
    By looking at the names of your Interactors, you should be able to tell what your application does for example SignUp,

    To start with add Interactor to your Gemfile and bundle install.

    gem "interactor-rails", "~> 2.0"

     

    It is compatible with Ruby 1.9.3, 2.0, 2.1, 2.2, or 2.3 on Rails 3, 4, or 5.

    It ensures that app/interactors is included in your autoload paths.

    After including the interacter gem, make sure you have   "app/interactors " in your application path.

    Lets create an interactor "AuthenticateUser", this interactor will authenticate a user for you

    class AuthenticateUser
      include Interactor
    
      def call
        if user = User.authenticate(context.email, context.password)
          context.user = user
          context.token = user.secret_token
        else
          context.fail!(message: "authenticate_user.failure")
        end
      end
    end


    Here "context" contains everything that an Interactor needs to work with. The params from the controller sets the context when an Interactor is called.


    Now call this interactor AuthenticateUser in the Controller

    class SessionsController < ApplicationController
      def create
        result = AuthenticateUser.call(session_params)
    
        if result.success?
          session[:user_token] = result.token
          redirect_to root_path
        else
          flash.now[:message] = t(result.message)
          render :new
        end
      end
    
      private
    
      def session_params
        params.require(:session).permit(:email, :password)
      end
    end


    Interactor has a couple of convenient methods like success? and failure? that controller can use as above.
    So Interactors are a nice way to reuse operations and specifying a sequence of actions in order to execute business logic.

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: