Sha256: d91dbc8dd20c72e80cba4b7e0196899ca6120f38bff68deb869ac26966bd930d

Contents?: true

Size: 1.22 KB

Versions: 13

Compression:

Stored size: 1.22 KB

Contents

# frozen_string_literal: true

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

    # Public: Matches the request against a component and injects it into the
    #         environment.
    #
    # request - The request that holds the current component relevant information.
    #
    # Returns a true if the request matched, false otherwise
    def matches?(request)
      env = request.env

      @participatory_space = env["decidim.current_participatory_space"]
      return false unless @participatory_space

      current_component(env, request.params) ? true : false
    end

    private

    def current_component(env, params)
      env["decidim.current_component"] ||= detect_current_component(params)
    end

    def detect_current_component(params)
      @participatory_space.components.find do |component|
        params["component_id"] == component.id.to_s && component.manifest_name == @manifest.name.to_s
      end
    end
  end
end

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/constraints/decidim/current_component.rb
decidim-core-0.28.4 app/constraints/decidim/current_component.rb
decidim-core-0.29.0 app/constraints/decidim/current_component.rb
decidim-core-0.28.3 app/constraints/decidim/current_component.rb
decidim-core-0.29.0.rc4 app/constraints/decidim/current_component.rb
decidim-core-0.29.0.rc3 app/constraints/decidim/current_component.rb
decidim-core-0.29.0.rc2 app/constraints/decidim/current_component.rb
decidim-core-0.29.0.rc1 app/constraints/decidim/current_component.rb
decidim-core-0.28.2 app/constraints/decidim/current_component.rb
decidim-core-0.28.1 app/constraints/decidim/current_component.rb
decidim-core-0.28.0 app/constraints/decidim/current_component.rb
decidim-core-0.28.0.rc5 app/constraints/decidim/current_component.rb
decidim-core-0.28.0.rc4 app/constraints/decidim/current_component.rb