Commit e8d3191c8c41f9c4973535f404c6e5887eff78f1
1 parent
bb67cc9e
Exists in
master
and in
28 other branches
ActionItem252: added the display_in_menu column to categories table and made the check in the helper
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1959 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
5 changed files
with
31 additions
and
17 deletions
Show diff stats
app/models/category.rb
| @@ -54,12 +54,19 @@ class Category < ActiveRecord::Base | @@ -54,12 +54,19 @@ class Category < ActiveRecord::Base | ||
| 54 | end | 54 | end |
| 55 | 55 | ||
| 56 | def display_in_menu? | 56 | def display_in_menu? |
| 57 | - if ENV['RAILS_ENV'] == 'development' | ||
| 58 | - return true | 57 | + display_in_menu |
| 58 | + end | ||
| 59 | + | ||
| 60 | + def children_for_menu | ||
| 61 | + results = [] | ||
| 62 | + pending = children.find(:all, :conditions => { :display_in_menu => true}) | ||
| 63 | + while !pending.empty? | ||
| 64 | + cat = pending.shift | ||
| 65 | + results << cat | ||
| 66 | + pending += cat.children.find(:all, :conditions => { :display_in_menu => true} ) | ||
| 59 | end | 67 | end |
| 60 | 68 | ||
| 61 | - # FIXME don't hardcode like this. Should be a setting of the environment, maybe | ||
| 62 | - total_items >= 10 | 69 | + results |
| 63 | end | 70 | end |
| 64 | 71 | ||
| 65 | end | 72 | end |
app/views/shared/categories_menu.rhtml
| @@ -4,7 +4,7 @@ | @@ -4,7 +4,7 @@ | ||
| 4 | <li id="category<%= item.display_color %>"<%= ' class="active"' if (@category && (@category.top_ancestor == item)) %>> | 4 | <li id="category<%= item.display_color %>"<%= ' class="active"' if (@category && (@category.top_ancestor == item)) %>> |
| 5 | <%= item.name %> | 5 | <%= item.name %> |
| 6 | <ul> | 6 | <ul> |
| 7 | - <% item.all_children.select{|i| i.display_in_menu?}.each do |child| %> | 7 | + <% item.children_for_menu.each do |child| %> |
| 8 | <% if (@controller.controller_name == 'search') && (@controller.action_name == 'assets') %> | 8 | <% if (@controller.controller_name == 'search') && (@controller.action_name == 'assets') %> |
| 9 | <li><%= link_to(content_tag('span', child.name), :controller => 'search', :action => 'assets', :asset => params[:asset], :category_path => child.explode_path) %></li> | 9 | <li><%= link_to(content_tag('span', child.name), :controller => 'search', :action => 'assets', :asset => params[:asset], :category_path => child.explode_path) %></li> |
| 10 | <% else %> | 10 | <% else %> |
script/anhetegua
| @@ -14,13 +14,13 @@ Product.destroy_all | @@ -14,13 +14,13 @@ Product.destroy_all | ||
| 14 | Article.destroy_all | 14 | Article.destroy_all |
| 15 | 15 | ||
| 16 | def new_category(parent, name, color = nil) | 16 | def new_category(parent, name, color = nil) |
| 17 | - category = Environment.default.categories.build(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil)) | 17 | + category = Environment.default.categories.build(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil), :display_in_menu => true) |
| 18 | category.save! | 18 | category.save! |
| 19 | category | 19 | category |
| 20 | end | 20 | end |
| 21 | 21 | ||
| 22 | def new_region(parent, name, color = nil) | 22 | def new_region(parent, name, color = nil) |
| 23 | - region = Environment.default.regions.build(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil)) | 23 | + region = Environment.default.regions.build(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil), :display_in_menu => true) |
| 24 | region.save! | 24 | region.save! |
| 25 | region | 25 | region |
| 26 | end | 26 | end |
test/unit/category_test.rb
| @@ -385,16 +385,14 @@ class CategoryTest < Test::Unit::TestCase | @@ -385,16 +385,14 @@ class CategoryTest < Test::Unit::TestCase | ||
| 385 | end | 385 | end |
| 386 | end | 386 | end |
| 387 | 387 | ||
| 388 | - should 'display in menu' do | ||
| 389 | - c = Category.create!(:name => 'test category1', :environment => Environment.default) | ||
| 390 | - c.expects(:total_items).returns(10) | ||
| 391 | - assert c.display_in_menu? | ||
| 392 | - end | ||
| 393 | - | ||
| 394 | - should 'not display in menu' do | ||
| 395 | - c = Category.create!(:name => 'test category1', :environment => Environment.default) | ||
| 396 | - c.expects(:total_items).returns(1) | ||
| 397 | - assert !c.display_in_menu? | 388 | + should 'display in menu only if have display_menu setted to true' do |
| 389 | + c = Category.create!(:name => 'test category top', :environment => Environment.default, :display_in_menu => true) | ||
| 390 | + c1 = Category.create!(:name => 'test category 1', :environment => Environment.default, :display_in_menu => true, :parent => c) | ||
| 391 | + c11 = Category.create!(:name => 'test category 11', :environment => Environment.default, :display_in_menu => true, :parent => c1) | ||
| 392 | + c2 = Category.create!(:name => 'test category 2', :environment => Environment.default, :display_in_menu => true, :parent => c) | ||
| 393 | + c3 = Category.create!(:name => 'test category 3', :environment => Environment.default, :parent => c) | ||
| 394 | + | ||
| 395 | + assert_equivalent [c1, c11, c2], c.children_for_menu | ||
| 398 | end | 396 | end |
| 399 | 397 | ||
| 400 | end | 398 | end |