Rails Configuration


Occasionally I’ll see things like this in a code base.

1
2
3
4
5
6
7
def api_host
  if Rails.production?
    "http://prod.fake.api.url"
  else
    "http://stag.fake.api.url"
  end
end

I try to avoid writting methods like this. Rails provides a nice way to set environment specific variables.

http://guides.rubyonrails.org/configuring.html#custom-configuration

config/environments/staging.rb

1
config.api_host = "http://stag.fake.api.url"

config/environments/production.rb

1
config.api_host = "http://prod.fake.api.url"

So now you can refactor the method to this.

1
2
3
def api_host
  Rails.configuration.api_host
end

Comments