Commit 118042b5d1e8de4bd9fe935a0ed7587930a50095

Authored by MoisesMachado
1 parent 4fc900d2

ActionItem28: added consume management

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@992 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/catalog_controller.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +class CatalogController < ApplicationController
  2 + needs_profile
  3 + before_filter :check_enterprise
  4 +
  5 + def index
  6 + @products = @profile.products
  7 + end
  8 +
  9 + def show
  10 + @product = @profile.products.find(params[:id])
  11 + end
  12 +
  13 + protected
  14 + def check_enterprise
  15 + @profile.kind_of? Enterprise
  16 + end
  17 +
  18 +end
... ...
app/controllers/public/product_category_viewer_controller.rb 0 → 100644
... ... @@ -0,0 +1,11 @@
  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/design_blocks/list_block/controllers/list_block_controller.rb
... ... @@ -21,7 +21,7 @@ class ListBlockController &lt; ApplicationController
21 21 #
22 22 # This hash will define the options menu on edit mode.
23 23 CONTROL_ACTION_OPTIONS = {
24   -# 'edit' => _('Edit')
  24 + 'edit' => _('Edit')
25 25 }
26 26  
27 27  
... ... @@ -30,22 +30,28 @@ class ListBlockController &lt; ApplicationController
30 30 ###########################
31 31  
32 32 def index
33   - @people = Person.find(:all)
  33 + @people = @design_block.people
34 34 design_render
35 35 end
36 36  
37 37 ###########################
38 38 # Other Sample of methods
39 39 ###########################
40   -
41   -# def edit
42   -# design_render_on_edit
43   -# end
44   -
45   -# def save
46   -# @design_block.update_attributes(params[:design_block])
47   -# design_render_on_edit :nothing => true
48   -# end
  40 +
  41 +
  42 + def edit
  43 + design_render_on_edit :controller => 'list_block', :action => 'edit'
  44 + end
49 45  
  46 + def save
  47 + if @design_block.update_attributes(params[:design_block])
  48 + design_render_on_edit :controller => 'list_block', :action => 'show'
  49 + else
  50 + design_render_on_edit :nothing => true
  51 + end
  52 + end
  53 +
  54 + def show
  55 + end
50 56  
51 57 end
... ...
app/design_blocks/list_block/models/list_block.rb
... ... @@ -11,5 +11,17 @@ class ListBlock &lt; Design::Block
11 11 def self.description
12 12 _('List Block')
13 13 end
  14 +
  15 + def limit_number= value
  16 + self.settings[:limit_number] = value.to_i == 0 ? nil : value.to_i
  17 + end
  18 +
  19 + def limit_number
  20 + self.settings[:limit_number]
  21 + end
  22 +
  23 + def people
  24 + Person.find(:all, :limit => limit_number)
  25 + end
14 26  
15 27 end
... ...
app/design_blocks/list_block/views/index.rhtml
1 1 <ul class='people_list_block'>
2 2 <% @people.each do |p| %>
3   - <li> <%= link_to_homepage(content_tag('span', p.name), p.identifier) %> </li>
  3 + <li> <%= link_to_homepage(content_tag('span', (p.image ? (image_tag p.image.public_filename) : p.name)), p.identifier) %> </li>
4 4 <% end %>
5 5 </ul>
... ...
app/helpers/catalog_helper.rb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +module CatalogHelper
  2 +end
... ...
app/helpers/product_category_viewer_helper.rb 0 → 100644
... ... @@ -0,0 +1,2 @@
  1 +module ProductCategoryViewerHelper
  2 +end
... ...
app/models/product.rb
... ... @@ -21,4 +21,9 @@ class Product &lt; ActiveRecord::Base
21 21 def save_image
22 22 image.save if image
23 23 end
  24 +
  25 + def category_name
  26 + product_category ? product_category.name : _('Uncategorized product')
  27 + end
  28 +
24 29 end
... ...
app/models/product_category.rb
1 1 class ProductCategory < Category
2 2 has_many :products
  3 + has_many :consumptions
  4 + has_many :consumers, :through => :consumptions, :source => :profile_id
3 5 end
... ...
app/models/profile.rb
... ... @@ -15,7 +15,7 @@ class Profile &lt; ActiveRecord::Base
15 15  
16 16 acts_as_design
17 17  
18   - acts_as_ferret :fields => [ :name ]
  18 +# acts_as_ferret :fields => [ :name ]
19 19  
20 20 # Valid identifiers must match this format.
21 21 IDENTIFIER_FORMAT = /^[a-z][a-z0-9_]*[a-z0-9]$/
... ... @@ -47,6 +47,9 @@ class Profile &lt; ActiveRecord::Base
47 47  
48 48 has_one :image, :as => :owner
49 49  
  50 + has_many :consumptions
  51 + has_many :consumed_product_categories, :through => :consumptions, :source => :product_category
  52 +
50 53 def top_level_articles(reload = false)
51 54 if reload
52 55 @top_level_articles = nil
... ...
app/views/catalog/index.rhtml 0 → 100644
... ... @@ -0,0 +1,11 @@
  1 +<h2> <%= _('Catalog') %> </h2>
  2 +
  3 +<ul>
  4 +<% @products.each do |p| %>
  5 + <li>
  6 + <%= link_to p.name, :action => 'show', :id => p %> <br/>
  7 + <%= image_tag p.image.public_filename if p.image %> <br/>
  8 + <%= p.price %> <br/><br/>
  9 + </li>
  10 +<% end %>
  11 +</ul>
... ...
app/views/catalog/show.rhtml 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +<h2> <%= @product.name %> </h2>
  2 +
  3 +<%= image_tag @product.image.public_filename if @product.image %> <br/>
  4 +<%= @product.price %> <br/>
  5 +<%= @product.description %> <br/>
  6 +<%= @product.category_name %> <br/>
... ...
app/views/product_category_viewer/index.rhtml 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +<h2> <%= _('Categories of products') %> </h2>
  2 +
  3 +<ul>
  4 +<% @categories.each do |c| %>
  5 + <li> <%= link_to c.name, :action => 'view_category', :id => c.id %> </li>
  6 +<% end %>
  7 +</ul>
... ...
app/views/product_category_viewer/view_category.rhtml 0 → 100644
... ... @@ -0,0 +1,13 @@
  1 +<h3><%= @category.name %> </h3>
  2 +
  3 +<%= @products.size.to_s + " products in this category" %>
  4 +<%= @enterprises.size.to_s + " enterprises sells products in this category" %>
  5 +
  6 +<ul>
  7 + <% @products.each do |p| %>
  8 + <li>
  9 + <%= image_tag p.image.public_filename(:thumb) %> <%= p.name %> <br/>
  10 + <% _('Price:') %> <% p.price %> <%= _('Enterprise:') %> <%= link_to_homepage p.enterprise.name, p.enterprise.identifier %> <br/><br/>
  11 + </li>
  12 + <% end %>
  13 +</ul>
... ...
config/routes.rb
... ... @@ -67,7 +67,10 @@ ActionController::Routing::Routes.draw do |map|
67 67 ## Test controllers.
68 68 ## FIXME: this should not be needed
69 69 ######################################################
70   - map.connect 'test/:controller/:action/:id' #, :controller => /.*test.*/
  70 + map.connect 'test/:controller/:action/:id' , :controller => /.*test.*/
  71 +
  72 + map.connect ':profile/catalog/:action/:id', :controller => 'catalog'
  73 +
71 74  
72 75 # *content viewing*
73 76 # XXX this route must come last so other routes have priority over it.
... ...
test/functional/catalog_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +require 'catalog_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class CatalogController; def rescue_action(e) raise e end; end
  6 +
  7 +class CatalogControllerTest < Test::Unit::TestCase
  8 + def setup
  9 + @controller = CatalogController.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
... ...
test/functional/product_category_viewer_controller_test.rb 0 → 100644
... ... @@ -0,0 +1,18 @@
  1 +require File.dirname(__FILE__) + '/../test_helper'
  2 +require 'product_category_viewer_controller'
  3 +
  4 +# Re-raise errors caught by the controller.
  5 +class ProductCategoryViewerController; def rescue_action(e) raise e end; end
  6 +
  7 +class ProductCategoryViewerControllerTest < Test::Unit::TestCase
  8 + def setup
  9 + @controller = ProductCategoryViewerController.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
... ...