Skip to content

Featury始め方

規約

  • すべてのフィーチャークラスはFeatury::Baseのサブクラスであり、app/featuresディレクトリに配置します。一般的な方法としてApplicationFeature::Baseクラスを作成し、Featury::Baseのサブクラスとして継承します。
  • フィーチャーは関連するプロセスに基づいて命名します。名前には名詞を使用し、可能な限りモデル名と一致させてください。例: User::OnboardFeatureではなくUser::OnboardingFeatureと命名します。

バージョンサポート

Ruby/Rails8.18.07.27.17.06.16.05.25.15.0
4.0
3.4
3.3
3.2
3.1

インストール

Gemfileに以下を追加します:

ruby
gem "featury"

そして以下を実行します:

shell
bundle install

準備

まず、継承用の基底クラスを準備することを推奨します。 この基底クラスには、プロジェクトのフィーチャーツールと統合されたアクションを含める必要があります。 例えば、ActiveRecordモデル、Flipper、またはその他のツールが使用できます。

ActiveRecordモデル

例としてFeatureFlagモデルを使用します。

ruby
module ApplicationFeature
  class Base < Featury::Base
    action :enabled? do |features:, **options|
      features.all? do |feature|
        FeatureFlag
          .find_or_create_by!(code: feature, actor: options[:user])
          .enabled?
      end
    end

    action :disabled? do |features:, **options|
      features.any? do |feature|
        !FeatureFlag
          .find_or_create_by!(code: feature, actor: options[:user])
          .enabled?
      end
    end

    action :enable do |features:, **options|
      features.all? do |feature|
        FeatureFlag
          .find_or_create_by!(code: feature, actor: options[:user])
          .update!(enabled: true)
      end
    end

    action :disable do |features:, **options|
      features.all? do |feature|
        FeatureFlag
          .find_or_create_by!(code: feature, actor: options[:user])
          .update!(enabled: false)
      end
    end

    before do |action:, features:|
      Slack::API::Notify.call!(action:, features:)
    end

    after :enabled?, :disabled? do |action:, features:|
      Slack::API::Notify.call!(action:, features:)
    end
  end
end