When a product is created, the user marked as the owner is given an
automatic role as a Developer.
When a product is modified, and the owner is changed, then the old
owner's product role is removed and the new owner is assigned a product
role instead.
Signed-off-by: Darryl L. Pierce <mcpierce(a)gmail.com>
---
app/controllers/products_controller.rb | 19 ++++++++++++++++
doc/ChangeLog | 1 +
test/functional/products_controller_test.rb | 31 ++++++++++++++++++++------
3 files changed, 44 insertions(+), 7 deletions(-)
diff --git a/app/controllers/products_controller.rb
b/app/controllers/products_controller.rb
index c8d8190..f2075fe 100644
--- a/app/controllers/products_controller.rb
+++ b/app/controllers/products_controller.rb
@@ -104,6 +104,7 @@ class ProductsController < ApplicationController
if @product.project.can_create_products?(@user)
if @product.save
+ create_role_for_owner
flash[:message] = "Product created successfully."
format.html { redirect_to product_path(@product) }
add_rss_entry(create_rss_entry("New product created:
#{(a)product.name}",
@@ -135,9 +136,16 @@ class ProductsController < ApplicationController
if @product.can_edit?(@user)
begin
Product.transaction do
+ old_owner = @product.owner
@product.update_attributes(params[:product])
if @product.save
+ # create a new product role if the owner changed
+ if (@product.owner_id != old_owner.id)
+ old_role = ProductRole.for_product((a)product).for_user(old_owner)
+ ProductRole.delete(old_role)
+ end
+ create_role_for_owner
add_rss_entry(create_rss_entry("Details updated:
#{(a)product.name}",
product_url(@product),
"The product's details have been
updated..."))
@@ -207,6 +215,17 @@ class ProductsController < ApplicationController
end
end
+ def create_role_for_owner
+ owner = User.find_by_id((a)product.owner_id)
+ unless @product.is_member?(owner)
+ ProductRole.create(:product => @product,
+ :user_id => @product.owner_id,
+ :role => Role.find_by_name("Developer"),
+ :pending => false,
+ :approved => true)
+ end
+ end
+
def path_to_list
@project = Project.find_by_id(params[:project]) if params[:project]
if @project
diff --git a/doc/ChangeLog b/doc/ChangeLog
index 514f40f..1f992fe 100644
--- a/doc/ChangeLog
+++ b/doc/ChangeLog
@@ -45,6 +45,7 @@ Change Log (0.3.0):
* #212 - Task events show up in the product's RSS feed.
* #213 - Users can disable receiving emails when replies are posted.
* #214 - The product activity list is shown in the sidebar. (BUG)
+ * #215 - Product roles are automatically created for product owners. (BUG)
* #216 - The user's most recent postings are displayed on their details page.
* #217 - All of a user's posts can be viewed.
* #218 - Sprints need a new stage called "proposed". (backed out)
diff --git a/test/functional/products_controller_test.rb
b/test/functional/products_controller_test.rb
index 74b489d..c2559b6 100644
--- a/test/functional/products_controller_test.rb
+++ b/test/functional/products_controller_test.rb
@@ -37,8 +37,9 @@ class ProductsControllerTest < ActionController::TestCase
@existing_product = products(:projxp_web)
@product_owner = @existing_product.owner
- @non_product_owner = users(:mcpierce)
+ @non_product_owner = users(:jdonuts)
raise "Owner and nonowner cannot be the same user!" if @product_owner.id ==
@non_product_owner.id
+ raise "Non-product owner can't have a product role!" if
@existing_product.is_member?(@non_product_owner)
@member = users(:mcpierce)
@nonmember = users(:jdonuts)
@@ -248,6 +249,9 @@ class ProductsControllerTest < ActionController::TestCase
result = Product.find_by_name(@new_product[:name])
assert result, "Failed to create the actual product."
assert !result.rss_entries.empty?, "Product should have an RSS entry for its
creation."
+ assert result.is_member?(result.owner), "The product owner should have been made
a product team member."
+ role = ProductRole.find_by_product_id_and_user_id(result.id, result.owner_id)
+ assert !role.pending, "The role should not be pending."
end
# Ensures that anonymous users can't update a product.
@@ -280,16 +284,29 @@ class ProductsControllerTest < ActionController::TestCase
# Ensures that updating a product works for the project owner.
def test_update
count = @existing_product.rss_entries.size
- put :update,
- {:id => @existing_product.id,
- :product => @product_update},
- {:user_id => @project_owner.id}
+ put :update,{:id => @existing_product.id,:product => @product_update},{:user_id
=> @project_owner.id}
+
+ assert_redirected_to product_path(@existing_product)
+ result = Product.find_by_id((a)existing_product.id)
+ assert_equal @product_update[:name], result.name, "Name should have been
updaed."
+ assert_equal count + 1, result.rss_entries.size, "Failed to add a new rss
entry."
+ assert result.is_member?(result.owner), "The product owner should not have lost
his role."
+ end
+
+ # Ensures that changing the product owner removes the old owner's product
+ # role and creates a role for the new owner.
+ def test_update_with_owner_change
+ count = @existing_product.rss_entries.size
+ old_owner = @existing_product.owner
+ @product_update[:owner_id] = @non_product_owner.id
+ put :update,{:id => @existing_product.id,:product => @product_update},{:user_id
=> @project_owner.id}
assert_redirected_to product_path(@existing_product)
result = Product.find_by_id((a)existing_product.id)
- assert_equal @product_update[:name], result.name,
- "Name should have been updaed."
+ assert_equal @product_update[:name], result.name, "Name should have been
updaed."
assert_equal count + 1, result.rss_entries.size, "Failed to add a new rss
entry."
+ assert result.is_member?(result.owner), "The owner should have a product
role."
+ assert !result.is_member?(old_owner), "The old owner's role should have been
removed."
end
# Ensures that anonymous users can't delete.
--
1.6.2.5