Module: Detour::Concerns::FlagActions::ClassMethods

Defined in:
app/models/detour/concerns/flag_actions.rb

Instance Method Summary collapse

Instance Method Details

#add_group_to_feature(flaggable_type, group_name, feature_name) ⇒ Detour::Flag

Add a group to the given feature. If the feature is not found, an ActiveRecord::RecordNotFound will be raised.

Examples:

Detour::Feature.add_group_to_feature "User", "admin", :delete_records

Parameters:

  • flaggable_type (String)

    The class (as a string) that the group should be associated with.

  • group_name (String)

    The name of the group to have the feature added to it.

  • feature_name (String, Symbol)

    The feature to be added to the group.

Returns:



81
82
83
84
# File 'app/models/detour/concerns/flag_actions.rb', line 81

def add_group_to_feature(flaggable_type, group_name, feature_name)
  feature = find_by_name!(feature_name)
  feature.group_flags.where(flaggable_type: flaggable_type, group_name: group_name).first_or_create!
end

#add_percentage_to_feature(flaggable_type, percentage, feature_name) ⇒ Detour::Flag

Add a percentage of records to the given feature. If the feature is not found, an ActiveRecord::RecordNotFound will be raised.

Examples:

Detour::Feature.add_percentage_to_feature "User", 75, :delete_records

Parameters:

  • flaggable_type (String)

    The class (as a string) that the percetnage should be associated with.

  • percentage (Integer)

    The percentage of ‘flaggable_type` records that the feature will be available for.

  • feature_name (String, Symbol)

    The feature to be added to the percentage of records.

Returns:



118
119
120
121
122
123
# File 'app/models/detour/concerns/flag_actions.rb', line 118

def add_percentage_to_feature(flaggable_type, percentage, feature_name)
  feature = find_by_name!(feature_name)

  flag = feature.percentage_flags.where(flaggable_type: flaggable_type).first_or_initialize
  flag.update_attributes!(percentage: percentage)
end

#add_record_to_feature(record, feature_name) ⇒ Detour::Flag

Add a record to the given feature. If the feature is not found, an ActiveRecord::RecordNotFound will be raised.

Examples:

Detour::Feature.add_record_to_feature user, :new_ui

Parameters:

  • record (ActiveRecord::Base)

    A record to add the feature to.

  • feature_name (String, Symbol)

    The feature to be added to the record.

Returns:



17
18
19
20
# File 'app/models/detour/concerns/flag_actions.rb', line 17

def add_record_to_feature(record, feature_name)
  feature = find_by_name!(feature_name)
  feature.flag_in_flags.where(flaggable_type: record.class.to_s, flaggable_id: record.id).first_or_create!
end

#opt_record_out_of_feature(record, feature_name) ⇒ Detour::OptOut

Opt the given record out of a feature. If the feature is not found, an ActiveRecord::RecordNotFound will be raised. An opt out ensures that no matter what, ‘record.rollout?(:rollout)` will always return false for any opted-out-of features.

Examples:

Detour::Feature.opt_record_out_of_feature user, :new_ui

Parameters:

  • record (ActiveRecord::Base)

    A record to opt out of the feature.

  • feature_name (String, Symbol)

    The feature to be opted out of.

Returns:

  • (Detour::OptOut)

    The OptOut created.



49
50
51
52
# File 'app/models/detour/concerns/flag_actions.rb', line 49

def opt_record_out_of_feature(record, feature_name)
  feature = find_by_name!(feature_name)
  feature.opt_out_flags.where(flaggable_type: record.class.to_s, flaggable_id: record.id).first_or_create!
end

#remove_group_from_feature(flaggable_type, group_name, feature_name) ⇒ Object

Remove a group from agiven feature. If the feature is not found, an ActiveRecord::RecordNotFound will be raised.

Examples:

Detour::Feature.remove_group_from_feature "User", "admin", :delete_records

Parameters:

  • flaggable_type (String)

    The class (as a string) that the group should be removed from.

  • group_name (String)

    The name of the group to have the feature removed from it.

  • feature_name (String, Symbol)

    The feature to be removed from the group.



98
99
100
101
# File 'app/models/detour/concerns/flag_actions.rb', line 98

def remove_group_from_feature(flaggable_type, group_name, feature_name)
  feature = find_by_name!(feature_name)
  feature.group_flags.where(flaggable_type: flaggable_type, group_name: group_name).destroy_all
end

#remove_percentage_from_feature(flaggable_type, feature_name) ⇒ Object

Remove any percentage flags for the given feature. If the feature is not found, an ActiveRecord::RecordNotFound will be raised.

Examples:

Detour::Feature.remove_percentage_from_feature "User", delete_records

Parameters:

  • flaggable_type (String)

    The class (as a string) that the percetnage should be removed from.

  • feature_name (String, Symbol)

    The feature to have the percentage flag removed from.



135
136
137
138
# File 'app/models/detour/concerns/flag_actions.rb', line 135

def remove_percentage_from_feature(flaggable_type, feature_name)
  feature = find_by_name!(feature_name)
  feature.percentage_flags.where(flaggable_type: flaggable_type).destroy_all
end

#remove_record_from_feature(record, feature_name) ⇒ Object

Remove a record from the given feature. If the feature is not found, an ActiveRecord::RecordNotFound will be raised.

Examples:

Detour::Feature.remove_record_from_feature user, :new_ui

Parameters:

  • record (ActiveRecord::Base)

    A record to remove the feature from.

  • feature_name (String, Symbol)

    The feature to be removed from the record.



31
32
33
34
# File 'app/models/detour/concerns/flag_actions.rb', line 31

def remove_record_from_feature(record, feature_name)
  feature = find_by_name!(feature_name)
  feature.flag_in_flags.where(flaggable_type: record.class.to_s, flaggable_id: record.id).destroy_all
end

#un_opt_record_out_of_feature(record, feature_name) ⇒ Object

Remove any opt out for the given record out of a feature. If the feature is not found, an ActiveRecord::RecordNotFound will be raised.

Examples:

Detour::Feature.un_opt_record_out_of_feature user, :new_ui

Parameters:

  • record (ActiveRecord::Base)

    A record to un-opt-out of the feature.

  • feature_name (String, Symbol)

    The feature to be un-opted-out of.



62
63
64
65
# File 'app/models/detour/concerns/flag_actions.rb', line 62

def un_opt_record_out_of_feature(record, feature_name)
  feature = find_by_name!(feature_name)
  feature.opt_out_flags.where(flaggable_type: record.class.to_s, flaggable_id: record.id).destroy_all
end