diff --git a/app/models/automate_workspace.rb b/app/models/automate_workspace.rb new file mode 100644 index 00000000000..096eb16ed2e --- /dev/null +++ b/app/models/automate_workspace.rb @@ -0,0 +1,17 @@ +class AutomateWorkspace < ApplicationRecord + include UuidMixin + belongs_to :user + belongs_to :tenant + validates :tenant, :presence => true + validates :user, :presence => true + + def merge_output!(hash) + if hash['workspace'].nil? || hash['state_vars'].nil? + raise ArgumentError, "No workspace or state_vars specified for edit" + end + + self[:output] = (output || {}).deep_merge(hash) + save! + self + end +end diff --git a/spec/factories/automate_workspace.rb b/spec/factories/automate_workspace.rb new file mode 100644 index 00000000000..e2fd67dd47c --- /dev/null +++ b/spec/factories/automate_workspace.rb @@ -0,0 +1,4 @@ +FactoryGirl.define do + factory :automate_workspace do + end +end diff --git a/spec/models/automate_workspace_spec.rb b/spec/models/automate_workspace_spec.rb new file mode 100644 index 00000000000..85f072c82ec --- /dev/null +++ b/spec/models/automate_workspace_spec.rb @@ -0,0 +1,22 @@ +describe AutomateWorkspace do + describe "#merge_output!" do + let(:user) { FactoryGirl.create(:user_with_group, :userid => "admin") } + let(:aw) { FactoryGirl.create(:automate_workspace, :user => user, :tenant => user.current_tenant) } + it "raises error on invalid hash" do + expect { aw.merge_output!({}) }.to raise_exception(ArgumentError) + end + + it "properly merges the hash with the new output" do + hash = {'workspace' => {'a' => 1}, 'state_vars' => {'b' => 2}} + partial_hash = {'workspace' => {'c' => 1}, 'state_vars' => {} } + merged_hash = {'workspace' => {'a' => 1, 'c' => 1}, 'state_vars' => {'b' => 2}} + + aw.merge_output!(hash) + aw.reload + aw.merge_output!(partial_hash) + aw.reload + + expect(aw.output).to eq(merged_hash) + end + end +end