2011-08-10 91 views
0

我開發了一個基於瀏覽器的正常Rails遊戲應用程序。我現在將CloudMailin添加到混音中,通過電子郵件有效地暴露出另一個界面。通過另一個控制器(CloudMailin)調用「正常」控制器

考慮,作爲一個代表性的例子,我現有的create行動:

class GamesController < ApplicationController 

    def create 
    @game = Game.params[:new] 

    if @game.random_create 
     # Asked to create a game using random choices. 
     # Make the random choices, then present it to the user for tweaking 
     @game.expand_random_choices 

     render :action => new 
    else 
     # Fully specified. Create the game 
     begin 
     @game.save! 

     # ...other work including DB operations ... 

     flash[:notice] += 'Game was successfully created.' 
     redirect_to :action => :play, :id => @game 
     rescue ActiveRecord::RecordInvalid 
     @game.valid? 
     render :action => 'new' 
     end 
    end 
    end 
end 

現在我有PbemController處理Cloudmailin電子郵件:

class PbemController < ApplicationController 

    # Handle inbound email 
    def handle 
    if email_is_a_game_creation 
    ... 
    end 

    render :text => "Handled" 
    end 
end 

什麼是最好的,DRYest的方式來調用現有的create行爲從PbemController?我的唯一真正的選擇是在/lib' and中提取每個「共享」動作,包括在每個控制器中?

回答

1

通常最好的選擇是儘可能多地移動到模型中。這樣,任何可以從控制器執行的代碼也可以從處理程序中執行。

你也許可以製作一個像create_or_build_random這樣的方法,這可能有幫助嗎?

+0

嗯,我想我也許在我的控制器中有點太多了。我會看看我能否做到這一點。 – Chowlett

+0

事實上,不,在審查大多數遊戲邏輯是在遊戲模型。控制器a)決定要問什麼問題; b)處理遊戲報告無效參數; c)爲會話用戶處理新遊戲與新玩家的連接; d)設置閃光燈; e)呈現或重定向。它不覺得在模型中應該有這樣的事情。 – Chowlett

+0

對我來說,如果項目不應該在模型中,那麼你不會在控制器的郵件處理程序中複製,而是在此處添加特定於該郵件處理程序的代碼? –

相關問題