Skip to content

Commit

Permalink
Allow automatic creation/activation of programme & project together i…
Browse files Browse the repository at this point in the history
…f setting is enabled
  • Loading branch information
fbacall committed Jan 9, 2024
1 parent 2e099c8 commit 8768624
Show file tree
Hide file tree
Showing 6 changed files with 128 additions and 16 deletions.
25 changes: 21 additions & 4 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,11 +221,28 @@ def request_create
elsif Seek::ProjectFormProgrammeOptions.creation_allowed?
prog_params = params.require(:programme).permit([:title])
@programme = Programme.new(prog_params)
log = ProjectCreationMessageLog.log_request(sender:current_person, programme:@programme, project:@project, institution:@institution)
if Seek::Config.email_enabled && !User.admin_logged_in?
Mailer.request_create_project_and_programme(current_user, @programme.to_json, @project.to_json, @institution.to_json, log).deliver_later
if Seek::Config.auto_activate_programmes
@project.programme = @programme
errors = confirm_project_create_request(current_person, skip_permissions: true)
if errors.present?
flash.now[:error] = errors
render action: :guided_create, status: :unprocessable_entity
else
@programme.activate
flash[:notice] = "Thank you, your #{t('programme')} and #{t('project')} have been created"
if Seek::Config.email_enabled
Mailer.notify_admins_project_creation_accepted(nil, current_person, @project).deliver_later
end
redirect_to(@project)
end
return
else
log = ProjectCreationMessageLog.log_request(sender:current_person, programme:@programme, project:@project, institution:@institution)
if Seek::Config.email_enabled && !User.admin_logged_in?
Mailer.request_create_project_and_programme(current_user, @programme.to_json, @project.to_json, @institution.to_json, log).deliver_later
end
flash.now[:notice] = "Thank you, your request for a new #{t('programme')} and #{t('project')} has been sent"
end
flash.now[:notice] = "Thank you, your request for a new #{t('programme')} and #{t('project')} has been sent"
# No Programme at all
elsif !Seek::ProjectFormProgrammeOptions.show_programme_box?
@programme=nil
Expand Down
3 changes: 3 additions & 0 deletions app/views/projects/guided_create.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@
}
checkSubmitButtonEnabled();
});

$j('input#managed_programme').change(); // Trigger initial state on page load
<% end %>

<% if Seek::ProjectFormProgrammeOptions.creation_allowed? && Seek::ProjectFormProgrammeOptions.programme_dropdown? %>
Expand All @@ -83,6 +85,7 @@
}
checkSubmitButtonEnabled();
});
$j('input#new_programme').change(); // Trigger initial state on page load
<% end %>

$j('input#programme_title').on('input',function() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,6 @@
it. To have one created, then please provide the name. You will be able to provide additional details, if you wish, once it has been created.
</div>
<label>
<%= check_box_tag :managed_programme, '1', true %>
<%= check_box_tag :managed_programme, '1', @programme.nil? || @programme.site_managed? %>
<%= "#{Seek::Config.instance_admins_name} managed #{t('programme')}?" %>
</label>
17 changes: 7 additions & 10 deletions app/views/projects/guided_create/_programme_details.html.erb
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
<% if Seek::ProjectFormProgrammeOptions.creation_allowed_only? %>
<div id='programme-details'>
<% else %>
<div id='programme-details' style="display:none;">
<% end %>
<div class="help-block">
Specify a title for a new <%= t('programme') %>, which your new <%= t('project') %> will be associated with.
</div>
<%= label_tag :programme_title, "Title" %><span class="required">*</span>
<%= text_field_tag 'programme[title]', '', class: 'form-control' %>
<% show = Seek::ProjectFormProgrammeOptions.creation_allowed_only? || (@programme && [email protected]_managed?) %>
<div id='programme-details'<%= 'style="display:none;"' unless show -%>>
<div class="help-block">
Specify a title for a new <%= t('programme') %>, which your new <%= t('project') %> will be associated with.
</div>
<%= label_tag :programme_title, "Title" %><span class="required">*</span>
<%= text_field_tag 'programme[title]', @programme&.title, class: 'form-control' %>
</div>
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
Alternatively you can choose to create a new <%= t('programme') %>, which your new <%= t('project') %> will be associated with.
</div>
<label>
<%= check_box_tag :new_programme, '1', false %>
<%= check_box_tag :new_programme, '1', @programme && !@programme.site_managed? %>
<%= "Create a new #{t('programme')}?" %>
</label>
<% end %>
95 changes: 95 additions & 0 deletions test/functional/projects_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2244,6 +2244,101 @@ def test_admin_can_edit
end
end

test 'request create project with new programme and institution with auto activation' do
with_config_value(:auto_activate_programmes, true) do
FactoryBot.create(:admin)
person = FactoryBot.create(:person_not_in_project)
managed_programme = FactoryBot.create(:programme)
assert Person.admins.count > 1
login_as(person)
with_config_value(:managed_programme_id, managed_programme.id) do
params = {
project: { title: 'The Project', description: 'description', web_page: 'https://example.com' },
institution: { id: ['the inst'], title: 'the inst', web_page: 'https://example.com/inst', city: 'London', country: 'GB' },
programme_id: '',
programme: { title: 'the prog' }
}
project = nil
assert_enqueued_emails(1) do
assert_difference('Programme.count', 1) do
assert_difference('Project.count', 1) do
assert_difference('Institution.count', 1) do
assert_no_difference('ProjectCreationMessageLog.count') do
post :request_create, params: params

project = Project.last
assert_redirected_to project_path(project)
end
end
end
end
end

programme = Programme.last
institution = Institution.last

assert flash[:notice]
gm = person.reload.group_memberships.last
assert_equal 'description', gm.project.description
assert_equal 'The Project', gm.project.title
assert_equal project, gm.project

assert_equal programme, gm.project.programme
assert programme.is_activated?
assert_equal 'the prog', programme.title

assert_equal institution, gm.institution
assert_equal 'the inst', institution.title
assert_equal 'https://example.com/inst', institution.web_page
assert_equal 'London', institution.city
assert_equal 'GB', institution.country
end
end
end

test 'request create project with new programme and institution with auto activation and errors' do
with_config_value(:auto_activate_programmes, true) do
FactoryBot.create(:admin)
person = FactoryBot.create(:person_not_in_project)
existing_programme = FactoryBot.create(:programme, title: 'A Cool Programme')
managed_programme = FactoryBot.create(:programme)
assert Person.admins.count > 1
login_as(person)
with_config_value(:managed_programme_id, managed_programme.id) do
params = {
project: { title: '', description: 'description', web_page: 'https://example.com' },
institution: { id: ['the inst'], title: 'the inst', web_page: 'https://example.com/inst', city: 'London', country: 'XY' },
programme_id: '',
programme: { title: 'A Cool Programme' }
}
project = nil
assert_no_enqueued_emails do
assert_no_difference('Programme.count') do
assert_no_difference('Project.count') do
assert_no_difference('Institution.count') do
assert_no_difference('ProjectCreationMessageLog.count') do
post :request_create, params: params

assert_response :unprocessable_entity
end
end
end
end
end

assert flash[:error].include?("The Project is invalid, Title can't be blank")
assert flash[:error].include?("The Programme is invalid, Title has already been taken")
assert flash[:error].include?("The Institution is invalid, Country isn't a valid country or code")
assert_equal 'the inst', assigns(:institution).title
assert_equal 'https://example.com/inst', assigns(:institution).web_page
assert_equal 'description', assigns(:project).description
assert_equal '', assigns(:project).title
assert_equal 'https://example.com', assigns(:project).web_page
assert_equal 'A Cool Programme', assigns(:programme).title
end
end
end

test 'request create project without programmes' do
person = FactoryBot.create(:person_not_in_project)

Expand Down

0 comments on commit 8768624

Please sign in to comment.