Last active
January 31, 2023 08:49
-
-
Save trusche/ed3521a17b52fdd6a43687cc5c45b999 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 AccountsAddFeatures < ActiveRecord::Migration[6.0] | |
def change | |
add_column :accounts, :features, :citext, array: true, null: false, default: [] | |
add_column :users, :features, :citext, array: true, null: false, default: [] | |
end | |
end |
This file contains hidden or 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 ApplicationPolicy | |
# ... pundit stuff | |
def enabled?(feature) | |
user.enabled?(feature) || user.account.enabled?(feature) | |
end | |
def disabled?(feature) | |
!enabled?(feature) | |
end | |
end |
This file contains hidden or 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
module Concerns | |
module Featured | |
extend ActiveSupport::Concern | |
class_methods do | |
def with_feature(feature) | |
return none if feature.blank? | |
where("? = ANY(features)", feature) | |
end | |
end | |
def enable!(feature) | |
features << feature | |
features.uniq! | |
save! | |
end | |
def disable!(feature) | |
features.delete(feature) | |
save! | |
end | |
def enabled?(feature) | |
features.include?(feature.to_s) | |
end | |
def disabled?(feature) | |
!enabled?(feature) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment