diff --git a/app/models/category.rb b/app/models/category.rb
index 1c251e3..5fd5339 100644
--- a/app/models/category.rb
+++ b/app/models/category.rb
@@ -54,12 +54,19 @@ class Category < ActiveRecord::Base
end
def display_in_menu?
- if ENV['RAILS_ENV'] == 'development'
- return true
+ display_in_menu
+ end
+
+ def children_for_menu
+ results = []
+ pending = children.find(:all, :conditions => { :display_in_menu => true})
+ while !pending.empty?
+ cat = pending.shift
+ results << cat
+ pending += cat.children.find(:all, :conditions => { :display_in_menu => true} )
end
- # FIXME don't hardcode like this. Should be a setting of the environment, maybe
- total_items >= 10
+ results
end
end
diff --git a/app/views/shared/categories_menu.rhtml b/app/views/shared/categories_menu.rhtml
index a79b10c..b9c41b4 100644
--- a/app/views/shared/categories_menu.rhtml
+++ b/app/views/shared/categories_menu.rhtml
@@ -4,7 +4,7 @@
>
<%= item.name %>
- <% item.all_children.select{|i| i.display_in_menu?}.each do |child| %>
+ <% item.children_for_menu.each do |child| %>
<% if (@controller.controller_name == 'search') && (@controller.action_name == 'assets') %>
- <%= link_to(content_tag('span', child.name), :controller => 'search', :action => 'assets', :asset => params[:asset], :category_path => child.explode_path) %>
<% else %>
diff --git a/db/migrate/039_add_display_in_menu_to_categories.rb b/db/migrate/039_add_display_in_menu_to_categories.rb
new file mode 100644
index 0000000..76823a9
--- /dev/null
+++ b/db/migrate/039_add_display_in_menu_to_categories.rb
@@ -0,0 +1,9 @@
+class AddDisplayInMenuToCategories < ActiveRecord::Migration
+ def self.up
+ add_column :categories, :display_in_menu, :boolean, :default => false
+ end
+
+ def self.down
+ remove_column :categories, :display_in_menu
+ end
+end
diff --git a/script/anhetegua b/script/anhetegua
index f27b516..2379dbe 100755
--- a/script/anhetegua
+++ b/script/anhetegua
@@ -14,13 +14,13 @@ Product.destroy_all
Article.destroy_all
def new_category(parent, name, color = nil)
- category = Environment.default.categories.build(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil))
+ category = Environment.default.categories.build(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil), :display_in_menu => true)
category.save!
category
end
def new_region(parent, name, color = nil)
- region = Environment.default.regions.build(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil))
+ region = Environment.default.regions.build(:name => name, :display_color => color, :parent_id => (parent ? parent.id: nil), :display_in_menu => true)
region.save!
region
end
diff --git a/test/unit/category_test.rb b/test/unit/category_test.rb
index ac049dd..a5bfb6e 100644
--- a/test/unit/category_test.rb
+++ b/test/unit/category_test.rb
@@ -385,16 +385,14 @@ class CategoryTest < Test::Unit::TestCase
end
end
- should 'display in menu' do
- c = Category.create!(:name => 'test category1', :environment => Environment.default)
- c.expects(:total_items).returns(10)
- assert c.display_in_menu?
- end
-
- should 'not display in menu' do
- c = Category.create!(:name => 'test category1', :environment => Environment.default)
- c.expects(:total_items).returns(1)
- assert !c.display_in_menu?
+ should 'display in menu only if have display_menu setted to true' do
+ c = Category.create!(:name => 'test category top', :environment => Environment.default, :display_in_menu => true)
+ c1 = Category.create!(:name => 'test category 1', :environment => Environment.default, :display_in_menu => true, :parent => c)
+ c11 = Category.create!(:name => 'test category 11', :environment => Environment.default, :display_in_menu => true, :parent => c1)
+ c2 = Category.create!(:name => 'test category 2', :environment => Environment.default, :display_in_menu => true, :parent => c)
+ c3 = Category.create!(:name => 'test category 3', :environment => Environment.default, :parent => c)
+
+ assert_equivalent [c1, c11, c2], c.children_for_menu
end
end
--
libgit2 0.21.2