Module: ForemanRemoteExecution::Exportable

Extended by:
ActiveSupport::Concern
Included in:
ForeignInputSet, JobTemplate, TemplateInput
Defined in:
app/models/concerns/foreman_remote_execution/exportable.rb

Defined Under Namespace

Modules: ClassMethods

Instance Method Summary collapse

Instance Method Details

#export_attr(attribute, exporter, include_blank) ⇒ Object

Export a particular attribute or association.

- If our exportable_attributes value is callable, we call it with self as an argument
- If our object is iterable, then we export each item
- If the attribute or association also includes this concern, call to_export on it


32
33
34
35
36
37
38
39
40
41
# File 'app/models/concerns/foreman_remote_execution/exportable.rb', line 32

def export_attr(attribute, exporter, include_blank)
  value = if exporter.respond_to?(:call)
            exporter.call(self)
          elsif self.respond_to?(exporter)
            self.send(exporter)
          end

  value = value.respond_to?(:map) ? export_iterable(value, include_blank) : value
  value.respond_to?(:to_export) ? value.to_export(include_blank) : value
end

#export_iterable(items, include_blank) ⇒ Object

Exports each item in an iterable. If it’s a hash, then export each value.



44
45
46
47
48
49
50
51
# File 'app/models/concerns/foreman_remote_execution/exportable.rb', line 44

def export_iterable(items, include_blank)
  if items.is_a?(Hash)
    items.each { |key, value| items[key] = value.respond_to?(:to_export) ? value.to_export(include_blank) : value }
    items.to_hash.stringify_keys
  else
    items.map { |item| item.respond_to?(:to_export) ? item.to_export(include_blank) : item }
  end
end

#to_export(include_blank = true) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
# File 'app/models/concerns/foreman_remote_execution/exportable.rb', line 15

def to_export(include_blank = true)
  self.class.exportable_attributes.keys.inject({}) do |hash, attribute|
    value = export_attr(attribute, self.class.exportable_attributes[attribute], include_blank)

    # Rails considers false blank, but if a boolean value is explicitly set false, we want to ensure we export it.
    if include_blank || value.present? || value == false
      hash.update(attribute => value)
    else
      hash
    end
  end.stringify_keys
end