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 | 1 | class ManageProductsController < ApplicationController |
| 2 | 2 | needs_profile |
| 3 | 3 | |
| 4 | + protect 'manage_products', :profile | |
| 5 | + | |
| 4 | 6 | def index |
| 5 | 7 | @products = @profile.products |
| 6 | 8 | end |
| ... | ... | @@ -43,5 +45,5 @@ class ManageProductsController < ApplicationController |
| 43 | 45 | redirect_back_or_default :action => 'show', :id => @product |
| 44 | 46 | end |
| 45 | 47 | end |
| 46 | - | |
| 48 | + | |
| 47 | 49 | end | ... | ... |
app/models/product.rb
| ... | ... | @@ -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 @@ |
| 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 @@ |
| 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
test/functional/manage_products_controller_test.rb
| ... | ... | @@ -5,14 +5,103 @@ require 'manage_products_controller' |
| 5 | 5 | class ManageProductsController; def rescue_action(e) raise e end; end |
| 6 | 6 | |
| 7 | 7 | class ManageProductsControllerTest < Test::Unit::TestCase |
| 8 | + all_fixtures | |
| 8 | 9 | def setup |
| 9 | 10 | @controller = ManageProductsController.new |
| 10 | 11 | @request = ActionController::TestRequest.new |
| 11 | 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 | 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 | 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 | 107 | end | ... | ... |