Simple App-wide settings for Rails, using Rails internals
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Failed to load latest commit information.



Rails now how built config via Rails.configuration.x and Rails.application.config_for(:...)

App Settings

Application-wide Settings for Rails

Build Status

Code Climate

Sometimes there is a need to have values stored that can be used anywhere inside a Rails application. Things like the application name, or the company name, etc. are simple use cases. Other times you might want to add oauth key/secret for twitter/facebook.

There are numerous ways in which you can handle that:

  • put it into a initializer
  • create a custom object, maybe subclassing OpenStruct/Struct
  • a Yaml file that you'll load/parse in an initializer file
  • and many other ways

I've been doing a mix of initializers and a custom object, but never been fully happy with those solutions. Rails already has a place for configuration settings for the application, so it made sense to me, to use that.

This tiny gem does just that. Here's how you can do it too:

In your Gemfile, include this gem:

gem 'app_settings'

You can use the following files to declare your application settings:

  • config/application.rb (for all environments)
  • config/environemnts/development.rb (for just dev env)
  • config/environemnts/test.rb (for just the test env)
  • config/environemnts/production.rb (for just the production env)

The settings will work in the same way that Rails' environment config, where the application.rb is for all environments, but can be overridden by the environment specific files (development.rb, test.rb, production.rb).

This is how you'd set your application settings in one or more of those files:

config.settings.appname       = 'My Rails Application'
config.settings.company_name  = 'Fancy Pants LLC'

Then, anywhere in your app, you can access those by doing:

Rails.configuration.settings.appname       # => 'My Rails Application'
Rails.configuration.settings.company_name  # => 'Fancy Pants LLC'


AppName::Application.config.settings.appname        # => 'My Rails Application'
AppName::Application.config.settings.company_name   # => 'Fancy Pants LLC'

From there, you could put those into the app/controllers/application_controller.rb file and shorten the name and then make it also a helper method. You could also put it into your models as a method or use Rails' config_accessor. You'll get the benefit of having your app settings all in one place.