Commit fd26529052b18f50d2db4483a6de41ae524169ef

Authored by MoisesMachado
1 parent 621630ae

ActionItem591: made new profiles coby articles from templates

git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2414 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/article.rb
... ... @@ -167,6 +167,16 @@ class Article < ActiveRecord::Base
167 167 profile.public? && public_article
168 168 end
169 169  
  170 + def copy(options)
  171 + attrs = attributes.reject! { |key, value| article_attr_blacklist.include?(key) }
  172 + attrs.merge!(options)
  173 + Article.create(attrs)
  174 + end
  175 +
  176 + def article_attr_blacklist
  177 + ['id', 'profile_id', 'parent_id', 'slug', 'path', 'updated_at', 'created_at', 'last_changed_by_id', 'version', 'lock_version', 'type', 'children_count', 'comments_count']
  178 + end
  179 +
170 180 private
171 181  
172 182 def sanitize_tag_list
... ...
app/models/profile.rb
... ... @@ -207,7 +207,7 @@ class Profile < ActiveRecord::Base
207 207 end
208 208 end
209 209  
210   - # this method should be override to provide the correct template
  210 + # this method should be overwritten to provide the correct template
211 211 def template
212 212 nil
213 213 end
... ... @@ -325,21 +325,38 @@ class Profile < ActiveRecord::Base
325 325  
326 326 after_create :insert_default_article_set
327 327 def insert_default_article_set
328   - # a default homepage
329   - hp = default_homepage(:name => _("%s's home page") % self.name, :body => _("<p>This is a default homepage created for %s. It can be changed though the control panel.</p>") % self.name, :advertise => false)
330   - hp.profile = self
331   - hp.save!
332   - self.home_page = hp
333   - self.save!
334   -
335   - # a default rss feed
336   - feed = RssFeed.new(:name => 'feed')
337   - self.articles << feed
338   -
339   - # a default private folder if public
340   - if self.public?
341   - folder = Folder.new(:name => _("Intranet"), :public_article => false)
342   - self.articles << folder
  328 + if template
  329 + copy_articles_from template
  330 + else
  331 + # a default homepage
  332 + hp = default_homepage(:name => _("My home page"), :body => _("<p>This is a default homepage created for me. It can be changed though the control panel.</p>"), :advertise => false)
  333 + hp.profile = self
  334 + hp.save!
  335 + self.home_page = hp
  336 + self.save!
  337 +
  338 + # a default rss feed
  339 + feed = RssFeed.new(:name => 'feed')
  340 + self.articles << feed
  341 +
  342 + # a default private folder if public
  343 + if self.public?
  344 + folder = Folder.new(:name => _("Intranet"), :public_article => false)
  345 + self.articles << folder
  346 + end
  347 + end
  348 + end
  349 +
  350 + def copy_articles_from other
  351 + other.top_level_articles.each do |a|
  352 + copy_article_tree a
  353 + end
  354 + end
  355 +
  356 + def copy_article_tree(article, parent=nil)
  357 + article_copy = article.copy(:profile => self, :parent => parent)
  358 + article.children.each do |a|
  359 + copy_article_tree a, article_copy
343 360 end
344 361 end
345 362  
... ...
test/unit/article_test.rb
... ... @@ -414,4 +414,23 @@ class ArticleTest &lt; Test::Unit::TestCase
414 414 assert article.display_to?(friend)
415 415 end
416 416  
  417 + should 'make a copy of the article as child of it' do
  418 + person = create_user('test_user').person
  419 + a = person.articles.create!(:name => 'test article', :body => 'some text')
  420 + b = a.copy(:parent => a, :profile => a.profile)
  421 +
  422 + assert_includes a.children, b
  423 + assert_equal 'some text', b.body
  424 + end
  425 +
  426 + should 'make a copy of the article to other profile' do
  427 + p1 = create_user('test_user1').person
  428 + p2 = create_user('test_user2').person
  429 + a = p1.articles.create!(:name => 'test article', :body => 'some text')
  430 + b = a.copy(:parent => a, :profile => p2)
  431 +
  432 + assert_includes p2.articles, b
  433 + assert_equal 'some text', b.body
  434 + end
  435 +
417 436 end
... ...
test/unit/community_test.rb
... ... @@ -82,24 +82,11 @@ class CommunityTest &lt; Test::Unit::TestCase
82 82 assert !RoleAssignment.exists?(i.id)
83 83 end
84 84 end
85   -
86   - should 'copy set of boxes from community template' do
87   - template = Community.create!(:name => 'test template', :identifier => 'test_template')
88   - template.boxes.destroy_all
89   - template.boxes << Box.new
90   - template.boxes[0].blocks << Block.new
91   - template.save!
92 85  
93   - env = Environment.create!(:name => 'test_env')
94   - env.settings[:community_template_id] = template.id
95   - env.save!
96   -
97   - assert_equal template, env.community_template
98   -
99   - com = Community.create!(:name => 'test ent', :identifier => 'test_ent', :environment => env)
100   -
101   - assert_equal 1, com.boxes.size
102   - assert_equal 1, com.boxes[0].blocks.size
  86 + should 'have a community template' do
  87 + env = Environment.create!(:name => 'test env')
  88 + p = Community.create!(:name => 'test_com', :identifier => 'test_com', :environment => env)
  89 + assert_kind_of Community, p.template
103 90 end
104 91  
105 92 end
... ...
test/unit/enterprise_test.rb
... ... @@ -205,23 +205,11 @@ class EnterpriseTest &lt; Test::Unit::TestCase
205 205 assert_not_includes ent.blocks.map(&:class), ProductsBlock
206 206 end
207 207  
208   - should 'copy set of boxes from enterprise template' do
209   - template = Enterprise.create!(:name => 'test template', :identifier => 'test_template')
210   - template.boxes.destroy_all
211   - template.boxes << Box.new
212   - template.boxes[0].blocks << Block.new
213   - template.save!
214   -
215   - env = Environment.create!(:name => 'test_env')
216   - env.settings[:enterprise_template_id] = template.id
217   - env.save!
218   -
219   - assert_equal template, env.enterprise_template
220   -
221   - ent = Enterprise.create!(:name => 'test ent', :identifier => 'test_ent', :environment => env)
222   -
223   - assert_equal 1, ent.boxes.size
224   - assert_equal 1, ent.boxes[0].blocks.size
  208 + should 'have a enterprise template' do
  209 + env = Environment.create!(:name => 'test env')
  210 + p = Enterprise.create!(:name => 'test_com', :identifier => 'test_com', :environment => env)
  211 + assert_kind_of Enterprise, p.template
225 212 end
226 213  
  214 +
227 215 end
... ...
test/unit/person_test.rb
... ... @@ -279,23 +279,10 @@ class PersonTest &lt; Test::Unit::TestCase
279 279 assert person.display_info_to?(friend)
280 280 end
281 281  
282   - should 'copy set of boxes from person template' do
283   - template = create_user('test_template').person
284   - template.boxes.destroy_all
285   - template.boxes << Box.new
286   - template.boxes[0].blocks << Block.new
287   - template.save!
288   -
289   - env = Environment.create!(:name => 'test_env')
290   - env.settings[:person_template_id] = template.id
291   - env.save!
292   -
293   - assert_equal template, env.person_template
294   -
295   - person = create_user('test_user', :environment => env).person
296   -
297   - assert_equal 1, person.boxes.size
298   - assert_equal 1, person.boxes[0].blocks.size
  282 + should 'have a person template' do
  283 + env = Environment.create!(:name => 'test env')
  284 + p = create_user('test_user', :environment => env).person
  285 + assert_kind_of Person, p.template
299 286 end
300   -
  287 +
301 288 end
... ...
test/unit/profile_test.rb
... ... @@ -805,11 +805,43 @@ class ProfileTest &lt; Test::Unit::TestCase
805 805 should 'create a initial private folder when a public profile is created' do
806 806 p1 = Profile.create!(:name => 'test profile 1', :identifier => 'test_profile1')
807 807 p2 = Profile.create!(:name => 'test profile 2', :identifier => 'test_profile2', :public_profile => false)
808   -
  808 +
809 809 assert p1.articles.find(:first, :conditions => {:public_article => false})
810 810 assert !p2.articles.find(:first, :conditions => {:public_article => false})
811 811 end
812 812  
  813 + should 'copy set of articles from a template' do
  814 + template = create_user('test_template').person
  815 + template.articles.destroy_all
  816 + a1 = template.articles.create(:name => 'some xyz article')
  817 + a2 = template.articles.create(:name => 'some child article', :parent => a1)
  818 +
  819 + Profile.any_instance.stubs(:template).returns(template)
  820 +
  821 + p = Profile.create!(:name => 'test_profile', :identifier => 'test_profile')
  822 +
  823 + assert_equal 1, p.top_level_articles.size
  824 + top_art = p.top_level_articles[0]
  825 + assert_equal 'some xyz article', top_art.name
  826 + assert_equal 1, top_art.children.size
  827 + child_art = top_art.children[0]
  828 + assert_equal 'some child article', child_art.name
  829 + end
  830 +
  831 + should 'copy set of boxes from profile template' do
  832 + template = Profile.create!(:name => 'test template', :identifier => 'test_template')
  833 + template.boxes.destroy_all
  834 + template.boxes << Box.new
  835 + template.boxes[0].blocks << Block.new
  836 + template.save!
  837 +
  838 + Profile.any_instance.stubs(:template).returns(template)
  839 +
  840 + p = Profile.create!(:name => 'test prof', :identifier => 'test_prof')
  841 +
  842 + assert_equal 1, p.boxes.size
  843 + assert_equal 1, p.boxes[0].blocks.size
  844 + end
813 845 private
814 846  
815 847 def assert_invalid_identifier(id)
... ...