Commit 2b4760fdadb701543f9b0ed04e771d73e405d93f

Authored by JoenioCosta
1 parent c4b2472c

ActionItem51: fixing display of products category initial page

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1607 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/controllers/public/category_controller.rb
... ... @@ -2,14 +2,26 @@ class CategoryController < PublicController
2 2  
3 3 # view the summary of one category
4 4 def view
5   - # TODO: load articles, documents, etc so the view can list them.
6   - @recent_articles = category.recent_articles
7   - @recent_comments = category.recent_comments
8   - @most_commented_articles = category.most_commented_articles
  5 + send('action_' + @category.class.name.underscore)
9 6 end
10 7  
11 8 attr_reader :category
12 9  
13 10 before_filter :load_category, :only => [ :view ]
  11 + private
  12 +
  13 + def action_product_category
  14 + @products = category.products
  15 + @enterprises = category.products.map{|p| p.enterprise}.flatten.uniq
  16 + @users = category.consumers
  17 + end
  18 +
  19 + def action_category
  20 + # TODO: load articles, documents, etc so the view can list them.
  21 + @recent_articles = category.recent_articles
  22 + @recent_comments = category.recent_comments
  23 + @most_commented_articles = category.most_commented_articles
  24 + end
  25 + alias :action_region :action_category
14 26  
15 27 end
... ...
app/helpers/application_helper.rb
... ... @@ -306,9 +306,10 @@ module ApplicationHelper
306 306 concat(content_tag('div', capture(&block) + tag('br', :style => 'clear: left;'), { :class => 'button-bar' }.merge(options)), block.binding)
307 307 end
308 308  
309   - def link_to_category(category)
  309 + def link_to_category(category, full = true)
310 310 return _('Uncategorized product') unless category
311   - link_to category.full_name, :controller => 'category', :action => 'view', :category_path => category.path.split('/')
  311 + name = full ? category.full_name : category.name
  312 + link_to name, :controller => 'category', :action => 'view', :category_path => category.path.split('/')
312 313 end
313 314  
314 315 def link_to_product(product)
... ...
app/models/product_category.rb
1 1 class ProductCategory < Category
2 2 has_many :products
3 3 has_many :consumptions
4   - has_many :consumers, :through => :consumptions, :source => :profile_id
  4 + has_many :consumers, :through => :consumptions, :source => :profile
5 5  
6 6 def tree
7 7 children.inject([]){|all,c| all + c.tree } << self
... ...
app/views/category/_product_category.rhtml
... ... @@ -9,7 +9,8 @@
9 9 <ul>
10 10 <% @products.each do |p| %>
11 11 <li>
12   - <%= image_tag p.image.public_filename(:thumb) if p.image%> <%= link_to_product(p) %> <br/>
  12 + <%= image_tag p.image.public_filename(:thumb) if p.image%>
  13 + <%= link_to_product(p) %> <br/>
13 14 <% if p.price %>
14 15 <%= _('Price: %d') % p.price %> <br/>
15 16 <% end %>
... ...
app/views/category/view.rhtml
1 1 <div id="view-category">
2 2  
3 3 <div id="category-ancestors">
4   - <%= @category.ancestors.reverse.map { |a| link_to_category(a) }.join(' &rarr; ') %>
  4 + <%= @category.ancestors.reverse.map { |a| link_to_category(a, false) }.join(' &rarr; ') %>
5 5 </div><!-- end id="category-ancestors" -->
6 6  
7 7 <h1 id="categiry-name"><%= _('Category: %s') % @category.name %></h1>
... ...
test/functional/category_controller_test.rb
... ... @@ -52,7 +52,10 @@ class CategoryControllerTest &lt; Test::Unit::TestCase
52 52  
53 53 should 'display category of products' do
54 54 cat = ProductCategory.create!(:name => 'Food', :environment => Environment.default)
  55 + ent = Enterprise.create!(:name => 'Enterprise test', :identifier => 'enterprise_test')
  56 + p = cat.products.create!(:name => 'product test', :enterprise => ent)
55 57 get :view, :category_path => cat.path.split('/')
  58 + assert_includes assigns(:products), p
56 59 end
57 60  
58 61 end
... ...
test/unit/product_category_test.rb
... ... @@ -3,36 +3,36 @@ require File.dirname(__FILE__) + &#39;/../test_helper&#39;
3 3 class ProductCategoryTest < Test::Unit::TestCase
4 4 # TODO: please write tests here when ProductCategory has something
5 5 def test_require_same_class_for_children
6   - c1 = ProductCategory.new(:name => 'Some Product Type', :environment_id => 1)
  6 + c1 = ProductCategory.new(:name => 'Some Product Type', :environment => Environment.default)
7 7 c1.save!
8 8  
9   - c2 = Category.new(:name => 'wrong', :environment_id => 1)
  9 + c2 = Category.new(:name => 'wrong', :environment => Environment.default)
10 10 c1.children << c2
11 11  
12 12 assert !c2.valid?
13 13 assert c2.errors.invalid?(:type)
14 14  
15   - c3 = ProductCategory.new(:name => 'right', :environment_id => 1)
  15 + c3 = ProductCategory.new(:name => 'right', :environment => Environment.default)
16 16 c1.children << c3
17 17 assert c3.valid?
18 18 assert !c3.errors.invalid?(:type)
19 19 end
20 20  
21 21 def test_tree
22   - c0 = ProductCategory.create(:name => 'base_cat', :environment_id => 1)
  22 + c0 = ProductCategory.create!(:name => 'base_cat', :environment => Environment.default)
23 23 assert ! c0.new_record?
24 24 assert_equivalent [c0], c0.tree
25 25  
26   - c1 = ProductCategory.create(:name => 'cat_1', :parent => c0, :environment_id => 1)
  26 + c1 = ProductCategory.create!(:name => 'cat_1', :parent => c0, :environment => Environment.default)
27 27 c0.reload
28 28 assert_equivalent [c1], c1.tree
29 29 assert_equivalent [c0, c1], c0.tree
30 30  
31   - c2 = ProductCategory.create(:name => 'cat_2', :parent => c0, :environment_id => 1)
  31 + c2 = ProductCategory.create!(:name => 'cat_2', :parent => c0, :environment => Environment.default)
32 32 c0.reload; c1.reload;
33 33 assert_equivalent [c0,c1,c2] , c0.tree
34 34  
35   - c3 = ProductCategory.create(:name => 'cat_3', :parent => c2, :environment_id => 1)
  35 + c3 = ProductCategory.create!(:name => 'cat_3', :parent => c2, :environment => Environment.default)
36 36 c0.reload; c1.reload; c2.reload
37 37 assert_equivalent [c0,c1,c2,c3], c0.tree
38 38 assert_equivalent [c2,c3], c2.tree
... ... @@ -40,17 +40,25 @@ class ProductCategoryTest &lt; Test::Unit::TestCase
40 40 end
41 41  
42 42 def test_all_products
43   - c0 = ProductCategory.create(:name => 'base_cat', :environment_id => 1)
  43 + c0 = ProductCategory.create!(:name => 'base_cat', :environment => Environment.default)
44 44 assert_equivalent [], c0.all_products
45 45  
46 46 p0 = Product.create(:name => 'product1', :product_category => c0)
47 47 c0.reload
48 48 assert_equivalent [p0], c0.all_products
49 49  
50   - c1 = ProductCategory.create(:name => 'cat_1', :parent => c0, :environment_id => 1)
  50 + c1 = ProductCategory.create!(:name => 'cat_1', :parent => c0, :environment => Environment.default)
51 51 p1 = Product.create(:name => 'product2', :product_category => c1)
52 52 c0.reload; c1.reload
53 53 assert_equivalent [p0, p1], c0.all_products
54 54 assert_equivalent [p1], c1.all_products
55 55 end
  56 +
  57 + should 'have consumers' do
  58 + c = ProductCategory.create!(:name => 'base_cat', :environment => Environment.default)
  59 + person = create_user('test_user').person
  60 + c.consumers << person
  61 + assert_includes c.consumers, person
  62 + end
  63 +
56 64 end
... ...
test/unit/profile_categorization_test.rb
1 1 require File.dirname(__FILE__) + '/../test_helper'
2 2  
3 3 class ProfileCategorizationTest < ActiveSupport::TestCase
  4 +
4 5 should 'have profile and category' do
5 6 person = create_user('test_user').person
6 7 cat = Environment.default.categories.build(:name => 'a category'); cat.save!
7 8 person.categories << cat
8 9 person.save!
9 10 assert_includes person.categories, cat
10   - assert_includes cat.profiles, person
  11 + assert_includes cat.people, person
11 12 assert_equal [cat.id], person.category_ids
12 13 end
  14 +
13 15 end
... ...