Sha256: f0833d456cf9f3f58bd948a0b0856cdf3013ce1003f8e49b0507dac82f51c99b

Contents?: true

Size: 1.31 KB

Versions: 18

Compression:

Stored size: 1.31 KB

Contents

module Pageflow
  # @api private
  module FeatureTarget
    STATE_MAPPING = {
      true => 'enabled',
      false => 'disabled',
      nil => 'default'
    }

    extend ActiveSupport::Concern

    included do
      serialize :features_configuration, JSON
    end

    def enabled_feature_names
      Pageflow.config.features.select do |feature|
        feature_state(feature.name) == true
      end.map(&:name)
    end

    def feature_state(name)
      state = own_feature_state(name)
      state == nil ? inherited_feature_state(name) : state
    end

    def own_feature_state(name)
      features_configuration[name]
    end

    def inherited_feature_state(name)
      Pageflow.config.features.enabled_by_default?(name)
    end

    def feature_states=(states)
      boolean_states = states.each_with_object({}) do |(key, value), result|
        if value == true || value == STATE_MAPPING[true]
          result[key] = true
        elsif value == false || value == STATE_MAPPING[false]
          result[key] = false
        elsif value.blank? || value == STATE_MAPPING[nil]
          result[key] = nil
        end
      end

      self.features_configuration = features_configuration
        .merge(boolean_states)
        .reject { |_, value| value.nil? }
    end

    def features_configuration
      super || {}
    end
  end
end

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
pageflow-12.0.4 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.3 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.2 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.1 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.0 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.0.rc7 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.0.rc6 app/models/concerns/pageflow/feature_target.rb
pageflow-0.11.4 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.0.rc5 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.0.rc4 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.0.rc3 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.0.rc2 app/models/concerns/pageflow/feature_target.rb
pageflow-12.0.0.rc1 app/models/concerns/pageflow/feature_target.rb
pageflow-0.11.3 app/models/concerns/pageflow/feature_target.rb
pageflow-0.11.2 app/models/concerns/pageflow/feature_target.rb
pageflow-0.11.1 app/models/concerns/pageflow/feature_target.rb
pageflow-0.11.0 app/models/concerns/pageflow/feature_target.rb
pageflow-0.10.0 app/models/concerns/pageflow/feature_target.rb