Created
November 30, 2021 20:17
-
-
Save danielvlopes/d69d5f47a8d5947496d906744c35562f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def feature_enabled?(feature) | |
FeatureFlag.enabled?(feature, current_organization) | |
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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