Sha256: e7dd5e82120955fcbffbfd0f34a511f32b3d20f1a16f8f185bb74c58e02cd265

Contents?: true

Size: 1.65 KB

Versions: 7

Compression:

Stored size: 1.65 KB

Contents

# frozen_string_literal: true

module Decidim
  module TermCustomizer
    module Context
      class JobContext < Base
        def resolve!
          # Figure out the organization and user through the job arguments if
          # passed for the job.
          user = nil
          data[:job].arguments.each do |arg|
            @organization ||= organization_from_argument(arg)
            @space ||= space_from_argument(arg)
            @component ||= component_from_argument(arg)
            user ||= arg if arg.is_a?(Decidim::User)
          end

          # In case a component was found, define the space as the component
          # space to avoid any conflicts.
          @space = component.participatory_space if component

          # In case a space was found, define the organization as the space
          # organization to avoid any conflicts.
          @organization = space.organization if space

          # In case an organization could not be resolved any other way, check
          # it through the user (if the user was passed).
          @organization ||= user.organization if user
        end

        protected

        def organization_from_argument(arg)
          return arg if arg.is_a?(Decidim::Organization)

          arg.organization if arg.respond_to?(:organization)
        end

        def space_from_argument(arg)
          return arg if arg.is_a?(Decidim::Participable)

          arg.participatory_space if arg.respond_to?(:participatory_space)
        end

        def component_from_argument(arg)
          return arg if arg.is_a?(Decidim::Component)

          arg.component if arg.respond_to?(:component)
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
decidim-term_customizer-0.23.0 lib/decidim/term_customizer/context/job_context.rb
decidim-term_customizer-0.22.0 lib/decidim/term_customizer/context/job_context.rb
decidim-term_customizer-0.21.0 lib/decidim/term_customizer/context/job_context.rb
decidim-term_customizer-0.20.0 lib/decidim/term_customizer/context/job_context.rb
decidim-term_customizer-0.19.1 lib/decidim/term_customizer/context/job_context.rb
decidim-term_customizer-0.19.0 lib/decidim/term_customizer/context/job_context.rb
decidim-term_customizer-0.18.0 lib/decidim/term_customizer/context/job_context.rb