If the project is not approved, then the add product link is not shown
on the page. If the controller method is invoked directly then an error
is shown.
Signed-off-by: Darryl L. Pierce <mcpierce(a)gmail.com>
---
app/controllers/products_controller.rb | 17 +++++++++++++++--
app/views/products/_edit.html.erb | 2 +-
app/views/projects/show.html.erb | 2 +-
test/functional/products_controller_test.rb | 19 ++++++++++++++-----
4 files changed, 31 insertions(+), 9 deletions(-)
diff --git a/app/controllers/products_controller.rb
b/app/controllers/products_controller.rb
index 3d394c9..0eaae5b 100644
--- a/app/controllers/products_controller.rb
+++ b/app/controllers/products_controller.rb
@@ -20,8 +20,9 @@
#
class ProductsController < ApplicationController
before_filter :authenticated, :except => [:index, :show]
- before_filter :load_project, :only => [:new]
- before_filter :load_product, :only => [:show, :edit, :update, :join]
+ before_filter :load_project, :only => [:new, :create]
+ before_filter :is_approved, :only => [:new, :create]
+ before_filter :load_product, :only => [:show, :edit, :update, :join]
# GET /products
def index
@@ -95,6 +96,7 @@ class ProductsController < ApplicationController
if params[:product]
Product.transaction do
@product = Product.new(params[:product])
+ @product.project = @project
if @product.project.can_create_products?(@user)
if @product.save
@@ -168,6 +170,15 @@ class ProductsController < ApplicationController
end
end
+ def is_approved
+ unless @project.approved
+ flash[:error] = "A project must be approved before it can have
products."
+ respond_to do |format|
+ format.html {redirect_to project_path(@project)}
+ end
+ end
+ end
+
def load_product
@product = Product.find_by_id(params[:id])
@@ -176,6 +187,8 @@ class ProductsController < ApplicationController
flash[:message] = 'Invalid product specified.'
format.html { redirect_to error_path }
end
+ else
+ @project = @product.project
end
end
end
diff --git a/app/views/products/_edit.html.erb b/app/views/products/_edit.html.erb
index e554591..06272d3 100644
--- a/app/views/products/_edit.html.erb
+++ b/app/views/products/_edit.html.erb
@@ -8,7 +8,7 @@
<% hidden_field_tag "id", @product.id %>
<% end %>
- <%= hidden_field :product, :project_id %>
+ <%= hidden_field_tag :project, @project.id %>
<table class="edit">
<colgroup>
diff --git a/app/views/projects/show.html.erb b/app/views/projects/show.html.erb
index 8d93f4c..8308d80 100644
--- a/app/views/projects/show.html.erb
+++ b/app/views/projects/show.html.erb
@@ -19,7 +19,7 @@
<%= link_to(image_tag("icons/edit.png", :title => "Edit this
project..."),
edit_project_path(@project)) if @project.can_edit?(@user) %>
<%= link_to(image_tag("icons/new.png", :title => "Create a
new product..."),
- new_product_path(:project => @project)) if
@project.can_create_products?(@user) %>
+ new_product_path(:project => @project)) if
@project.can_create_products?(@user) && @project.approved %>
</td>
</tr>
diff --git a/test/functional/products_controller_test.rb
b/test/functional/products_controller_test.rb
index f690fe6..2ebbc1e 100644
--- a/test/functional/products_controller_test.rb
+++ b/test/functional/products_controller_test.rb
@@ -27,6 +27,8 @@ class ProductsControllerTest < ActionController::TestCase
def setup
@project = projects(:projxp)
+ @unapproved_project = projects(:unapproved_project)
+ raise "Unapproved project cannot be approved!" if
@unapproved_project.approved
@project_owner = @project.owner
@non_project_owner = users(:mcpierce)
@@ -44,12 +46,10 @@ class ProductsControllerTest < ActionController::TestCase
@new_product = {
:name => 'product1',
- :project_id => @project.id,
:owner_id => @project_owner.id,
:description => 'This is a new product.'
}
@product_update = {
- :project_id => @project.id,
:id => @existing_product.id.to_f,
:name => @existing_product.name + " Gold"
}
@@ -63,7 +63,7 @@ class ProductsControllerTest < ActionController::TestCase
assert_response :success
assert assigns['products'], "Failed to load any products."
- assigns['products'].each do |product|
+ assigns['products'].each do |product|
assert_equal @project.id, product.project_id,
"Failed to load the right set of products."
end
@@ -142,6 +142,15 @@ class ProductsControllerTest < ActionController::TestCase
assert_redirected_to project_path(@project)
end
+ # Ensures that a product cannot be created for an unapproved project.
+ def test_new_for_unapproved_project
+ get :new,
+ {:project => @unapproved_project.id },
+ { :user_id => @unapproved_project.owner_id}
+
+ assert_redirected_to project_path(@unapproved_project)
+ end
+
# Ensures that creating a project works as expected.
def test_new
get :new,
@@ -227,7 +236,7 @@ class ProductsControllerTest < ActionController::TestCase
# Ensures that only the project owner can call the create method.
def test_create_as_non_project_owner
put :create,
- {:product => @new_product},
+ {:product => @new_product, :project => @project.id},
{:user_id => @non_project_owner.id}
assert_redirected_to project_path(@project)
@@ -236,7 +245,7 @@ class ProductsControllerTest < ActionController::TestCase
# Ensures that creating a product work as expected.
def test_create
put :create,
- {:product => @new_product},
+ {:product => @new_product, :project => @project.id},
{:user_id => @project_owner.id}
assert_redirected_to product_path(assigns['product'])
--
1.6.0.6