/
dialog_field.rb
191 lines (154 loc) · 4.96 KB
/
dialog_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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
class DialogField < ApplicationRecord
include NewWithTypeStiMixin
attr_accessor :value
attr_accessor :dialog
belongs_to :dialog_group
has_one :resource_action, :as => :resource, :dependent => :destroy
has_many :dialog_field_associations,
:foreign_key => :trigger_id,
:class_name => :DialogFieldAssociation,
:dependent => :destroy
has_many :reverse_dialog_field_associations,
:foreign_key => :respond_id,
:class_name => :DialogFieldAssociation,
:dependent => :destroy
has_many :dialog_field_responders,
:source => :respond,
:through => :dialog_field_associations
has_many :dialog_field_triggers,
:source => :trigger,
:through => :reverse_dialog_field_associations
alias_attribute :order, :position
validates_presence_of :name
validates :name, :exclusion => {:in => %w(action controller),
:message => "Field Name %{value} is reserved."}
default_value_for :required, false
default_value_for(:visible, :allows_nil => false) { true }
serialize :values
serialize :values_method_options, Hash
serialize :display_method_options, Hash
serialize :required_method_options, Hash
serialize :options, Hash
after_initialize :default_resource_action
# Sample data from V4 dialog.rb files
# :data_type # => :string / :integer / :button / :boolean / :time
# :notes_display # => :show / :hide
# :display # => :edit / :hide / :show / :ignore
# :display_options # => {:method => ???, :options => {???}}
# :values # => {false => 0, true => 1}
# :values_options # => {:category => :Vm}
FIELD_CONTROLS = [
:dialog_field_text_box,
:dialog_field_text_area_box,
:dialog_field_check_box,
:dialog_field_drop_down_list,
:dialog_field_button,
:dialog_field_tag_control,
:dialog_field_radio_button,
:dialog_field_dynamic_list,
# Enable when UI support is available
# #:dialog_field_list_view # Future
]
DIALOG_FIELD_TYPES = {
"DialogFieldTextBox" => _("Text Box"),
"DialogFieldTextAreaBox" => _("Text Area Box"),
"DialogFieldCheckBox" => _("Check Box"),
"DialogFieldDropDownList" => _("Drop Down List"),
# Commented out next to field types until they can be implemented
# "DialogFieldButton" => "Button",
"DialogFieldTagControl" => _("Tag Control"),
"DialogFieldDateControl" => _("Date Control"),
"DialogFieldDateTimeControl" => _("Date/Time Control"),
"DialogFieldRadioButton" => _("Radio Button")
}
DIALOG_FIELD_DYNAMIC_CLASSES = %w(
DialogFieldCheckBox
DialogFieldDateControl
DialogFieldDateTimeControl
DialogFieldDropDownList
DialogFieldRadioButton
DialogFieldTextAreaBox
DialogFieldTextBox
)
def self.dialog_field_types
DIALOG_FIELD_TYPES
end
def self.field_types
FIELD_CONTROLS
end
def extract_dynamic_values
value
end
def initialize_value_context
if @value.blank?
@value = dynamic ? values_from_automate : default_value
end
end
def initialize_static_values
if @value.blank? && !dynamic
@value = default_value
end
end
def initialize_with_given_value(given_value)
self.default_value = given_value
end
def initialize_with_values(dialog_values)
# override in subclasses
nil
end
def update_values(_dialog_values)
# override in subclasses
nil
end
def normalize_automate_values(_passed_in_values)
# override in subclasses
nil
end
def automate_output_value
data_type == "integer" ? @value.to_i : @value
end
def automate_key_name
"dialog_#{name}"
end
def validate_field_data(dialog_tab, dialog_group)
validate_error_message(dialog_tab, dialog_group) if visible? && required? && required_value_error?
end
def resource
self
end
def update_and_serialize_values
trigger_automate_value_updates
DialogFieldSerializer.serialize(self)
end
def trigger_automate_value_updates
@value = values_from_automate
end
def update_dialog_field_responders(id_list)
dialog_field_responders.destroy_all
self.dialog_field_responders = available_dialog_field_responders(id_list) unless id_list.blank?
end
def deep_copy
dup.tap do |new_field|
new_field.resource_action = resource_action.dup
end
end
private
def available_dialog_field_responders(id_list)
DialogField.find(id_list)
end
def default_resource_action
build_resource_action if resource_action.nil?
end
def validate_error_message(dialog_tab, dialog_group)
"#{dialog_tab.label}/#{dialog_group.label}/#{label} is required"
end
def required_value_error?
value.blank?
end
def value_from_dialog_fields(dialog_values)
dialog_values[automate_key_name] || dialog_values[name]
end
def values_from_automate
DynamicDialogFieldValueProcessor.values_from_automate(self)
end
end