Browse Source

Move display_attributes

pull/976/head
Kat 3 years ago
parent
commit
ecf00d24c5
  1. 14
      app/helpers/locations_helper.rb
  2. 30
      app/models/location.rb
  3. 2
      app/views/locations/show.html.erb
  4. 27
      spec/helpers/locations_helper_spec.rb
  5. 27
      spec/models/location_spec.rb

14
app/helpers/locations_helper.rb

@ -22,4 +22,18 @@ module LocationsHelper
resource.map { |key, _| OpenStruct.new(id: key, name: key.to_s.humanize) }
end
def display_attributes(location)
[
{ name: "Postcode", value: location.postcode },
{ name: "Local authority", value: location.location_admin_district },
{ name: "Location name", value: location.name, edit: true },
{ name: "Total number of units at this location", value: location.units },
{ name: "Common type of unit", value: location.type_of_unit },
{ name: "Mobility type", value: location.mobility_type },
{ name: "Code", value: location.location_code },
{ name: "Availability", value: "Available from #{location.available_from.to_formatted_s(:govuk_date)}" },
{ name: "Status", value: location.status },
]
end
end

30
app/models/location.rb

@ -359,20 +359,6 @@ class Location < ApplicationRecord
enum type_of_unit: TYPE_OF_UNIT
def display_attributes
[
{ name: "Postcode", value: postcode },
{ name: "Local authority", value: location_admin_district },
{ name: "Location name", value: name, edit: true },
{ name: "Total number of units at this location", value: units },
{ name: "Common type of unit", value: type_of_unit },
{ name: "Mobility type", value: mobility_type },
{ name: "Code", value: location_code },
{ name: "Availability", value: "Available from #{available_from.to_formatted_s(:govuk_date)}" },
{ name: "Status", value: status },
]
end
def postcode=(postcode)
if postcode
super UKPostcode.parse(postcode).to_s
@ -381,6 +367,14 @@ class Location < ApplicationRecord
end
end
def available_from
startdate || created_at
end
def status
"active"
end
private
PIO = PostcodeService.new
@ -399,12 +393,4 @@ private
self.location_admin_district = result[:location_admin_district]
end
end
def available_from
startdate || created_at
end
def status
"active"
end
end

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

@ -13,7 +13,7 @@
<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds-from-desktop">
<%= govuk_summary_list do |summary_list| %>
<% @location.display_attributes.each do |attr| %>
<% display_attributes(@location).each do |attr| %>
<%= summary_list.row do |row| %>
<% row.key { attr[:name] } %>
<% row.value { attr[:name].eql?("Status") ? status_tag(attr[:value]) : details_html(attr) } %>

27
spec/helpers/locations_helper_spec.rb

@ -46,4 +46,31 @@ RSpec.describe LocationsHelper do
expect(selection_options(%w[example])).to eq([OpenStruct.new(id: "example", name: "Example")])
end
end
describe "display_attributes" do
let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 8, 8)) }
it "returns correct display attributes" do
attributes = [
{ name: "Postcode", value: location.postcode },
{ name: "Local authority", value: location.location_admin_district },
{ name: "Location name", value: location.name, edit: true },
{ name: "Total number of units at this location", value: location.units },
{ name: "Common type of unit", value: location.type_of_unit },
{ name: "Mobility type", value: location.mobility_type },
{ name: "Code", value: location.location_code },
{ name: "Availability", value: "Available from 8 August 2022" },
{ name: "Status", value: "active" },
]
expect(display_attributes(location)).to eq(attributes)
end
it "displays created_at as availability date if startdate is not present" do
location.update!(startdate: nil)
availability_attribute = display_attributes(location).find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Available from #{location.created_at.to_formatted_s(:govuk_date)}")
end
end
end

27
spec/models/location_spec.rb

@ -111,31 +111,4 @@ RSpec.describe Location, type: :model do
end
end
end
describe "#display_attributes" do
let(:location) { FactoryBot.build(:location, startdate: Time.zone.local(2022, 8, 8)) }
it "returns correct display attributes" do
attributes = [
{ name: "Postcode", value: location.postcode },
{ name: "Local authority", value: location.location_admin_district },
{ name: "Location name", value: location.name, edit: true },
{ name: "Total number of units at this location", value: location.units },
{ name: "Common type of unit", value: location.type_of_unit },
{ name: "Mobility type", value: location.mobility_type },
{ name: "Code", value: location.location_code },
{ name: "Availability", value: "Available from 8 August 2022" },
{ name: "Status", value: "active" },
]
expect(location.display_attributes).to eq(attributes)
end
it "displays created_at as availability date if startdate is not present" do
location.update!(startdate: nil)
availability_attribute = location.display_attributes.find { |x| x[:name] == "Availability" }[:value]
expect(availability_attribute).to eq("Available from #{location.created_at.to_formatted_s(:govuk_date)}")
end
end
end

Loading…
Cancel
Save