Browse Source

routes refactoring

pull/704/head
JG 3 years ago
parent
commit
a2fe222db7
  1. 8
      app/controllers/locations_controller.rb
  2. 2
      app/controllers/schemes_controller.rb
  3. 2
      app/helpers/tab_nav_helper.rb
  4. 2
      app/models/location.rb
  5. 49
      app/views/locations/index.html.erb
  6. 2
      app/views/locations/new.html.erb
  7. 6
      app/views/schemes/check_answers.html.erb
  8. 4
      app/views/schemes/locations.html.erb
  9. 11
      config/routes.rb

8
app/controllers/locations_controller.rb

@ -1,4 +1,5 @@
class LocationsController < ApplicationController class LocationsController < ApplicationController
include Pagy::Backend
before_action :authenticate_user! before_action :authenticate_user!
before_action :authenticate_scope! before_action :authenticate_scope!
before_action :find_location, except: %i[new create] before_action :find_location, except: %i[new create]
@ -10,6 +11,7 @@ class LocationsController < ApplicationController
end end
def create def create
debugger
@location = Location.new(location_params) @location = Location.new(location_params)
if @location.save if @location.save
@ -29,6 +31,12 @@ class LocationsController < ApplicationController
end end
end end
def index
@scheme = Scheme.find_by(id: params[:id])
@pagy, @locations = pagy(@scheme.locations)
@total_count = @scheme.locations.size
end
private private
def find_scheme def find_scheme

2
app/controllers/schemes_controller.rb

@ -100,7 +100,7 @@ private
when "secondary-client-group" when "secondary-client-group"
scheme_support_path(@scheme) scheme_support_path(@scheme)
when "support" when "support"
location_new_scheme_path new_location_path
when "details" when "details"
scheme_primary_client_group_path(@scheme) scheme_primary_client_group_path(@scheme)
end end

2
app/helpers/tab_nav_helper.rb

@ -8,7 +8,7 @@ module TabNavHelper
def location_cell(location) def location_cell(location)
link_text = location.postcode link_text = location.postcode
[govuk_link_to(link_text, location_scheme_path(location, scheme_id: location.scheme.id)), "<span class=\"govuk-visually-hidden\">Location </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{location.name}</span>"].join("\n") [govuk_link_to(link_text, location_path(location)), "<span class=\"govuk-visually-hidden\">Location </span><span class=\"govuk-!-font-weight-regular app-!-colour-muted\">#{location.name}</span>"].join("\n")
end end
def scheme_cell(scheme) def scheme_cell(scheme)

2
app/models/location.rb

@ -7,7 +7,7 @@ class Location < ApplicationRecord
WHEELCHAIR_ADAPTATIONS = { WHEELCHAIR_ADAPTATIONS = {
Yes: 1, Yes: 1,
No: 0 No: 0,
}.freeze }.freeze
enum wheelchair_adaptation: WHEELCHAIR_ADAPTATIONS enum wheelchair_adaptation: WHEELCHAIR_ADAPTATIONS

49
app/views/locations/index.html.erb

@ -0,0 +1,49 @@
<% title = @scheme.service_name %>
<% content_for :title, title %>
<% content_for :before_content do %>
<%= govuk_back_link(
text: "Back",
href: "/schemes/#{@scheme.id}",
) %>
<% end %>
<%= render partial: "organisations/headings", locals: { main: @scheme.service_name, sub: nil } %>
<% location_caption = @scheme.locations.count.eql?(1) ? "1 location" : "#{@scheme.locations.count} locations" %>
<%= render SubNavigationComponent.new(items: scheme_items(request.path, @scheme.id, location_caption)) %>
<div class="govuk-grid-row">
<div class="govuk-grid-column-three-quarters">
<%= govuk_table do |table| %>
<%= table.caption(classes: %w[govuk-!-font-size-19 govuk-!-font-weight-regular]) do |caption| %>
<strong><%= @scheme.locations.count %></strong> <%= @scheme.locations.count.eql?(1) ? "location" : "locations" %>.
<% end %>
<%= table.head do |head| %>
<%= head.row do |row| %>
<% row.cell(header: true, text: "Code", html_attributes: {
scope: "col",
}) %>
<% row.cell(header: true, text: "Postcode", html_attributes: {
scope: "col",
}) %>
<% row.cell(header: true, text: "Units", html_attributes: {
scope: "col",
}) %>
<% row.cell(header: true, text: "Common unit type", html_attributes: {
scope: "col",
}) %>
<% end %>
<% end %>
<% @locations.each do |location| %>
<%= table.body do |body| %>
<%= body.row do |row| %>
<% row.cell(text: location.id) %>
<% row.cell(text: location.postcode) %>
<% row.cell(text: location.total_units) %>
<% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>#{location.wheelchair_adaptation == 'Yes' ? "\n<span class=\"govuk-!-font-weight-regular app-!-colour-muted\">With wheelchair adaptations</span>" : ''}")) %>
<% end %>
<% end %>
<% end %>
<% end %>
</div>
</div>
<%== render partial: "pagy/nav", locals: { pagy: @pagy, item_name: "locations" } %>

2
app/views/locations/new.html.erb

@ -9,7 +9,7 @@
<%= render partial: "organisations/headings", locals: { main: "Add a location to this scheme", sub: @scheme.service_name } %> <%= render partial: "organisations/headings", locals: { main: "Add a location to this scheme", sub: @scheme.service_name } %>
<%= form_for(@location, method: :post, url: location_create_scheme_path) do |f| %> <%= form_for(@location, method: :post, url: locations_path) do |f| %>
<div class="govuk-grid-row"> <div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds"> <div class="govuk-grid-column-two-thirds">
<%= f.govuk_error_summary %> <%= f.govuk_error_summary %>

6
app/views/schemes/check_answers.html.erb

@ -83,9 +83,6 @@
<% row.cell(header: true, text: "Common unit type", html_attributes: { <% row.cell(header: true, text: "Common unit type", html_attributes: {
scope: "col", scope: "col",
}) %> }) %>
<% row.cell(header: true, text: "Actions", html_attributes: {
scope: "col",
}) %>
<% end %> <% end %>
<% end %> <% end %>
<% @scheme.locations.each do |location| %> <% @scheme.locations.each do |location| %>
@ -95,12 +92,11 @@
<% row.cell(text: simple_format(location_cell(location), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %> <% row.cell(text: simple_format(location_cell(location), { class: "govuk-!-font-weight-bold" }, wrapper_tag: "div")) %>
<% row.cell(text: location.total_units) %> <% row.cell(text: location.total_units) %>
<% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>#{location.wheelchair_adaptation == 'Yes' ? "\n<span class=\"govuk-!-font-weight-regular app-!-colour-muted\">With wheelchair adaptations</span>" : ''}")) %> <% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>#{location.wheelchair_adaptation == 'Yes' ? "\n<span class=\"govuk-!-font-weight-regular app-!-colour-muted\">With wheelchair adaptations</span>" : ''}")) %>
<% row.cell(text: "Delete") %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>
<%= govuk_button_link_to "Add a location", location_new_scheme_path(id: @scheme.id), secondary: true %> <%= govuk_button_link_to "Add a location", new_location_path(id: @scheme.id), secondary: true %>
<% end %> <% end %>
<% end %> <% end %>
</div> </div>

4
app/views/schemes/locations.html.erb

@ -30,9 +30,6 @@
<% row.cell(header: true, text: "Common unit type", html_attributes: { <% row.cell(header: true, text: "Common unit type", html_attributes: {
scope: "col", scope: "col",
}) %> }) %>
<% row.cell(header: true, text: "Actions", html_attributes: {
scope: "col",
}) %>
<% end %> <% end %>
<% end %> <% end %>
<% @locations.each do |location| %> <% @locations.each do |location| %>
@ -42,7 +39,6 @@
<% row.cell(text: location.postcode) %> <% row.cell(text: location.postcode) %>
<% row.cell(text: location.total_units) %> <% row.cell(text: location.total_units) %>
<% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>#{location.wheelchair_adaptation == 'Yes' ? "\n<span class=\"govuk-!-font-weight-regular app-!-colour-muted\">With wheelchair adaptations</span>" : ''}")) %> <% row.cell(text: simple_format("<span>#{location.type_of_unit}</span>#{location.wheelchair_adaptation == 'Yes' ? "\n<span class=\"govuk-!-font-weight-regular app-!-colour-muted\">With wheelchair adaptations</span>" : ''}")) %>
<% row.cell(text: "Delete") %>
<% end %> <% end %>
<% end %> <% end %>
<% end %> <% end %>

11
config/routes.rb

@ -44,11 +44,12 @@ Rails.application.routes.draw do
get "check-answers", to: "schemes#check_answers" get "check-answers", to: "schemes#check_answers"
member do member do
get "locations", to: "schemes#locations" resources :locations
get "location/new", to: "locations#new" # get "locations", to: "schemes#locations"
post "location/create", to: "locations#create" # get "location/new", to: "locations#new"
get "location", to: "locations#details" # post "location/create", to: "locations#create"
patch "location", to: "locations#update" # get "location", to: "locations#details"
# patch "location", to: "locations#update"
end end
end end

Loading…
Cancel
Save