Commit 4fa88f4

mo <mo.khan@gmail.com>
2018-09-23 23:55:25
add helper to check if current user has feature.
1 parent 6588d27
Changed files (2)
app/controllers/concerns/featurable.rb
@@ -0,0 +1,26 @@
+# frozen_string_literal: true
+
+module Featurable
+  extend ActiveSupport::Concern
+
+  included do
+    helper_method :feature_enabled?
+  end
+
+  class_methods do
+    def require_feature(features, options = {})
+      before_action(options) do
+        missing_feature = Array(features).any? { |x| feature_disabled?(x) }
+        render plain: "Forbidden", status: :forbidden if missing_feature
+      end
+    end
+  end
+
+  def feature_enabled?(feature)
+    Current.user&.feature_enabled?(feature) || Flipper.enabled?(feature.to_sym)
+  end
+
+  def feature_disabled?(feature)
+    !feature_enabled?(feature)
+  end
+end
app/controllers/application_controller.rb
@@ -2,6 +2,7 @@
 
 class ApplicationController < ActionController::Base
   include Authenticatable
+  include Featurable
   protect_from_forgery with: :exception
   add_flash_types :error, :warning