Module: Subflag::Rails::Helpers
- Defined in:
- lib/subflag/rails/helpers.rb
Overview
Helpers for using Subflag in controllers and views
All helpers automatically use current_user for targeting if available.
Override with user: nil or user: other_user when needed.
Instance Method Summary collapse
-
#subflag_enabled?(flag_name, user: :auto, context: nil, default: false) ⇒ Boolean
Check if a boolean flag is enabled.
-
#subflag_for(user = :auto, context: nil) ⇒ FlagAccessor
Get a flag accessor, optionally for a specific user.
-
#subflag_prefetch(user = :auto, context: nil) ⇒ Array<Hash>
Prefetch all flags for a user in a single API call.
-
#subflag_value(flag_name, default:, user: :auto, context: nil) ⇒ Object
Get a flag value (default required).
Instance Method Details
#subflag_enabled?(flag_name, user: :auto, context: nil, default: false) ⇒ Boolean
Check if a boolean flag is enabled
Automatically scoped to current_user if available.
47 48 49 50 |
# File 'lib/subflag/rails/helpers.rb', line 47 def subflag_enabled?(flag_name, user: :auto, context: nil, default: false) resolved = resolve_user(user) Subflag.flags(user: resolved, context: context).public_send(:"#{flag_name}?", default: default) end |
#subflag_for(user = :auto, context: nil) ⇒ FlagAccessor
Get a flag accessor, optionally for a specific user
Automatically scoped to current_user if no user provided.
91 92 93 94 |
# File 'lib/subflag/rails/helpers.rb', line 91 def subflag_for(user = :auto, context: nil) resolved = resolve_user(user) Subflag.flags(user: resolved, context: context) end |
#subflag_prefetch(user = :auto, context: nil) ⇒ Array<Hash>
Prefetch all flags for a user in a single API call
Call this early in a request (e.g., in a before_action) to fetch all flags at once. Subsequent flag lookups will use the cached values.
Automatically scoped to current_user if available.
121 122 123 124 |
# File 'lib/subflag/rails/helpers.rb', line 121 def subflag_prefetch(user = :auto, context: nil) resolved = resolve_user(user) Subflag.prefetch_flags(user: resolved, context: context) end |
#subflag_value(flag_name, default:, user: :auto, context: nil) ⇒ Object
Get a flag value (default required)
Automatically scoped to current_user if available.
66 67 68 69 |
# File 'lib/subflag/rails/helpers.rb', line 66 def subflag_value(flag_name, default:, user: :auto, context: nil) resolved = resolve_user(user) Subflag.flags(user: resolved, context: context).public_send(flag_name, default: default) end |