diff --git a/app/controllers/my_profile/manage_products_controller.rb b/app/controllers/my_profile/manage_products_controller.rb new file mode 100644 index 0000000..65dd4b3 --- /dev/null +++ b/app/controllers/my_profile/manage_products_controller.rb @@ -0,0 +1,47 @@ +class ManageProductsController < ApplicationController + needs_profile + + def index + @products = @profile.products + end + + def show + @product = @profile.products.find(params[:id]) + end + + def new + @product = @profile.products.build(params[:product]) + if request.post? + if @product.save + flash[:notice] = _('Product succesfully created') + redirect_to :action => 'show', :id => @product + else + flash[:notice] = _('Could not create the product') + end + end + end + + def edit + @product = @profile.products.find(params[:id]) + if request.post? + if @product.update_attributes(params[:product]) + flash[:notice] = _('Product succesfully updated') + redirect_back_or_default :action => 'show', :id => @product + else + flash[:notice] = _('Could not update the product') + end + end + end + + def destroy + @product = @profile.products.find(params[:id]) + if @product.destroy + flash[:notice] = _('Product succesfully removed') + redirect_back_or_default :action => 'index' + else + flash[:notice] = _('Could not remove the product') + redirect_back_or_default :action => 'show', :id => @product + end + end + +end diff --git a/app/helpers/manage_products_helper.rb b/app/helpers/manage_products_helper.rb new file mode 100644 index 0000000..dad5dd1 --- /dev/null +++ b/app/helpers/manage_products_helper.rb @@ -0,0 +1,2 @@ +module ManageProductsHelper +end diff --git a/app/models/enterprise.rb b/app/models/enterprise.rb index ecec2e9..8ae71ac 100644 --- a/app/models/enterprise.rb +++ b/app/models/enterprise.rb @@ -1,4 +1,5 @@ # An enterprise is a kind of organization. According to the system concept, # only enterprises can offer products and services. class Enterprise < Organization + has_many :products end diff --git a/app/models/product.rb b/app/models/product.rb new file mode 100644 index 0000000..2ea42f7 --- /dev/null +++ b/app/models/product.rb @@ -0,0 +1,6 @@ +class Product < ActiveRecord::Base + belongs_to :enterprise + belongs_to :products_category + + validates_uniqueness_of :name, :scope => :enterprise_id +end diff --git a/app/models/product_category.rb b/app/models/product_category.rb index 077ebe8..6964f0e 100644 --- a/app/models/product_category.rb +++ b/app/models/product_category.rb @@ -1,2 +1,3 @@ class ProductCategory < Category + has_many :products end diff --git a/config/environment.rb b/config/environment.rb index 39a7bc2..cf4881b 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -86,7 +86,7 @@ Comatose.configure do |config| config.default_filter = '[No Filter]' end -Comatose::AdminController.design :holder => 'environment' +#Comatose::AdminController.design :holder => 'environment' Comatose::AdminController.before_filter do |controller| # TODO: copy/paste; extract this into a method (see # app/controllers/application.rb) diff --git a/db/migrate/016_create_products.rb b/db/migrate/016_create_products.rb new file mode 100644 index 0000000..f53d03c --- /dev/null +++ b/db/migrate/016_create_products.rb @@ -0,0 +1,14 @@ +class CreateProducts < ActiveRecord::Migration + def self.up + create_table :products do |t| + t.column :enterprise_id, :integer + t.column :product_category_id, :integer + t.column :name, :string + t.column :price, :decimal + end + end + + def self.down + drop_table :products + end +end diff --git a/test/fixtures/products.yml b/test/fixtures/products.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/products.yml @@ -0,0 +1,5 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +one: + id: 1 +two: + id: 2 diff --git a/test/functional/manage_products_controller_test.rb b/test/functional/manage_products_controller_test.rb new file mode 100644 index 0000000..75c6d2e --- /dev/null +++ b/test/functional/manage_products_controller_test.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'manage_products_controller' + +# Re-raise errors caught by the controller. +class ManageProductsController; def rescue_action(e) raise e end; end + +class ManageProductsControllerTest < Test::Unit::TestCase + def setup + @controller = ManageProductsController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + # Replace this with your real tests. + def test_truth + assert true + end +end diff --git a/test/unit/product_test.rb b/test/unit/product_test.rb new file mode 100644 index 0000000..3185a41 --- /dev/null +++ b/test/unit/product_test.rb @@ -0,0 +1,26 @@ +require File.dirname(__FILE__) + '/../test_helper' + +class ProductTest < Test::Unit::TestCase + + should 'create product' do + assert_difference Product, :count do + p = Product.new(:name => 'test product') + assert p.save + end + end + + should 'destroy product' do + p = Product.create(:name => 'test product') + assert_difference Product, :count, -1 do + p.destroy + end + end + + should 'name be unique' do + Product.create(:name => 'test product') + assert_no_difference Product, :count do + p = Product.new(:name => 'test product') + assert !p.save + end + end +end -- libgit2 0.21.2