Reputation: 13949
Using rails 4, I need to generate an environment variable from a relative url in development, and an absolute-one in production
DEV : SOME_PATH = [Rails root]/some_path
PROD : SOME_PATH = [Some absolute path]/some_other_path
I'm storing these variables in a yml file, following the instructions I found there. What would be a nice way/best practice to store and generate these variables ?
For example, in my yml I could write:
development:
DOCETUDE_PATH: some_path
production:
DOCETUDE_PATH: /home/public/some_other_path
And somewhere in my code I must generate the Pathname variables. This seems to work but looks hackish:
if (ENV['DOCETUDE_PATH'].start_with?("/"))
MY_CST = Pathname.new(ENV['DOCETUDE_PATH']
else
MY_CST = File.join(Rails.root, ENV['DOCETUDE_PATH'])
end
Upvotes: 1
Views: 1820
Reputation: 160191
If those are your requirements, those are your requirements.
I might base it on the Rails environment rather than the path name, in case of fat-fingering, or include a check of path v. environment to make sure nobody fat-fingers the configuration.
I might use a Ruby environment init file instead of the environment, too, so it's all code.
If I was doing it your way I'd refactor some of the code so it looked more like:
path = ENV['DOCETUDE_PATH']
MY_CST = relative_path?(path) ? Pathname.new(path) : File.join(Rails.root, path)
(And create the relative_path?
method, of course, if it doesn't already exist somewhere.)
Upvotes: 1