Should I put constants for my Rails project in environment.rb? - ruby-on-rails

I want to store a path for a special directory used by my Rails application. Should I store that in environment.rb, or is there another place this is meant to go?
THE_DIRECTORY_PATH = '/path/to/directory'
Let's assume my controllers + models or libraries in /lib need access as well.

How about storing it in a YAML configuration file that gets loaded by an initializer? This Railscast has the details.

Use a robust YAML-file approach that allows per-environment settings. Try app_config, which has loads of great features, including referring syntax like AppConfig.the_directory_path.

If controllers need access to it, then a better place would be the ApplicationController.

Related

Where can I store non-localizable fixed strings in rails?

I have some url templates that I use in my app - they are not localizable strings, so config/locales/en.yml does not seem like the best place for them - where do I store these? For example, "http://www.example.com/apps?id=" is one such string.
I know there are several approaches possible (a constants file in the lib folder, or using rails configuration in rails4, or some config gem like configatron), but what is the'right' way?
I don't know that there is "a right way" - the new 'x' configuration option system in Rails 4 seems to be the most "Railsy" solution out there.
I personally think it is totally okay to have a GlobalConstants module defined in an initializer or a file in the lib directory. Put all your global constants in there and be diligent about using them throughout your app where needed.
For projects where there's a mix and match of i18n strings I like having a "base" i18n file and then using the override system to create a sort of inheritance chain between the i18n files. That's not always the appropriate solution but I've had it work out quite well a few times.
Hope that helps you.
As you said there are many solutions and it dependence on usage.
You can store them in environment variables.
dotenv gem help you handle it easier:
https://github.com/bkeepers/dotenv

Where should Rails 3 custom validators be stored?

I've seen docs/websites show that custom validators should go in a /lib or /lib/validators directory of a project. I've found (by reading an answer to another post) that they only seem to work in config/initializers. Does anyone know, or have a pointer to official documentation that shows where custom validators should live?
If you place your custom validators in app/validators they will be automatically loaded without needing to alter your config/application.rb file.
If you add this to your /config/application.rb file:
config.autoload_paths += %W["#{config.root}/lib/validators/"]
Then Rails will automatically load your validators on start up (just like /config/initializers/), but you keep the clean structure of having your validators in one nice, well named spot.
lib/validators seems by far the cleanest. However you may need to load them in before your models, so probably from an initializer.
Here's the official docs about custom validations. AFAIK its a good practice to keep them in the relevant models.

Rails - Changing the config path

I copied the 'config' directory and renamed it to 'config_dev' so that I don't alter the current settings. How do I configure Rails to use the config directory of 'config_dev'?
Well, i'm not sure whether you can rename that and still make it work or not. However, i would highly not recommend that approach. If you must do something like that, better rename the files inside the folder, like environment.rb.bak or the likes.
Generally speaking a config folder is where important settings initiate from and i think that changing that convention can lead to more problems. I could be wrong, but i would just change the files (that's what the rails 2 to rails 3 conversion plugin does as well).

Differences in putting a module in /helpers or in /lib?

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.

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