Commit e8d3191c8c41f9c4973535f404c6e5887eff78f1

Authored by MoisesMachado
1 parent bb67cc9e

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
app/models/category.rb
... ... @@ -54,12 +54,19 @@ class Category < ActiveRecord::Base
54 54 end
55 55  
56 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 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 70 end
64 71  
65 72 end
... ...
app/views/shared/categories_menu.rhtml
... ... @@ -4,7 +4,7 @@
4 4 <li id="category<%= item.display_color %>"<%= ' class="active"' if (@category && (@category.top_ancestor == item)) %>>
5 5 <%= item.name %>
6 6 <ul>
7   - <% item.all_children.select{|i| i.display_in_menu?}.each do |child| %>
  7 + <% item.children_for_menu.each do |child| %>
8 8 <% if (@controller.controller_name == 'search') && (@controller.action_name == 'assets') %>
9 9 <li><%= link_to(content_tag('span', child.name), :controller => 'search', :action => 'assets', :asset => params[:asset], :category_path => child.explode_path) %></li>
10 10 <% else %>
... ...
db/migrate/039_add_display_in_menu_to_categories.rb 0 → 100644
... ... @@ -0,0 +1,9 @@
  1 +class AddDisplayInMenuToCategories < ActiveRecord::Migration
  2 + def self.up
  3 + add_column :categories, :display_in_menu, :boolean, :default => false
  4 + end
  5 +
  6 + def self.down
  7 + remove_column :categories, :display_in_menu
  8 + end
  9 +end
... ...
script/anhetegua
... ... @@ -14,13 +14,13 @@ Product.destroy_all
14 14 Article.destroy_all
15 15  
16 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 18 category.save!
19 19 category
20 20 end
21 21  
22 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 24 region.save!
25 25 region
26 26 end
... ...
test/unit/category_test.rb
... ... @@ -385,16 +385,14 @@ class CategoryTest &lt; Test::Unit::TestCase
385 385 end
386 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 396 end
399 397  
400 398 end
... ...