views:

177

answers:

2

I am interested in doing this the "Rails Way" on a new application. I would also like to refer to constants in some sort of context to make the code more readable. I have an application where a user can request access to another users's data set. This AccessRequest can have one of the following statuses:

Review Denied Approved

I can see these values being used in reporting features in the future, so I want to make them constants in order to avoid any spelling or capitalization issues. I thought I would just put these in a constants.rb file in the config/initializers directory.

I would like to refer to these as AccessRequest::REVIEW. Since I already have a model called AccessRequest, does it make sense to put them there? Or wrap them in a class in a constants.rb file in the config/initializers directory? Which way is the Rails Way?

+3  A: 

You don't need anymore your constant in Rails 3. The better way is using the Rails::Application singleton.

In your application.rb you can define your constante like :

module Yourapp
  class Application < Rails::Application

    config.access_request.review = 'xxx'
  end
end

After in your code you can call

Yourapp::Application.config.access_request.review

After if you change value in each environment, You just define the config.xx in your config/environments/production.rb or other environment.

shingara
What about constants that change between development and production: api keys, enable/disable emails or features, etc?
Brian Maltzan
Yes, but you just need made your change in config/environments/production.rb and other environement
shingara
Thanks for all of your feedback shingara and Brian.
AKWF
+1  A: 

You might like app_constants. There's also a railscast that covers configuration.

Brian Maltzan