-
Notifications
You must be signed in to change notification settings - Fork 898
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added AutomateWorkspace model #15817
Changes from 7 commits
32db7e5
c7e2b5b
039a94a
1d18775
5353c1f
c93ce23
fd8b964
bb02c3c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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'].blank? && hash['state_vars'].blank? | ||
raise ArgumentError, "No workspace or state_var specified for edit" | ||
end | ||
|
||
self[:output] = (output || {}).deep_merge(hash) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I believe it's preferable to use write_attribute over []= . @jrafanie @chrisarcand Do you know what's the preferable way to do this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Oh duh...the preferable way is to use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Prefer |
||
save! | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. A setter should never call .save! directly. It's should be the caller's concern as they may choose to defer saving, or use update_attributes or whatever. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That being said, can the setter just be the setter, and deal with the validation at validation time? Doing all this on a setter seems wrong. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you must have a method that sets and saves, then prefer creating a method like |
||
self | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
FactoryGirl.define do | ||
factory :automate_workspace do | ||
end | ||
end |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}} | ||
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 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should this be an
||
? Based on the error message it should fail if either one is blank. Additionally, ifstate_vars => {}
that would technically be blank as well, so maybe this should check for nil instead?