forked from ManageIQ/manageiq
-
Notifications
You must be signed in to change notification settings - Fork 1
/
miq_ae_field.rb
117 lines (90 loc) · 3.89 KB
/
miq_ae_field.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
class MiqAeField < ApplicationRecord
include MiqAeSetUserInfoMixin
include MiqAeYamlImportExportMixin
belongs_to :ae_class, :class_name => "MiqAeClass", :foreign_key => :class_id, :touch => true
belongs_to :ae_method, :class_name => "MiqAeMethod", :foreign_key => :method_id, :touch => true
has_many :ae_values, :class_name => "MiqAeValue", :foreign_key => :field_id, :dependent => :destroy,
:inverse_of => :ae_field
validates :name, :uniqueness_when_changed => {:scope => [:class_id, :method_id], :case_sensitive => false},
:presence => true,
:format => {:with => /\A[\w]+\z/i, :message => N_("may contain only alphanumeric and _ characters")}
validates :substitute, :inclusion => {:in => [true, false]}
NULL_COALESCING_DATATYPE = "null coalescing".freeze
AVAILABLE_SCOPES = ["class", "instance", "local"]
validates :scope, :inclusion => {:in => AVAILABLE_SCOPES, :allow_nil => true} # nil => instance
AVAILABLE_AETYPES = ["assertion", "attribute", "method", "relationship", "state"]
validates :aetype, :inclusion => {:in => AVAILABLE_AETYPES, :allow_nil => true} # nil => attribute
AVAILABLE_DATATYPES_FOR_UI = ["string", "symbol", "integer", "float", "boolean", "time",
"array", "password", NULL_COALESCING_DATATYPE].freeze
AVAILABLE_DATATYPES = AVAILABLE_DATATYPES_FOR_UI +
%w[host
vm
storage
ems
policy
server
request
provision
user]
validates :datatype, :inclusion => {:in => AVAILABLE_DATATYPES, :allow_nil => true} # nil => string
before_save :set_message_and_default_value
DEFAULTS = {:substitute => true, :datatype => "string", :aetype => "attribute", :scope => "instance", :message => "create"}
def self.available_aetypes
AVAILABLE_AETYPES
end
def self.available_datatypes
AVAILABLE_DATATYPES
end
class << self
alias available_datatypes_for_ui available_datatypes
end
def self.defaults
DEFAULTS
end
def self.default(key)
DEFAULTS[key.to_sym]
end
def self.lookup_by_name(name)
where("lower(name) = ?", name.downcase).first
end
singleton_class.send(:alias_method, :find_by_name, :lookup_by_name)
Vmdb::Deprecation.deprecate_methods(singleton_class, :find_by_name => :lookup_by_name)
def default_value=(value)
set_default_value(value)
end
def to_export_yaml
{"field" => export_attributes}
end
def to_export_xml(options = {})
require 'builder'
xml = options[:builder] ||= ::Builder::XmlMarkup.new(:indent => options[:indent])
xml_attrs = {:name => name, :substitute => substitute.to_s}
self.class.column_names.each do |cname|
# Remove any columns that we do not want to export
next if %w[id created_on updated_on updated_by].include?(cname) || cname.ends_with?("_id")
# Skip any columns that we process explicitly
next if %w[name default_value substitute].include?(cname)
# Process the column
xml_attrs[cname.to_sym] = send(cname) if send(cname).present?
end
xml.MiqAeField(xml_attrs) do
xml.text!(default_value) if default_value.present?
end
end
def substitute=(value)
value = false if value.nil?
super
end
def set_message_and_default_value
self.message ||= DEFAULTS[:message]
set_default_value(default_value)
end
delegate :editable?, :to => :ae_class
def self.display_name(number = 1)
n_('Automation Field', 'Automation Fields', number)
end
private
def set_default_value(value)
write_attribute(:default_value, datatype == "password" ? MiqAePassword.encrypt(value) : value)
end
end