Skip to content
This repository has been archived by the owner on Mar 18, 2024. It is now read-only.

Commit

Permalink
Adding and removing Service Areas to/from Partners
Browse files Browse the repository at this point in the history
  • Loading branch information
ivankocienski committed Feb 1, 2022
1 parent a40fc66 commit 67b7ae3
Show file tree
Hide file tree
Showing 6 changed files with 46 additions and 9 deletions.
2 changes: 0 additions & 2 deletions app/components/admin_edit/_admin_edit.html.erb
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
<%= render_component 'admin_flash' %>

<h1 class="page-header"><%= title %></h1>

<div class="form">
Expand Down
38 changes: 34 additions & 4 deletions app/controllers/admin/partners_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/models/partner.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion app/policies/partner_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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?
Expand Down
10 changes: 9 additions & 1 deletion app/views/admin/partners/_form.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,15 @@
<% @all_neighbourhoods.each do |neighbourhood| %>
<%= fields_for "partner[service_areas_attributes][#{neighbourhood.id}]" do |sna|%>
<label>
<%= sna.check_box :neighbourhood_id, {include_hidden: false, checked: false}, neighbourhood.id %>
<%- if (@service_area_id_map.include?(neighbourhood.id)) %>
<%= sna.hidden_field :id, value: @service_area_id_map[neighbourhood.id] %>
<%# if box is unchecked, this will result in the removal of the association %>
<%= sna.check_box :_destroy, {checked: true}, false, true %>

<% else %>
<%= sna.check_box :neighbourhood_id, { include_hidden: false, checked: false }, neighbourhood.id %>
<% end %>

<%= neighbourhood.name %>
</label><br>
<% end %>
Expand Down
1 change: 1 addition & 0 deletions app/views/layouts/admin/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
</div>
</nav>
<main role="main" class="col-md-9 mt-3 ml-sm-auto col-lg-10 px-4">
<%= render_component "admin_flash" %>
<%= yield %>

<% if Rails.env.development? %>
Expand Down

0 comments on commit 67b7ae3

Please sign in to comment.