Differences in putting a module in /helpers or in /lib? - ruby-on-rails

What are the reasons putting a module in /helpers over the /lib folder in a RoR app?
Are /helpers more controller specific, while the /lib is more general in nature?

I think this is a good question because the MVC notion makes us forget that it's all really just metaphors for us to organize code so we don't get too mixed up. If you need to do some simple formating go with a helper, otherwise probably a module in /lib.

Helpers are strictly for defining methods that you want available in your views. /lib modules can be used for anything and are available throughout the application.

Related

Why use /app/lib instead of /lib in Rails?

In the sidekiq documentation, there is this quote about preferring to use /app/lib instead of /lib in Rails projects related to autoloading errors:
A lib/ directory will only cause pain. Move the code to app/lib/ and make sure the code inside follows the class/filename conventions.
Additionally, there is also:
Don't configure extra paths in autoload_paths or eager_load_paths. That's a hack; follow the conventions! Any directory underneath app/ may contain Ruby code, you don't need to explicitly configure anything.
My questions are:
Is there any truth to these statements that using /app/lib is better than /lib?
Is this only helpful for autoloading Rails-related objects (such as AR models, controllers, jobs, etc)? Or will it also help POROs?
Is there only a specific context in which these comments make sense?
In my experience app/lib is easier to use. You can literally stick in something like Class MathFunction and use it elsewhere (e.g. controllers or modules) with MathFunction.sqrRoot.
To use /lib you need to configure your Rails app with autoload_paths. autoload_paths also need some tweaking to work properly in production. Matz himself discourages autoload because it's in the process of being deprecated.
The only time I've needed to use the lib directory is for making custom rake tasks. Otherwise I stick to app/lib.

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 Newby: Where should plain old Ruby classes reside?

Where is the best location to put a plain old Ruby class in a Rails application? I'm not sure if it should go in the helper, model, or controller folder or should I create another folder to handle Ruby classes.
In the /lib folder is fine.
The question really is, what purpose does it serve? Is it data-like stuff? model for that. Is it a helper class that adds functionality to other classes or is a utility? Probably lib. Etc...
It would help to know what your class is trying to accomplish, but in general /lib is a decent enough location.

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