Skip to content

Instantly share code, notes, and snippets.

@stakes
Created January 22, 2012 22:56
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save stakes/1659221 to your computer and use it in GitHub Desktop.
Save stakes/1659221 to your computer and use it in GitHub Desktop.
Feature Flags / Rails + Mongoid
class User
include Mongoid::Document
# herein might appear a metric shitton of fields
# but what you need to know for feature flags follows
field :feature_flags, :type => Hash, :default => {}
# likewise, this is only what you need for
# feature flagging and nothing more
def feature_enabled?(name)
!self.feature_flags[name].blank? ? self.feature_flags[name] : false
end
def feature_added?(name)
!self.feature_flags[name].nil? ? true : false
end
def enable_feature(name)
self.feature_flags[name] = true
self.save!
end
def disable_feature(name)
self.feature_flags[name] = false
self.save!
end
def self.enable_feature_globally(name)
User.all.each do |u|
u.enable_feature(name)
end
end
def self.disable_feature_globally(name)
User.all.each do |u|
u.disable_feature(name)
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment