Skip to content

Instantly share code, notes, and snippets.

@hannesfostie
Created November 20, 2015 14:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save hannesfostie/f50b7022b4e1bee46e74 to your computer and use it in GitHub Desktop.
Save hannesfostie/f50b7022b4e1bee46e74 to your computer and use it in GitHub Desktop.
class GlobalSetting
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::ForceBoolean
field :test, type: String, default: "foo bar"
def self.value(attribute)
global_setting = first || create!
global_setting.public_send(attribute)
end
end
[maloik] ~/dev/project % bin/rails c
Loading development environment (Rails 4.2.3)
irb(main):001:0> @text = GlobalSetting.value(:test)
=> "foo bar"
irb(main):002:0> @text.gsub! /bar/, 'foo'
=> "foo foo"
irb(main):003:0> @text
=> "foo foo"
irb(main):004:0> GlobalSetting.value(:test)
=> "foo foo"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment