Tag: trailblazer

Trailblazer + Devise: Integrating Devise validatable model with Trailblazer operation + error propagation

Devise is one of those gems that are tightly bound to the Rails stack. It means that as long as you follow the "Rails way" and you do things the recommended way, you should not have any problems.

However, Trailblazer is not the recommended way and the way it works, not always makes it painless to integrate with external gems (note: it's not a Trailblazer fault, more often poorly designed external gems). Unfortunately Devise is one of those libraries. Models have validations, things happen magically in the controllers and so on.

Most of the time, you can leave it that way, as the scope of what Devise does is pretty isolated (authentication + authorization). But what if you want to integrate it with Trailblazer, for example to provide a custom built password change page? You can do this by following those steps

  • Provide a contract with similar (or the same) validation rules as Devise (this will make it easier to migrate if you decide to drop model validations)
  • Create an operation that will save the contract and propagate changes to the model
  • Copy model errors (if any) into contract errors

Here's the code you need (I removed more complex validation rules to simplify things):

# Contract object
class Contracts::Update < Reform::Form
  include Reform::Form::ActiveRecord
  # Devise validatable model
  model User

  property :password
  property :password_confirmation

  validates :password,
    presence: true,
    confirmation: true
  validates :password_confirmation,
    presence: true
end

Operation is fairly simple as well:

class Operations::Update < Trailblazer::Operation
  include Trailblazer::Operation::Model
  contract Contracts::Update
  # Devise validatable model
  model User

  def process(params)
    validate(params[:user]) do
      # When our validations passes, we can try to save contract
      contract.save do |hash|
        # update our user instance
        model.update(hash)
        # and propagate any model based errors to our contract and operation
        model.errors.each { |name, desc| errors.add(name, desc) }
      end
    end
  end

And the best part - controller:

class PasswordsController < BaseController
  def edit
    respond_with(form Operations::Update)
  end

  def update
    respond_with(run Operations::Update)
  end

Integrating Trailblazer and ActiveRecord transactions outside of a request lifecycle

When you use Ruby on Rails with ActiveRecord, you can get used to having separate transaction on each request. This is valid also when using Trailblazer (when inside of a request scope), however Trailblazer on its own does not provide such functionality. It means that when you're using it from the console and/or when you process stuff inside background workers, you no longer have an active transaction wrapping around an operation.

This behavior is good most of the time. Since background tasks can run for a long period of time, there might be a risk of unintentional locking a big part of your database. However, sometimes you just need to have transactions.

In order to provide this feature for each operation, we will use a concern that will include that logic. We will also make it configurable, so if we inherit from a given operation, we will still have an option to disable/enable transaction support based on the operation requirements.

The code itself is pretty simple - it will just wraps around a #run method of the operation class with a transaction (as long as transaction support is enabled). Note, that by default transactional flag is set to false.

module Concerns
  module Transactional
    extend ActiveSupport::Concern

    included do
      class_attribute :transactional

      self.transactional = false
    end

    def run
      if self.class.transactional
        self.class.transaction do
          super
        end
      else
        super
      end
    end

    class_methods do
      def transaction
        ActiveRecord::Base.transaction do
          return yield
        end
      end
    end
  end
end

In order to use it, just include it into your operation:

class ApplicationOperation < Trailblazer::Operation
  # Including on its own won't turn transactions on
  include Concerns::Transactional
end

class DataOperation < ApplicationOperation
  # This operation will have a single transaction wrapping it around
  self.transactional = true
end

Copyright © 2024 Closer to Code

Theme by Anders NorenUp ↑