Commit 7787940bff0d37f89fe7780e82cc5e00344c5c26
1 parent
428da059
Exists in
master
and in
29 other branches
ActionItem70: finishing implementation of categories
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@521 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
2 changed files
with
37 additions
and
3 deletions
Show diff stats
app/models/category.rb
... | ... | @@ -24,9 +24,31 @@ class Category < ActiveRecord::Base |
24 | 24 | |
25 | 25 | def name=(value) |
26 | 26 | self[:name] = value |
27 | - unless self.name.empty? | |
27 | + unless self.name.blank? | |
28 | 28 | self.slug = self.name.transliterate.downcase.gsub( /[^-a-z0-9~\s\.:;+=_]/, '').gsub(/[\s\.:;=_+]+/, '-').gsub(/[\-]{2,}/, '-').to_s |
29 | 29 | end |
30 | 30 | end |
31 | 31 | |
32 | + def slug=(value) | |
33 | + self[:slug] = value | |
34 | + unless self.slug.blank? | |
35 | + self.path = self.calculate_path | |
36 | + end | |
37 | + end | |
38 | + | |
39 | + # calculates the full path to this category using parent's path. | |
40 | + def calculate_path | |
41 | + if self.top_level? | |
42 | + self.slug | |
43 | + else | |
44 | + self.parent.calculate_path + "/" + self.slug | |
45 | + end | |
46 | + end | |
47 | + | |
48 | + before_create do |cat| | |
49 | + if cat.path == cat.slug && (! cat.top_level?) | |
50 | + cat.path = cat.calculate_path | |
51 | + end | |
52 | + end | |
53 | + | |
32 | 54 | end | ... | ... |
test/unit/category_test.rb
... | ... | @@ -105,13 +105,25 @@ class CategoryTest < Test::Unit::TestCase |
105 | 105 | |
106 | 106 | def test_path_for_toplevel |
107 | 107 | c = Category.new(:name => 'top_level') |
108 | - assert_equal 'top_level', c.path | |
108 | + assert_equal 'top-level', c.path | |
109 | 109 | end |
110 | 110 | |
111 | 111 | def test_path_for_subcategory |
112 | 112 | c1 = Category.new(:name => 'parent') |
113 | - c2 = Category.new(:name => 'child') | |
113 | + | |
114 | + c2 = Category.new | |
115 | + c2.parent = c1 | |
116 | + c2.name = 'child' | |
117 | + | |
118 | + assert_equal 'parent/child', c2.path | |
119 | + end | |
120 | + | |
121 | + def test_save_ | |
122 | + c1 = Category.create!(:name => 'parent', :environment_id => @env.id) | |
123 | + | |
124 | + c2 = Category.new(:name => 'child', :environment_id => @env.id) | |
114 | 125 | c2.parent = c1 |
126 | + c2.save! | |
115 | 127 | |
116 | 128 | assert_equal 'parent/child', c2.path |
117 | 129 | end | ... | ... |