How to define application configurations in Rails 5? - ruby-on-rails

I've normally put settings like the below in config/application.rb
config.generators.stylesheets = false
config.time_zone = 'Berlin'
But in Rails 5 the message below is found in config/application.rb
# Settings in config/environments/* take precedence over those specified here.
# Application configuration should go into files in config/initializers
# -- all .rb files in that directory are automatically loaded.
What does this mean? Am I supposed to add an initializer-file for every config setting? And in that case, what should such a file contain?

You should still be able to put the configuration in your config/application.rb, however the message is informing you that your environment specific configurations take precedence over those specified there, so if you have another configuration overriding any of such values in your config/environments those in the environment specific would be used.
If you're using the initializers approach, in your config/initializers/stylesheet_generator.rb, you'd have:
Rails.application.config.generators.stylesheets = false
and in your config/initializers/time_zone.rb, you'd have:
Rails.application.config.time_zone = 'Berlin'

Related

Rails: How to change the logger filename?

I'd like to change the logger filename. The default, say, on production, is production.log.
I'd like it be customized, for example, to production.mymachine.log.
How can I do this? I can't find any obvious way.
You can change this application wide in config/application.rb or for each environment in their dedicated files in config/environments
# e.g. in config/environments/development.rb
config.logger = Logger.new(Rails.root.join('log', 'development.my_machine.log'))

Is there a way to override production.rb? ( do initializers run after the config/environments folder )

I'm trying to set config.public_file_server.headers on many different apps (using a gem), some of which may already have this setting in production.rb. My initial thought is that I may be able to do it in a custom initializer, however I am unsure of whether or not initializers have the ability to overwrite configuration settings in this file. Any and all help is much appreciated.
It looks like initializers run after environment.rb as well as the specific environment files.
1. config/preinitializer.rb
2. config/environment.rb
3. config/environments/#{RAILS_ENV}.rb
4. plugin initialization
5. gem initialization
6. config/initializer/*.rb
7. all after_initialize blocks, in the order they were defined in (so same order as above)
8. any junk left below the Rails::Initializer.run call/block in environment.rb
source: http://willbryant.net/rails_initialization_and_configuration_order

Setting Rails time zone in an initializer does not work?

Since Rails 5 the application.rb tells me to place my app's configuration in initializers. In order to follow this convention, I wanted to set my timezone in an initializer.
config/initializers/time_zone.rb
Rails.application.config.time_zone = "Paris"
Rails seems to ignore this setting and keeps its default time zone. Using the same code inside of my application.rb works. Why is the initializer being ignored?
Per Rails5 guide it should be configured in config/application.rb file.
#application.rb
class Application < Rails::Application
config.time_zone = 'Paris'
end

Change pseudo-absolute assets path in Rails 3

I tried every way I found, but I don't know how to do it.
Rails always writes /assets/... and would like to have ./assets/ or assets/ because my application is in a subfolder, not in the root folder.
I tried with config.action_controller.asset_host = "." but Rails writes http://./assets/
I tried with config.assets.prefix = "." but Rails writes /./assets/
Suppose that my application lives in: http://domain.com/example/, with the default behaviour of Rails every path points to http://domain.com/ and not to http://domain.com/example/. I don't want to specify an absolute path, because that is not a portable solution.
Check this as an example: http://apidock.com/rails/ActionView/Helpers/AssetTagHelper/image_tag
You should be able to edit your environment file (config/environments/development.rb) under Rails 3, adding the line:
config.action_controller.relative_url_root = '/example'
This line allows you to run in a subdirectory called "example." Note that you'll want to duplicate this change in production.rb and test.rb as well.

Rails - why would a model inside RAILS_ROOT/lib not be available in production mode?

I have a class located inside RAILS_ROOT/lib folder, which I use in one of my helpers, and it works great in development.
When I switch to production, the application throws a NameError (uninitialized constant SomeHelper::SomeClass), and I have to load it manually in the helper:
load "#{Rails.root}/lib/some_class.rb"
module SomeHelper
def some_method
sc = SomeClass.new
# blah
end
end
I was under the impression that everything inside RAILS_ROOT/lib/* should be available all to the app - is there anything I need to configure to make this happen in prod mode? thanks.
When you call SomeHelper::SomeClass, Rails' autoloading mechanism will try to load file at lib/some_helper/some_class.rb
Rails won't load everything in lib/*, it will only try to load files when ConstMissing occurs.
You might need to check differences between the configuration settings between development and production environment:
config/environments/production.rb and config/environments/development.rb.
During the Rails initialization routine, load_plugins() is called which loads all plugins in config.plugin_paths. You need to make sure that your folder lib/ is included, like in
config.plugin_paths = ["#{RAILS_ROOT}/lib/plugins", "#{RAILS_ROOT}/vendor/plugins"]
In addition to config.plugin_paths, you can also name the plugins that should be loaded in config.plugins. If that variable contains :all then all plugins (found) will be loaded.
(By the way: configuration settings equal to either environment should go in config/environment.rb. Any differences between enviroments are due to settings in the respective .rb files.)

Resources