From 20000188d14625f03f460f912b14d909172ce386 Mon Sep 17 00:00:00 2001 From: AntonioTerceiro Date: Mon, 26 Nov 2007 17:46:06 +0000 Subject: [PATCH] ActionItem21: association with articles + top-level articles --- app/models/profile.rb | 8 ++++++++ test/unit/profile_test.rb | 51 +++++++++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/app/models/profile.rb b/app/models/profile.rb index 4b1df3d..606d29f 100644 --- a/app/models/profile.rb +++ b/app/models/profile.rb @@ -42,7 +42,15 @@ class Profile < ActiveRecord::Base has_many :role_assignments, :as => :resource + has_many :articles + def top_level_articles(reload = false) + if reload + @top_level_articles = nil + end + @top_level_articles ||= Article.top_level_for(self) + end + # Sets the identifier for this profile. Raises an exception when called on a # existing profile (since profiles cannot be renamed) def identifier=(value) diff --git a/test/unit/profile_test.rb b/test/unit/profile_test.rb index 88c06fe..0868f1c 100644 --- a/test/unit/profile_test.rb +++ b/test/unit/profile_test.rb @@ -89,7 +89,7 @@ class ProfileTest < Test::Unit::TestCase end def test_should_remove_pages_when_removing_profile - fail 'neet to be reimplemented' + flunk 'pending' end def test_should_define_info @@ -108,11 +108,11 @@ class ProfileTest < Test::Unit::TestCase def test_should_provide_recent_documents profile = Profile.create!(:name => 'Testing Recent documents', :identifier => 'testing_recent_documents') - doc1 = Article.new(:title => 'document 1', :body => 'la la la la la') + doc1 = Article.new(:name => 'document 1', :body => 'la la la la la') doc1.parent = profile.homepage doc1.save! - doc2 = Article.new(:title => 'document 2', :body => 'la la la la la') + doc2 = Article.new(:name => 'document 2', :body => 'la la la la la') doc2.parent = profile.homepage doc2.save! @@ -124,7 +124,7 @@ class ProfileTest < Test::Unit::TestCase def test_should_provide_most_recent_documents profile = Profile.create!(:name => 'Testing Recent documents', :identifier => 'testing_recent_documents') - doc1 = Article.new(:title => 'document 1', :body => 'la la la la la') + doc1 = Article.new(:name => 'document 1', :body => 'la la la la la') doc1.parent = profile.homepage doc1.save! @@ -157,6 +157,49 @@ class ProfileTest < Test::Unit::TestCase assert person.has_permission?('edit_profile', profile) end + should 'have articles' do + env = Environment.create!(:name => 'test_env') + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env) + + assert_raise ActiveRecord::AssociationTypeMismatch do + profile.articles << 1 + end + + assert_nothing_raised do + profile.articles << Article.new(:name => 'testing article') + end + end + + should 'list top-level articles' do + env = Environment.create!(:name => 'test_env') + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env) + + p1 = profile.articles.build(:name => 'parent1') + p1.save! + p2 = profile.articles.build(:name => 'parent2') + p2.save! + + child = profile.articles.build(:name => 'parent2', :parent_id => p1.id) + child.save! + + top = profile.top_level_articles + assert top.include?(p1) + assert top.include?(p2) + assert !top.include?(child) + end + + should 'be able to optionally reload the list of top level articles' do + env = Environment.create!(:name => 'test_env') + profile = Profile.create!(:name => 'Profile for testing ', :identifier => 'profilefortesting', :environment => env) + + list = profile.top_level_articles + same_list = profile.top_level_articles + assert_same list, same_list + + other_list = profile.top_level_articles(true) + assert_not_same list, other_list + end + private def assert_invalid_identifier(id) -- libgit2 0.21.2