Skip to content

RYS feature toggler

The RYS feature toggler (Rys::Feature) is the older mechanism for switching parts of a RYS engine on and off. New code uses EasyFeature and Flipper, see Feature toggles.

Features are registered with a dotted key. A feature is active when its database record is active, or when your own condition says so.

How to work with it?

First register features. Put this code into config/initializers.

# Basic definition
# This feature will be active if record on the DB will have active == true
Rys::Feature.add('issue.show')

# For specific plugin
# Preferred way because you can control features directly on plugin settings
Rys::Feature.for_plugin(MyRys::Engine) do
  Rys::Feature.add('my_rys.show')
end

Features have tree structure so if parent is disabled all children are disabled too. For example if issue is disabled -> all issue.* are disabled too regardless to theirs state.

Rys::Feature.add('issue.show')
Rys::Feature.add('issue.show.sidebar')

# issue
# `-- issue.show
#     `-- issue.show.sidebar

You can set default feature state via:

# issue.show is default activated
Rys::Feature.add('issue.show', default_db_status: true)

# issue.show is default deactivated
Rys::Feature.add('issue.show', default_db_status: false)

You can check feature state via:

Rys::Feature.on('issue.show') do
  # issue.show is active
end

if Rys::Feature.active?('issue.show')
  # issue.show is active
end

Callbacks

You can define action which will be triggered after a status change.

Rys::Feature.add('issue.show', status_changed: lambda { |active| ... })

In routes

Turn routes on or off:

rys_feature 'issue.show' do
  get 'path'
end

In patches

See Patches.

instance_methods(feature: 'issue.show') do
  def show
    # Something todo
    # if feature is active
    super
  end
end

Custom condition

If you do not want to use DB records for state checking, you can define custom conditions.

# Ruby block state
# Active in about 10% of cases
Rys::Feature.add('issue.show') do
  rand(0..10) == 1
end

or you can use both approaches by:

Rys::Feature.add('issue.show') do
  rand(0..10) == 1 && RysFeatureRecord.active?('issue.show')
end

Translations

All features have title and description. You can set them via language file or via options.

Rys::Feature.add('issue.show')
# => I18n.t 'rys_features.issue_show.title'
# => I18n.t 'rys_features.issue_show.description'

Rys::Feature.add('issue.show', title: :feature_title,
                               description: :feature_description)
# => I18n.t 'feature_title'
# => I18n.t 'feature_description'