Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Explain to me how config works in Rails

Tags:

I have a Rails 3 application, call it "MyApp". In my config\environments\production.rb file I see such things as

MyApp::Application.configure do   config.log_level = :info   config.logger = Logger.new(config.paths.log.first, 'daily')    ...or...   config.logger = Logger.new(Rails.root.join("log",Rails.env + ".log"),3,20*1024*1024) 

So, questions are focusing on terminology and wtf they mean... (or point me to some site ,I have looked but not found, to explain how this works.)

  1. MyApp is a module?
  2. MyApp::Application is a ...? What, a module too?
  3. MyApp::Application.configure is a method?
  4. config is a variable? How do I see it in console?
  5. config.logger is a ???
  6. config.paths.log.first is a ...?? --in console I can see "MyApp::Application.configure.config.paths.log.first" but don't know what that means or how to extract info from it!?!

Is this too much for one question? :)

I have looked at the tutorial http://guides.rubyonrails.org/configuring.html but it jumps right into what things do.

like image 553
rtfminc Avatar asked Jul 15 '11 21:07

rtfminc


People also ask

What is Rails application config?

In general, the work of configuring Rails means configuring the components of Rails, as well as configuring Rails itself. The configuration file config/application. rb and environment-specific configuration files (such as config/environments/production.

Where is Rails application config?

You probably know that you can configure Rails in config/application. rb and config/environments/development. rb etc. But you can also leverage that for configuring your own custom settings for your application.

How do Initializers work in Rails?

An initializer is any file of ruby code stored under /config/initializers in your application. You can use initializers to hold configuration settings that should be made after all of the frameworks and plugins are loaded.

How is Rails ENV set?

The ENV hash in your Rails application is set when your Rails application starts. Rails loads into ENV any environment variables stored in your computer and any other key-value pairs you add using Figaro gem.


1 Answers

A six sided question! Oh my. Let's ahem roll.1 Here's hoping I receive 6 times the upvotes for it then? :)

1. MyApp is a module?

Yes, it's a module. It acts as a "container" for all things pertaining to your application. For instance you could define a class like this:

module MyApp   class MyFunClass     def my_fun_method     end   end end 

Then if someone else has a MyFunClass, it won't interfere with your MyFunClass. It's just a nice way of separating out the code.

2. MyApp::Application is a ...? What, a module too?

MyApp::Application is actually a class, which inherits from Rails::Application. This does a quite a lot of things, including setting up the Rails.application object which is actually an instance of MyApp::Application that you can do all sorts of fun things on like making requests to your application (in a rails console or rails c session). This code for instance would make a dummy request to the root path of your application, returning a 3-sized Array which is just a plain Rack response:

 Rails.application.call(Rack::MockRequest.env_for("/")) 

You can also get the routes for your application by calling this:

 Rails.application.routes 

The main purpose of defining MyApp::Application is not these fun things that you'll probably never use, but rather so that you can define application-specific configuration inside config/application.rb. Things like what parameters are filtered, the time zone of the application or what directories should be autoloaded. These are all covered in the Configuration Guide for Rails.

3. MyApp::Application.configure is a method?

Indeed it is a method, and it allows you to add further configuration options to your application's configuration after config/application.rb has been loaded. You've probably seen this used in config/environments/development.rb or one of the other two files in that directory, but basically they all use the same options as shown in that Configuration Guide linked to earlier.

4. config is a variable? How do I see it in console?

The config "variable" is actually a method defined within the code used for Rails::Application and returns quite simply a configuration object which stores the configuration for the application.

To access it in the console, just use Rails.application.config. This will return quite a large Rails::Application::Configuration object for your viewing pleasure.

5. config.logger is a ???

The method you're referring to, I assume, comes from this line in config/environments/production.rb:

# Use a different logger for distributed setups # config.logger = SyslogLogger.new 

The method in this example is not config.logger, but rather config.logger=, which is referred to as a "setter" method in Ruby-land. The one without the equal sign is referred to as a "getter". This method sets up an alternative logger for the production environment in Rails, which then can be accessed by using Rails.logger within the console or the application itself.

This is useful if you want to output something to the logs, as you can simply call this code:

Rails.logger.info("DEBUG INFO GOES HERE") 

6. config.paths.log.first is a ...?? --in console I can see "MyApp::Application.configure.config.paths.log.first" but don't know what that means or how to extract info from it!?!

Within a Rails application, you can modify the locations of certain directories. And so, this config.paths method is a way of keeping track of where these directories map to. In my entire Rails life I have never had to use or modify this variable and that can mean either one of two things:

  1. It's not used often by Rails programmers, or;
  2. I don't live a very varied life.

Interpret it as you will. My main point is that you're probably never going to use it either.


I hope these help you understand Rails a little more!

1 Terrible dice joke.

like image 90
Ryan Bigg Avatar answered Nov 18 '22 00:11

Ryan Bigg