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.

Examples:

class ApplicationController < ActionController::Base
  include Toggly::Rails::ControllerConcern

  # Optional: customize how the current user is identified
  def toggly_current_user
    current_user
  end
end

Instance Method Summary collapse

Instance Method Details

#feature_disabled?(feature_key, context: nil) ⇒ Boolean

Check if a feature is disabled

Parameters:

  • feature_key (String, Symbol)

    Feature key

  • context (Toggly::Context, nil) (defaults to: nil)

    Optional override context

Returns:

  • (Boolean)


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

Parameters:

  • feature_key (String, Symbol)

    Feature key

  • context (Toggly::Context, nil) (defaults to: nil)

    Optional override context

Returns:

  • (Boolean)


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

Parameters:

  • feature_key (String, Symbol)

    Feature key

  • context (Toggly::Context, nil) (defaults to: nil)

    Optional override context



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_contextToggly::Context

Get the current evaluation context

Returns:

  • (Toggly::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

Parameters:

  • context (Toggly::Context)

    Context to use



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