Welcome to Working With Rails

 

Discussion Forums

Discuss all things Ruby on Rails with perhaps the web's most vibrant group of Ruby on Rails enthusiasts.
Static Objects in Rails
9 Posts
Static Objects in Rails

Hi,

I want to create some static objects in rails that get created on startup. What is the best way to do this and where is best to put them for visibility from views (and possible controllers)?

Tks, Michael

You can define them as constants and place them in your environment.rb file

Example

SITE_TITLE = 'Welcome to my website'

Person
Forum Rating: 100
Sign In To Rate Post

Hi Clemens,

Won't load_products get called every time a controller method gets called? Sound like alot of overhead. Imagine the case where what you are loading are almost never going to change.

Hi,

I like to create a module in my lib folder with all these values. I also prefer to have them defined as class/module variables rather than as constants. That way, if I need to override a value (for example, in a initializer file) I will not get a warning.

You could put a file named my_config.rb in your lib folder with the following content

module MyConfig
mattr_accessor :my_variable
my_variable='whatever' end

Then from any place in your code (model, view, controller, helper, lib method, whatever) you can do something like MyConfig.my_variable

If you prefer to use constants, you can do something like

module MyConfig
MY_CONSTANT='whatever' end

and then use it like

MyConfig::MY_CONSTANT

Regards, javier

You may want to check out the plugin code here:

http://github.com/ctran/application_config/tree/master

There is a really useful bit of code in section 61 of the Advanced Rails Recipes book. It's called ConstantCache. I use a modified version of the code from the book in my app, but you can get it as a plugin now:

http://github.com/vigetlabs/constant_cache

In my app I have a few different Role objects, with title "Admin", "User" etc. With this plugin I can modify my model by adding a "caches_constants" line like this:

class Role < ActiveRecord::Base

caches_constants :key => :title

end

now when my rails app loads up, all of the different roles are loaded, and constants are added to the Role class. So in my authentication code I can refer to Role::ADMIN or Role::USER.

The instances of Role which are associated with each constant are only loaded once, when the application starts up.

Javier:

I gave your method a shot and it looks like for the non-constant variables to work you have to declare them as instance variables:

module MyConfig mattr_accessor :my_variable @@my_variable='whatever' end

The way that works well for my projects is to have a config.yml file in the config/ folder. In environment.rb, I just load that into a constant.

I might have this in the config file:


development:
    email:
      server: smtp.myserver.com
      port: 25
      domain: myserver.com
      authentication: none
      username: 
      password: 
      contact_recipient: admin@myserver.com
      content_type: text/html
  test_mode:true
  send_emails: false
  google_maps_api_key: 
production:
    email:
      server: smtp.myserver.com
      port: 25
      domain: myserver.com
      authentication: none
      username: 
      password: 
      contact_recipient: admin@myserver.com
      content_type: text/html
  test_mode:true
  send_emails: false
  google_maps_api_key: 

Then all I have to do is load that YAML to a constant in environment.rb.

CONFIGURLATION = YAML::load(File.open("#{RAILS_ROOT}/config/config.yml"))

In code, you can use CONFIGURATION[RAILS_ENV] to grab the values. Getting nested values is easy as it's just a nested hash like params[].

For example

ActionMailer::Base.smtp_settings = {

:address => CONFIGURATION[RAILS_ENV]['email']['server'],
:port => CONFIGURATION[RAILS_ENV]['email']['port'],
:domain => CONFIGURATION[RAILS_ENV]['email']['domain'],
:authentication => CONFIGURATION[RAILS_ENV]['email']['authentication'],
:user_name => CONFIGURATION[RAILS_ENV]['email']['username'],
:password => CONFIGURATION[RAILS_ENV]['email']['password']

}

Hope this helps. It's simple and extremely flexible.

If you want a database solution, I have a gem that generates a controller and model for simple situations. (a single table, one row, one column for each configuration). It also auto-generates the admin form to manage the table. More info at http://www.napcsweb.com/blog/category/products/

Hi Jerod,

I forgot a detail in my code when I pasted ;) you don't need to use the @@ syntax, that's why I used an mattr_accessor instead. You can do

module MyConfig mattr_accessor :my_variable MyConfig.my_variable=’whatever’ end

You can also use "self" instead of "MyConfig" but to me it looks clearer like that

9 Posts
Login to add your message