Sha256: e9d123c63d12a9830a31847469119ed1f01cc4d3af04a86b1bb50232ba87bcf0

Contents?: true

Size: 1.39 KB

Versions: 3

Compression:

Stored size: 1.39 KB

Contents

# frozen_string_literal: true

module Decidim
  # This class infers the current feature we're scoped to by looking at the
  # request parameters and injects it into the environment.
  class CurrentFeature
    # Public: Initializes the class.
    #
    # manifest - The manifest of the feature to check against.
    def initialize(manifest)
      @manifest = manifest
    end

    # Public: Injects the current feature into the environment.
    #
    # request - The request that holds the current feature relevant
    #           information.
    #
    # Returns nothing.
    def matches?(request)
      env = request.env
      params = request.params

      organization = env["decidim.current_organization"]

      @participatory_process = request.env["decidim.current_participatory_process"] ||
                               organization.participatory_processes.find_by_id(params["participatory_process_id"])

      env["decidim.current_participatory_process"] ||= @participatory_process

      feature = detect_current_feature(params)

      return false unless feature

      env["decidim.current_feature"] ||= feature
      true
    end

    private

    def detect_current_feature(params)
      return nil unless params["feature_id"]

      @participatory_process.features.find do |feature|
        params["feature_id"] == feature.id.to_s && feature.manifest_name == @manifest.name.to_s
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
decidim-core-0.3.2 app/constraints/decidim/current_feature.rb
decidim-core-0.3.1 app/constraints/decidim/current_feature.rb
decidim-core-0.3.0 app/constraints/decidim/current_feature.rb