diff --git a/app/components/admin_edit/_admin_edit.html.erb b/app/components/admin_edit/_admin_edit.html.erb
index 59709009e..604fadfcb 100644
--- a/app/components/admin_edit/_admin_edit.html.erb
+++ b/app/components/admin_edit/_admin_edit.html.erb
@@ -1,5 +1,3 @@
-<%= render_component 'admin_flash' %>
-
diff --git a/app/controllers/admin/partners_controller.rb b/app/controllers/admin/partners_controller.rb
index f31d946ca..ee113b32f 100644
--- a/app/controllers/admin/partners_controller.rb
+++ b/app/controllers/admin/partners_controller.rb
@@ -6,6 +6,7 @@ class PartnersController < Admin::ApplicationController
before_action :set_partner, only: %i[show edit update destroy]
before_action :set_tags, only: %i[new create edit]
before_action :set_neighbourhoods, only: %i[new edit]
+ before_action :set_service_area_map_ids, only: %i[new edit]
def index
@partners = policy_scope(Partner).order(:name).includes(:address)
@@ -38,11 +39,19 @@ def create
respond_to do |format|
if @partner.save
- format.html { redirect_to admin_partners_path, notice: 'Partner was successfully created.' }
+ format.html do
+ flash[:success] = 'Partner was successfully created.'
+ redirect_to admin_partners_path
+ end
+
format.json { render :show, status: :created, location: @partner }
else
- set_neighbourhoods
- format.html { render :new }
+ format.html do
+ flash.now[:danger] = 'Partner was not saved.'
+ set_neighbourhoods
+ set_service_area_map_ids
+ render :new
+ end
format.json { render json: @partner.errors, status: :unprocessable_entity }
end
end
@@ -58,8 +67,13 @@ def update
@partner.accessed_by_id = current_user.id
if @partner.update(permitted_attributes(@partner))
+ flash[:success] = 'Partner was successfully updated.'
redirect_to edit_admin_partner_path(@partner)
+
else
+ flash.now[:danger] = 'Partner was not saved.'
+ set_neighbourhoods
+ set_service_area_map_ids
render :edit
end
end
@@ -68,7 +82,10 @@ def destroy
authorize @partner
@partner.destroy
respond_to do |format|
- format.html { redirect_to admin_partners_url, notice: 'Partner was successfully destroyed.' }
+ format.html do
+ flash[:success] = 'Partner was successfully destroyed.'
+ redirect_to admin_partners_url
+ end
format.json { head :no_content }
end
end
@@ -91,6 +108,19 @@ def setup
private
+ def set_service_area_map_ids
+ # maps neighbourhood ID to service_area ID
+ if @partner
+ @service_area_id_map = @partner.
+ service_areas.select(:id, :neighbourhood_id).
+ map { |sa| { sa.neighbourhood_id => sa.id } }.
+ reduce({}, :merge)
+
+ else
+ @service_area_id_map = {}
+ end
+ end
+
def set_neighbourhoods
@all_neighbourhoods = policy_scope(Neighbourhood).order(:name)
end
diff --git a/app/models/partner.rb b/app/models/partner.rb
index 6a3665168..75c38158c 100644
--- a/app/models/partner.rb
+++ b/app/models/partner.rb
@@ -38,7 +38,7 @@ class Partner < ApplicationRecord
accepts_nested_attributes_for :address, reject_if: ->(c) { c[:postcode].blank? && c[:street_address].blank? }
- accepts_nested_attributes_for :service_areas
+ accepts_nested_attributes_for :service_areas, allow_destroy: true
# Validations
validates :name,
diff --git a/app/policies/partner_policy.rb b/app/policies/partner_policy.rb
index 36c3d39b4..954435159 100644
--- a/app/policies/partner_policy.rb
+++ b/app/policies/partner_policy.rb
@@ -47,7 +47,7 @@ def permitted_attributes
:opening_times,
calendars_attributes: %i[id name source strategy place_id partner_id _destroy],
address_attributes: %i[street_address street_address2 street_address3 city postcode],
- service_areas_attributes: %i[id neighbourhood_id],
+ service_areas_attributes: %i[id neighbourhood_id _destroy],
tag_ids: [] ]
attrs << :slug if user.root?
diff --git a/app/views/admin/partners/_form.html.erb b/app/views/admin/partners/_form.html.erb
index ee2f3125e..aac2d010c 100644
--- a/app/views/admin/partners/_form.html.erb
+++ b/app/views/admin/partners/_form.html.erb
@@ -28,7 +28,15 @@
<% @all_neighbourhoods.each do |neighbourhood| %>
<%= fields_for "partner[service_areas_attributes][#{neighbourhood.id}]" do |sna|%>
<% end %>
diff --git a/app/views/layouts/admin/application.html.erb b/app/views/layouts/admin/application.html.erb
index 88f2217af..2d73c565a 100644
--- a/app/views/layouts/admin/application.html.erb
+++ b/app/views/layouts/admin/application.html.erb
@@ -24,6 +24,7 @@
+ <%= render_component "admin_flash" %>
<%= yield %>
<% if Rails.env.development? %>