Module: Detour::Flaggable

Defined in:
lib/detour/flaggable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#detour_featuresObject



44
45
46
# File 'lib/detour/flaggable.rb', line 44

def detour_features
  @detour_features ||= []
end

#has_feature?(feature_name, &block) ⇒ Boolean

Returns whether or not the object has access to the given feature. If given a block, it will call the block if the user has access to the feature.

Examples:

if user.has_feature?(:new_user_interface)
  # ...
end

Parameters:

  • feature_name (Symbol)

    The name of the Feature being checked.

  • &block (Proc)

    A block to be called if the user is flagged in to the feature.

Returns:

  • (Boolean)


24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/detour/flaggable.rb', line 24

def has_feature?(feature_name, &block)
  if detour_features.include? feature_name.to_s
    match = true
  else
    feature = Detour::Feature.find_by_name(feature_name)
    return false unless feature

    opt_out = opt_out_flags.find_by_feature_id(feature.id)
    return false if opt_out

    match = feature.match? self

    if match
      detour_features << feature.name.to_s
    end
  end

  match
end