I am writing this widget that is to be included with a single page of the site, so I don’t see a reason to declare what I am doing in the ApplicationController. However, this new widget does have a fair amount of complexity to it, so I want to keep it separate from the controller that normally handles this page since that controller already has quite the bit of complexity in it. I originally figured in my design I could just make a helper module for the controller and keep most of the widget logic there.
I am trying to write this helper module, but then I realized that there is a particular array I need to be able to initialize from a single location, but multiple methods need access to it and it needs to be insured that it is initialized before any other method makes calls to this array. Is there a way to do something similar to a before_filter in such a module where I can ensure this variable is initialized before use or is there a better overall way of approaching this that is the “Rails way”?
I think you can create a new class and instantiate it in the before_filter or your controller.
Not sure what you want to achieve but an example could be :
Then you pass the
@arrayvariable to your helper.I’m not sure there is a “Rails way” of doing what you want : Rails as a lot of convention but when you start to care a lot about design and DRY, I believe it’s better to think in term of good practice for Oriented Object Programming than Rails convention.