From 07e24499d212b00d38aa0a508f0e2d0136d6f72c Mon Sep 17 00:00:00 2001 From: Leandro Nunes dos Santos Date: Mon, 11 Aug 2014 11:20:14 -0300 Subject: [PATCH] refactoring community template management --- app/models/community.rb | 2 +- app/models/environment.rb | 14 +++++++++----- test/unit/environment_test.rb | 81 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------------------ 3 files changed, 73 insertions(+), 24 deletions(-) diff --git a/app/models/community.rb b/app/models/community.rb index e36ddf0..41d2177 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -78,7 +78,7 @@ class Community < Organization end def default_template - environment.community_template + environment.community_default_template end def news(limit = 30, highlight = false) diff --git a/app/models/environment.rb b/app/models/environment.rb index 5f42b26..ebe5870 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -717,13 +717,17 @@ class Environment < ActiveRecord::Base ] end - def community_template + def community_templates + self.communities.templates + end + + def community_default_template template = Community.find_by_id settings[:community_template_id] - template if template && template.is_template + template if template && template.is_template? end - def community_template=(value) - settings[:community_template_id] = value.id + def community_default_template=(value) + settings[:community_template_id] = value.kind_of?(Community) ? value.id : value end def person_templates @@ -845,7 +849,7 @@ class Environment < ActiveRecord::Base self.enterprise_template = enterprise_template self.inactive_enterprise_template = inactive_enterprise_template - self.community_template = community_template + self.community_default_template = community_template self.person_default_template = person_template self.save! end diff --git a/test/unit/environment_test.rb b/test/unit/environment_test.rb index 40e83e7..451838d 100644 --- a/test/unit/environment_test.rb +++ b/test/unit/environment_test.rb @@ -494,32 +494,16 @@ class EnvironmentTest < ActiveSupport::TestCase # the templates must be created assert_kind_of Enterprise, e.enterprise_template assert_kind_of Enterprise, e.inactive_enterprise_template - assert_kind_of Community, e.community_template + assert_kind_of Community, e.community_default_template assert_kind_of Person, e.person_default_template # the templates must be private assert !e.enterprise_template.visible? assert !e.inactive_enterprise_template.visible? - assert !e.community_template.visible? + assert !e.community_default_template.visible? assert !e.person_default_template.visible? end - should 'set templates' do - e = fast_create(Environment) - - comm = fast_create(Community, :is_template => true) - e.community_template = comm - assert_equal comm, e.community_template - - person = fast_create(Person, :is_template => true) - e.person_default_template = person - assert_equal person, e.person_default_template - - enterprise = fast_create(Enterprise, :is_template => true) - e.enterprise_template = enterprise - assert_equal enterprise, e.enterprise_template - end - should 'person_templates return all templates of person' do e = fast_create(Environment) @@ -581,6 +565,67 @@ class EnvironmentTest < ActiveSupport::TestCase assert_equal p3, e.person_default_template end + should 'community_templates return all templates of community' do + e = fast_create(Environment) + + c1= fast_create(Community, :is_template => true, :environment_id => e.id) + c2 = fast_create(Community, :environment_id => e.id) + c3 = fast_create(Community, :is_template => true, :environment_id => e.id) + assert_equivalent [c1,c3], e.community_templates + end + + should 'community_templates return an empty array if there is no templates of community' do + e = fast_create(Environment) + + fast_create(Community, :environment_id => e.id) + fast_create(Community, :environment_id => e.id) + assert_equivalent [], e.community_templates + end + + should 'community_default_template return the template defined as default' do + e = fast_create(Environment) + + c1= fast_create(Community, :is_template => true, :environment_id => e.id) + c2 = fast_create(Community, :environment_id => e.id) + c3 = fast_create(Community, :is_template => true, :environment_id => e.id) + + e.settings[:community_template_id]= c3.id + assert_equal c3, e.community_default_template + end + + should 'community_default_template not return a community if its not a template' do + e = fast_create(Environment) + + c1= fast_create(Community, :is_template => true, :environment_id => e.id) + c2 = fast_create(Community, :environment_id => e.id) + c3 = fast_create(Community, :is_template => true, :environment_id => e.id) + + e.settings[:community_template_id]= c2.id + assert_nil e.community_default_template + end + + should 'community_default_template= define a community model passed as paremeter as default template' do + e = fast_create(Environment) + + c1= fast_create(Community, :is_template => true, :environment_id => e.id) + c2 = fast_create(Community, :environment_id => e.id) + c3 = fast_create(Community, :is_template => true, :environment_id => e.id) + + e.community_default_template= c3 + assert_equal c3, e.community_default_template + end + + should 'community_default_template= define an id passed as paremeter as the default template' do + e = fast_create(Environment) + + c1= fast_create(Community, :is_template => true, :environment_id => e.id) + c2 = fast_create(Community, :environment_id => e.id) + c3 = fast_create(Community, :is_template => true, :environment_id => e.id) + + e.community_default_template= c3.id + assert_equal c3, e.community_default_template + end + should 'have a layout template' do e = build(Environment, :layout_template => 'mytemplate') -- libgit2 0.21.2