I’m doing a Rails application where people can take quizzes. I have a model BrowserGame that’s taking care of the controller logic (sessions, redirecting etc.). Currently, this is my #initialize method:
class BrowserGame
def initialize(controller)
@controller = controller
end
end
And in the controller I have a method
class GamesController < ApplicationController
# actions
private
def browser_game
BrowserGame.new(self)
end
end
As you can see, I’m passing the whole controller to BrowserGame#initialize (so I can manipulate with sessions and others). Is this a good idea? Are there any side effects, since the controller instance is a large object?
Yes, it is fine to pass large objects as method parameters. You’re not placing the object on the stack, just a pointer to it. As far as side-effects — anything you do to
@controllerfrom withinBrowserGameis seen through any other reference to the controller, but that’s probably what you already expect.