diff --git a/app/controllers/my_profile/manage_products_controller.rb b/app/controllers/my_profile/manage_products_controller.rb
index 65dd4b3..9782432 100644
--- a/app/controllers/my_profile/manage_products_controller.rb
+++ b/app/controllers/my_profile/manage_products_controller.rb
@@ -1,6 +1,8 @@
class ManageProductsController < ApplicationController
needs_profile
+ protect 'manage_products', :profile
+
def index
@products = @profile.products
end
@@ -43,5 +45,5 @@ class ManageProductsController < ApplicationController
redirect_back_or_default :action => 'show', :id => @product
end
end
-
+
end
diff --git a/app/models/product.rb b/app/models/product.rb
index 2ea42f7..02a4e51 100644
--- a/app/models/product.rb
+++ b/app/models/product.rb
@@ -1,6 +1,7 @@
class Product < ActiveRecord::Base
belongs_to :enterprise
- belongs_to :products_category
+ belongs_to :product_category
+ validates_presence_of :name
validates_uniqueness_of :name, :scope => :enterprise_id
end
diff --git a/app/views/manage_products/_form.rhtml b/app/views/manage_products/_form.rhtml
new file mode 100644
index 0000000..033a0c3
--- /dev/null
+++ b/app/views/manage_products/_form.rhtml
@@ -0,0 +1,9 @@
+<%= error_messages_for :product %>
+
+<% form_for :product, @product, :url => {:action => mode} do |f| %>
+ <%= _('Name:') %> <%= f.text_field :name %>
+ <%= _('Price:') %> <%= f.text_field :price %>
+ <%= _('Description:') %>
<%= f.text_area :description %>
+ <%= _('Category:') %> <%= f.select :product_category_id,ProductCategory.find(:all).map{|pc|[pc.name,pc.id]}, {:include_blank => true} %>
+ <%= submit_tag mode == 'new' ? _('Create product') : _('Save changes') %>
+<% end %>
diff --git a/app/views/manage_products/edit.rhtml b/app/views/manage_products/edit.rhtml
new file mode 100644
index 0000000..723e6d9
--- /dev/null
+++ b/app/views/manage_products/edit.rhtml
@@ -0,0 +1,3 @@
+
<%= _('Editing') %> <%= @product.name %>
+
+<%= render :partial => 'form', :locals => {:mode => 'edit'} %>
diff --git a/app/views/manage_products/index.rhtml b/app/views/manage_products/index.rhtml
new file mode 100644
index 0000000..d5c4c98
--- /dev/null
+++ b/app/views/manage_products/index.rhtml
@@ -0,0 +1,10 @@
+ <%=_('Listing products') %>
+
+ <%= link_to _('New product'), :action => 'new' %>
+
+<% @products.each do |product| %>
+ <%= link_to product.name, :action => 'show', :id => product %>
+ <%= _('Price: ') %> <%= product.price %>
+ <%= link_to _('edit'), :action => 'edit', :id => product %>
+ <%= link_to _('destroy'), :action => 'destroy', :id => product %>
+<% end %>
diff --git a/app/views/manage_products/new.rhtml b/app/views/manage_products/new.rhtml
new file mode 100644
index 0000000..e3902cb
--- /dev/null
+++ b/app/views/manage_products/new.rhtml
@@ -0,0 +1,3 @@
+ <%= _('New product') %>
+
+<%= render :partial => 'form', :locals => {:mode => 'new'} %>
diff --git a/app/views/manage_products/show.rhtml b/app/views/manage_products/show.rhtml
new file mode 100644
index 0000000..2734ee7
--- /dev/null
+++ b/app/views/manage_products/show.rhtml
@@ -0,0 +1,10 @@
+ <%= @product.name %>
+
+ <%= _('Price: ') %> <%= @product.price %>
+ <%= _('Description: ') %> <%= @product.description %>
+
+
+<%= link_to _('edit'), :action => 'edit', :id => @product %>
+<%= link_to _('destroy'), :action => 'destroy', :id => @product %>
+
+<%= link_to _('back'), :action => 'index' %>
diff --git a/db/migrate/016_create_products.rb b/db/migrate/016_create_products.rb
index f53d03c..d90a1f6 100644
--- a/db/migrate/016_create_products.rb
+++ b/db/migrate/016_create_products.rb
@@ -5,6 +5,7 @@ class CreateProducts < ActiveRecord::Migration
t.column :product_category_id, :integer
t.column :name, :string
t.column :price, :decimal
+ t.column :description, :text
end
end
diff --git a/test/functional/manage_products_controller_test.rb b/test/functional/manage_products_controller_test.rb
index 75c6d2e..cb8cce4 100644
--- a/test/functional/manage_products_controller_test.rb
+++ b/test/functional/manage_products_controller_test.rb
@@ -5,14 +5,103 @@ require 'manage_products_controller'
class ManageProductsController; def rescue_action(e) raise e end; end
class ManageProductsControllerTest < Test::Unit::TestCase
+ all_fixtures
def setup
@controller = ManageProductsController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
+ @enterprise = Enterprise.create(:name => 'teste', :identifier => 'test_ent')
+ @user = create_user_with_permission('test_user', 'manage_products', @enterprise)
+ login_as :test_user
end
- # Replace this with your real tests.
- def test_truth
- assert true
+ should "not have permission" do
+ u = create_user('user_test')
+ login_as :user_test
+ get 'index', :profile => @enterprise.identifier
+ assert :success
+ assert_template 'access_denied.rhtml'
end
+
+ should "get index" do
+ get 'index', :profile => @enterprise.identifier
+ assert_response :success
+ assert assigns(:products)
+ end
+
+ should "get new form" do
+ get 'new', :profile => @enterprise.identifier
+ assert_response :success
+ assert assigns(:product)
+ assert_template 'new'
+ assert_tag :tag => 'form', :attributes => { :action => /new/ }
+ end
+
+ should "create new product" do
+ assert_difference Product, :count do
+ post 'new', :profile => @enterprise.identifier, :product => {:name => 'test product'}
+ assert_response :redirect
+ assert assigns(:product)
+ assert ! assigns(:product).new_record?
+ end
+ end
+
+ should "not create invalid product" do
+ assert_no_difference Product, :count do
+ post 'new', :profile => @enterprise.identifier, :product => {:price => 'test product'}
+ assert_response :success
+ assert assigns(:product)
+ assert assigns(:product).new_record?
+ end
+ end
+
+ should "get edit form" do
+ p = @enterprise.products.create(:name => 'test product')
+ get 'edit', :profile => @enterprise.identifier, :id => p.id
+ assert_response :success
+ assert assigns(:product)
+ assert_template 'edit'
+ assert_tag :tag => 'form', :attributes => { :action => /edit/ }
+ end
+
+ should "edit product" do
+ p = @enterprise.products.create(:name => 'test product')
+ post 'edit', :profile => @enterprise.identifier, :product => {:name => 'new test product'}, :id => p.id
+ assert_response :redirect
+ assert assigns(:product)
+ assert ! assigns(:product).new_record?
+ assert_equal p, Product.find_by_name('new test product')
+ end
+
+ should "not edit to invalid parameters" do
+ p = @enterprise.products.create(:name => 'test product')
+ post 'edit', :profile => @enterprise.identifier, :product => {:name => ''}, :id => p.id
+ assert_response :success
+ assert assigns(:product)
+ assert ! assigns(:product).valid?
+ end
+
+ should "destroy product" do
+ p = @enterprise.products.create(:name => 'test product')
+ assert_difference Product, :count, -1 do
+ post 'destroy', :profile => @enterprise.identifier, :id => p.id
+ assert_response :redirect
+ assert_redirected_to :action => 'index'
+ assert assigns(:product)
+ assert ! Product.find_by_name('test product')
+ end
+ end
+
+ should "fail to destroy product" do
+ p = @enterprise.products.create(:name => 'test product')
+ Product.any_instance.stubs(:destroy).returns(false)
+ assert_no_difference Product, :count do
+ post 'destroy', :profile => @enterprise.identifier, :id => p.id
+ assert_response :redirect
+ assert_redirected_to :action => 'show'
+ assert assigns(:product)
+ assert Product.find_by_name('test product')
+ end
+ end
+
end
--
libgit2 0.21.2