10.15.07

Ruby on Rails plugin configuration

Posted in Uncategorized at 8:07 am by Andrew Kuklewicz

I work on Ruby on Rails plugins fairly often, and I’m always curious how people make their plugins configurable.

For example, ActiveMessaging uses several config files - some are yaml, some are ruby - all of them get loaded when the plugin gets initialized. ActiveMessaging has more than a bit of configuration, so distinct files for it makes sense. Also, it has less singular values that get set, and more sets of configuration values (e.g. all the values for a message broker connection).

Comatose uses a singleton with attributes that get defined in a block - so like the Rails initializer, you can call this define method in an environment.rb. Again, this makes sense, there are only a few values to fiddle with, and you very often don’t have to mess with them at all.

Not sure if I’ll use it, but I’ve been playing with an idea where you can use constants to override config values using constants. Makes sense for a plugin where you have default values that would rarely, and often individually, be overridden. Nice thing about this is that constants can be defined even before the config class, so if you need them to be set before the plugin is loaded, this provides a way.

Anyway, to make it easier, I wrote a little helper method to define class attributes, set the default value, and check for constants to override the default, all at one time.


class Config

class < def set_config_value(var_sym, default=nil)
cattr_accessor var_sym
const_name = var_sym.to_s.upcase
value = (Object.constants.include?(const_name)) ? Object.const_get(const_name): default
class_variable_set("@@#{var_sym}".intern, value)
end
end

# SOME_CONFIG_VALUE set as a constant will override the default
set_config_value :some_config_value, "this is a default"

end

2 Comments »

  1. Daniel said,

    October 21, 2007 at 6:44 am

    I’m wondering maybe my plugin AppConfig will be usefull for You vide: http://jarmark.org/projects/app-config

  2. Andrew Kuklewicz said,

    October 21, 2007 at 8:17 am

    I like AppConfig - and I’ve used it on 2 apps now - but I don’t think I want to embed that plugin in another plugin. It is another interesting approach - requires an environment.rb include to then be able to put your own config into the regular rails initializer (if I recall right).

Leave a Comment

You must be logged in to post a comment.

buy cheap levitra cheapest clomid prices viagra sales propecia sale buy cheapest cialis viagra prescription viagra malaysia order clomid online soma pharmacy levitra sale certified cialis buy cheap cialis internet buy lasix without prescription synthroid discount buy viagra online cheap order viagra no prescription required viagra side effects buy viagra buy viagra overnight delivery viagra free delivery buy cialis overnight delivery cost cialis order synthroid online soma online cheap viagra from canada buy viagra in canada soma prescription order cialis from canada order discount cialis online accutane sale viagra overnight delivery generic viagra online purchase cialis without prescription lasix sale cialis cheapest synthroid cialis cheapest price order viagra overnight delivery low cost cialis order no rx cialis cheap viagra from usa clomid find discount viagra accutane purchase acomplia propecia pills viagra cheap soma cheap lasix propecia prices buy cialis internet buy generic synthroid purchase soma online lowest price accutane cialis in australia generic cialis propecia no prescription clomid prescription buy cheapest viagra on line buy synthroid discount viagra no rx cheapest acomplia prices generic soma order viagra no rx buy propecia buy viagra generic cheap cialis tablets discount cialis without prescription order levitra online acomplia without a prescription viagra purchase acomplia online cheap