-
Notifications
You must be signed in to change notification settings - Fork 55
/
template_import.rb
65 lines (60 loc) · 2.02 KB
/
template_import.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
65
module ForemanTemplates
module TemplateImport
extend ActiveSupport::Concern
module ClassMethods
def import_snippet!(name, text, force = false)
# Data
snippet = self.where(:name => name).first_or_initialize
data = {
:template => text,
:snippet => true
}
# Printout helpers
c_or_u = snippet.new_record? ? 'Creating' : 'Updating'
id_string = snippet.new_record? ? '' : "id #{snippet.id}"
if snippet.locked? && !snippet.new_record? && !force
return { :diff => nil,
:status => false,
:result => "Skipping snippet #{id_string}:#{name} - template is locked" }
end
status = nil
if data[:template] != snippet.template
diff = Diffy::Diff.new(
snippet.template,
data[:template],
:include_diff_info => true
).to_s(:color)
snippet.ignore_locking do
status = snippet.update_attributes(data)
end
result = " #{c_or_u} Snippet #{id_string}:#{name}"
else
diff = nil
status = true
result = " No change to Snippet #{id_string}:#{name}"
end
{ :diff => diff, :status => status, :result => result, :errors => snippet.errors }
end
def map_metadata(metadata, param)
if metadata[param]
case param
when 'oses'
metadata[param].map do |os|
Operatingsystem.all.map { |db| db.to_label =~ /^#{os}/ ? db : nil }
end.flatten.compact
when 'locations'
metadata[param].map do |loc|
User.current.my_locations.map { |db| db.name =~ /^#{loc}/ ? db : nil }
end.flatten.compact
when 'organizations'
metadata[param].map do |org|
User.current.my_organizations.map { |db| db.name =~ /^#{org}/ ? db : nil }
end.flatten.compact
end
else
[]
end
end
end
end
end