Skip to content

Commit 7c5039d

Browse files
author
Seth Falcon
committed
Move update logic from environments controller to environment model object
1 parent 5e85014 commit 7c5039d

3 files changed

Lines changed: 29 additions & 4 deletions

File tree

chef-server-api/app/controllers/environments.rb

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,7 @@ def update
6666
raise NotFound, "Cannot load environment #{params[:id]}"
6767
end
6868

69-
env.description(params["inflated_object"].description)
70-
env.cookbook_versions(params["inflated_object"].cookbook_versions)
69+
env.update_from!(params["inflated_object"])
7170
env.cdb_save
7271
env.couchdb_rev = nil
7372
self.status = 200
@@ -87,4 +86,4 @@ def destroy
8786

8887
private
8988

90-
end
89+
end

chef/lib/chef/environment.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,12 @@ def to_json(*a)
131131
to_hash.to_json(*a)
132132
end
133133

134+
def update_from!(o)
135+
description(o.description)
136+
cookbook_versions(o.cookbook_versions)
137+
self
138+
end
139+
134140
def self.json_create(o)
135141
environment = new
136142
environment.name(o["name"])

chef/spec/unit/environment_spec.rb

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,26 @@
110110
end
111111
end
112112

113+
describe "update_from!" do
114+
before(:each) do
115+
@environment.name("prod")
116+
@environment.description("this is prod")
117+
@environment.cookbook_versions({ "apt" => "1.2.3" })
118+
119+
@example = Chef::Environment.new
120+
@example.name("notevenprod")
121+
@example.description("this is pre-prod")
122+
@example.cookbook_versions({ "apt" => "2.3.4" })
123+
end
124+
125+
it "should update everything but name" do
126+
@environment.update_from!(@example)
127+
@environment.name.should == "prod"
128+
@environment.description.should == @example.description
129+
@environment.cookbook_versions.should == @example.cookbook_versions
130+
end
131+
end
132+
113133
describe "to_hash" do
114134
before(:each) do
115135
@environment.name("spec")
@@ -219,4 +239,4 @@
219239
Chef::Environment.validate_cookbook_version(Chef::CookbookVersion.new("meta")).should == false
220240
end
221241
end
222-
end
242+
end

0 commit comments

Comments
 (0)