From b1526ae1ae8e90e6b2af5e5b733d4a8c2b3fe88f Mon Sep 17 00:00:00 2001 From: Marcos Ronaldo Date: Tue, 2 Feb 2016 16:05:47 -0200 Subject: [PATCH] fix signup with custom_fields --- app/controllers/my_profile/memberships_controller.rb | 4 +++- app/models/community.rb | 2 +- lib/acts_as_customizable.rb | 2 +- test/functional/account_controller_test.rb | 9 +++++++++ test/functional/memberships_controller_test.rb | 11 +++++++++++ 5 files changed, 25 insertions(+), 3 deletions(-) diff --git a/app/controllers/my_profile/memberships_controller.rb b/app/controllers/my_profile/memberships_controller.rb index a29b90c..c4d24d4 100644 --- a/app/controllers/my_profile/memberships_controller.rb +++ b/app/controllers/my_profile/memberships_controller.rb @@ -18,12 +18,14 @@ class MembershipsController < MyProfileController def new_community @community = Community.new(params[:community]) + custom_values = params[:profile_data][:custom_values] if (params[:profile_data] && params[:profile_data][:custom_values]) + @community.custom_values = custom_values @community.environment = environment @back_to = params[:back_to] || url_for(:action => 'index') if request.post? && @community.valid? begin # Community was created - @community = Community.create_after_moderation(user, params[:community].merge({:environment => environment})) + @community = Community.create_after_moderation(user, params[:community].merge({:environment => environment, :custom_values => custom_values})) @community.reload redirect_to :action => 'welcome', :community_id => @community.id, :back_to => @back_to rescue ActiveRecord::RecordNotFound diff --git a/app/models/community.rb b/app/models/community.rb index aa45ea7..e9a7191 100644 --- a/app/models/community.rb +++ b/app/models/community.rb @@ -33,7 +33,7 @@ class Community < Organization community = Community.new(attributes) community.environment = environment if community.environment.enabled?('admin_must_approve_new_communities') - CreateCommunity.create!(attributes.merge(:requestor => requestor, :environment => environment)) + CreateCommunity.create!(attributes.merge(:requestor => requestor, :environment => environment).except(:custom_values)) else community.save! community.add_admin(requestor) diff --git a/lib/acts_as_customizable.rb b/lib/acts_as_customizable.rb index 6d9db36..5e73b91 100644 --- a/lib/acts_as_customizable.rb +++ b/lib/acts_as_customizable.rb @@ -90,7 +90,7 @@ module Customizable custom_values.each_pair do |key, value| custom_field = environment.custom_fields.detect{|cf|cf.name==key} next if custom_field.blank? - custom_field_value = self.custom_field_values.detect{|cv| cv.custom_field.name==key} + custom_field_value = self.custom_field_values(true).detect{|cv| cv.custom_field.name==key} if custom_field_value.nil? custom_field_value = CustomFieldValue.new diff --git a/test/functional/account_controller_test.rb b/test/functional/account_controller_test.rb index 852655a..4d634fb 100644 --- a/test/functional/account_controller_test.rb +++ b/test/functional/account_controller_test.rb @@ -108,6 +108,15 @@ class AccountControllerTest < ActionController::TestCase end end + def test_shoud_save_with_custom_field_on_signup + assert_difference 'User.count' do + assert_difference 'CustomFieldValue.count' do + CustomField.create!(:name => "zombies", :format=>"String", :default_value => "awrrr", :customized_type=>"Profile", :active => true, :required => true, :signup => true, :environment => Environment.default) + new_user({},{"profile_data"=> {"custom_values"=>{"zombies"=>{"value"=>"BRAINSSS"}}}}) + end + end + end + def test_should_logout login_as :johndoe get :logout diff --git a/test/functional/memberships_controller_test.rb b/test/functional/memberships_controller_test.rb index f08eb9f..10b7352 100644 --- a/test/functional/memberships_controller_test.rb +++ b/test/functional/memberships_controller_test.rb @@ -36,6 +36,17 @@ class MembershipsControllerTest < ActionController::TestCase end end + should 'be able to create a new community with custom field' do + assert_difference 'Community.count' do + assert_difference 'CustomFieldValue.count' do + CustomField.create!(:name => "zombies", :format=>"String", :default_value => "awrrr", :customized_type=>"Community", :active => true, :required => true, :signup => true, :environment => Environment.default) + post :new_community, :profile => profile.identifier, :community => { :name => 'My shiny new community', :description => 'This is a community devoted to anything interesting we find in the internet '}, "profile_data"=>{"custom_values"=>{"zombies"=>{"value"=>"BRAINSSS"}}} + assert_response :redirect + assert Community.find_by_identifier('my-shiny-new-community').members.include?(profile), "Creator user should be added as member of the community just created" + end + end + end + should 'link to new community creation in index' do get :index, :profile => profile.identifier assert_tag :tag => 'a', :attributes => { :href => "/myprofile/#{profile.identifier}/memberships/new_community" } -- libgit2 0.21.2