How to Reference a Constant in a Yaml with Rails

String constant for use in en.yml file

Example:

default: &default
adapter: mysql2
encoding: utf8
username: username
password: YOURDBPASSWORD

development:
<<: *default

test:
<<: *default

Also see answer: Reuse a block of code in YAML

How can I access a constant defined in one initializer file into another initializer file in rails?

Suggested solution

In your application.rb do the following:

::THROTTLE_CONFIG = YAML.load_file('config/throttling_request.yml').with_indifferent_access

Now, anywhere in the project, including config/initializers/rack_attack.rb you can do:

THROTTLE_CONFIG[:non_logged_in_user]

Rails 4.2 constants - where to put them: application.yml vs custom config file.rb

I would advise using config/secrets.yml for sensitive data (1 and 2):

  development:
some_password: ...

test:
some_password: ...

production:
some_password: <%= ENV["SOME_PASSWORD"] %>

This way you can put different settings for different environments (for development, test and production).
And I would highly recommend keeping the production sensitive data in ENV variables.

When it goes for non sensitive data (3) I would put in config/environments/*.rb files when different settings are needed for different environments.
If that's not the case and the setting is the same for all environments, I would put it into application.rb, application.yml or in a custom file in initializers/* or even as class constants.

Is it efficient to load a YAML file as a constant in my Rails controller?

I assume that this constant is stored in memory when Rails loads
the file during environment setup

Yep, when the file is loaded/required, everything in there is executed
and assigned. Therefore it's loaded only once.

but the paranoid part of me wonders if I'm hitting the filesystem
each time that controller is accessed.

Partially true, in development mode, constants are unset with each
request, but that shouldn't matter in production.

Can anyone suggest best practices in this area?

Leave it as it is, caching only forwards the parsing to the first request
instead of at startup where you've got the time because the old worker
is still running.

Calling en.yml constants from views

I checked out SimpleForm documentation actually User::COLOR is a constant in user model not loading it from en.

You're getting uninitialized constant error SimpleForm::LABELS because there is no constant defined on it.

If you want to define them to a constant and use them in you're views, you can do something like this:

In app_config.rb first read the en.yml file like this

info = Rails.root.to_s + '/config/locales/en.yml'
data = YAML.load_file(info).deep_symbolize_keys!

and then define a constant something like below,

LABELS = data[:simple_form][:labels]

So that you can LABELS constant across you're application.

How to access application constants from within a model?

Urgh, I just realised why this wasn't working. I was declaring the constants after the app had been initialized.

Doesn't work

# Load the rails application
require File.expand_path('../application', __FILE__)

# Initialize the rails application
MyApplication::Application.initialize!

APP_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/config.yml")[Rails.env]
RESERVED_DIRECTORIES = YAML.load_file("#{Rails.root.to_s}/config/reserved_directories.yml")

Does work

# Load the rails application
require File.expand_path('../application', __FILE__)

APP_CONFIG = YAML.load_file("#{Rails.root.to_s}/config/config.yml")[Rails.env]
RESERVED_DIRECTORIES = YAML.load_file("#

# Initialize the rails application
MyApplication::Application.initialize!
{Rails.root.to_s}/config/reserved_directories.yml")

Is there a way to specify ruby class/module constant as value in Ruby YAML file

Looks like you can do it for classes/modules but not their constants

2.0.0-p247 :046 > YAML.load("!ruby/class 'String'")
=> String
2.0.0-p247 :047 > YAML.load("!ruby/class 'String'").class
=> Class
2.0.0-p247 :065 > YAML.load("!ruby/class 'Logger'")
=> Logger


Related Topics



Leave a reply



Submit