Module: RocketJobMissionControl::JobSanitizer

Defined in:
app/models/rocket_job_mission_control/job_sanitizer.rb

Constant Summary collapse

CATEGORIES_FIELDS =
%i[id name format format_options mode skip_unknown slice_size columns].freeze

Class Method Summary collapse

Class Method Details

.sanitize(properties, job_class, target, nil_blank = true) ⇒ Object

Returns [Hash] the permissible params for the specified job class, after sanitizing. Parameters

properties [Hash]
  Parameters to extract the values from.

job_class [RocketJob::Job]
  Job class from which the user editable fields and types will be retrieved.

target [ActiveModel::Base]
  Model to set the errors on.

nil_blank [Boolean]
  true: Nil out blank fields.
  false: Do not return blank fields.
  Default: true


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'app/models/rocket_job_mission_control/job_sanitizer.rb', line 20

def self.sanitize(properties, job_class, target, nil_blank = true)
  permissible_params = {}

  job_class.user_editable_fields.each do |field_name|
    next unless value = properties[field_name]

    field = job_class.fields[field_name.to_s]
    next unless field&.type

    case field.type.name
    when "String"
      value.gsub(/\r\n/, "\n")
    when "Hash"
      begin
        value = value.blank? ? nil : JSON.parse(value)
      rescue JSON::ParserError => e
        target.errors.add(:properties, e.message)
        value = nil
      end
    when "Array"
      # remove blank entries from rails converted arrays when using multi-select input
      if !value.blank?
        value.reject! { |v| v.empty? }
      end
    end

    if value.blank? && !value.is_a?(Hash)
      permissible_params[field_name] = nil if nil_blank
    else
      permissible_params[field_name] = value
    end
  end

  if properties.key?(:input_categories_attributes)
    categories                            = sanitize_categories(properties[:input_categories_attributes])
    permissible_params[:input_categories] = categories unless categories == [{}]
  end

  if properties.key?(:output_categories_attributes)
    categories                             = sanitize_categories(properties[:output_categories_attributes])
    permissible_params[:output_categories] = categories unless categories == [{}]
  end

  permissible_params
end

.sanitize_categories(properties) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/rocket_job_mission_control/job_sanitizer.rb', line 66

def self.sanitize_categories(properties)
  categories = []

  properties.each_pair do |_, category|
    hash = {}
    CATEGORIES_FIELDS.each do |key|
      next unless category.key?(key)

      value = category[key]
      next if value.blank?
      next if (key == :columns) && value == [""]

      value     = JSON.parse(value) if key == :format_options
      hash[key] = value
    end
    categories << hash
  end

  categories
end