2012-09-16 26 views
0

我有我的訂單控制器這個動作:重構模型

# 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 

現在我要重構它一點,我想移動下面的幾行命令模式的方法:

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 
+0

是的,它真的有道理!我是'瘦控制器/胖模式'模式的粉絲:) –

+0

嗨@BlueSmith,謝謝!我在想,所有那些「調用」其他模型Project和LineItems可能是模型中的一個問題。 – enricostn

回答

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 

但總的想法是對的,移動那些東西進入模型! ;)

+0

嗨,謝謝!這非常有幫助。 – enricostn

+0

使用'line_items << LineItem.new項目:項目,數量:數量,價格:item.price'我得到'語法錯誤,意外的tLABEL,期待keyword_end(SyntaxError)..._項目<< LineItem.new project:item,數量:數量,pr ...' – enricostn

+0

第4行是語法錯誤。在傳遞LineItem.new時需要使用parens。 – davidcelis