Skip to content

Instantly share code, notes, and snippets.

@danielvlopes
Created November 30, 2021 20:17
Show Gist options
  • Save danielvlopes/d69d5f47a8d5947496d906744c35562f to your computer and use it in GitHub Desktop.
Save danielvlopes/d69d5f47a8d5947496d906744c35562f to your computer and use it in GitHub Desktop.
def feature_enabled?(feature)
FeatureFlag.enabled?(feature, current_organization)
end
class FeatureFlag < ApplicationRecord
serialize :organization_ids, Array
scope :enabled, -> { where(enabled: true) }
before_save :normalize_name
validates_presence_of :name
def self.enabled?(name, organization)
feature = FeatureFlag.find_by(name: name.to_s)
return false if feature.blank?
normalized_ids = feature.organization_ids.map { |i| i.to_s }
normalized_ids.include?(organization.id.to_s) && feature.enabled?
end
def organization_names
Organization.where(id: organization_ids).pluck(:name)
end
def enable!
update(enabled: true)
end
def disable!
update(enabled: false)
end
private
def normalize_name
self[:name] = name.downcase.underscore
end
end
create_table "feature_flags", force: :cascade do |t|
t.string "name", limit: 255
t.text "organization_ids"
t.boolean "enabled", default: true
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["name"], name: "index_feature_flags_on_name"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment