Class: Togglefy::Feature

Inherits:
Object
  • Object
show all
Defined in:
app/models/togglefy/feature.rb

Overview

Represents a feature in the Togglefy system. A feature can have various attributes such as name, identifier, status, and associations with assignables.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#feature_assignmentsActiveRecord::Relation

Returns The feature assignments associated with this feature.

Returns:

  • (ActiveRecord::Relation)

    The feature assignments associated with this feature.



21
# File 'app/models/togglefy/feature.rb', line 21

has_many :feature_assignments, dependent: :destroy

Instance Method Details

#activeActiveRecord::Relation

Finds features with an active status.

Returns:

  • (ActiveRecord::Relation)

    The features with an active status.



66
# File 'app/models/togglefy/feature.rb', line 66

scope :active, -> { where(status: :active) }

#assignablesActiveRecord::Relation

Note:

This method includes all assignables, regardless of their class.

This method retrieves all assignables linked to the feature through feature assignments.

Examples:

feature.assignables
Togglefy.feature(:super_powers).assignables
Togglefy::Feature.find_by(identifier: :super_powers).assignables

Returns:

  • (ActiveRecord::Relation)

    The assignables associated with the feature.



89
90
91
# File 'app/models/togglefy/feature.rb', line 89

def assignables
  feature_assignments.includes(:assignable).map(&:assignable)
end

#assignables_for_type(klass) ⇒ ActiveRecord::Relation

This method retrieves assignables of a specific class linked to the feature through feature assignments.

Examples:

feature.assignables_for_klass("User")
feature.assignables_for_klass(User)
Togglefy.feature(:super_powers).assignables_for_klass(User)
Togglefy::Feature.find_by(identifier: :super_powers).assignables_for_klass(User)

Parameters:

  • klass (String, Class)

    The class name or class of the assignable class.

Returns:

  • (ActiveRecord::Relation)

    The assignables of the specified class associated with the feature.



101
102
103
# File 'app/models/togglefy/feature.rb', line 101

def assignables_for_type(klass)
  feature_assignments.includes(:assignable).where(assignable_type: klass.to_s).map(&:assignable)
end

#for_environmentActiveRecord::Relation

Finds features by their environment.

Parameters:

  • environment (String)

    The environment to search for.

Returns:

  • (ActiveRecord::Relation)

    The features matching the environment.



45
# File 'app/models/togglefy/feature.rb', line 45

scope :for_environment, ->(environment) { where(environment: environment) }

#for_groupActiveRecord::Relation

Finds features by their group.

Parameters:

  • group (String)

    The group to search for.

Returns:

  • (ActiveRecord::Relation)

    The features matching the group.



36
# File 'app/models/togglefy/feature.rb', line 36

scope :for_group, ->(group) { where(group: group) }

#for_tenantActiveRecord::Relation

Finds features by their tenant ID.

Parameters:

  • tenant_id (String)

    The tenant ID to search for.

Returns:

  • (ActiveRecord::Relation)

    The features matching the tenant ID.



54
# File 'app/models/togglefy/feature.rb', line 54

scope :for_tenant, ->(tenant_id) { where(tenant_id: tenant_id) }

#identifierActiveRecord::Relation

Scopes Finds features by their identifier.

Parameters:

  • identifier (Symbol, String, Array<Symbol, String>)

    The identifier to search for.

Returns:

  • (ActiveRecord::Relation)

    The features matching the identifier.



31
# File 'app/models/togglefy/feature.rb', line 31

scope :identifier, ->(identifier) { where(identifier: identifier) }

#inactiveActiveRecord::Relation

Finds features with an inactive status.

Returns:

  • (ActiveRecord::Relation)

    The features with an inactive status.



62
# File 'app/models/togglefy/feature.rb', line 62

scope :inactive, -> { where(status: :inactive) }

#with_statusActiveRecord::Relation

Finds features by their status. (:inactive || “inactive” || 0) or (:active || “active” || 1).

Parameters:

  • status (Symbol, String, Integer)

    The status to search

Returns:

  • (ActiveRecord::Relation)

    The features matching the status.



72
# File 'app/models/togglefy/feature.rb', line 72

scope :with_status, ->(status) { where(status: status) }

#without_environmentActiveRecord::Relation

Finds features without an environment.

Returns:

  • (ActiveRecord::Relation)

    The features without an environment.



49
# File 'app/models/togglefy/feature.rb', line 49

scope :without_environment, -> { where(environment: nil) }

#without_groupActiveRecord::Relation

Finds features without a group.

Returns:

  • (ActiveRecord::Relation)

    The features without a group.



40
# File 'app/models/togglefy/feature.rb', line 40

scope :without_group, -> { where(group: nil) }

#without_tenantActiveRecord::Relation

Finds features without a tenant.

Returns:

  • (ActiveRecord::Relation)

    The features without a tenant.



58
# File 'app/models/togglefy/feature.rb', line 58

scope :without_tenant, -> { where(tenant_id: nil) }