Commit 739d739dbce9d90434469dc066db8b4dce2ad8cb

Authored by Joenio Costa
Committed by Antonio Terceiro
1 parent d3bae473

ActionItem1160: cant register new community

* set environment when register new community
 * description duplicated in Community.fields model
 * remove duplicated code
app/controllers/my_profile/memberships_controller.rb
@@ -8,9 +8,9 @@ class MembershipsController < MyProfileController @@ -8,9 +8,9 @@ class MembershipsController < MyProfileController
8 8
9 def new_community 9 def new_community
10 @community = Community.new(params[:community]) 10 @community = Community.new(params[:community])
  11 + @community.environment = environment
11 @wizard = params[:wizard].blank? ? false : params[:wizard] 12 @wizard = params[:wizard].blank? ? false : params[:wizard]
12 if request.post? 13 if request.post?
13 - @community.environment = environment  
14 if @community.save 14 if @community.save
15 @community.add_admin(profile) 15 @community.add_admin(profile)
16 if @wizard 16 if @wizard
app/helpers/application_helper.rb
@@ -729,8 +729,16 @@ module ApplicationHelper @@ -729,8 +729,16 @@ module ApplicationHelper
729 form_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc) 729 form_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc)
730 end 730 end
731 731
732 - def optional_field(profile, name, field_html = nil, options = {}, &block) 732 + def optional_field(profile, name, field_html = nil, only_required = false, &block)
733 result = "" 733 result = ""
  734 +
  735 + is_required = false
  736 + if profile.required_fields.include?(name)
  737 + is_required = true
  738 + else
  739 + return result if only_required
  740 + end
  741 +
734 if block 742 if block
735 field_html ||= '' 743 field_html ||= ''
736 field_html += capture(&block) 744 field_html += capture(&block)
@@ -744,7 +752,7 @@ module ApplicationHelper @@ -744,7 +752,7 @@ module ApplicationHelper
744 result = field_html 752 result = field_html
745 end 753 end
746 end 754 end
747 - if profile.required_fields.include?(name) 755 + if is_required
748 result = required(result) 756 result = required(result)
749 end 757 end
750 758
app/models/community.rb
@@ -10,7 +10,6 @@ class Community < Organization @@ -10,7 +10,6 @@ class Community < Organization
10 FIELDS = %w[ 10 FIELDS = %w[
11 description 11 description
12 nickname 12 nickname
13 - description  
14 language 13 language
15 ] 14 ]
16 15
app/models/environment.rb
@@ -339,7 +339,7 @@ class Environment < ActiveRecord::Base @@ -339,7 +339,7 @@ class Environment < ActiveRecord::Base
339 end 339 end
340 340
341 def custom_community_fields=(values) 341 def custom_community_fields=(values)
342 - self.settings[:custom_community_fields] = values.delete_if { |key, value| ! Community.fields.include?(key)} 342 + self.settings[:custom_community_fields] = values.delete_if { |key, value| ! Community.fields.include?(key) }
343 end 343 end
344 344
345 def custom_community_field(field, status) 345 def custom_community_field(field, status)
app/views/memberships/new_community.rhtml
@@ -10,10 +10,10 @@ @@ -10,10 +10,10 @@
10 10
11 <%= required f.text_field(:name, :style => 'width: 100%') %> 11 <%= required f.text_field(:name, :style => 'width: 100%') %>
12 12
13 - <%= f.text_area :description, :style => 'width: 100%; height: 150px;' %>  
14 -  
15 <%= hidden_field_tag :wizard, params[:wizard] %> 13 <%= hidden_field_tag :wizard, params[:wizard] %>
16 14
  15 + <%= render :partial => 'shared/custom_fields', :locals => { :f => f, :object_name => 'community', :profile => @community, :only_required => true } %>
  16 +
17 <%= f.text_field('tag_list', :size => 64) %> 17 <%= f.text_field('tag_list', :size => 64) %>
18 <%= content_tag( 'small', _('Separate tags with commas') ) %> 18 <%= content_tag( 'small', _('Separate tags with commas') ) %>
19 19
app/views/profile_editor/_community_fields.rhtml
@@ -1 +0,0 @@ @@ -1 +0,0 @@
1 -<%= optional_field(@profile, 'language', f.text_field(:language)) %>  
app/views/profile_editor/_enterprise_fields.rhtml
@@ -1,11 +0,0 @@ @@ -1,11 +0,0 @@
1 -<%= optional_field(@profile, 'contact_phone', f.text_field(:contact_phone)) %>  
2 -<%= optional_field(@profile, 'address', labelled_form_field(_('Address (street and number)'), text_field(:profile_data, :address))) %>  
3 -  
4 -<%= optional_field(@profile, 'zip_code', labelled_form_field(_('ZIP code'), text_field(:profile_data, :zip_code))) %>  
5 -<%= optional_field(@profile, 'city', f.text_field(:city)) %>  
6 -<%= optional_field(@profile, 'state', f.text_field(:state)) %>  
7 -<%= optional_field(@profile, 'country', select_country(_('Country'), 'profile_data', 'country', {:class => 'type-select'})) %>  
8 -<%= optional_field(@profile, 'organization_website', f.text_field(:organization_website)) %>  
9 -<%= optional_field(@profile, 'historic_and_current_context', f.text_area(:historic_and_current_context, :rows => 5)) %>  
10 -<%= optional_field(@profile, 'activities_short_description', f.text_area(:activities_short_description, :rows => 5)) %>  
11 -<%= optional_field(@profile, 'management_information', f.text_area(:management_information, :rows => 5)) %>  
app/views/profile_editor/_organization.rhtml
@@ -2,24 +2,12 @@ @@ -2,24 +2,12 @@
2 2
3 <%= required_fields_message if @profile.required_fields.any? %> 3 <%= required_fields_message if @profile.required_fields.any? %>
4 4
5 -<div class="formfieldline">  
6 - <label class="formlabel" for="profile_data_nickname"><%= _('Display name') %></label>  
7 - <div class="formfield type-text">  
8 - <%= text_field_tag 'profile_data[nickname]', @profile_data.nickname, :id => 'profile_data_nickname', :size => 30, :maxlength => 16 %>  
9 - <em><%= _('A short name by which the organization is know.')%></em>  
10 -</div>  
11 -  
12 - </div>  
13 <%= f.text_field(:acronym) %> 5 <%= f.text_field(:acronym) %>
14 <%= f.text_field(:foundation_year) %> 6 <%= f.text_field(:foundation_year) %>
15 - <%= optional_field(@profile, 'contact_person', f.text_field(:contact_person)) %>  
16 - <%= optional_field(@profile, 'contact_email', f.text_field(:contact_email)) %>  
17 - <%= optional_field(@profile, 'economic_activity', f.text_field(:economic_activity)) %>  
18 - <%= optional_field(@profile, 'description', f.text_area(:description, :rows => 5)) if @profile.community? %> 7 +
  8 + <%= render :partial => 'shared/custom_fields', :locals => { :f => f, :object_name => 'profile_data', :profile => @profile, :only_required => false } %>
  9 +
19 <%= f.check_box(:enable_contact_us) if @profile.enterprise? %> 10 <%= f.check_box(:enable_contact_us) if @profile.enterprise? %>
20 -  
21 - <%= render :partial => 'community_fields', :locals => { :f => f } if @profile.community? %>  
22 - <%= render :partial => 'enterprise_fields', :locals => { :f => f } if @profile.enterprise? %>  
23 11
24 <h1><%= _('Moderation options') %></h1> 12 <h1><%= _('Moderation options') %></h1>
25 <div style='margin-bottom: 1em'> 13 <div style='margin-bottom: 1em'>
app/views/shared/_custom_fields.rhtml 0 → 100644
@@ -0,0 +1,23 @@ @@ -0,0 +1,23 @@
  1 +<% if profile.community? %>
  2 + <%= optional_field(profile, 'nickname', labelled_form_field(_('Display name'), text_field(object_name, :nickname) + '&nbsp;' + content_tag('em', _('A short name by which the organization is know.'))), only_required) %>
  3 + <%= optional_field(profile, 'description', f.text_area(:description, :rows => 5)) %>
  4 + <%= optional_field(profile, 'language', f.text_field(:language), only_required) %>
  5 +<% end %>
  6 +
  7 +<%= optional_field(profile, 'contact_person', f.text_field(:contact_person), only_required) %>
  8 +<%= optional_field(profile, 'contact_email', f.text_field(:contact_email), only_required) %>
  9 +<%= optional_field(profile, 'contact_phone', f.text_field(:contact_phone), only_required) %>
  10 +<%= optional_field(profile, 'legal_form', f.text_field(:legal_form), only_required) %>
  11 +<%= optional_field(profile, 'economic_activity', f.text_field(:economic_activity), only_required) %>
  12 +<%= optional_field(profile, 'management_information', f.text_area(:management_information, :rows => 5), only_required) %>
  13 +<%= optional_field(profile, 'address', labelled_form_field(_('Address (street and number)'), text_field(object_name, :address)), only_required) %>
  14 +
  15 +<% if profile.enterprise? %>
  16 + <%= optional_field(profile, 'zip_code', labelled_form_field(_('ZIP code'), text_field(object_name, :zip_code)), only_required) %>
  17 + <%= optional_field(profile, 'city', f.text_field(:city), only_required) %>
  18 + <%= optional_field(profile, 'state', f.text_field(:state), only_required) %>
  19 + <%= optional_field(profile, 'country', select_country(_('Country'), object_name, 'country', {:class => 'type-select'}), only_required) %>
  20 + <%= optional_field(profile, 'organization_website', f.text_field(:organization_website), only_required) %>
  21 + <%= optional_field(profile, 'historic_and_current_context', f.text_area(:historic_and_current_context, :rows => 5), only_required) %>
  22 + <%= optional_field(profile, 'activities_short_description', f.text_area(:activities_short_description, :rows => 5), only_required) %>
  23 +<% end %>
test/functional/memberships_controller_test.rb
@@ -187,4 +187,49 @@ class MembershipsControllerTest &lt; Test::Unit::TestCase @@ -187,4 +187,49 @@ class MembershipsControllerTest &lt; Test::Unit::TestCase
187 assert_equal 1, assigns(:community).boxes[0].blocks.size 187 assert_equal 1, assigns(:community).boxes[0].blocks.size
188 end 188 end
189 189
  190 + should 'display only required fields when register new community' do
  191 + env = Environment.default
  192 + env.custom_community_fields = {
  193 + 'contact_email' => {'active' => 'true', 'required' => 'true'},
  194 + 'contact_phone' => {'active' => 'true', 'required' => 'false'}
  195 + }
  196 + env.save!
  197 +
  198 + get :new_community, :profile => profile.identifier
  199 +
  200 + assert_tag :tag => 'input', :attributes => { :name => 'community[contact_email]' }
  201 + assert_no_tag :tag => 'input', :attributes => { :name => 'community[contact_phone]' }
  202 + end
  203 +
  204 + should 'display all required fields when register new community' do
  205 + env = Environment.default
  206 + env.custom_community_fields = {
  207 + 'contact_email' => {'active' => 'true', 'required' => 'true'},
  208 + 'contact_phone' => {'active' => 'true', 'required' => 'true'}
  209 + }
  210 + env.save!
  211 +
  212 + get :new_community, :profile => profile.identifier
  213 +
  214 + env.required_community_fields.each do |field|
  215 + assert_tag :tag => 'input', :attributes => { :name => "community[#{field}]" }
  216 + end
  217 + end
  218 +
  219 + should 'set environment when render new community form' do
  220 + get :new_community, :profile => profile.identifier
  221 +
  222 + assert_not_nil assigns(:community).environment
  223 + end
  224 +
  225 + should 'not show description if isnt enabled when register new community' do
  226 + env = Environment.default
  227 + env.custom_community_fields = { :description => {:active => 'false', :required => 'false'} }
  228 + env.save!
  229 +
  230 + get :new_community, :profile => profile.identifier
  231 +
  232 + assert_no_tag :tag => 'textarea', :attributes => {:name => 'community[description]'}
  233 + end
  234 +
190 end 235 end
test/functional/profile_editor_controller_test.rb
@@ -694,4 +694,49 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase @@ -694,4 +694,49 @@ class ProfileEditorControllerTest &lt; Test::Unit::TestCase
694 assert_template 'edit' 694 assert_template 'edit'
695 end 695 end
696 696
  697 + should 'show active fields when edit community' do
  698 + env = Environment.default
  699 + env.custom_community_fields = {
  700 + 'contact_email' => {'active' => 'true', 'required' => 'false'},
  701 + 'contact_phone' => {'active' => 'true', 'required' => 'false'}
  702 + }
  703 + env.save!
  704 + community = Community.create(:name => 'test_profile')
  705 +
  706 + get :edit, :profile => community.identifier
  707 +
  708 + community.active_fields.each do |field|
  709 + assert_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" }
  710 + end
  711 + end
  712 +
  713 + should 'not show disabled fields when edit community' do
  714 + env = Environment.default
  715 + env.custom_community_fields = {
  716 + 'contact_email' => {'active' => 'false', 'required' => 'false'},
  717 + 'contact_phone' => {'active' => 'false', 'required' => 'false'}
  718 + }
  719 + env.save!
  720 + community = Community.create(:name => 'test_profile')
  721 +
  722 + get :edit, :profile => community.identifier
  723 +
  724 + (Community.fields - community.active_fields).each do |field|
  725 + assert_no_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" }
  726 + end
  727 + end
  728 +
  729 + should 'display nickname field only if active when edit community' do
  730 + community = Community.create(:name => 'test_profile')
  731 + Environment.any_instance.stubs(:required_community_fields).returns([])
  732 +
  733 + Environment.any_instance.stubs(:active_community_fields).returns(['description'])
  734 + get :edit, :profile => community.identifier
  735 + assert_no_tag :tag => 'input', :attributes => { :name => "profile_data[nickname]" }
  736 +
  737 + Environment.any_instance.stubs(:active_community_fields).returns(['nickname'])
  738 + get :edit, :profile => community.identifier
  739 + assert_tag :tag => 'input', :attributes => { :name => "profile_data[nickname]" }
  740 + end
  741 +
697 end 742 end
test/unit/application_helper_test.rb
@@ -465,6 +465,13 @@ class ApplicationHelperTest &lt; Test::Unit::TestCase @@ -465,6 +465,13 @@ class ApplicationHelperTest &lt; Test::Unit::TestCase
465 assert_equal "@import url(/designs/icons/default/style.css);", icon_theme_stylesheet_tag 465 assert_equal "@import url(/designs/icons/default/style.css);", icon_theme_stylesheet_tag
466 end 466 end
467 467
  468 + should 'not display active field if only required' do
  469 + profile = mock
  470 + profile.expects(:required_fields).returns([])
  471 +
  472 + assert_equal '', optional_field(profile, :field_name, '<html tags>', true)
  473 + end
  474 +
468 protected 475 protected
469 476
470 def url_for(args = {}) 477 def url_for(args = {})