Reputation: 6363
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.)
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.
Upvotes: 23
Views: 4008
Reputation: 107718
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:
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.
Upvotes: 45
Reputation: 115521
MyApp
is a module, it's a namespace including an app you'll launch, see next line
MyApp::Application
is a Class and you're running it's instances when running a Rails app
MyApp::Application.configure
is a method. It passes all instructions to the class. See Ref.
config
is a method or an instance variable (when set) which belongs through inheritance to Rails::Application::Configuration
. See Ref.
You can see it in console doing: MyApp::Application.config
config.logger
doesn't exist until you define it, so it's a Logger instance. See Ref.
config.paths.log
is a Rails::Paths::Path
you can access it in console using: MyApp::Application.config.paths.log
Upvotes: 6