Skip to content

Instantly share code, notes, and snippets.

@augustf
Created April 23, 2012 02:11
Show Gist options
  • Save augustf/2468275 to your computer and use it in GitHub Desktop.
Save augustf/2468275 to your computer and use it in GitHub Desktop.
Config key-value store\
class Configuration < ActiveRecord::Base
TRUE = "t"
FALSE = "f"
validates_presence_of :key
validates_uniqueness_of :key
# Enable hash-like access to table for ease of use
# Returns false if key isn't found
# Example: Configuration[:public_concerto] => 80
def self.[](key)
rec = self.find_by_key(key.to_s)
if rec.nil?
return false
end
rec.value
end
# Override self.method_missing to allow
# instance attribute type access to Configuration
# table. This helps with forms.
def self.method_missing(method, *args)
unless method.to_s.include?('find') # skip AR find methods
value = self[method]
return value unless value.nil?
end
super(method, args)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment