# Calling methods from the "main" application

**URL:** <https://www.ruby-forum.com/t/calling-methods-from-the-main-application/85356>\
**Category:** Engines\
**Created:** [March 22, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/calling-methods-from-the-main-application/85356 "2006-03-22T16:56:55Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 22, 2006, 4:56pm UTC](https://www.ruby-forum.com/t/calling-methods-from-the-main-application/85356/1 "2006-03-22T16:56:55Z")

</div>

I’ve successfully setup a publication engine (Publish) which I use to  
learn ROR but there is something I can’t find a solution for : the main  
application (the one which make uses of this engine) has a “admin”  
layout which need to get a menu for EVERY engine installed… Let’s call  
it getMenu()

1. I don’t know where to define getMenu() - I’ve tried in several places

- init\_engine.rb included but got several error messages (ranging from  
“method not found” to “trying to access private method”)

1. How can I iterate throught all the engines installed and check if the  
getMenu method exists ?

Point 1 is the most important for me as I don’t understand how I can  
make methods and attributes accessible to the outside world. For now,  
every piece of code executed by my engine is the result of a  
controller/action sequence.

Same goes for engine to engine calls… How can I do ?

Thanks

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 22, 2006, 5:08pm UTC](https://www.ruby-forum.com/t/calling-methods-from-the-main-application/85356/2 "2006-03-22T17:08:25Z")

</div>

Hi,

Firstly, to get an array of all the running engines, you can use  
simply Engines.active - that part is easy. Read the Engines RDoc for  
more details.

You should define your getMenu() method as part of some module, and  
use a consistent convention for the naming of those modules. For  
instance, if you have three engines (‘alpha\_engine’, ‘beta\_engine’,  
‘gamma\_engine’), then you will probably be defining modules for each  
of those engines, so you could have:

/vendor/plugins/alpha\_engine/lib/alpha\_engine.rb:  
module AlphaEngine  
def AlphaEngine.getMenu()  
#…  
end  
end

… and so on for BetaEngine and GammaEngine.

That said, where you place the method really depends what your getMenu  
method does. You might instead want to put it in an appropriately  
named module within the app/helpers directory of each engine. The key  
to detecting the availablity of this method is going to be following a  
well-defined convention for it’s placement.

- james

On 3/22/06, Christophe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> ## getMenu method exists ?
> 
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> engine-users mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org](http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org)

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 22, 2006, 6:35pm UTC](https://www.ruby-forum.com/t/calling-methods-from-the-main-application/85356/3 "2006-03-22T18:35:18Z")

</div>

Thank you for your help James, using your solution I solved my problem  
in a minute…

Now, you said that I could use the same solution and put getMenu() in a  
helper… It would be better because the script in the lib directory is  
only reloaded when webrick starts - So, I’ve tried with something in a  
helper (helper directory under my engine):

module PublishHelper

def PublishHelper.getmenu()  
# snip  
end

end

But that doesn’t work - In the app’s layout , using  
PublishHelper::getmenu produces an error “uninitialized constant  
ArticleHelper”

Is there something special to do to use helpers or each script in the  
engine’s helper directory is automatically provided to the main  
application ?

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 22, 2006, 6:40pm UTC](https://www.ruby-forum.com/t/calling-methods-from-the-main-application/85356/4 "2006-03-22T18:40:26Z")

</div>

Helpers are automatically loaded by controllers if they share the same  
root name, i.e. the ‘PublishController’ will try and load the  
‘PublishHelper’… if you want to include other helpers, you need to  
explicitly include them in your controller, probably using the  
‘helper’ class method, i.e.

class PublishHelper \< ApplicationController  
helper :article  
# …  
end

BTW, to call a ‘class-level’ method, you should be using  
PublishHelper.getmenu(), rather than PublishHelper::getmenu()

- james

On 3/22/06, Christophe C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> def PublishHelper.getmenu()  
> engine’s helper directory is automatically provided to the main  
> application ?
> 
> –  
> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).
> 
> * * *
> 
> engine-users mailing list  
> removed\_email\_address@domain.invalid  
> [http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org](http://lists.rails-engines.org/listinfo.cgi/engine-users-rails-engines.org)

–

- J \*  
~

---

<div class="post-metadata">

**Author:** ![chriscole](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/5fc32e/32.png) [@chriscole](https://www.ruby-forum.com/u/chriscole)\
**Post date:** [March 23, 2006, 11:11am UTC](https://www.ruby-forum.com/t/calling-methods-from-the-main-application/85356/5 "2006-03-23T11:11:29Z")

</div>

Thanks again James - This timeit works also with an helper ! But I  
wonder why the helpers are not reloaded dynamically ?  
I’m working in development mode and I need to restart WebRicks to see  
the results of the changes made in the helper source code…
