从控制器到模型的重构

时间:2012-09-16 18:10:37

标签: ruby-on-rails ruby ruby-on-rails-3 refactoring

我在订单控制器中执行此操作:

# POST /orders
# POST /orders.json
def create
  @order = Order.new(params[:order])

  #spostare il ciclo cart > line_items come metodo del modello Order
  cart = session[:cart]

  cart.each do | id, quantity |
    item = Project.find_by_id(id)
    @line_item = LineItem.new
    @line_item.project = item
    @line_item.quantity = quantity
    @line_item.price = item.price
    @order.line_items << @line_item
  end

  @order.user = current_user

  session.delete(:cart)

  respond_to do |format|
    if @order.save
      #Send an email to admin
      UserMailer.order_created_to_admin(@order).deliver

      format.html { redirect_to :dashboard, notice: "L'ordine è stato creato correttamente." }
      format.json { render json: @order, status: :created, location: @order }
    else
      format.html { render action: "new" }
      format.json { render json: @order.errors, status: :unprocessable_entity }
    end
  end
end

现在我想稍微重构一下,我想把以下几行移到Order Model作为一种方法:

  cart.each do | id, quantity |
    item = Project.find_by_id(id)
    @line_item = LineItem.new
    @line_item.project = item
    @line_item.quantity = quantity
    @line_item.price = item.price
    @order.line_items << @line_item
  end
像那样:

class Order < ActiveRecord::Base
  attr_accessible :user_id

  belongs_to :user
  has_many :line_items, :dependent => :destroy
  accepts_nested_attributes_for :line_items, :allow_destroy => true

  def add_line_items_to_order_from_cart(cart) do
    cart.each do | id, quantity |
      item = Project.find_by_id(id)
      @line_item = LineItem.new
      @line_item.project = item
      @line_item.quantity = quantity
      @line_item.price = item.price
      line_items << @line_item
    end
  end
end

并从控制器调用它,如:

order.add_line_items_to_order_from_cart(cart)

在“瘦控制器/脂肪模型”模式方面是否有意义?

谢谢

修改

我的订单模型现在:

class Order < ActiveRecord::Base
  attr_accessible :user_id

  belongs_to :user
  has_many :line_items, :dependent => :destroy
  accepts_nested_attributes_for :line_items, :allow_destroy => true
  validates :user, :presence => :true

  def add_line_items_to_order_from_cart(cart)
    cart.each do | id, quantity |
      item = Project.find(id)
      line_items << LineItem.new(project: item,
                                 quantity: quantity,
                                 price: item.price)
    end
  end

end

1 个答案:

答案 0 :(得分:0)

对我来说很有意义,你为模型提供了一个哈希,模型负责解析哈希并将其转换成数据库记录。

有点清洁甚至可能是将整个line_item事物移动到一行:

def add_line_items_to_order_from_cart(cart) do
  cart.each do |id, quantity|
    item = Project.find_by_id(id)
    line_items << LineItem.new( project: item,
                                quantity: quantity,
                                price: item.price )
  end
end

你甚至可以这样试试:

def add_line_items_to_order_from_cart(cart) do
  line_items = cart.map do |id, quantity|
    item = Project.find_by_id(id)
    LineItem.new( project: item,
                  quantity: quantity,
                  price: item.price )
  end
end

但总体思路是正确的,将这些东西转移到模型中! ;)