Commit 74da6b9ddc01be4ce5661e1b9256a36c566a347a
1 parent
4c7be69e
Exists in
master
and in
23 other branches
ActionItem28: initial implemetation of products still without the views
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@919 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
10 changed files
with
121 additions
and
1 deletions
Show diff stats
app/controllers/my_profile/manage_products_controller.rb
0 → 100644
| @@ -0,0 +1,47 @@ | @@ -0,0 +1,47 @@ | ||
| 1 | +class ManageProductsController < ApplicationController | ||
| 2 | + needs_profile | ||
| 3 | + | ||
| 4 | + def index | ||
| 5 | + @products = @profile.products | ||
| 6 | + end | ||
| 7 | + | ||
| 8 | + def show | ||
| 9 | + @product = @profile.products.find(params[:id]) | ||
| 10 | + end | ||
| 11 | + | ||
| 12 | + def new | ||
| 13 | + @product = @profile.products.build(params[:product]) | ||
| 14 | + if request.post? | ||
| 15 | + if @product.save | ||
| 16 | + flash[:notice] = _('Product succesfully created') | ||
| 17 | + redirect_to :action => 'show', :id => @product | ||
| 18 | + else | ||
| 19 | + flash[:notice] = _('Could not create the product') | ||
| 20 | + end | ||
| 21 | + end | ||
| 22 | + end | ||
| 23 | + | ||
| 24 | + def edit | ||
| 25 | + @product = @profile.products.find(params[:id]) | ||
| 26 | + if request.post? | ||
| 27 | + if @product.update_attributes(params[:product]) | ||
| 28 | + flash[:notice] = _('Product succesfully updated') | ||
| 29 | + redirect_back_or_default :action => 'show', :id => @product | ||
| 30 | + else | ||
| 31 | + flash[:notice] = _('Could not update the product') | ||
| 32 | + end | ||
| 33 | + end | ||
| 34 | + end | ||
| 35 | + | ||
| 36 | + def destroy | ||
| 37 | + @product = @profile.products.find(params[:id]) | ||
| 38 | + if @product.destroy | ||
| 39 | + flash[:notice] = _('Product succesfully removed') | ||
| 40 | + redirect_back_or_default :action => 'index' | ||
| 41 | + else | ||
| 42 | + flash[:notice] = _('Could not remove the product') | ||
| 43 | + redirect_back_or_default :action => 'show', :id => @product | ||
| 44 | + end | ||
| 45 | + end | ||
| 46 | + | ||
| 47 | +end |
app/models/enterprise.rb
| 1 | # An enterprise is a kind of organization. According to the system concept, | 1 | # An enterprise is a kind of organization. According to the system concept, |
| 2 | # only enterprises can offer products and services. | 2 | # only enterprises can offer products and services. |
| 3 | class Enterprise < Organization | 3 | class Enterprise < Organization |
| 4 | + has_many :products | ||
| 4 | end | 5 | end |
app/models/product_category.rb
config/environment.rb
| @@ -86,7 +86,7 @@ Comatose.configure do |config| | @@ -86,7 +86,7 @@ Comatose.configure do |config| | ||
| 86 | 86 | ||
| 87 | config.default_filter = '[No Filter]' | 87 | config.default_filter = '[No Filter]' |
| 88 | end | 88 | end |
| 89 | -Comatose::AdminController.design :holder => 'environment' | 89 | +#Comatose::AdminController.design :holder => 'environment' |
| 90 | Comatose::AdminController.before_filter do |controller| | 90 | Comatose::AdminController.before_filter do |controller| |
| 91 | # TODO: copy/paste; extract this into a method (see | 91 | # TODO: copy/paste; extract this into a method (see |
| 92 | # app/controllers/application.rb) | 92 | # app/controllers/application.rb) |
| @@ -0,0 +1,14 @@ | @@ -0,0 +1,14 @@ | ||
| 1 | +class CreateProducts < ActiveRecord::Migration | ||
| 2 | + def self.up | ||
| 3 | + create_table :products do |t| | ||
| 4 | + t.column :enterprise_id, :integer | ||
| 5 | + t.column :product_category_id, :integer | ||
| 6 | + t.column :name, :string | ||
| 7 | + t.column :price, :decimal | ||
| 8 | + end | ||
| 9 | + end | ||
| 10 | + | ||
| 11 | + def self.down | ||
| 12 | + drop_table :products | ||
| 13 | + end | ||
| 14 | +end |
| @@ -0,0 +1,18 @@ | @@ -0,0 +1,18 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | +require 'manage_products_controller' | ||
| 3 | + | ||
| 4 | +# Re-raise errors caught by the controller. | ||
| 5 | +class ManageProductsController; def rescue_action(e) raise e end; end | ||
| 6 | + | ||
| 7 | +class ManageProductsControllerTest < Test::Unit::TestCase | ||
| 8 | + def setup | ||
| 9 | + @controller = ManageProductsController.new | ||
| 10 | + @request = ActionController::TestRequest.new | ||
| 11 | + @response = ActionController::TestResponse.new | ||
| 12 | + end | ||
| 13 | + | ||
| 14 | + # Replace this with your real tests. | ||
| 15 | + def test_truth | ||
| 16 | + assert true | ||
| 17 | + end | ||
| 18 | +end |
| @@ -0,0 +1,26 @@ | @@ -0,0 +1,26 @@ | ||
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | ||
| 2 | + | ||
| 3 | +class ProductTest < Test::Unit::TestCase | ||
| 4 | + | ||
| 5 | + should 'create product' do | ||
| 6 | + assert_difference Product, :count do | ||
| 7 | + p = Product.new(:name => 'test product') | ||
| 8 | + assert p.save | ||
| 9 | + end | ||
| 10 | + end | ||
| 11 | + | ||
| 12 | + should 'destroy product' do | ||
| 13 | + p = Product.create(:name => 'test product') | ||
| 14 | + assert_difference Product, :count, -1 do | ||
| 15 | + p.destroy | ||
| 16 | + end | ||
| 17 | + end | ||
| 18 | + | ||
| 19 | + should 'name be unique' do | ||
| 20 | + Product.create(:name => 'test product') | ||
| 21 | + assert_no_difference Product, :count do | ||
| 22 | + p = Product.new(:name => 'test product') | ||
| 23 | + assert !p.save | ||
| 24 | + end | ||
| 25 | + end | ||
| 26 | +end |