ActiveSupport::JSON.backend = "JSONGem" - ruby-on-rails

I need to add this code to my application initializer for my Rails application. But some things that I'm reading / researching is advising that I place this call in my environment.rb file(s), while other information is advising that I place this in my application.rb file(s).
Which one is the correct location?
Thanks!

Honestly, it doesn't matter. Some people will create a config file, other will just include it in the code. Having a config file does make it easier to keep track of everything, and keeps it all in the same place. But as far as performance, it doesn't matter.

Related

How can/should I include Ruby project file dependencies in config/environment?

New Rubyist here. In my lectures on Ruby, I've come across various tricks/shortcuts for adding file dependencies to the config/environment file so that one's program runs smoothly. It has been unclear to me which of these, when combined, are redundant; which are best practices; and which are completely useless and/or wrong and should be done the long way. Clarification would be appreciated!
The ones I've come across:
require_relative "../lib/test1.rb"
: << '.' & require "lib/test1.rb"
: << '.' & Dir['lib/*.rb'].each {|f| require f}
require File.dirname(__FILE__)
"require-all" gem
Feel free to include other ways, too!
config/environment.rb is a file that the Rails framework itself has opinions about. Over the lifetime of your application, you will be positively incented not to modify this file (if you can avoid it). This will allow for the easiest possible upgrade path. Consider placing your initialization in a file in config/initializers.
If your code alters the behavior of Rails itself in such a fundamental way that placement in config/initializers loses its potency, or if the long-term maintenance of your code is not a concern, I'll consider the above. Items 2+3 work by appending to your LOAD_PATH, which I would not recommend, let alone calling a best practice. (Adding RAILS_ROOT/config may not be a major issue by default, but you might create some very difficult to debug errors.)
The location of config/environment.rb hasn't changed in a long time, so relative requirement (i.e. option #1) is probably fine. require File.dirname(__FILE__) by itself does nothing (you're requiring a directory) but is worthwhile to remember in an approach like require File.join[File.dirname(__FILE__), '../lib/your_file.rb'] which will work the same regardless of load path, or working directory concerns. This is what I would do.
I would not use a gem for this, since the behavior of the gem could change in unpredictable ways, and you're already in an area where Rails can make your choices inconvenient.

Load multiple configuration files in Rails engine

I'm building a Rails Engine and right now I set all my configuration variables in config/environments/development.rb (within the engine itself, i can also overwrite it from the application later) and can access it from the application with ::Rails.application.config.my_item
This file is getting big and some variables such as config.title = 'Lambda Website' could be placed somewhere else, I was thinking to make a config/settings/my_file.rb and just include it to be able to call it the same way as the development.rb variables but it's more complicated than I expected ... I tried a couple of gems that didn't work at all, maybe because it's an engine. I also tried to require files but it blows up too.
How can I simply split this configuration file easily ? Is there an easy way to include configuration files within an engine ? Both YAML/ERB solution are welcome ...
Thank you guys ;)
Inside your
app/config/initializerz/custom_setting.rb
#custom_setting.rb
YOUR_CONSTANT = WHATEVER
Then feel free to use this constant this anywhere in your app.

How can I make some code available throughout a Rails app conveniently?

I want to add a library that I wrote to a Rails app (and to other Rails apps later). I tried putting it in /lib which seemed logical...
[RAILS_ROOT]/lib/my_lib/the_main_file.rb
[RAILS_ROOT]/lib/my_lib/some_other_file.rb
Then...
require 'my_lib/the_main_file'
That works fine.
But is that a great way to do it?
Now I have to put that require everywhere I want to call the library.
I thought about putting the require in an initializer but that seems kind of weird.
What do people usually do about this?
Using an initializer may look weird when you have a single file to include, but sometimes I have many files that I want to add, and end up using an intializer that only includes stuff. It's actually pretty neat.
I'm not sure about the "best practices"(tm) or anything, but we do a similar thing for our project as well. The library is in lib, and the require in an initializer (app_config.rb in our case). This seems like a good way to do things, and hasn't bitten us in the butt thus far :) Hope that helps.
I usually wrap up my stuff in classes. If you add config.autoload_paths += %W(#{config.root}/lib) to your application.rb then any reference to a missing constant will result in an attempt to autoload it, i.e just using MyClass.new will make it try to load `lib/my_class.rb'.
Have a look at Best way to load module/class from lib folder in Rails 3?

Should I put constants for my Rails project in environment.rb?

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.

Where to reopen a class in RoR

I'm attempting to reopen the String class in rails and add a bunch more methods for my app to use. Writing the code isn't a problem - my question is rather about where this code should go.
It doesn't make sense to me to reopen a class inside a different model file, because it really has nothing to do with any of the models specifically. I thought perhaps somewhere in config or lib would make sense, but I'm not particularly well versed with RoR yet.
To summarize, where would be the most logical place to define class-modifying code, and are there any implications depending on where/when the code is loaded?
The most logical place is probably in a file in the config/initializers directory. Any *.rb file you put in here will be automatically executed when rails boots. If you want, you could put them in a sub folder, so you could do something like config/initializers/extensions/*.rb.
I try keep these monkey-patches to a minimum, only when they are very clearly in the best interest of my code.
Lately I have preferred to keep the files organized in folders such as lib/monkey/string.rb, lib/monkey/hash.rb, etc. I then require all files in the lib/monkey folder in my environment.rb file.
# Load all monkey-patches.
Dir["lib/monkey/*.rb"].each {|monkeyfile| require monkeyfile}
This keeps all of my class modifying code isolated to one location, should a problem arise. I also enjoy the somewhat goofy naming, because it makes it stand out as something to be mindful of. Someone may have a better system, if so ... I'd love to hear about it!

Resources