Topic: Helper method available to model, controller, and view?

Is there a way to make a helper method available to all three?

Re: Helper method available to model, controller, and view?

Technically you can just 'include' modules full of helpers into anything you want.  This is intentionally cumbersome though because it wears down the advantages of having a MVC architecture.

If you want to do it though, you could do:

class User < ActiveRecord::Base

  include ActionView::Helpers::UrlHelper

end

Re: Helper method available to model, controller, and view?

Yeah, it's really just for one method in my whole application.
I can see how mixing in modules to each of the MV+C can defeat the purpose of their separation.

Re: Helper method available to model, controller, and view?

What is the purpose of this method?  The need for such a method sounds like it might be indicative of bad design.  Not knowing anything at all, I wonder if it should be a method attached to a particular model class (perhaps a static method).

Re: Helper method available to model, controller, and view?

It's transforming the name of the controller into the name of the class by singularizing and capitalizing it.  I'm using it with STI in the controller, model, and view so that I can keep the code DRY by using the parent class 90% of the time--only using the controller_to_class method to say "You're #{controller_to_class} has been saved".

But the more I think through the problem, I think I might be ok in the model without it.

Re: Helper method available to model, controller, and view?

Yes, I really only need it in the controller and in the helper.

So I have to do include ActionView::Helpers::UrlHelper in the controller?

Re: Helper method available to model, controller, and view?

You may not need to include it at all.  I'm pretty sure you can do:

In the controller:
self.class.name #=> "UsersController"
self.class.name.underscore #=> "users_controller"
self.class.name.titlecase #=> "Users Controller"

and so on.  Since underscore and titlecase (and camelcase) and such are instance methods of the String class you can do them anywhere.  Would that solve your problem?