Sha256: 4d742e1c6197a01a75fafa5abefc56843b18e460739012e573474778bb2218a5

Contents?: true

Size: 1.68 KB

Versions: 3

Compression:

Stored size: 1.68 KB

Contents

module Flipflop
  class FeaturesController < ApplicationController
    include ActionController::RequestForgeryProtection
    include ActionController::Rendering
    include ActionView::Rendering
    include ActionView::Layouts

    # Allows overriding layout by inheriting from this controller.
    layout "flipflop"

    def index
      @feature_set = FeaturesPresenter.new(FeatureSet.current)
      render
    end

    class FeaturesPresenter
      include Flipflop::Engine.routes.url_helpers

      attr_reader :strategies, :grouped_features, :application_name

      def initialize(feature_set)
        @cache = {}
        @feature_set = feature_set

        @strategies = @feature_set.strategies.reject(&:hidden?)
        @grouped_features = @feature_set.features.group_by(&:group)

        @application_name = Rails.application.class.parent_name.underscore.titleize
      end

      def grouped?
        grouped_features.keys != [nil]
      end

      def status(feature)
        cache(nil, feature) do
          status_to_sym(@feature_set.enabled?(feature.key))
        end
      end

      def strategy_status(strategy, feature)
        cache(strategy, feature) do
          status_to_sym(strategy.enabled?(feature.key))
        end
      end

      def switch_url(strategy, feature)
        feature_strategy_path(feature.key, strategy.key)
      end

      private

      def cache(strategy, feature)
        key = feature.key.to_s + (strategy ? "-" + strategy.key.to_s : "")
        return @cache[key] if @cache.has_key?(key)
        @cache[key] = yield
      end

      def status_to_sym(status)
        return :enabled if status == true
        return :disabled if status == false
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
flipflop-2.4.0 app/controllers/flipflop/features_controller.rb
flipflop-2.3.1 app/controllers/flipflop/features_controller.rb
flipflop-2.3.0 app/controllers/flipflop/features_controller.rb