These are chat archives for dry-rb/chat

15th
Nov 2018
Nick Luparev
@SuperManEver
Nov 15 2018 18:03

I need some help
suppose I have few steps in my transation declared as

step :store_params
step :assign_record_attributes

how store_params will give a control to assign_record_attributes and pass some params along the way?

Jaromír Červenka
@Cervajz
Nov 15 2018 19:35
@SuperManEver What do you mean by give a control?
Nick Luparev
@SuperManEver
Nov 15 2018 19:37
store_params finished its job and assign_record_attributes should continue
Jaromír Červenka
@Cervajz
Nov 15 2018 19:38
A step can return only two states: Success(some_data) or Failure(some_data_or_error). Transaction will run the next step if the previous one returned Success
If you want to pass some data to the next step give it to the Success(<data_here>) object
Nick Luparev
@SuperManEver
Nov 15 2018 19:40
how do I send multiple things, using hash?
Jaromír Červenka
@Cervajz
Nov 15 2018 19:41
the data can be whatever :) I use hash and do it like this:
module Api
  class AuthenticateRequest < ApplicationTransaction

    step :validate_input
    step :authenticate
    map :log_request

    private

    def validate_input(input)
      Api::AuthenticateRequestSchema.call(input).to_monad hints: false
    end

    def authenticate(input)
      api_token = repository.find_by token: input[:token]
      return Failure(not_authenticated_error) if api_token.blank?
      return Failure(invalid_customer_error) unless valid_customer?(api_token)

      Success input.merge(api_token: api_token)
    end

    def log_request(input)
      api_token = input[:api_token]
      api_token.update_last_access_info ip_address: input[:ip_address]

      api_token
    end

...
end
Success input.merge(api_token: api_token)
The above is called like this:
    Api::AuthenticateRequest.new.call(token: token, ip_address: request.remote_ip) do |result|
      result.success do |api_token|
        @current_customer = api_token.customer
        @current_customer_id = api_token.customer_id
      end

      result.failure do |errors|
        render_error([errors], :unauthorized)
      end
    end
Nick Luparev
@SuperManEver
Nov 15 2018 19:46
got it. I think this is what I'm looking for. thanks
Jaromír Červenka
@Cervajz
Nov 15 2018 19:46
@SuperManEver :+1: np
Nick Luparev
@SuperManEver
Nov 15 2018 19:46
I saw somewhere probable old tutorial used Right and Left
is that deprecated in favor of Success and Failure?
also can you suggest a tutorial on how to test operations and transations using RSpec?
Jaromír Červenka
@Cervajz
Nov 15 2018 19:53
@SuperManEver I think Left/Right were used in older versions. However Success and Failure are basically that.
As for tutorial - I can recommend book from @radar - https://leanpub.com/explodingrails
It helped me a lot
Rachel Lanman
@RSR312-206
Nov 15 2018 22:35
Is is possible to skip a step if a param is not present?