Commit 6b0b91c8ce9d83b46b081c2aee129fb3a3c11764
1 parent
4c828ae2
Exists in
master
and in
28 other branches
ActionItem28: manage products controller tested and views added
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@930 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
9 changed files
with
133 additions
and
5 deletions
Show diff stats
app/controllers/my_profile/manage_products_controller.rb
1 | class ManageProductsController < ApplicationController | 1 | class ManageProductsController < ApplicationController |
2 | needs_profile | 2 | needs_profile |
3 | 3 | ||
4 | + protect 'manage_products', :profile | ||
5 | + | ||
4 | def index | 6 | def index |
5 | @products = @profile.products | 7 | @products = @profile.products |
6 | end | 8 | end |
@@ -43,5 +45,5 @@ class ManageProductsController < ApplicationController | @@ -43,5 +45,5 @@ class ManageProductsController < ApplicationController | ||
43 | redirect_back_or_default :action => 'show', :id => @product | 45 | redirect_back_or_default :action => 'show', :id => @product |
44 | end | 46 | end |
45 | end | 47 | end |
46 | - | 48 | + |
47 | end | 49 | end |
app/models/product.rb
1 | class Product < ActiveRecord::Base | 1 | class Product < ActiveRecord::Base |
2 | belongs_to :enterprise | 2 | belongs_to :enterprise |
3 | - belongs_to :products_category | 3 | + belongs_to :product_category |
4 | 4 | ||
5 | + validates_presence_of :name | ||
5 | validates_uniqueness_of :name, :scope => :enterprise_id | 6 | validates_uniqueness_of :name, :scope => :enterprise_id |
6 | end | 7 | end |
@@ -0,0 +1,9 @@ | @@ -0,0 +1,9 @@ | ||
1 | +<%= error_messages_for :product %> <br/> | ||
2 | + | ||
3 | +<% form_for :product, @product, :url => {:action => mode} do |f| %> | ||
4 | + <%= _('Name:') %> <%= f.text_field :name %><br/> | ||
5 | + <%= _('Price:') %> <%= f.text_field :price %><br/> | ||
6 | + <%= _('Description:') %><br/> <%= f.text_area :description %><br/> | ||
7 | + <%= _('Category:') %> <%= f.select :product_category_id,ProductCategory.find(:all).map{|pc|[pc.name,pc.id]}, {:include_blank => true} %><br/> | ||
8 | + <%= submit_tag mode == 'new' ? _('Create product') : _('Save changes') %> | ||
9 | +<% end %> |
@@ -0,0 +1,10 @@ | @@ -0,0 +1,10 @@ | ||
1 | +<h2> <%=_('Listing products') %> </h2> | ||
2 | + | ||
3 | +<p> <%= link_to _('New product'), :action => 'new' %> </p> | ||
4 | + | ||
5 | +<% @products.each do |product| %> | ||
6 | + <p> <b> <%= link_to product.name, :action => 'show', :id => product %> </b> | ||
7 | + <%= _('Price: ') %> <%= product.price %> | ||
8 | + <%= link_to _('edit'), :action => 'edit', :id => product %> | ||
9 | + <%= link_to _('destroy'), :action => 'destroy', :id => product %> </p> | ||
10 | +<% end %> |
@@ -0,0 +1,10 @@ | @@ -0,0 +1,10 @@ | ||
1 | +<h3> <%= @product.name %> </h3> | ||
2 | + | ||
3 | +<p> <%= _('Price: ') %> <%= @product.price %> </p> | ||
4 | +<p> <%= _('Description: ') %> <%= @product.description %> </p> | ||
5 | + | ||
6 | + | ||
7 | +<%= link_to _('edit'), :action => 'edit', :id => @product %> | ||
8 | +<%= link_to _('destroy'), :action => 'destroy', :id => @product %> | ||
9 | +<br/> | ||
10 | +<%= link_to _('back'), :action => 'index' %> |
db/migrate/016_create_products.rb
@@ -5,6 +5,7 @@ class CreateProducts < ActiveRecord::Migration | @@ -5,6 +5,7 @@ class CreateProducts < ActiveRecord::Migration | ||
5 | t.column :product_category_id, :integer | 5 | t.column :product_category_id, :integer |
6 | t.column :name, :string | 6 | t.column :name, :string |
7 | t.column :price, :decimal | 7 | t.column :price, :decimal |
8 | + t.column :description, :text | ||
8 | end | 9 | end |
9 | end | 10 | end |
10 | 11 |
test/functional/manage_products_controller_test.rb
@@ -5,14 +5,103 @@ require 'manage_products_controller' | @@ -5,14 +5,103 @@ require 'manage_products_controller' | ||
5 | class ManageProductsController; def rescue_action(e) raise e end; end | 5 | class ManageProductsController; def rescue_action(e) raise e end; end |
6 | 6 | ||
7 | class ManageProductsControllerTest < Test::Unit::TestCase | 7 | class ManageProductsControllerTest < Test::Unit::TestCase |
8 | + all_fixtures | ||
8 | def setup | 9 | def setup |
9 | @controller = ManageProductsController.new | 10 | @controller = ManageProductsController.new |
10 | @request = ActionController::TestRequest.new | 11 | @request = ActionController::TestRequest.new |
11 | @response = ActionController::TestResponse.new | 12 | @response = ActionController::TestResponse.new |
13 | + @enterprise = Enterprise.create(:name => 'teste', :identifier => 'test_ent') | ||
14 | + @user = create_user_with_permission('test_user', 'manage_products', @enterprise) | ||
15 | + login_as :test_user | ||
12 | end | 16 | end |
13 | 17 | ||
14 | - # Replace this with your real tests. | ||
15 | - def test_truth | ||
16 | - assert true | 18 | + should "not have permission" do |
19 | + u = create_user('user_test') | ||
20 | + login_as :user_test | ||
21 | + get 'index', :profile => @enterprise.identifier | ||
22 | + assert :success | ||
23 | + assert_template 'access_denied.rhtml' | ||
17 | end | 24 | end |
25 | + | ||
26 | + should "get index" do | ||
27 | + get 'index', :profile => @enterprise.identifier | ||
28 | + assert_response :success | ||
29 | + assert assigns(:products) | ||
30 | + end | ||
31 | + | ||
32 | + should "get new form" do | ||
33 | + get 'new', :profile => @enterprise.identifier | ||
34 | + assert_response :success | ||
35 | + assert assigns(:product) | ||
36 | + assert_template 'new' | ||
37 | + assert_tag :tag => 'form', :attributes => { :action => /new/ } | ||
38 | + end | ||
39 | + | ||
40 | + should "create new product" do | ||
41 | + assert_difference Product, :count do | ||
42 | + post 'new', :profile => @enterprise.identifier, :product => {:name => 'test product'} | ||
43 | + assert_response :redirect | ||
44 | + assert assigns(:product) | ||
45 | + assert ! assigns(:product).new_record? | ||
46 | + end | ||
47 | + end | ||
48 | + | ||
49 | + should "not create invalid product" do | ||
50 | + assert_no_difference Product, :count do | ||
51 | + post 'new', :profile => @enterprise.identifier, :product => {:price => 'test product'} | ||
52 | + assert_response :success | ||
53 | + assert assigns(:product) | ||
54 | + assert assigns(:product).new_record? | ||
55 | + end | ||
56 | + end | ||
57 | + | ||
58 | + should "get edit form" do | ||
59 | + p = @enterprise.products.create(:name => 'test product') | ||
60 | + get 'edit', :profile => @enterprise.identifier, :id => p.id | ||
61 | + assert_response :success | ||
62 | + assert assigns(:product) | ||
63 | + assert_template 'edit' | ||
64 | + assert_tag :tag => 'form', :attributes => { :action => /edit/ } | ||
65 | + end | ||
66 | + | ||
67 | + should "edit product" do | ||
68 | + p = @enterprise.products.create(:name => 'test product') | ||
69 | + post 'edit', :profile => @enterprise.identifier, :product => {:name => 'new test product'}, :id => p.id | ||
70 | + assert_response :redirect | ||
71 | + assert assigns(:product) | ||
72 | + assert ! assigns(:product).new_record? | ||
73 | + assert_equal p, Product.find_by_name('new test product') | ||
74 | + end | ||
75 | + | ||
76 | + should "not edit to invalid parameters" do | ||
77 | + p = @enterprise.products.create(:name => 'test product') | ||
78 | + post 'edit', :profile => @enterprise.identifier, :product => {:name => ''}, :id => p.id | ||
79 | + assert_response :success | ||
80 | + assert assigns(:product) | ||
81 | + assert ! assigns(:product).valid? | ||
82 | + end | ||
83 | + | ||
84 | + should "destroy product" do | ||
85 | + p = @enterprise.products.create(:name => 'test product') | ||
86 | + assert_difference Product, :count, -1 do | ||
87 | + post 'destroy', :profile => @enterprise.identifier, :id => p.id | ||
88 | + assert_response :redirect | ||
89 | + assert_redirected_to :action => 'index' | ||
90 | + assert assigns(:product) | ||
91 | + assert ! Product.find_by_name('test product') | ||
92 | + end | ||
93 | + end | ||
94 | + | ||
95 | + should "fail to destroy product" do | ||
96 | + p = @enterprise.products.create(:name => 'test product') | ||
97 | + Product.any_instance.stubs(:destroy).returns(false) | ||
98 | + assert_no_difference Product, :count do | ||
99 | + post 'destroy', :profile => @enterprise.identifier, :id => p.id | ||
100 | + assert_response :redirect | ||
101 | + assert_redirected_to :action => 'show' | ||
102 | + assert assigns(:product) | ||
103 | + assert Product.find_by_name('test product') | ||
104 | + end | ||
105 | + end | ||
106 | + | ||
18 | end | 107 | end |