Skip to content

Commit

Permalink
Use given values when running through a dialog for a request review
Browse files Browse the repository at this point in the history
  • Loading branch information
eclarizio committed Mar 7, 2017
1 parent 745a3d9 commit ff6d20c
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 3 deletions.
6 changes: 6 additions & 0 deletions app/models/dialog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,12 @@ def init_fields_with_values(values)
dialog_field_hash.each { |_key, field| field.update_values(values) }
end

def init_fields_with_values_for_request(values)
dialog_field_hash.each do |_key, field|
field.value = values[field.automate_key_name] || values[field.name]
end
end

def field(name)
dialog_field_hash[name.to_s]
end
Expand Down
10 changes: 7 additions & 3 deletions app/models/resource_action_workflow.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ def initialize(values, requester, resource_action, options = {})
@requester = requester
@target = options[:target]
@initiator = options[:initiator]
@dialog = load_dialog(resource_action, values)
@dialog = load_dialog(resource_action, values, options)

@settings[:resource_action_id] = resource_action.id unless resource_action.nil?
@settings[:dialog_id] = @dialog.id unless @dialog.nil?
Expand Down Expand Up @@ -86,7 +86,7 @@ def create_values_hash
}
end

def load_dialog(resource_action, values)
def load_dialog(resource_action, values, options)
if resource_action.nil?
resource_action = load_resource_action(values)
@settings[:resource_action_id] = resource_action.id unless resource_action.nil?
Expand All @@ -95,7 +95,11 @@ def load_dialog(resource_action, values)
dialog = resource_action.dialog unless resource_action.nil?
unless dialog.nil?
dialog.target_resource = @target
dialog.init_fields_with_values(values)
if options[:request_view]
dialog.init_fields_with_values_for_request(values)
else
dialog.init_fields_with_values(values)
end
end
dialog
end
Expand Down
23 changes: 23 additions & 0 deletions spec/models/dialog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -435,4 +435,27 @@
expect(ResourceAction.count).to eq(num_actions * 2)
end
end

describe "#init_fields_with_values_for_request" do
let(:dialog) { described_class.new(:dialog_tabs => [dialog_tab]) }
let(:dialog_tab) { DialogTab.new(:dialog_groups => [dialog_group]) }
let(:dialog_group) { DialogGroup.new(:dialog_fields => [dialog_field1]) }
let(:dialog_field1) { DialogField.new(:value => "123", :name => "field1") }

context "when the values use the automate key name" do
it "initializes the fields with the given values" do
values = {"dialog_field1" => "field 1 new value"}
dialog.init_fields_with_values_for_request(values)
expect(dialog_field1.value).to eq("field 1 new value")
end
end

context "when the values use the regular name" do
it "initializes the fields with the given values" do
values = {"field1" => "field 1 new value"}
dialog.init_fields_with_values_for_request(values)
expect(dialog_field1.value).to eq("field 1 new value")
end
end
end
end
32 changes: 32 additions & 0 deletions spec/models/resource_action_workflow_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,36 @@
end
end
end

describe "#initialize #load_dialog" do
let(:resource_action) { instance_double("ResourceAction", :id => 123, :dialog => dialog) }
let(:dialog) { instance_double("Dialog", :id => 321) }
let(:values) { "the values" }
let(:options) { {:request_view => request_view} }

before do
allow(ResourceAction).to receive(:find).and_return(resource_action)
allow(dialog).to receive(:init_fields_with_values).with(values)
allow(dialog).to receive(:init_fields_with_values_for_request).with(values)
allow(dialog).to receive(:target_resource=)
end

context "when the options set request_view to true" do
let(:request_view) { true }

it "calls init_fields_with_values_for_request" do
expect(dialog).to receive(:init_fields_with_values_for_request).with(values)
ResourceActionWorkflow.new(values, nil, resource_action, options)
end
end

context "when the options set request_view to false" do
let(:request_view) { false }

it "calls init_fields_with_values" do
expect(dialog).to receive(:init_fields_with_values).with(values)
ResourceActionWorkflow.new(values, nil, resource_action, options)
end
end
end
end

0 comments on commit ff6d20c

Please sign in to comment.