Commit 9f26e921e6a31fef3ca147db2851b5e7bf5a782b

Authored by Rodrigo Souto
2 parents 3f5de83e e909e2c6

Merge commit 'refs/merge-requests/302' of git://gitorious.org/noosfero/noosfero …

…into merge-requests/302
app/helpers/application_helper.rb
@@ -1330,11 +1330,12 @@ module ApplicationHelper @@ -1330,11 +1330,12 @@ module ApplicationHelper
1330 end 1330 end
1331 1331
1332 def template_options(klass, field_name) 1332 def template_options(klass, field_name)
1333 - return '' if klass.templates.count == 0  
1334 - return hidden_field_tag("#{field_name}[template_id]", klass.templates.first.id) if klass.templates.count == 1 1333 + templates = klass.templates(environment.id)
  1334 + return '' if templates.count == 0
  1335 + return hidden_field_tag("#{field_name}[template_id]", templates.first.id) if templates.count == 1
1335 1336
1336 counter = 0 1337 counter = 0
1337 - radios = klass.templates.map do |template| 1338 + radios = templates.map do |template|
1338 counter += 1 1339 counter += 1
1339 content_tag('li', labelled_radio_button(link_to(template.name, template.url, :target => '_blank'), "#{field_name}[template_id]", template.id, counter==1)) 1340 content_tag('li', labelled_radio_button(link_to(template.name, template.url, :target => '_blank'), "#{field_name}[template_id]", template.id, counter==1))
1340 end.join("\n") 1341 end.join("\n")
app/models/profile.rb
@@ -68,7 +68,7 @@ class Profile < ActiveRecord::Base @@ -68,7 +68,7 @@ class Profile < ActiveRecord::Base
68 #FIXME: these will work only if the subclass is already loaded 68 #FIXME: these will work only if the subclass is already loaded
69 named_scope :enterprises, lambda { {:conditions => (Enterprise.send(:subclasses).map(&:name) << 'Enterprise').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")} } 69 named_scope :enterprises, lambda { {:conditions => (Enterprise.send(:subclasses).map(&:name) << 'Enterprise').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")} }
70 named_scope :communities, lambda { {:conditions => (Community.send(:subclasses).map(&:name) << 'Community').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")} } 70 named_scope :communities, lambda { {:conditions => (Community.send(:subclasses).map(&:name) << 'Community').map { |klass| "profiles.type = '#{klass}'"}.join(" OR ")} }
71 - named_scope :templates, :conditions => {:is_template => true} 71 + named_scope :templates, lambda { |environment_id| { :conditions => {:is_template => true, :environment_id => environment_id} } }
72 72
73 def members 73 def members
74 scopes = plugins.dispatch_scopes(:organization_members, self) 74 scopes = plugins.dispatch_scopes(:organization_members, self)
app/views/templates/index.html.erb
@@ -2,9 +2,9 @@ @@ -2,9 +2,9 @@
2 2
3 <%= _('Manage the templates used on creation of profiles') %> 3 <%= _('Manage the templates used on creation of profiles') %>
4 4
5 -<% list_of_templates = [[_('Person') , Person.templates , 'person' ],  
6 - [_('Community') , Community.templates , 'community' ],  
7 - [_('Enterprise'), Enterprise.templates, 'enterprise']] %> 5 +<% list_of_templates = [[_('Person') , Person.templates(environment.id) , 'person' ],
  6 + [_('Community') , Community.templates(environment.id) , 'community' ],
  7 + [_('Enterprise'), Enterprise.templates(environment.id), 'enterprise']] %>
8 8
9 <% list_of_templates.each do |title, templates, kind|%> 9 <% list_of_templates.each do |title, templates, kind|%>
10 <div class='template-kind'> 10 <div class='template-kind'>
test/functional/account_controller_test.rb
@@ -579,6 +579,21 @@ class AccountControllerTest &lt; ActionController::TestCase @@ -579,6 +579,21 @@ class AccountControllerTest &lt; ActionController::TestCase
579 assert_equal 1, assigns(:user).person.boxes[0].blocks.size 579 assert_equal 1, assigns(:user).person.boxes[0].blocks.size
580 end 580 end
581 581
  582 + should 'display only templates of the current environment' do
  583 + env2 = fast_create(Environment)
  584 +
  585 + template1 = fast_create(Person, :name => 'template1', :environment_id => Environment.default.id, :is_template => true)
  586 + template2 = fast_create(Person, :name => 'template2', :environment_id => Environment.default.id, :is_template => true)
  587 + template3 = fast_create(Person, :name => 'template3', :environment_id => env2.id, :is_template => true)
  588 +
  589 + get :signup
  590 + assert_select '#template-options' do |elements|
  591 + assert_match /template1/, elements[0].to_s
  592 + assert_match /template2/, elements[0].to_s
  593 + assert_no_match /template3/, elements[0].to_s
  594 + end
  595 + end
  596 +
582 should 'render person partial' do 597 should 'render person partial' do
583 Environment.any_instance.expects(:signup_person_fields).returns(['contact_phone']).at_least_once 598 Environment.any_instance.expects(:signup_person_fields).returns(['contact_phone']).at_least_once
584 get :signup 599 get :signup
test/functional/enterprise_registration_controller_test.rb
@@ -203,4 +203,21 @@ class EnterpriseRegistrationControllerTest &lt; ActionController::TestCase @@ -203,4 +203,21 @@ class EnterpriseRegistrationControllerTest &lt; ActionController::TestCase
203 assert_tag :tag => 'input', :attributes => {:id => 'create_enterprise_plugin1', :type => 'hidden', :value => 'Plugin 1'} 203 assert_tag :tag => 'input', :attributes => {:id => 'create_enterprise_plugin1', :type => 'hidden', :value => 'Plugin 1'}
204 assert_tag :tag => 'input', :attributes => {:id => 'create_enterprise_plugin2', :type => 'hidden', :value => 'Plugin 2'} 204 assert_tag :tag => 'input', :attributes => {:id => 'create_enterprise_plugin2', :type => 'hidden', :value => 'Plugin 2'}
205 end 205 end
  206 +
  207 + should 'display only templates of the current environment' do
  208 + env2 = fast_create(Environment)
  209 +
  210 + template1 = fast_create(Enterprise, :name => 'template1', :environment_id => Environment.default.id, :is_template => true)
  211 + template2 = fast_create(Enterprise, :name => 'template2', :environment_id => Environment.default.id, :is_template => true)
  212 + template3 = fast_create(Enterprise, :name => 'template3', :environment_id => env2.id, :is_template => true)
  213 +
  214 + get :index
  215 +
  216 + assert_select '#template-options' do |elements|
  217 + assert_match /template1/, elements[0].to_s
  218 + assert_match /template2/, elements[0].to_s
  219 + assert_no_match /template3/, elements[0].to_s
  220 + end
  221 + end
  222 +
206 end 223 end
test/functional/memberships_controller_test.rb
@@ -154,6 +154,22 @@ class MembershipsControllerTest &lt; ActionController::TestCase @@ -154,6 +154,22 @@ class MembershipsControllerTest &lt; ActionController::TestCase
154 assert_equal 1, assigns(:community).boxes[0].blocks.size 154 assert_equal 1, assigns(:community).boxes[0].blocks.size
155 end 155 end
156 156
  157 + should 'display only templates of the current environment' do
  158 + env2 = fast_create(Environment)
  159 +
  160 + template1 = fast_create(Community, :name => 'template1', :environment_id => Environment.default.id, :is_template => true)
  161 + template2 = fast_create(Community, :name => 'template2', :environment_id => Environment.default.id, :is_template => true)
  162 + template3 = fast_create(Community, :name => 'template3', :environment_id => env2.id, :is_template => true)
  163 +
  164 + get :new_community, :profile => profile.identifier
  165 +
  166 + assert_select '#template-options' do |elements|
  167 + assert_match /template1/, elements[0].to_s
  168 + assert_match /template2/, elements[0].to_s
  169 + assert_no_match /template3/, elements[0].to_s
  170 + end
  171 + end
  172 +
157 should 'display only required fields when register new community' do 173 should 'display only required fields when register new community' do
158 env = Environment.default 174 env = Environment.default
159 env.custom_community_fields = { 175 env.custom_community_fields = {
test/unit/application_helper_test.rb
@@ -243,6 +243,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase @@ -243,6 +243,7 @@ class ApplicationHelperTest &lt; ActiveSupport::TestCase
243 end 243 end
244 244
245 should 'not display templates options when there is no template' do 245 should 'not display templates options when there is no template' do
  246 + self.stubs(:environment).returns(Environment.default)
246 [Person, Community, Enterprise].each do |klass| 247 [Person, Community, Enterprise].each do |klass|
247 assert_equal '', template_options(klass, 'profile_data') 248 assert_equal '', template_options(klass, 'profile_data')
248 end 249 end
test/unit/profile_test.rb
@@ -1444,9 +1444,9 @@ class ProfileTest &lt; ActiveSupport::TestCase @@ -1444,9 +1444,9 @@ class ProfileTest &lt; ActiveSupport::TestCase
1444 t2 = fast_create(Profile, :is_template => true) 1444 t2 = fast_create(Profile, :is_template => true)
1445 profile = fast_create(Profile) 1445 profile = fast_create(Profile)
1446 1446
1447 - assert_includes Profile.templates, t1  
1448 - assert_includes Profile.templates, t2  
1449 - assert_not_includes Profile.templates, profile 1447 + assert_includes Profile.templates(Environment.default.id), t1
  1448 + assert_includes Profile.templates(Environment.default.id), t2
  1449 + assert_not_includes Profile.templates(Environment.default.id), profile
1450 end 1450 end
1451 1451
1452 should 'provide URL to leave' do 1452 should 'provide URL to leave' do