← Back to Upcase

Ruby Science: Introduce Parameter Object | Upcase


(Upcase ) #1
Melanie joins Joe to demonstrate a simple (but useful) pattern: Parameter Objects. Melanie replaces a long parameter list with one object in a single bound! Try it for yourself in our refactoring trail.
This is a companion discussion topic for the original entry at https://thoughtbot.com/upcase/videos/ruby-science-introduce-parameter-object

(Anthony Lee) #2

This was great lesson @jferris. One quick question…

I have a form that requires a lot of parameters. What would be the best way to refactor if you have like 40+ fields?

class SomethingsController < ApplicationController
  before_action :authenticate_user!

  def index
  end

  def show
  end

  def new
  end

  def create
  end

  def edit
  end

  def update
  end

  def destroy
  end

  private

  def somethings_params
    params.require(:retreat_attendees__c).permit(
      :sample_a,
      :sample_b,
      :sample_c,
      :sample_d,
      :sample_e,
      :and_so_on..... 40+ more
    )
  end
end

(Joe Ferris) #3

If you have a crazy huge form, you can use composite form objects. You can make several form objects, each of which represents a set of fields on the form. Each can define which fields it requires and define validations that make more sense on the form than they do in your models. You can then define a higher-level form object that doesn’t accept fields, but instead composes the individual form objects which handle their own fields.


(Zaid Al Lahham) #4

Hi @mrgilman, great lesson!
I was wondering what command you use to maximize the rspec pane in tmux when you want to see the whole pane. I noticed you do this is when you usually see a failure and you want to see the whole message rather than just the number of failures at the bottom.


(Jack) #5

It was probably LeaderZ, which zooms the focused pane.