I have developed a normal browser-based Rails game application. I'm now adding CloudMailin to the mix, effectively exposing an alternative interface via email.
Consider, as a representative example, my existing create
action:
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
I now have my PbemController for handling Cloudmailin emails:
class PbemController < ApplicationController
# Handle inbound email
def handle
if email_is_a_game_creation
...
end
render :text => "Handled"
end
end
What the best and DRYest way to invoke the existing create
behaviour from the PbemController
? Is my only real option extracting each "shared" action to a module in /lib' and
include`ing that in each controller?
Normally the best option is to move as much as you can into the model. That way any code that can be executed from the controller can also be executed from the handler.
You may be able to make a method like
create_or_build_random
that could potentially help here?