Skip to content

Instantly share code, notes, and snippets.

@tboerger
Created August 20, 2013 21:08
Show Gist options
  • Save tboerger/6287371 to your computer and use it in GitHub Desktop.
Save tboerger/6287371 to your computer and use it in GitHub Desktop.
class Setting
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::History::Trackable
track_history track_create: false,
track_update: true,
track_destroy: true
cattr_accessor :succeeds
@@succeeds = false
cattr_accessor :settings
@@settings = nil
field :internal, type: String
field :cast, type: String
field :description, type: String, localize: true
field :content, type: String, localize: true
scope :ordered, lambda {
order_by(:internal, :asc)
}
validates :internal, presence: true, uniqueness: true
validates :cast, :description, :content, presence: true
class << self
def [](internal)
load!
if @@settings[internal.to_s]
@@settings[internal]
else
nil
end
end
def load!
return if succeeds and not @@settings.nil?
file = Rails.root.join('config', 'application.yml')
@@settings = {}
if File.exists? file
yaml = YAML.load(open(file)).to_hash
@@settings.deep_merge! yaml['defaults']
@@settings.deep_merge! yaml[Rails.env]
end
if self.exists?
self.all.each do |setting|
content = case setting.cast
when 'boolean'
!!setting.content
when 'array'
setting.content.split ','
else
setting.content
end
setting.internal.split('.').tap do |ks|
ks[0...-1].inject(@@settings) { |s, k| s[k] ||= { } }[ks[-1]] = content
end
end
@@succeeds = true
end
end
end
end
defaults:
mailer:
from: 'Foo <[email protected]>'
method: 'smtp'
smtp:
address: 'smtp.gmail.com'
port: 587
domain: 'foo.de'
user_name: '[email protected]'
password: 'foofoo'
authentication: 'plain'
enable_starttls_auto: true
default:
mime_version: '1.0'
charset: 'UTF-8'
content_type: 'text/plain'
parts_order:
- 'text/html'
- 'text/plain'
- 'text/enriched'
url:
host: 'localhost:3000'
protocol: 'http://'
images:
asset_host: 'localhost:3000'
assets:
asset_host: 'localhost:3000'
development:
foo: 'bar'
test:
foo: 'bar'
production:
mailer:
url:
host: 'www.foo.de'
protocol: 'https://'
images:
asset_host: 'images.foo.de'
assets:
asset_host: 'assets.foo.de'
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment