“hell” • Nested forms • Controllers with logic not relating to HTTP • Models with logic not relating to persistence • Views with several nested if statements and partials
authorize @recording if @recording.save if @recording.reason != 'Not a weight challenge' if @recording.reason == 'Success' flash.now[:notice] = 'Success: Congrats!' else flash.now[:error] = "You were not awarded credits! Reason: #{@recording.reason}" end render :show else render json: ErrorValue.new(@recording), status: :unprocessable_entity end end end
do property :title property :body validates :title, length: { minimum: 4 } validates :body, presence: true end def process(params) validate(params[:post]) do contract.save end end end end
model end def create? @user.admin == true end end class Post < ActiveRecord::Base class Create < Trailblazer::Operation include Trailblazer::Operation::Policy policy Post::Policy, :create? contract do ... end def process(params) ... end end end
params.merge(current_user: current_user) do |op| class PostsController < ApplicationController before_action :add_user_to_params … private def add_user_to_params params[:current_user] = current_user end end
Model model Post, :create # or :find or :update def process(params) validate(params[:post]) do contract.save end end end end • :create calls Post.new • :update is the same as :find, they call Post.find(params[:id])
Model model Post, :update def process(params) validate(params[:post]) do contract.save end end private def update_model!(params) model_class.find_by(slug: params[:slug]) end end end
Trailblazer::Cell property :title property :body private def author model.author || AnonymousAuthor.new end def full_name "#{author.first_name} #{author.last_name}" end end end end