refactoring

ruby
class SignupForm
  include ActiveModel::Model
  include ActiveModel::Attributes

  attribute :account_name, :string
  attribute :email, :string

Shallow Controller, Deep Params: Form Object Pattern

rails activemodel form-object
by codesnips 3 tabs
ruby
class ApplicationCommand
  Result = Struct.new(:value, :errors) do
    def success?
      errors.nil? || errors.empty?
    end

Keep Controllers Thin: Use Command Objects

rails architecture command-object
by codesnips 3 tabs
ruby
class ProjectPresenter
  def initialize(project, include_tasks: true)
    @project = project
    @include_tasks = include_tasks
  end

Building Nested JSON Presenters in Rails Without ActiveModel::Serializer

rails presenter json
by codesnips 3 tabs
ruby
class OrderPresenter < SimpleDelegator
  attr_reader :current_user

  def initialize(order, current_user:)
    super(order)
    @current_user = current_user

Presenter + fast_jsonapi Serializer for Clean JSON:API Responses in Rails

rails json-api serialization
by codesnips 3 tabs
ruby
module Result
  def self.Success(value)
    Success.new(value)
  end

  def self.Failure(error)

Railway-Oriented Result Monad for Chaining Lead Enrichment Steps in Ruby

result-monad railway-oriented functional
by codesnips 3 tabs
ruby
class ApplicationPolicy
  attr_reader :user, :record

  def initialize(user, record)
    @user = user
    @record = record

Enforcing Controller Authorization with a Pundit-Style Policy Object in Rails

rails authorization pundit
by codesnips 4 tabs
ruby
class PriceBreakdown < Struct.new(:subtotal_cents, :discount_cents, :tax_cents, keyword_init: true)
  def initialize(*)
    super
    freeze
  end

Memoizing a Pricing Breakdown as an Immutable Value Object in Rails

rails memoization value-object
by codesnips 3 tabs
ruby
class InvoicesController < ApplicationController
  def index
    invoices = InvoicesQuery.new(current_account.invoices, filter_params).call

    @invoices = invoices.page(params[:page]).per(25)
    render :index

Building a Composable Query Object for Filtering Rails ActiveRecord Scopes

rails activerecord query-object
by codesnips 3 tabs