Toshi
Toshi

Reputation: 6360

Rails4; Where to put global constant accessed in project

I've been looking for where to put global constant which can be accessed within the rails application.

I put secrets constants into .env file such as secret key or password for third party APIs.

However I wonder where to put global constants which are not really need to be hidden.

For example; I've been building a payment_system

DEFAULT_INTERVAL = 'month'.freeze
DEFAULT_CURRENCY = 'us'.freeze

Where should I put those constant?

Any best practice?

Upvotes: 0

Views: 485

Answers (1)

Zzz
Zzz

Reputation: 1703

If you do not change these constant often, you can have a class similar to following:

class DefaultSetting
  DEFAULT_INTERVAL = 'month'.freeze
  DEFAULT_CURRENCY = 'us'.freeze
End

Then you can reference those constant by DefaultSetting::DEFAULT_INTERVAL.

However, by having these as ENV variables (i.e in .env), you can change these values on the fly(without code-change/deployment).

Upvotes: 1

Related Questions