Commit f36a52e1b05da6ba88a49ec0db2198a9f01548d3

Authored by MoisesMachado
1 parent 3a8dd51e

ActionItem28: added consumed products management


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1019 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/category_controller.rb
... ... @@ -23,7 +23,18 @@ class CategoryController < ApplicationController
23 23  
24 24 # view the summary of one category
25 25 def view
  26 + send(@category.class.name.underscore.to_sym)
26 27 # TODO: load articles, documents, etc so the view can list them.
27 28 end
  29 +
  30 + protected
  31 + def product_category
  32 + @products = Product.find(:all, :conditions => ['product_category_id = ?', @category.id])
  33 + @enterprises = Enterprise.find(:all, :conditions => ['products.id in (?)', @products.map(&:id)], :include => :products)
  34 + end
  35 +
  36 + def category
  37 + end
  38 +
28 39  
29 40 end
... ...
app/controllers/public/product_category_viewer_controller.rb
... ... @@ -1,11 +0,0 @@
1   -class ProductCategoryViewerController < ApplicationController
2   - def index
3   - @categories = ProductCategory.find(:all)
4   - end
5   -
6   - def view_category
7   - @category = ProductCategory.find(params[:id])
8   - @products = Product.find(:all, :conditions => ['product_category_id = ?', params[:id]])
9   - @enterprises = Enterprise.find(:all, :conditions => ['products.id in (?)', @products.map(&:id)], :include => :products)
10   - end
11   -end
app/helpers/application_helper.rb
... ... @@ -334,4 +334,13 @@ module ApplicationHelper
334 334 concat(content_tag('div', capture(&block) + tag('br', :style => 'clear: left;'), options), block.binding)
335 335 end
336 336  
  337 + def link_to_category(category)
  338 + return _('Uncategorized product') unless category
  339 + link_to category.name, :controller => 'category', :action => 'view', :path => category.path.split('/')
  340 + end
  341 +
  342 + def link_to_product(product)
  343 + return _('No product') unless product
  344 + link_to product.name, :controller => 'catalog', :action => 'show', :id => product, :profile => product.enterprise.identifier
  345 + end
337 346 end
... ...
app/models/consumption.rb 0 → 100644
... ... @@ -0,0 +1,4 @@
  1 +class Consumption < ActiveRecord::Base
  2 + belongs_to :profile
  3 + belongs_to :product_category
  4 +end
... ...
app/models/product.rb
... ... @@ -10,6 +10,8 @@ class Product &lt; ActiveRecord::Base
10 10  
11 11 after_update :save_image
12 12  
  13 + acts_as_searchable :fields => [:name, :description]
  14 +
13 15 def image_builder=(img)
14 16 if image && image.id == img[:id]
15 17 image.attributes = img
... ...
app/views/catalog/show.rhtml
... ... @@ -3,4 +3,4 @@
3 3 <%= image_tag @product.image.public_filename if @product.image %> <br/>
4 4 <%= @product.price %> <br/>
5 5 <%= @product.description %> <br/>
6   -<%= @product.category_name %> <br/>
  6 +<%= link_to_category(@product.product_category) %> <br/>
... ...
app/views/category/view.rhtml
1 1 <h2><%= _('Category: %s') % @category.full_name %></h2>
2 2  
3   -<%# FIXME %>
4   -<p>
5   -This page will list everything (articles, documents, photos, etc) that is
6   -related to <%= @category.full_name %>. Actually generating this content is not
7   -implement yet, though.
8   -</p>
9   -
10   -<p>
11   -And yes, this placeholder text is not translated.
12   -</p>
  3 +<%= render :partial => @category.class.name.underscore %>
... ...
app/views/consumed_products/index.rhtml 0 → 100644
... ... @@ -0,0 +1,8 @@
  1 +<h2> <%=_('Listing products') %> </h2>
  2 +
  3 +<p> <%= link_to _('Add product'), :action => 'new' %> </p>
  4 +
  5 +<% @consumptions.each do |consumption| %>
  6 + <p> <b> <%= link_to consumption.product_category.name %> </b>
  7 + <%= link_to _('destroy'), :action => 'destroy', :id => consumption %> <br/>
  8 +<% end %>
... ...
app/views/consumed_products/new.rhtml 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +<h2> <%= _('Add product') %> </h2>
  2 +
  3 +<% form_for :consumption, @consuption do |f| %>
  4 + <%= _('Product: ') %> <%= f.select "product_category_id", ProductCategory.find(:all).map{|pc| [pc.name, pc.id]} %>
  5 + <%= display_submit_tag _('Add product') %>
  6 +<% end %>
... ...
app/views/consumed_products/show.rhtml 0 → 100644
... ... @@ -0,0 +1,10 @@
  1 +<h3> <%= @product.name %> </h3>
  2 +
  3 +<p> <%= image_tag @product.image.public_filename if @product.image %> </p>
  4 +<p> <%= _('Price: ') %> <%= @product.price %> </p>
  5 +<p> <%= _('Description: ') %> <%= @product.description %> </p>
  6 +<p> <%= _('Category: ') %> <%= @product.product_category ? @product.product_category.name : _('Uncategorized product') %> </p>
  7 +
  8 +<%= link_to _('destroy'), :action => 'destroy', :id => @product %>
  9 +<br/>
  10 +<%= link_to _('back'), :action => 'index' %>
... ...
app/views/manage_products/show.rhtml
1 1 <h3> <%= @product.name %> </h3>
2 2  
3   -<p> <%= image_tag @product.image.public_filename %> </p>
  3 +<p> <%= image_tag @product.image.public_filename if @product.image %> </p>
4 4 <p> <%= _('Price: ') %> <%= @product.price %> </p>
5 5 <p> <%= _('Description: ') %> <%= @product.description %> </p>
6   -<p> <%= _('Category: ') %> <%= @product.product_category ? @product.product_category.name : _('Uncategorized product') %> </p>
  6 +<p> <%= _('Category: ') %> <%= link_to_category(@product.product_category) %> </p>
7 7  
8 8 <%= link_to _('edit'), :action => 'edit', :id => @product %>
9 9 <%= link_to _('destroy'), :action => 'destroy', :id => @product %>
... ...
db/migrate/018_create_consumptions.rb 0 → 100644
... ... @@ -0,0 +1,12 @@
  1 +class CreateConsumptions < ActiveRecord::Migration
  2 + def self.up
  3 + create_table :consumptions do |t|
  4 + t.column :product_category_id, :integer
  5 + t.column :profile_id, :integer
  6 + end
  7 + end
  8 +
  9 + def self.down
  10 + drop_table :consumptions
  11 + end
  12 +end
... ...
test/fixtures/consumptions.yml 0 → 100644
... ... @@ -0,0 +1,5 @@
  1 +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
  2 +one:
  3 + id: 1
  4 +two:
  5 + id: 2
... ...
test/functional/consumed_products_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +require 'consumed_products_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class ConsumedProductsController; def rescue_action(e) raise e end; end
  6 +
  7 +class ConsumedProductsControllerTest < Test::Unit::TestCase
  8 + def setup
  9 + @controller = ConsumedProductsController.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
... ...