Agile Web Development

Build it. Launch it. Love it.

App Config

Rails framework default configuration way is, as we all know, flexible and simple at the same time. Troubles begins when Your application starts to grow - some form of app specific configuration is then required to keep all as DRY as possible.

AppConfig plugin provides a convenient way for configuration application wide settings. Extra custom files in config/ or other unusual configuration hacks are no longer needed. AppConfig extends Rails configuration process in a way that allows to setup application specific options within default configuration block.

Installation:

It’s been posted on the Rails Plugin wiki page, so theoretically You could install it with:

        $ script/plugin discover
        $ script/plugin install app_config

Otherwise You can install directly from the repository:

        $ script/plugin install http://svn.jarmark.org/rails/app_config

NOTE: because AppConfig modifies Rails configuration internals, must be included before Rails::Initializer#run. If You are using Rails v1.1+ this step is taken automatically during installation. Otherwise add the following line to conf/environment.rb file:

        require 'plugins/app_config/lib/configuration'

(somewhere between line ‘require File.join(File.dirname(FILE), ‘boot’) and ‘Rails::Initializer.run’)

Usage

You can specify app-wide settings in configuration file(s) using ’app_config’ prefix e.g. environment.rb:

        Rails::Initializer.run do |config|
        ...
           # --- My killer-app specific settings:
           config.app_config.my_key = 'my value'
           config.app_config.boot_time = Time.now
           config.app_config.default_mail_subject = '[appconfig] '
           config.app_config.my_hash = {:a=>1, :b=>2}
        ...

but also in e.g. environment/development.rb

        ...
        config.app_config.default_message = "I'm now in development mode..."
        ...

Access to previously configured settings from within application code is also dead easy:

        AppConfig.my_key
        AppConfig[:my_key]
        AppConfig['my_key']
        AppConfig.param('my_key')
        AppConfig.param(:my_key)
        AppConfig.param('my_key', 'value if my_key is nil')
        AppConfig.param(:my_key)do
         ... #executed when my_key is nil
        end

Happy (re)configurations!

Vitals

Home http://jarmark.org/projects/app-config/
Repository http://svn.jarmark.org/rails/app_config
License Rails' (MIT)
Tags Tag_red
Rating (33 votes)
Owner Daniel Owsianski
Created 21 July 2006

Comments

  • Avatar
    Andrew Kaspick
    6 January 2008

    Was about to write a similar plugin, but a search came up with this and it's "exactly" what I was thinking of myself. Thanks!

  • Avatar
    22 January 2008

    I am newbie in rails world. This plug in helps me start to store the system wide settings like Administrator password.

  • Avatar
    jc
    29 June 2008

    Doesn't work in Rails 2.1. Tried fixing it but the code is mostly incomprehensible.

  • caiges
    8 October 2008

    Add this before class OrderedOptions in app_config.rb

    Add this back into ActiveSupport as it seems to be removed from rails 2.1.

    module ActiveSupport # Hash is ordered in Ruby 1.9! if RUBY_VERSION >= '1.9'

    OrderedHash = ::Hash
    

    else

    class OrderedHash < Array #:nodoc:
      def []=(key, value)
        if pair = assoc(key)
          pair.pop
          pair << value
        else
          self << [key, value]
        end
      end
    
      def [](key)
        pair = assoc(key)
        pair ? pair.last : nil
      end
    
      def keys
        collect { |key, value| key }
      end
    
      def values
        collect { |key, value| value }
      end
    end
    

    end end

  • Avatar
    joe
    20 November 2008

    Does anyone know how to fix this problem when using Rails 2.1.0?

    uninitialized constant AppConfig::Base::OrderedOptions

  • Thyme
    28 May 2009

    I found problem "uninitialized constant AppConfig::Base::OrderedOptions" in Rails 2.3.2 also. I think that it is caused by differences in OrderedOptions class exposed by Rails and Active_Support. To solve it I had to convince plugin to use class exposed by Rails. To do that One have to change plugin source file: ./vendor/plugins/app_config/lib/app_config.rb and change line: @@parameters = OrderedOptions.new to: @@parameters = Rails::OrderedOptions.new and now everything works fine for me.

Add a comment