Module: Toggly::Rails::ControllerConcern
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/toggly/rails/controller_concern.rb
Overview
Controller concern for feature flag checks.
Include this module in your ApplicationController to add feature flag helpers.
Instance Method Summary collapse
-
#feature_disabled?(feature_key, context: nil) ⇒ Boolean
Check if a feature is disabled.
-
#feature_enabled?(feature_key, context: nil) ⇒ Boolean
Check if a feature is enabled.
-
#require_feature!(feature_key, context: nil) ⇒ void
Require a feature to be enabled, otherwise render not found.
-
#toggly_context ⇒ Toggly::Context
Get the current evaluation context.
-
#toggly_context=(context) ⇒ void
Set a custom context for this request.
Instance Method Details
#feature_disabled?(feature_key, context: nil) ⇒ Boolean
Check if a feature is disabled
44 45 46 |
# File 'lib/toggly/rails/controller_concern.rb', line 44 def feature_disabled?(feature_key, context: nil) !feature_enabled?(feature_key, context: context) end |
#feature_enabled?(feature_key, context: nil) ⇒ Boolean
Check if a feature is enabled
34 35 36 37 |
# File 'lib/toggly/rails/controller_concern.rb', line 34 def feature_enabled?(feature_key, context: nil) ctx = context || toggly_context Toggly.enabled?(feature_key, context: ctx) end |
#require_feature!(feature_key, context: nil) ⇒ void
This method returns an undefined value.
Require a feature to be enabled, otherwise render not found
53 54 55 56 57 58 59 60 61 |
# File 'lib/toggly/rails/controller_concern.rb', line 53 def require_feature!(feature_key, context: nil) return if feature_enabled?(feature_key, context: context) respond_to do |format| format.html { render file: "public/404.html", status: :not_found, layout: false } format.json { render json: { error: "Not found" }, status: :not_found } format.any { head :not_found } end end |
#toggly_context ⇒ Toggly::Context
Get the current evaluation context
66 67 68 |
# File 'lib/toggly/rails/controller_concern.rb', line 66 def toggly_context @toggly_context ||= build_toggly_context end |
#toggly_context=(context) ⇒ void
This method returns an undefined value.
Set a custom context for this request
74 75 76 77 |
# File 'lib/toggly/rails/controller_concern.rb', line 74 def toggly_context=(context) @toggly_context = context Middleware.set_context(request.env, context) if request end |