Sha256: 46edc0ca899b56b8cf84bb8b38ed6c2e7dbfb57b6cae559023f9b8da9e4a8fe9

Contents?: true

Size: 1.59 KB

Versions: 90

Compression:

Stored size: 1.59 KB

Contents

class ForeignInputSet < ApplicationRecord
  include ::Exportable

  class CircularDependencyError < Foreman::Exception
  end

  attr_exportable :exclude, :include, :include_all, :template => ->(input_set) { input_set.target_template.name }

  belongs_to :template
  belongs_to :target_template, :class_name => 'Template'

  scoped_search :on => :target_template_id, :complete_value => true

  validates :target_template, :presence => true
  validate :check_circular_dependency

  def inputs(templates_stack = [])
    return [] unless target_template

    if templates_stack.include?(target_template) || template&.id == target_template&.id
      raise CircularDependencyError.new(N_("Circular dependency detected in foreign input set '%{template}' -> '%{target_template}'. Templates stack: %{templates_stack}"),
        :template => template.name, :target_template => target_template.name, :templates_stack => templates_stack.map(&:name).inspect)
    end
    inputs = target_template.template_inputs_with_foreign(templates_stack + [target_template])
    unless self.include_all?
      inputs = inputs.select { |input| included_names.include?(input.name) }
    end
    inputs = inputs.reject { |input| excluded_names.include?(input.name) }
    return inputs
  end

  def included_names
    comma_separated_names(self.include)
  end

  def excluded_names
    comma_separated_names(self.exclude)
  end

  private

  def comma_separated_names(value)
    value.to_s.split(',').map(&:strip)
  end

  def check_circular_dependency
    self.inputs
    true
  rescue CircularDependencyError => e
    self.errors.add :base, e.message
  end
end

Version data entries

90 entries across 90 versions & 1 rubygems

Version Path
foreman_remote_execution-13.2.8 app/models/foreign_input_set.rb
foreman_remote_execution-15.0.2 app/models/foreign_input_set.rb
foreman_remote_execution-15.0.1 app/models/foreign_input_set.rb
foreman_remote_execution-13.2.7 app/models/foreign_input_set.rb
foreman_remote_execution-15.0.0 app/models/foreign_input_set.rb
foreman_remote_execution-14.1.4 app/models/foreign_input_set.rb
foreman_remote_execution-14.1.3 app/models/foreign_input_set.rb
foreman_remote_execution-14.1.2 app/models/foreign_input_set.rb
foreman_remote_execution-14.1.1 app/models/foreign_input_set.rb
foreman_remote_execution-14.1.0 app/models/foreign_input_set.rb
foreman_remote_execution-14.0.2 app/models/foreign_input_set.rb
foreman_remote_execution-14.0.1 app/models/foreign_input_set.rb
foreman_remote_execution-13.2.6 app/models/foreign_input_set.rb
foreman_remote_execution-14.0.0 app/models/foreign_input_set.rb
foreman_remote_execution-13.2.5 app/models/foreign_input_set.rb
foreman_remote_execution-13.2.4 app/models/foreign_input_set.rb
foreman_remote_execution-13.2.3 app/models/foreign_input_set.rb
foreman_remote_execution-13.2.2 app/models/foreign_input_set.rb
foreman_remote_execution-12.0.7 app/models/foreign_input_set.rb
foreman_remote_execution-13.2.1 app/models/foreign_input_set.rb