Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where's the best place to define a constant in a Ruby on Rails application?

In a Ruby on Rails application, where is the best place to define a constant?

I have an array of constant data that I need available across all the controllers in my application.

like image 334
mlambie Avatar asked Jul 10 '09 05:07

mlambie


People also ask

How do you define a constant in Ruby?

What is a constant in Ruby? A constant is a type of variable which always starts with a capital letter. They can only be defined outside of methods, unless you use metaprogramming. Constants are used for values that aren't supposed to change, but Ruby doesn't prevent you from changing them.

Are constants global in Ruby?

Although constants look like local variables with capital letters, they have the visibility of global variables: they can be used anywhere in a Ruby program without regard to scope.

What is a class constant Ruby?

Class constants. A constant has a name starting with an uppercase character. It should be assigned a value at most once. In the current implementation of ruby, reassignment of a constant generates a warning but not an error (the non-ANSI version of eval.rb does not report the warning):

Do not define constants this way within a block rails?

From the docs: Do not define constants within a block, since the block's scope does not isolate or namespace the constant in any way. If you are trying to define that constant once, define it outside of the block instead, or use a variable or method if defining the constant in the outer scope would be problematic.


2 Answers

Rails >= 3, the application is itself a module (living in config/application.rb). You can store them in the application module

module MyApplication   SUPER_SECRET_TOKEN = "123456" end 

Then use MyApplication::SUPER_SECRET_TOKEN to reference the constant.


Rails >= 2.1 && < 3 you should place them

  1. in /config/initializers when the constant has the applications scope
  2. when the constant refers to a specific model/controller/helper you can scope it within the class/module itself

Prior to Rails 2.1 and initializers support, programmers were used to place application constants in environment.rb.

Here's a few examples

# config/initializers/constants.rb SUPER_SECRET_TOKEN = "123456"  # helpers/application_helper.rb module ApplicationHelper   THUMBNAIL_SIZE= "100x20"    def thumbnail_tag(source, options = {})     image_tag(source, options.merge(:size => THUMBNAIL_SIZE)   end  end 
like image 74
Simone Carletti Avatar answered Oct 19 '22 03:10

Simone Carletti


You can place them in config/environment.rb:

Rails::Initializer.run do |config|     ...     SITE_NAME = 'example.com' end 

If you have large amounts of global constants, this can be messy. Consider sourcing from a YAML file, or keeping the constants in the database.

EDIT:

weppos' answer is the better answer.

Keep your constants in a file in config/initializers/*.rb

like image 25
guns Avatar answered Oct 19 '22 03:10

guns