Skip to content

Latest commit

 

History

History
75 lines (52 loc) · 1.4 KB

File metadata and controls

75 lines (52 loc) · 1.4 KB

Extract Form Objects

http://blog.codeclimate.com/blog/2012/10/17/7-ways-to-decompose-fat-activerecord-models/

適合場景

當一個表單裡面,需要牽涉到多個表單的 update。有一個方法會比 accepts_nested_attributes_for 更乾淨。

就是使用 ActiveModel 再造一個 Form Object。例如以下:

signup.rb

class Signup
  include Virtus

  extend ActiveModel::Naming
  include ActiveModel::Conversion
  include ActiveModel::Validations

  attr_reader :user
  attr_reader :company

  attribute :name, String
  attribute :company_name, String
  attribute :email, String

  validates :email, presence: true
  # … more validations …

  # Forms are never themselves persisted
  def persisted?
    false
  end

  def save
    if valid?
      persist!
      true
    else
      false
    end
  end

private

  def persist!
    @company = Company.create!(name: company_name)
    @user = @company.users.create!(name: name, email: email)
  end
end

有現成的 gem : virtus 可以使用。

那麼 SignupsController 就可以寫成這樣。

class SignupsController < ApplicationController
  def create
    @signup = Signup.new(params[:signup])

    if @signup.save
      redirect_to dashboard_path
    else
      render "new"
    end
  end
end