Skip to content

Instantly share code, notes, and snippets.

@jimryan
Last active March 24, 2017 01:37
Show Gist options
  • Save jimryan/1ff3d6a19f9ec1abd6612864a05e3185 to your computer and use it in GitHub Desktop.
Save jimryan/1ff3d6a19f9ec1abd6612864a05e3185 to your computer and use it in GitHub Desktop.
Restore lederman-rails-settings global Settings
# Restores lederman-rails-settings 1.x global Settings interface
class Settings < ActiveRecord::Base
serialize :value
def self.[](var)
find_by_var(var).try :value
end
def self.[]=(var, value)
setting = find_or_initialize_by(var: var)
setting.value = value
setting.save!
value
end
def self.method_missing(meth, *args, &block)
if [:find_by_var, :find_or_initialize_by].include?(meth)
super
elsif meth =~ /=\Z/
self[meth.to_s.chop] = args[0]
else
self[meth]
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment