Commit 428da059b46d76aef63e4465896aa7e3e036a4ac
1 parent
ca62da08
Exists in
staging
and in
42 other branches
ActionItem70: implementing categories
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@520 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
4 changed files
with
170 additions
and
0 deletions
Show diff stats
| ... | ... | @@ -0,0 +1,32 @@ |
| 1 | +class Category < ActiveRecord::Base | |
| 2 | + | |
| 3 | + validates_presence_of :name, :environment_id | |
| 4 | + belongs_to :environment | |
| 5 | + | |
| 6 | + acts_as_tree :order => 'name' | |
| 7 | + | |
| 8 | + def full_name(sep = '/') | |
| 9 | + self.parent ? (self.parent.full_name(sep) + sep + self.name) : (self.name) | |
| 10 | + end | |
| 11 | + def level | |
| 12 | + self.parent ? (self.parent.level + 1) : 0 | |
| 13 | + end | |
| 14 | + def top_level? | |
| 15 | + self.parent.nil? | |
| 16 | + end | |
| 17 | + def leaf? | |
| 18 | + self.children.empty? | |
| 19 | + end | |
| 20 | + | |
| 21 | + def self.top_level_for(environment) | |
| 22 | + self.find(:all, :conditions => ['parent_id is null and environment_id = ?', environment.id ]) | |
| 23 | + end | |
| 24 | + | |
| 25 | + def name=(value) | |
| 26 | + self[:name] = value | |
| 27 | + unless self.name.empty? | |
| 28 | + self.slug = self.name.transliterate.downcase.gsub( /[^-a-z0-9~\s\.:;+=_]/, '').gsub(/[\s\.:;=_+]+/, '-').gsub(/[\-]{2,}/, '-').to_s | |
| 29 | + end | |
| 30 | + end | |
| 31 | + | |
| 32 | +end | ... | ... |
config/environment.rb
| ... | ... | @@ -0,0 +1,17 @@ |
| 1 | +class CreateCategories < ActiveRecord::Migration | |
| 2 | + def self.up | |
| 3 | + create_table :categories do |t| | |
| 4 | + t.column :name, :string | |
| 5 | + t.column :slug, :string | |
| 6 | + t.column :path, :text, :default => '' | |
| 7 | + | |
| 8 | + t.column :environment_id, :integer | |
| 9 | + t.column :parent_id, :integer | |
| 10 | + t.column :type, :string | |
| 11 | + end | |
| 12 | + end | |
| 13 | + | |
| 14 | + def self.down | |
| 15 | + drop_table :categories | |
| 16 | + end | |
| 17 | +end | ... | ... |
| ... | ... | @@ -0,0 +1,119 @@ |
| 1 | +require File.dirname(__FILE__) + '/../test_helper' | |
| 2 | + | |
| 3 | +class CategoryTest < Test::Unit::TestCase | |
| 4 | + | |
| 5 | + def setup | |
| 6 | + @env = Environment.create!(:name => 'Enviroment for testing') | |
| 7 | + end | |
| 8 | + | |
| 9 | + def test_mandatory_field_name | |
| 10 | + c = Category.new | |
| 11 | + c.organization = @env | |
| 12 | + c.save | |
| 13 | + assert c.errors.invalid?(:name) | |
| 14 | + end | |
| 15 | + | |
| 16 | + def test_mandatory_field_name | |
| 17 | + c = Category.new | |
| 18 | + c.name = 'product category for testing' | |
| 19 | + assert !c.valid? | |
| 20 | + assert c.errors.invalid?(:environment_id) | |
| 21 | + end | |
| 22 | + | |
| 23 | + def test_relationship_with_environment | |
| 24 | + c = Category.create!(:name => 'product category for testing', :environment_id => @env.id) | |
| 25 | + assert_equal @env, c.environment | |
| 26 | + end | |
| 27 | + | |
| 28 | + def test_relation_with_parent | |
| 29 | + parent_category = Category.create!(:name => 'parent category for testing', :environment_id => @env.id) | |
| 30 | + c = Category.create!(:name => 'product category for testing', :environment_id => @env.id, :parent_id => parent_category.id) | |
| 31 | + assert_equal parent_category, c.parent | |
| 32 | + end | |
| 33 | + | |
| 34 | + # def test_full_text_search | |
| 35 | + # c = Category.create!(:name => 'product category for testing', :environment_id => @env.id) | |
| 36 | + # assert @env.product_categories.full_text_search('product*').include?(c) | |
| 37 | + # end | |
| 38 | + | |
| 39 | + def test_category_full_name | |
| 40 | + cat = Category.new(:name => 'category_name') | |
| 41 | + assert_equal 'category_name', cat.full_name | |
| 42 | + end | |
| 43 | + | |
| 44 | + def test_subcategory_full_name | |
| 45 | + cat = Category.new(:name => 'category_name') | |
| 46 | + sub_cat = Category.new(:name => 'subcategory_name') | |
| 47 | + sub_cat.stubs(:parent).returns(cat) | |
| 48 | + sub_cat.parent = cat | |
| 49 | + assert_equal 'category_name/subcategory_name', sub_cat.full_name | |
| 50 | + end | |
| 51 | + | |
| 52 | + def test_category_level | |
| 53 | + cat = Category.new(:name => 'category_name') | |
| 54 | + assert_equal 0, cat.level | |
| 55 | + end | |
| 56 | + | |
| 57 | + def test_subegory_level | |
| 58 | + cat = Category.new(:name => 'category_name') | |
| 59 | + sub_cat = Category.new(:name => 'subcategory_name') | |
| 60 | + sub_cat.stubs(:parent).returns(cat) | |
| 61 | + sub_cat.parent = cat | |
| 62 | + assert_equal 1, sub_cat.level | |
| 63 | + end | |
| 64 | + | |
| 65 | + def test_top_level | |
| 66 | + cat = Category.new(:name => 'category_name') | |
| 67 | + assert cat.top_level? | |
| 68 | + end | |
| 69 | + | |
| 70 | + def test_not_top_level | |
| 71 | + cat = Category.new(:name => 'category_name') | |
| 72 | + sub_cat = Category.new(:name => 'subcategory_name') | |
| 73 | + sub_cat.stubs(:parent).returns(cat) | |
| 74 | + sub_cat.parent = cat | |
| 75 | + assert !sub_cat.top_level? | |
| 76 | + end | |
| 77 | + | |
| 78 | + def test_leaf | |
| 79 | + cat = Category.new(:name => 'category_name') | |
| 80 | + sub_cat = Category.new(:name => 'subcategory_name') | |
| 81 | + cat.stubs(:children).returns([sub_cat]) | |
| 82 | + assert !cat.leaf? | |
| 83 | + end | |
| 84 | + | |
| 85 | + def test_not_leaf | |
| 86 | + cat = Category.new(:name => 'category_name') | |
| 87 | + sub_cat = Category.new(:name => 'subcategory_name') | |
| 88 | + cat.stubs(:children).returns([]) | |
| 89 | + assert cat.leaf? | |
| 90 | + end | |
| 91 | + | |
| 92 | + def test_top_level_for | |
| 93 | + cat = Category.create(:name => 'Category for testing', :environment_id => @env.id) | |
| 94 | + sub_cat = Category.create(:name => 'SubCategory for testing', :environment_id => @env.id, :parent_id => cat.id) | |
| 95 | + | |
| 96 | + roots = Category.top_level_for(@env) | |
| 97 | + | |
| 98 | + assert_equal 1, roots.size | |
| 99 | + end | |
| 100 | + | |
| 101 | + def test_slug | |
| 102 | + c = Category.create(:name => 'Category name') | |
| 103 | + assert_equal 'category-name', c.slug | |
| 104 | + end | |
| 105 | + | |
| 106 | + def test_path_for_toplevel | |
| 107 | + c = Category.new(:name => 'top_level') | |
| 108 | + assert_equal 'top_level', c.path | |
| 109 | + end | |
| 110 | + | |
| 111 | + def test_path_for_subcategory | |
| 112 | + c1 = Category.new(:name => 'parent') | |
| 113 | + c2 = Category.new(:name => 'child') | |
| 114 | + c2.parent = c1 | |
| 115 | + | |
| 116 | + assert_equal 'parent/child', c2.path | |
| 117 | + end | |
| 118 | + | |
| 119 | +end | ... | ... |