How to properly handle custom helper methods in a 'shared' folder? - ruby-on-rails

I am using Ruby on Rails 3.2.2 and I would like to organize and share custom helper-view methods as-like I made for my view files. That is, in my app/views directory I have a shared folder where I put all shared templates and I would like to have a shared folder (intended to be used the same "sharing way" but for helper files) also in the app/helpers directory.
However, my doubts are:
Is right to share helper methods instead of putting those in the ApplicationHelper module (even if those helper methods are specific for shared views and not directly related to any model or controller)?
How can I load modules present in the app/shared/helpers directory in order to make those available to views?
Is there some prescriptions to this approach?

In Rails, helpers are actually global. Which means that you can call a user helper in a, say, posts view. So you don't really have to pollute ApplicationHelper, just divide them in the best way possible, and just use them normally.

Related

How can I create globally-accessible modules in Rails?

In Rails, I can create models which are global (accessible anywhere in the application). However, I'd like to create some constructs that are global, but don't correspond to any database table (which is why they can't be models) and won't ever be instantiated (so I need modules instead of classes).
I've tried using the initializers directory, which works, but is annoying because it requires restarting my server every time I change a value (whereas I can change the code in my models without restarting the server). What's the best way to create globally-accessible modules in this manner?
Just put it into lib sub-directory and add that to your config/application.rb:
config.autoload_paths += ["#{config.root}/lib"]
In addition to adding modules to the lib directory (which is appropriate), you can also put classes in the models directory. They don't have to be ActiveRecord based.

In rails, what is the equivalent of asp.net's App_Code folder?

In rails, what is the equivalent of asp.net's App_Code folder?
Eg, where do you put your various utility classes for your complicated business logic that you don't want to shove in your controllers or helpers?
That would be the lib directory.
To be clear, complicated business logic goes into your model. Stick with the convention of thick models, skinny controllers.
If you have view related helper methods these go into your helpers. Other miscellaneous stuff can be put in your lib directory as #Jakub said.

Where should libraries go in Rails 3?

Where's the recommended location for libraries in Rails 3? Is it as simple as 'lib'?
I'm not sure because 'lib' seems more like a Rails 2 remnant, especially considering that it's no longer auto-loaded (and there was a lot of discussion about that, apparently).
Initializers are more for (obviously) initialization tasks such as overrides.
Specifically I have a small module for attachment handling (Paperclip doesn't fit here) that's too large and distinct to include in my model, but not generic or worthwhile enough to implement as a gem.
From a functionality standpoint it lives somewhere in the middle among the model, view, and controller. This makes it sound like a helper, but in Rails helpers are intended for views AFAIK.
Should I just put it in 'lib' and autoload it in application.rb? Or maybe I could create a custom form builder to handle the presentation (or both).
I know how to make it work, but I'm hoping to learn something new. :)
lib is still the right place to put these kind of things.
Autoloading lib was removed in Rails 3 because of the way engines work, but mainly because it's easy to just add it to the autoload_paths if you do want it automatically loaded and if not, you can require as needed. lib is still in the load path, so you don't need to specify where the module or class you're requiring is.
You're correct, helpers are intended for the view, and would not be the place to put any model-related logic.
I'd put the module in lib, and require and include it in your model as needed.

Rails 2 route helpers in plugin

I'm trying to write a plugin, and among the tasks I want to perform I want to be able to call route helper methods from within the plugin. For instance, if I have map.resources :user, I want to be able to call user_path(:id => 1) from my plugin. I keep getting undefined method user_path error.
In rails 3, you can do this using Rails.application.routes.url_helpers, but I don't seem to be able to find an alternative for rails 2. Including ActionController::UrlWriter does not help. Any ideas?
I'm using rails 2.3.4 and i can use my regular path helpers in the controllers and views of my plugins, at least within the ones i tested.
I can't use them in the lib files for the plugins, but that's because the helpers aren't available outside the controllers (the views are dealt with inside the controllers so they can use them too). The lib files (the meat of the plugins) tends to be modules and classes which get loaded into the model environment.
Can you provide more details about what you're trying to do?
You should be able to do:
app.user_path(1)

Creating Plugins in rubyonrails

I am creating a plugin which involves a controller, model & views. while i can move these files from the vendor/plugin directory to app/controllers, models & views respectively.
now i can run my controller & model just by copying them in lib folder of vendor/plugins/plugin_name/lib and they are directly accessible, but my views are not initialized from there, so i need a technique which can make my views in vendor/plugins/plugin_name/lib/views accessible to rails framework without copying.
i am trying to add them to actionview, but not sure how to do that.
please guide me on this.
There's ways you can add your plugin's views directory to the "search path" for ActionView, but the easiest way to handle all this is to just use something like the Rails Engines plugin to do all the hard work for you.
If you're looking to add models, views and controllers via a plugin, take a look at Desert: http://github.com/pivotal/desert. I'm not too keen on this approach, but Desert seems to work for people who like it.

Resources