Skip to content

Commit

Permalink
Allow nil values to not overwrite old values in #load_values_into_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
eclarizio committed Aug 13, 2018
1 parent 4b04629 commit f15c18b
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 5 deletions.
7 changes: 5 additions & 2 deletions app/models/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,12 +88,15 @@ def validate_field_data
result
end

def load_values_into_fields(values)
def load_values_into_fields(values, overwrite = true)
values = values.with_indifferent_access

dialog_field_hash.each_value do |field|
field.dialog = self
field.value = values[field.automate_key_name] || values[field.name]
new_value = values[field.automate_key_name] || values[field.name]
new_value ||= field.value unless overwrite

field.value = new_value
end
end

Expand Down
2 changes: 1 addition & 1 deletion app/models/resource_action_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ def load_dialog(resource_action, values, options)
dialog.init_fields_with_values_for_request(values)
elsif options[:provision_workflow]
dialog.initialize_value_context(values)
dialog.load_values_into_fields(values)
dialog.load_values_into_fields(values, false)
elsif options[:refresh] || options[:submit_workflow]
dialog.load_values_into_fields(values)
elsif options[:reconfigure]
Expand Down
2 changes: 1 addition & 1 deletion app/models/service_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -395,7 +395,7 @@ def queue_order(user_id, options, request_options)
)
end

def order(user_or_id, options = nil, request_options = nil, schedule_time = nil)
def order(user_or_id, options = nil, request_options = {}, schedule_time = nil)
user = user_or_id.kind_of?(User) ? user_or_id : User.find(user_or_id)
workflow = provision_workflow(user, options, request_options)
if schedule_time
Expand Down
33 changes: 33 additions & 0 deletions spec/models/dialog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,12 @@
dialog.load_values_into_fields(vars)
expect(dialog_field1.value).to eq("10.8.99.248")
end

it "sets field value when prefixed with 'dialog'" do
vars = {"dialog_field1" => "10.8.99.248"}
dialog.load_values_into_fields(vars)
expect(dialog_field1.value).to eq("10.8.99.248")
end
end

context "symbol values" do
Expand All @@ -478,6 +484,33 @@
dialog.load_values_into_fields(vars)
expect(dialog_field1.value).to eq("10.8.99.248")
end

it "sets field value when prefixed with 'dialog'" do
vars = {:dialog_field1 => "10.8.99.248"}
dialog.load_values_into_fields(vars)
expect(dialog_field1.value).to eq("10.8.99.248")
end
end

context "when the incoming values are missing keys" do
let(:dialog_group) { DialogGroup.new(:dialog_fields => [dialog_field1, dialog_field2]) }
let(:dialog_field2) { DialogField.new(:value => "321", :name => "field2") }

context "when overwrite is true" do
it "sets nil values" do
vars = {:field1 => "10.8.99.248"}
dialog.load_values_into_fields(vars)
expect(dialog_field2.value).to eq(nil)
end
end

context "when overwrite is false" do
it "does not set nil values" do
vars = {:field1 => "10.8.99.248"}
dialog.load_values_into_fields(vars, false)
expect(dialog_field2.value).to eq("321")
end
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/models/resource_action_workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@

it "initializes the value context and then loads the values into fields" do
expect(dialog).to receive(:initialize_value_context).with(values).ordered
expect(dialog).to receive(:load_values_into_fields).with(values).ordered
expect(dialog).to receive(:load_values_into_fields).with(values, false).ordered
ResourceActionWorkflow.new(values, nil, resource_action, options)
end
end
Expand Down

0 comments on commit f15c18b

Please sign in to comment.