Sha256: 6a27709498ce4691f85836943eff10d261787667c7fcecd744cd6ba960963028

Contents?: true

Size: 1.23 KB

Versions: 52

Compression:

Stored size: 1.23 KB

Contents

module ForemanTasks
  module Concerns
    module ActionSubject
      extend ActiveSupport::Concern

      def action_input_key
        self.class.name.demodulize.underscore
      end

      def to_action_input
        raise 'The resource needs to be saved first' if new_record?

        { id: id, name: name }.tap do |hash|
          hash.update(label: label) if respond_to? :label
        end
      end

      # @api override to return the objects that relate to this one, usually parent
      # objects, e.g. repository would return product it belongs to, product would return
      # provider etc.
      #
      # It's used to link a task running on top of this resource to it's related objects,
      # so that is't possible to see all the sync tasks for a product etc.
      def related_resources
        []
      end

      # Recursively searches for related resources of this one, avoiding cycles
      def all_related_resources
        mine = Set.new Array(related_resources)

        get_all_related_resources = lambda do |resource|
          resource.is_a?(ActionSubject) ? resource.all_related_resources : []
        end

        mine + mine.reduce(Set.new) { |s, resource| s + get_all_related_resources.call(resource) }
      end
    end
  end
end

Version data entries

52 entries across 52 versions & 1 rubygems

Version Path
foreman-tasks-10.0.1 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-10.0.0 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-9.2.3 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-9.2.2 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-9.2.1 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-9.2.0 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-9.1.1 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-9.0.4 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-9.1.0 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-9.0.2 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-9.0.1 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-8.3.3 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-9.0.0 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-8.2.1 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-8.1.4 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-8.3.2 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-8.3.1 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-8.1.3 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-8.3.0 app/models/foreman_tasks/concerns/action_subject.rb
foreman-tasks-8.2.0 app/models/foreman_tasks/concerns/action_subject.rb