diff --git a/app/controllers/my_profile/memberships_controller.rb b/app/controllers/my_profile/memberships_controller.rb index c3d34a9..fe2d4c7 100644 --- a/app/controllers/my_profile/memberships_controller.rb +++ b/app/controllers/my_profile/memberships_controller.rb @@ -8,9 +8,9 @@ class MembershipsController < MyProfileController def new_community @community = Community.new(params[:community]) + @community.environment = environment @wizard = params[:wizard].blank? ? false : params[:wizard] if request.post? - @community.environment = environment if @community.save @community.add_admin(profile) if @wizard diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 51d08c5..a31aa40 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -729,8 +729,16 @@ module ApplicationHelper form_for(name, object, { :builder => NoosferoFormBuilder }.merge(options), &proc) end - def optional_field(profile, name, field_html = nil, options = {}, &block) + def optional_field(profile, name, field_html = nil, only_required = false, &block) result = "" + + is_required = false + if profile.required_fields.include?(name) + is_required = true + else + return result if only_required + end + if block field_html ||= '' field_html += capture(&block) @@ -744,7 +752,7 @@ module ApplicationHelper result = field_html end end - if profile.required_fields.include?(name) + if is_required result = required(result) end diff --git a/app/models/community.rb b/app/models/community.rb index a94a442..5793f5b 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -10,7 +10,6 @@ class Community < Organization FIELDS = %w[ description nickname - description language ] diff --git a/app/models/environment.rb b/app/models/environment.rb index d02e9c4..81d071c 100644 --- a/app/models/environment.rb +++ b/app/models/environment.rb @@ -339,7 +339,7 @@ class Environment < ActiveRecord::Base end def custom_community_fields=(values) - self.settings[:custom_community_fields] = values.delete_if { |key, value| ! Community.fields.include?(key)} + self.settings[:custom_community_fields] = values.delete_if { |key, value| ! Community.fields.include?(key) } end def custom_community_field(field, status) diff --git a/app/views/memberships/new_community.rhtml b/app/views/memberships/new_community.rhtml index dd8d32c..4b20ed5 100644 --- a/app/views/memberships/new_community.rhtml +++ b/app/views/memberships/new_community.rhtml @@ -10,10 +10,10 @@ <%= required f.text_field(:name, :style => 'width: 100%') %> - <%= f.text_area :description, :style => 'width: 100%; height: 150px;' %> - <%= hidden_field_tag :wizard, params[:wizard] %> + <%= render :partial => 'shared/custom_fields', :locals => { :f => f, :object_name => 'community', :profile => @community, :only_required => true } %> + <%= f.text_field('tag_list', :size => 64) %> <%= content_tag( 'small', _('Separate tags with commas') ) %> diff --git a/app/views/profile_editor/_community_fields.rhtml b/app/views/profile_editor/_community_fields.rhtml deleted file mode 100644 index 05c09a1..0000000 --- a/app/views/profile_editor/_community_fields.rhtml +++ /dev/null @@ -1 +0,0 @@ -<%= optional_field(@profile, 'language', f.text_field(:language)) %> diff --git a/app/views/profile_editor/_enterprise_fields.rhtml b/app/views/profile_editor/_enterprise_fields.rhtml deleted file mode 100644 index 2283686..0000000 --- a/app/views/profile_editor/_enterprise_fields.rhtml +++ /dev/null @@ -1,11 +0,0 @@ -<%= optional_field(@profile, 'contact_phone', f.text_field(:contact_phone)) %> -<%= optional_field(@profile, 'address', labelled_form_field(_('Address (street and number)'), text_field(:profile_data, :address))) %> - -<%= optional_field(@profile, 'zip_code', labelled_form_field(_('ZIP code'), text_field(:profile_data, :zip_code))) %> -<%= optional_field(@profile, 'city', f.text_field(:city)) %> -<%= optional_field(@profile, 'state', f.text_field(:state)) %> -<%= optional_field(@profile, 'country', select_country(_('Country'), 'profile_data', 'country', {:class => 'type-select'})) %> -<%= optional_field(@profile, 'organization_website', f.text_field(:organization_website)) %> -<%= optional_field(@profile, 'historic_and_current_context', f.text_area(:historic_and_current_context, :rows => 5)) %> -<%= optional_field(@profile, 'activities_short_description', f.text_area(:activities_short_description, :rows => 5)) %> -<%= optional_field(@profile, 'management_information', f.text_area(:management_information, :rows => 5)) %> diff --git a/app/views/profile_editor/_organization.rhtml b/app/views/profile_editor/_organization.rhtml index f37d201..cc869a0 100644 --- a/app/views/profile_editor/_organization.rhtml +++ b/app/views/profile_editor/_organization.rhtml @@ -2,24 +2,12 @@ <%= required_fields_message if @profile.required_fields.any? %> -
- -
- <%= text_field_tag 'profile_data[nickname]', @profile_data.nickname, :id => 'profile_data_nickname', :size => 30, :maxlength => 16 %> - <%= _('A short name by which the organization is know.')%> -
- -
<%= f.text_field(:acronym) %> <%= f.text_field(:foundation_year) %> - <%= optional_field(@profile, 'contact_person', f.text_field(:contact_person)) %> - <%= optional_field(@profile, 'contact_email', f.text_field(:contact_email)) %> - <%= optional_field(@profile, 'economic_activity', f.text_field(:economic_activity)) %> - <%= optional_field(@profile, 'description', f.text_area(:description, :rows => 5)) if @profile.community? %> + + <%= render :partial => 'shared/custom_fields', :locals => { :f => f, :object_name => 'profile_data', :profile => @profile, :only_required => false } %> + <%= f.check_box(:enable_contact_us) if @profile.enterprise? %> - - <%= render :partial => 'community_fields', :locals => { :f => f } if @profile.community? %> - <%= render :partial => 'enterprise_fields', :locals => { :f => f } if @profile.enterprise? %>

<%= _('Moderation options') %>

diff --git a/app/views/shared/_custom_fields.rhtml b/app/views/shared/_custom_fields.rhtml new file mode 100644 index 0000000..afe46d7 --- /dev/null +++ b/app/views/shared/_custom_fields.rhtml @@ -0,0 +1,23 @@ +<% if profile.community? %> + <%= optional_field(profile, 'nickname', labelled_form_field(_('Display name'), text_field(object_name, :nickname) + ' ' + content_tag('em', _('A short name by which the organization is know.'))), only_required) %> + <%= optional_field(profile, 'description', f.text_area(:description, :rows => 5)) %> + <%= optional_field(profile, 'language', f.text_field(:language), only_required) %> +<% end %> + +<%= optional_field(profile, 'contact_person', f.text_field(:contact_person), only_required) %> +<%= optional_field(profile, 'contact_email', f.text_field(:contact_email), only_required) %> +<%= optional_field(profile, 'contact_phone', f.text_field(:contact_phone), only_required) %> +<%= optional_field(profile, 'legal_form', f.text_field(:legal_form), only_required) %> +<%= optional_field(profile, 'economic_activity', f.text_field(:economic_activity), only_required) %> +<%= optional_field(profile, 'management_information', f.text_area(:management_information, :rows => 5), only_required) %> +<%= optional_field(profile, 'address', labelled_form_field(_('Address (street and number)'), text_field(object_name, :address)), only_required) %> + +<% if profile.enterprise? %> + <%= optional_field(profile, 'zip_code', labelled_form_field(_('ZIP code'), text_field(object_name, :zip_code)), only_required) %> + <%= optional_field(profile, 'city', f.text_field(:city), only_required) %> + <%= optional_field(profile, 'state', f.text_field(:state), only_required) %> + <%= optional_field(profile, 'country', select_country(_('Country'), object_name, 'country', {:class => 'type-select'}), only_required) %> + <%= optional_field(profile, 'organization_website', f.text_field(:organization_website), only_required) %> + <%= optional_field(profile, 'historic_and_current_context', f.text_area(:historic_and_current_context, :rows => 5), only_required) %> + <%= optional_field(profile, 'activities_short_description', f.text_area(:activities_short_description, :rows => 5), only_required) %> +<% end %> diff --git a/test/functional/memberships_controller_test.rb b/test/functional/memberships_controller_test.rb index 0556331..154abf9 100644 --- a/test/functional/memberships_controller_test.rb +++ b/test/functional/memberships_controller_test.rb @@ -187,4 +187,49 @@ class MembershipsControllerTest < Test::Unit::TestCase assert_equal 1, assigns(:community).boxes[0].blocks.size end + should 'display only required fields when register new community' do + env = Environment.default + env.custom_community_fields = { + 'contact_email' => {'active' => 'true', 'required' => 'true'}, + 'contact_phone' => {'active' => 'true', 'required' => 'false'} + } + env.save! + + get :new_community, :profile => profile.identifier + + assert_tag :tag => 'input', :attributes => { :name => 'community[contact_email]' } + assert_no_tag :tag => 'input', :attributes => { :name => 'community[contact_phone]' } + end + + should 'display all required fields when register new community' do + env = Environment.default + env.custom_community_fields = { + 'contact_email' => {'active' => 'true', 'required' => 'true'}, + 'contact_phone' => {'active' => 'true', 'required' => 'true'} + } + env.save! + + get :new_community, :profile => profile.identifier + + env.required_community_fields.each do |field| + assert_tag :tag => 'input', :attributes => { :name => "community[#{field}]" } + end + end + + should 'set environment when render new community form' do + get :new_community, :profile => profile.identifier + + assert_not_nil assigns(:community).environment + end + + should 'not show description if isnt enabled when register new community' do + env = Environment.default + env.custom_community_fields = { :description => {:active => 'false', :required => 'false'} } + env.save! + + get :new_community, :profile => profile.identifier + + assert_no_tag :tag => 'textarea', :attributes => {:name => 'community[description]'} + end + end diff --git a/test/functional/profile_editor_controller_test.rb b/test/functional/profile_editor_controller_test.rb index 8f20f2b..c3dd7ca 100644 --- a/test/functional/profile_editor_controller_test.rb +++ b/test/functional/profile_editor_controller_test.rb @@ -694,4 +694,49 @@ class ProfileEditorControllerTest < Test::Unit::TestCase assert_template 'edit' end + should 'show active fields when edit community' do + env = Environment.default + env.custom_community_fields = { + 'contact_email' => {'active' => 'true', 'required' => 'false'}, + 'contact_phone' => {'active' => 'true', 'required' => 'false'} + } + env.save! + community = Community.create(:name => 'test_profile') + + get :edit, :profile => community.identifier + + community.active_fields.each do |field| + assert_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" } + end + end + + should 'not show disabled fields when edit community' do + env = Environment.default + env.custom_community_fields = { + 'contact_email' => {'active' => 'false', 'required' => 'false'}, + 'contact_phone' => {'active' => 'false', 'required' => 'false'} + } + env.save! + community = Community.create(:name => 'test_profile') + + get :edit, :profile => community.identifier + + (Community.fields - community.active_fields).each do |field| + assert_no_tag :tag => 'input', :attributes => { :name => "profile_data[#{field}]" } + end + end + + should 'display nickname field only if active when edit community' do + community = Community.create(:name => 'test_profile') + Environment.any_instance.stubs(:required_community_fields).returns([]) + + Environment.any_instance.stubs(:active_community_fields).returns(['description']) + get :edit, :profile => community.identifier + assert_no_tag :tag => 'input', :attributes => { :name => "profile_data[nickname]" } + + Environment.any_instance.stubs(:active_community_fields).returns(['nickname']) + get :edit, :profile => community.identifier + assert_tag :tag => 'input', :attributes => { :name => "profile_data[nickname]" } + end + end diff --git a/test/unit/application_helper_test.rb b/test/unit/application_helper_test.rb index 3f5c3c2..c4cd8fd 100644 --- a/test/unit/application_helper_test.rb +++ b/test/unit/application_helper_test.rb @@ -465,6 +465,13 @@ class ApplicationHelperTest < Test::Unit::TestCase assert_equal "@import url(/designs/icons/default/style.css);", icon_theme_stylesheet_tag end + should 'not display active field if only required' do + profile = mock + profile.expects(:required_fields).returns([]) + + assert_equal '', optional_field(profile, :field_name, '', true) + end + protected def url_for(args = {}) -- libgit2 0.21.2