Commit 20000188d14625f03f460f912b14d909172ce386

Authored by AntonioTerceiro
1 parent 6a0a8a12

ActionItem21: association with articles + top-level articles



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@947 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/profile.rb
@@ -42,7 +42,15 @@ class Profile < ActiveRecord::Base @@ -42,7 +42,15 @@ class Profile < ActiveRecord::Base
42 42
43 has_many :role_assignments, :as => :resource 43 has_many :role_assignments, :as => :resource
44 44
  45 + has_many :articles
45 46
  47 + def top_level_articles(reload = false)
  48 + if reload
  49 + @top_level_articles = nil
  50 + end
  51 + @top_level_articles ||= Article.top_level_for(self)
  52 + end
  53 +
46 # Sets the identifier for this profile. Raises an exception when called on a 54 # Sets the identifier for this profile. Raises an exception when called on a
47 # existing profile (since profiles cannot be renamed) 55 # existing profile (since profiles cannot be renamed)
48 def identifier=(value) 56 def identifier=(value)
test/unit/profile_test.rb
@@ -89,7 +89,7 @@ class ProfileTest < Test::Unit::TestCase @@ -89,7 +89,7 @@ class ProfileTest < Test::Unit::TestCase
89 end 89 end
90 90
91 def test_should_remove_pages_when_removing_profile 91 def test_should_remove_pages_when_removing_profile
92 - fail 'neet to be reimplemented' 92 + flunk 'pending'
93 end 93 end
94 94
95 def test_should_define_info 95 def test_should_define_info
@@ -108,11 +108,11 @@ class ProfileTest < Test::Unit::TestCase @@ -108,11 +108,11 @@ class ProfileTest < Test::Unit::TestCase
108 108
109 def test_should_provide_recent_documents 109 def test_should_provide_recent_documents
110 profile = Profile.create!(:name => 'Testing Recent documents', :identifier => 'testing_recent_documents') 110 profile = Profile.create!(:name => 'Testing Recent documents', :identifier => 'testing_recent_documents')
111 - doc1 = Article.new(:title => 'document 1', :body => 'la la la la la') 111 + doc1 = Article.new(:name => 'document 1', :body => 'la la la la la')
112 doc1.parent = profile.homepage 112 doc1.parent = profile.homepage
113 doc1.save! 113 doc1.save!
114 114
115 - doc2 = Article.new(:title => 'document 2', :body => 'la la la la la') 115 + doc2 = Article.new(:name => 'document 2', :body => 'la la la la la')
116 doc2.parent = profile.homepage 116 doc2.parent = profile.homepage
117 doc2.save! 117 doc2.save!
118 118
@@ -124,7 +124,7 @@ class ProfileTest < Test::Unit::TestCase @@ -124,7 +124,7 @@ class ProfileTest < Test::Unit::TestCase
124 124
125 def test_should_provide_most_recent_documents 125 def test_should_provide_most_recent_documents
126 profile = Profile.create!(:name => 'Testing Recent documents', :identifier => 'testing_recent_documents') 126 profile = Profile.create!(:name => 'Testing Recent documents', :identifier => 'testing_recent_documents')
127 - doc1 = Article.new(:title => 'document 1', :body => 'la la la la la') 127 + doc1 = Article.new(:name => 'document 1', :body => 'la la la la la')
128 doc1.parent = profile.homepage 128 doc1.parent = profile.homepage
129 doc1.save! 129 doc1.save!
130 130
@@ -157,6 +157,49 @@ class ProfileTest < Test::Unit::TestCase @@ -157,6 +157,49 @@ class ProfileTest < Test::Unit::TestCase
157 assert person.has_permission?('edit_profile', profile) 157 assert person.has_permission?('edit_profile', profile)
158 end 158 end
159 159
  160 + should 'have articles' do
  161 + env = Environment.create!(:name => 'test_env')
  162 + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env)
  163 +
  164 + assert_raise ActiveRecord::AssociationTypeMismatch do
  165 + profile.articles << 1
  166 + end
  167 +
  168 + assert_nothing_raised do
  169 + profile.articles << Article.new(:name => 'testing article')
  170 + end
  171 + end
  172 +
  173 + should 'list top-level articles' do
  174 + env = Environment.create!(:name => 'test_env')
  175 + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env)
  176 +
  177 + p1 = profile.articles.build(:name => 'parent1')
  178 + p1.save!
  179 + p2 = profile.articles.build(:name => 'parent2')
  180 + p2.save!
  181 +
  182 + child = profile.articles.build(:name => 'parent2', :parent_id => p1.id)
  183 + child.save!
  184 +
  185 + top = profile.top_level_articles
  186 + assert top.include?(p1)
  187 + assert top.include?(p2)
  188 + assert !top.include?(child)
  189 + end
  190 +
  191 + should 'be able to optionally reload the list of top level articles' do
  192 + env = Environment.create!(:name => 'test_env')
  193 + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env)
  194 +
  195 + list = profile.top_level_articles
  196 + same_list = profile.top_level_articles
  197 + assert_same list, same_list
  198 +
  199 + other_list = profile.top_level_articles(true)
  200 + assert_not_same list, other_list
  201 + end
  202 +
160 private 203 private
161 204
162 def assert_invalid_identifier(id) 205 def assert_invalid_identifier(id)