Skip to content

Commit

Permalink
Merge pull request #17329 from eclarizio/dialog_field_dynamic_loading…
Browse files Browse the repository at this point in the history
…_sev2

Dialog field loading/refresh refactor to fix automate delays
(cherry picked from commit c719d7f)

https://bugzilla.redhat.com/show_bug.cgi?id=1572716
https://bugzilla.redhat.com/show_bug.cgi?id=1572711
  • Loading branch information
gmcculloug authored and simaishi committed Apr 27, 2018
1 parent b8bc7be commit 8545d9a
Show file tree
Hide file tree
Showing 18 changed files with 284 additions and 218 deletions.
20 changes: 11 additions & 9 deletions app/models/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,13 +88,19 @@ def validate_field_data
result
end

def init_fields_with_values(values)
dialog_field_hash.each do |key, field|
def load_values_into_fields(values)
dialog_field_hash.each_value do |field|
field.dialog = self
values[key] = field.value
field.value = values[field.automate_key_name] || values[field.name]
end
dialog_field_hash.each { |key, field| values[key] = field.initialize_with_values(values) }
dialog_field_hash.each { |_key, field| field.update_values(values) }
end

def initialize_value_context(_values)
dialog_field_hash.each_value do |field|
field.dialog = self
end

dialog_field_hash.each_value(&:initialize_value_context)
end

def init_fields_with_values_for_request(values)
Expand All @@ -112,10 +118,6 @@ def content(target = nil, resource_action = nil, all_attributes = false)

workflow = ResourceActionWorkflow.new({}, User.current_user, resource_action, :target => target)

workflow.dialog.dialog_fields.each do |dialog_field|
# Accessing dialog_field.values forces an update for any values coming from automate
dialog_field.values = dialog_field.values
end
DialogSerializer.new.serialize(Array[workflow.dialog], all_attributes)
end

Expand Down
16 changes: 15 additions & 1 deletion app/models/dialog_field.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,17 @@ def self.field_types
FIELD_CONTROLS
end

def extract_dynamic_values
value
end

def initialize_value_context
@value = values_from_automate if dynamic && @value.blank?
end

def initialize_with_values(dialog_values)
@value = value_from_dialog_fields(dialog_values) || default_value
# override in subclasses
nil
end

def update_values(_dialog_values)
Expand Down Expand Up @@ -122,9 +131,14 @@ def resource
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

Expand Down
9 changes: 0 additions & 9 deletions app/models/dialog_field_check_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,6 @@ def checked?
value == "t"
end

def value
@value = values_from_automate if dynamic && @value.blank?
@value
end

def initial_values
false
end
Expand All @@ -32,10 +27,6 @@ def refresh_json_value
{:checked => checked?, :read_only => read_only?, :visible => visible?}
end

def trigger_automate_value_updates
values_from_automate
end

private

def required_value_error?
Expand Down
9 changes: 2 additions & 7 deletions app/models/dialog_field_date_control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,8 @@ def automate_output_value
end

def value
@value = dynamic ? values_from_automate : default_time if @value.blank?

Date.parse(@value).strftime("%m/%d/%Y")
value_to_parse = @value.presence || default_time
Date.parse(value_to_parse).strftime("%m/%d/%Y")
end

def normalize_automate_values(automate_hash)
Expand All @@ -45,10 +44,6 @@ def refresh_json_value
{:date => Date.parse(@value).strftime("%m/%d/%Y"), :read_only => read_only?, :visible => visible?}
end

def trigger_automate_value_updates
values_from_automate
end

private

def default_time
Expand Down
5 changes: 2 additions & 3 deletions app/models/dialog_field_date_time_control.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,8 @@ def automate_output_value
end

def value
@value = dynamic ? values_from_automate : default_time if @value.blank?

Time.parse(@value).strftime("%m/%d/%Y %H:%M")
value_to_parse = @value.presence || default_time
Time.zone.parse(value_to_parse).strftime("%m/%d/%Y %H:%M")
end

def refresh_json_value
Expand Down
3 changes: 1 addition & 2 deletions app/models/dialog_field_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ def serialize(dialog_field, all_attributes = false)
}

if dialog_field.dynamic?
dynamic_values = dialog_field.trigger_automate_value_updates
extra_attributes["values"] = dynamic_values
extra_attributes["values"] = dialog_field.extract_dynamic_values
end

if dialog_field.type == "DialogFieldTagControl"
Expand Down
21 changes: 19 additions & 2 deletions app/models/dialog_field_sorted_item.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
class DialogFieldSortedItem < DialogField
AUTOMATE_VALUE_FIELDS = %w(sort_by sort_order data_type default_value required read_only visible description).freeze

def initialize_value_context
if load_values_on_init?
raw_values
else
@raw_values = initial_values
end
end

def initialize_with_values(dialog_values)
if load_values_on_init?
raw_values
Expand Down Expand Up @@ -36,6 +44,10 @@ def values
raw_values
end

def extract_dynamic_values
@raw_values
end

def get_default_value
trigger_automate_value_updates
default_value
Expand Down Expand Up @@ -75,6 +87,11 @@ def refresh_json_value(checked_value)
{:refreshed_values => refreshed_values, :checked_value => @value, :read_only => read_only?, :visible => visible?}
end

def force_multi_value
# override in subclasses
nil
end

private

def add_nil_option
Expand All @@ -95,7 +112,7 @@ def sort_data(data_to_sort)
data_to_sort
end

def determine_selected_default_value
def determine_selected_value
if dynamic? && force_multi_value && !default_value.kind_of?(Array)
self.default_value = Array.wrap(default_value)
end
Expand All @@ -108,7 +125,7 @@ def raw_values
reject_extranneous_nil_values unless dynamic?
@raw_values = sort_data(@raw_values)
add_nil_option unless dynamic?
determine_selected_default_value
determine_selected_value
@raw_values
end

Expand Down
4 changes: 4 additions & 0 deletions app/models/dialog_field_tag_control.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
class DialogFieldTagControl < DialogFieldSortedItem
def initialize_value_context
@value = get_default_value
end

def initialize_with_values(dialog_values)
@value = value_from_dialog_fields(dialog_values) || get_default_value
end
Expand Down
5 changes: 0 additions & 5 deletions app/models/dialog_field_text_box.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ class DialogFieldTextBox < DialogField
AUTOMATE_VALUE_FIELDS = %w(data_type protected required validator_rule validator_type read_only visible description).freeze

def value
@value = values_from_automate if dynamic && @value.blank?
return nil if @value.nil?
convert_value_to_type
end
Expand Down Expand Up @@ -70,10 +69,6 @@ def refresh_json_value
{:text => @value, :read_only => read_only?, :visible => visible?}
end

def trigger_automate_value_updates
values_from_automate
end

private

def convert_value_to_type
Expand Down
4 changes: 3 additions & 1 deletion app/models/resource_action_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,10 @@ def load_dialog(resource_action, values, options)
dialog.target_resource = @target
if options[:display_view_only]
dialog.init_fields_with_values_for_request(values)
elsif options[:refresh]
dialog.load_values_into_fields(values)
else
dialog.init_fields_with_values(values)
dialog.initialize_value_context(values)
end
end
dialog
Expand Down
37 changes: 0 additions & 37 deletions spec/models/dialog_field_check_box_spec.rb
Original file line number Diff line number Diff line change
@@ -1,41 +1,4 @@
describe DialogFieldCheckBox do
describe "#value" do
let(:dialog_field) { described_class.new(:dynamic => dynamic, :value => value) }

context "when the dialog field is dynamic" do
let(:dynamic) { true }

context "when the current value is blank" do
let(:value) { "" }

before do
allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return("processor")
end

it "returns the values from the value processor" do
expect(dialog_field.value).to eq("processor")
end
end

context "when the current value is not blank" do
let(:value) { "test" }

it "returns the current value" do
expect(dialog_field.value).to eq("test")
end
end
end

context "when the dialog field is not dynamic" do
let(:dynamic) { false }
let(:value) { "test" }

it "returns the current value" do
expect(dialog_field.value).to eq("test")
end
end
end

describe "#checked?" do
let(:dialog_field) { described_class.new(:value => value) }

Expand Down
18 changes: 8 additions & 10 deletions spec/models/dialog_field_date_control_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,25 +25,23 @@
context "when the value is blank" do
let(:value) { "" }

before do
allow(described_class).to receive(:server_timezone).and_return("UTC")
end

context "when the field is dynamic" do
let(:dynamic) { true }

before do
allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate).with(dialog_field).and_return("2015-01-02")
end

it "returns the values from the value processor" do
expect(dialog_field.value).to eq("01/02/2015")
it "returns tomorrow's date" do
Timecop.freeze(Time.new(2015, 1, 2, 0, 0, 0, 0)) do
expect(dialog_field.value).to eq("01/03/2015")
end
end
end

context "when the field is not dynamic" do
let(:dynamic) { false }

before do
allow(described_class).to receive(:server_timezone).and_return("UTC")
end

it "returns tomorrow's date" do
Timecop.freeze(Time.new(2015, 1, 2, 0, 0, 0, 0)) do
expect(dialog_field.value).to eq("01/03/2015")
Expand Down
26 changes: 13 additions & 13 deletions spec/models/dialog_field_date_time_control_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,10 @@

describe "#automate_output_value" do
let(:dialog_field) { described_class.new(:value => value) }
let(:server) { double("MiqServer", :server_timezone => "UTC") }

before do
allow(described_class).to receive(:server_timezone).and_return("UTC")
allow(MiqServer).to receive(:my_server).and_return(server)
end

context "when the dialog_field is blank" do
Expand Down Expand Up @@ -83,6 +84,11 @@

describe "#value" do
let(:dialog_field) { described_class.new(:dynamic => dynamic, :value => value) }
let(:server) { double("MiqServer", :server_timezone => "UTC") }

before do
allow(MiqServer).to receive(:my_server).and_return(server)
end

context "when the value is not blank" do
let(:value) { "04/07/2015 00:00" }
Expand Down Expand Up @@ -110,23 +116,16 @@
context "when the field is dynamic" do
let(:dynamic) { true }

before do
allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate)
.with(dialog_field).and_return("2015-01-02")
end

it "returns the values from the value processor" do
expect(dialog_field.value).to eq("01/02/2015 00:00")
it "returns tomorrow's date" do
Timecop.freeze(Time.utc(2015, 1, 2, 4, 30)) do
expect(dialog_field.value).to eq("01/03/2015 04:30")
end
end
end

context "when the field is not dynamic" do
let(:dynamic) { false }

before do
allow(described_class).to receive(:server_timezone).and_return("UTC")
end

it "returns tomorrow's date" do
Timecop.freeze(Time.utc(2015, 1, 2, 4, 30)) do
expect(dialog_field.value).to eq("01/03/2015 04:30")
Expand All @@ -138,9 +137,10 @@

describe "#refresh_json_value" do
let(:dialog_field) { described_class.new(:read_only => true) }
let(:server) { double("MiqServer", :server_timezone => "UTC") }

before do
allow(described_class).to receive(:server_timezone).and_return("UTC")
allow(MiqServer).to receive(:my_server).and_return(server)
allow(DynamicDialogFieldValueProcessor).to receive(:values_from_automate)
.with(dialog_field).and_return("2015-02-03T18:50:00Z")
end
Expand Down
2 changes: 1 addition & 1 deletion spec/models/dialog_field_serializer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
let(:dynamic) { true }

before do
allow(dialog_field).to receive(:trigger_automate_value_updates).and_return("dynamic values")
allow(dialog_field).to receive(:extract_dynamic_values).and_return("dynamic values")
end

context 'when wanting the excluded set of attributes' do
Expand Down
Loading

0 comments on commit 8545d9a

Please sign in to comment.