Skip to content

Features

jiribruk edited this page Jan 17, 2019 · 3 revisions

You can toggle some code based on database state or by custom definiton.

First you need to register features. Puts this 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
# Prefered way because you can controll 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 trigered after status change.

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

In routes

You can easily turn on/off routes.

get 'path', rys_feature: 'issue.show'

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

In patches

More details can be found at Patch manager section.

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

Custom condition

If you don't want to use DB record for state checking you can define custom condition.

# Ruby block state
# Will be active on 10% of the case
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'

Rys basic

Dependencies

Tests

Management

Clone this wiki locally