Products are listed by the specified project id. If no project id is
supplied then the named scope includes all projects.
Signed-off-by: Darryl L. Pierce <mcpierce(a)gmail.com>
---
app/controllers/products_controller.rb | 18 +++++++-----------
app/models/product.rb | 6 ++++++
2 files changed, 13 insertions(+), 11 deletions(-)
diff --git a/app/controllers/products_controller.rb
b/app/controllers/products_controller.rb
index 0eaae5b..aebb2d6 100644
--- a/app/controllers/products_controller.rb
+++ b/app/controllers/products_controller.rb
@@ -26,21 +26,17 @@ class ProductsController < ApplicationController
# GET /products
def index
- @project = Project.find_by_id(params[:project])
+ project_id = params[:project]
+
+ @products = Product.for_project(project_id).paginate(:page => params[:page],
+ :per_page => 10,
+ :order => 'name
ASC')
- if @project
+ if project_id
+ @project = Project.find_by_id(project_id)
@title = "Products For #{(a)project.name}"
- @products = Product.paginate(
- :page => params[:page],
- :per_page => 10,
- :conditions => ['project_id = ?', @project.id],
- :order => 'name ASC')
else
@title = "All Products"
- @products = Product.paginate(
- :page => params[:page],
- :per_page => 10,
- :order => 'project_id, name ASC')
end
respond_to do |format|
diff --git a/app/models/product.rb b/app/models/product.rb
index abc8d99..7f612ad 100644
--- a/app/models/product.rb
+++ b/app/models/product.rb
@@ -46,6 +46,12 @@ class Product < ActiveRecord::Base
has_many :user_stories
has_many :sprints
+ named_scope :for_project, lambda { |project_id|
+ {
+ :conditions => project_id ? ["project_id = ?", project_id] : []
+ }
+ }
+
# Returns the set of roles that are active.
def active_roles
result = []
--
1.6.0.6