From f36a52e1b05da6ba88a49ec0db2198a9f01548d3 Mon Sep 17 00:00:00 2001 From: MoisesMachado Date: Sat, 1 Dec 2007 20:57:53 +0000 Subject: [PATCH] ActionItem28: added consumed products management --- app/controllers/public/category_controller.rb | 11 +++++++++++ app/controllers/public/product_category_viewer_controller.rb | 11 ----------- app/helpers/application_helper.rb | 9 +++++++++ app/models/consumption.rb | 4 ++++ app/models/product.rb | 2 ++ app/views/catalog/show.rhtml | 2 +- app/views/category/view.rhtml | 11 +---------- app/views/consumed_products/index.rhtml | 8 ++++++++ app/views/consumed_products/new.rhtml | 6 ++++++ app/views/consumed_products/show.rhtml | 10 ++++++++++ app/views/manage_products/show.rhtml | 4 ++-- db/migrate/018_create_consumptions.rb | 12 ++++++++++++ test/fixtures/consumptions.yml | 5 +++++ test/functional/consumed_products_controller_test.rb | 18 ++++++++++++++++++ 14 files changed, 89 insertions(+), 24 deletions(-) delete mode 100644 app/controllers/public/product_category_viewer_controller.rb create mode 100644 app/models/consumption.rb create mode 100644 app/views/consumed_products/index.rhtml create mode 100644 app/views/consumed_products/new.rhtml create mode 100644 app/views/consumed_products/show.rhtml create mode 100644 db/migrate/018_create_consumptions.rb create mode 100644 test/fixtures/consumptions.yml create mode 100644 test/functional/consumed_products_controller_test.rb diff --git a/app/controllers/public/category_controller.rb b/app/controllers/public/category_controller.rb index 1250e43..1ad73a9 100644 --- a/app/controllers/public/category_controller.rb +++ b/app/controllers/public/category_controller.rb @@ -23,7 +23,18 @@ class CategoryController < ApplicationController # view the summary of one category def view + send(@category.class.name.underscore.to_sym) # TODO: load articles, documents, etc so the view can list them. end + + protected + def product_category + @products = Product.find(:all, :conditions => ['product_category_id = ?', @category.id]) + @enterprises = Enterprise.find(:all, :conditions => ['products.id in (?)', @products.map(&:id)], :include => :products) + end + + def category + end + end diff --git a/app/controllers/public/product_category_viewer_controller.rb b/app/controllers/public/product_category_viewer_controller.rb deleted file mode 100644 index b228714..0000000 --- a/app/controllers/public/product_category_viewer_controller.rb +++ /dev/null @@ -1,11 +0,0 @@ -class ProductCategoryViewerController < ApplicationController - def index - @categories = ProductCategory.find(:all) - end - - def view_category - @category = ProductCategory.find(params[:id]) - @products = Product.find(:all, :conditions => ['product_category_id = ?', params[:id]]) - @enterprises = Enterprise.find(:all, :conditions => ['products.id in (?)', @products.map(&:id)], :include => :products) - end -end diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 797d4bd..b3c3d75 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -334,4 +334,13 @@ module ApplicationHelper concat(content_tag('div', capture(&block) + tag('br', :style => 'clear: left;'), options), block.binding) end + def link_to_category(category) + return _('Uncategorized product') unless category + link_to category.name, :controller => 'category', :action => 'view', :path => category.path.split('/') + end + + def link_to_product(product) + return _('No product') unless product + link_to product.name, :controller => 'catalog', :action => 'show', :id => product, :profile => product.enterprise.identifier + end end diff --git a/app/models/consumption.rb b/app/models/consumption.rb new file mode 100644 index 0000000..d2c182f --- /dev/null +++ b/app/models/consumption.rb @@ -0,0 +1,4 @@ +class Consumption < ActiveRecord::Base + belongs_to :profile + belongs_to :product_category +end diff --git a/app/models/product.rb b/app/models/product.rb index b178805..ebbbc0d 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -10,6 +10,8 @@ class Product < ActiveRecord::Base after_update :save_image + acts_as_searchable :fields => [:name, :description] + def image_builder=(img) if image && image.id == img[:id] image.attributes = img diff --git a/app/views/catalog/show.rhtml b/app/views/catalog/show.rhtml index e411321..557904a 100644 --- a/app/views/catalog/show.rhtml +++ b/app/views/catalog/show.rhtml @@ -3,4 +3,4 @@ <%= image_tag @product.image.public_filename if @product.image %>
<%= @product.price %>
<%= @product.description %>
-<%= @product.category_name %>
+<%= link_to_category(@product.product_category) %>
diff --git a/app/views/category/view.rhtml b/app/views/category/view.rhtml index dbd43c5..7a9f435 100644 --- a/app/views/category/view.rhtml +++ b/app/views/category/view.rhtml @@ -1,12 +1,3 @@

<%= _('Category: %s') % @category.full_name %>

-<%# FIXME %> -

-This page will list everything (articles, documents, photos, etc) that is -related to <%= @category.full_name %>. Actually generating this content is not -implement yet, though. -

- -

-And yes, this placeholder text is not translated. -

+<%= render :partial => @category.class.name.underscore %> diff --git a/app/views/consumed_products/index.rhtml b/app/views/consumed_products/index.rhtml new file mode 100644 index 0000000..a0295c0 --- /dev/null +++ b/app/views/consumed_products/index.rhtml @@ -0,0 +1,8 @@ +

<%=_('Listing products') %>

+ +

<%= link_to _('Add product'), :action => 'new' %>

+ +<% @consumptions.each do |consumption| %> +

<%= link_to consumption.product_category.name %> + <%= link_to _('destroy'), :action => 'destroy', :id => consumption %>
+<% end %> diff --git a/app/views/consumed_products/new.rhtml b/app/views/consumed_products/new.rhtml new file mode 100644 index 0000000..8068071 --- /dev/null +++ b/app/views/consumed_products/new.rhtml @@ -0,0 +1,6 @@ +

<%= _('Add product') %>

+ +<% form_for :consumption, @consuption do |f| %> + <%= _('Product: ') %> <%= f.select "product_category_id", ProductCategory.find(:all).map{|pc| [pc.name, pc.id]} %> + <%= display_submit_tag _('Add product') %> +<% end %> diff --git a/app/views/consumed_products/show.rhtml b/app/views/consumed_products/show.rhtml new file mode 100644 index 0000000..10a3a4d --- /dev/null +++ b/app/views/consumed_products/show.rhtml @@ -0,0 +1,10 @@ +

<%= @product.name %>

+ +

<%= image_tag @product.image.public_filename if @product.image %>

+

<%= _('Price: ') %> <%= @product.price %>

+

<%= _('Description: ') %> <%= @product.description %>

+

<%= _('Category: ') %> <%= @product.product_category ? @product.product_category.name : _('Uncategorized product') %>

+ +<%= link_to _('destroy'), :action => 'destroy', :id => @product %> +
+<%= link_to _('back'), :action => 'index' %> diff --git a/app/views/manage_products/show.rhtml b/app/views/manage_products/show.rhtml index 85fbe20..6d375a3 100644 --- a/app/views/manage_products/show.rhtml +++ b/app/views/manage_products/show.rhtml @@ -1,9 +1,9 @@

<%= @product.name %>

-

<%= image_tag @product.image.public_filename %>

+

<%= image_tag @product.image.public_filename if @product.image %>

<%= _('Price: ') %> <%= @product.price %>

<%= _('Description: ') %> <%= @product.description %>

-

<%= _('Category: ') %> <%= @product.product_category ? @product.product_category.name : _('Uncategorized product') %>

+

<%= _('Category: ') %> <%= link_to_category(@product.product_category) %>

<%= link_to _('edit'), :action => 'edit', :id => @product %> <%= link_to _('destroy'), :action => 'destroy', :id => @product %> diff --git a/db/migrate/018_create_consumptions.rb b/db/migrate/018_create_consumptions.rb new file mode 100644 index 0000000..2507d05 --- /dev/null +++ b/db/migrate/018_create_consumptions.rb @@ -0,0 +1,12 @@ +class CreateConsumptions < ActiveRecord::Migration + def self.up + create_table :consumptions do |t| + t.column :product_category_id, :integer + t.column :profile_id, :integer + end + end + + def self.down + drop_table :consumptions + end +end diff --git a/test/fixtures/consumptions.yml b/test/fixtures/consumptions.yml new file mode 100644 index 0000000..b49c4eb --- /dev/null +++ b/test/fixtures/consumptions.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/consumed_products_controller_test.rb b/test/functional/consumed_products_controller_test.rb new file mode 100644 index 0000000..d7e76fb --- /dev/null +++ b/test/functional/consumed_products_controller_test.rb @@ -0,0 +1,18 @@ +require File.dirname(__FILE__) + '/../test_helper' +require 'consumed_products_controller' + +# Re-raise errors caught by the controller. +class ConsumedProductsController; def rescue_action(e) raise e end; end + +class ConsumedProductsControllerTest < Test::Unit::TestCase + def setup + @controller = ConsumedProductsController.new + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + # Replace this with your real tests. + def test_truth + assert true + end +end -- libgit2 0.21.2