Skip to content

Commit

Permalink
chore: Migrate the AddressComponent to using ViewComponent (#2638)
Browse files Browse the repository at this point in the history
Co-authored-by: Dr Kim Foale <[email protected]>
  • Loading branch information
lenikadali and kimadactyl authored Nov 18, 2024
1 parent e4b2ed6 commit 4a78e5f
Show file tree
Hide file tree
Showing 10 changed files with 48 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -131,4 +131,4 @@ Lint/SelfAssignment:
Enabled: false
Lint/RedundantCopDisableDirective:
Enabled: false

Empty file removed app/components/address/address.js
Empty file.
Empty file.
Empty file removed app/components/address/address.yml
Empty file.
21 changes: 0 additions & 21 deletions app/components/address/address_component.rb

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
<p class="place_info__address" property="address" typeof="PostalAddress">
<%= formatted_address %>
<%= sanitize formatted_address %>
</p>
22 changes: 22 additions & 0 deletions app/components/address_component.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

class AddressComponent < ViewComponent::Base
def initialize(address:, raw_location: nil)
super
@address = address
@raw_location = raw_location
end

def formatted_address
if @address.present?
address_lines = @address.all_address_lines.map(&:strip)
return address_lines.join(", #{tag.br}")
end

uri = URI.parse(@raw_location)
"<a href='#{uri}'>#{uri.hostname}</a>"

rescue URI::InvalidURIError
@raw_location
end
end
5 changes: 1 addition & 4 deletions app/views/events/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,7 @@
<div class="gi gi__1-3">
<h3 class="h4 udl">Event address</h3>
<div class="small">
<%= render_component "address",
address: @event.address,
raw_location: @event.raw_location_from_source
%>
<%= render AddressComponent.new(address: @event.address, raw_location: @event.raw_location_from_source) %>
</div>
</div>
<div class="gi gi__1-3">
Expand Down
9 changes: 3 additions & 6 deletions app/views/partners/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,8 @@
<% if @partner.has_service_areas? %>
<p>We operate in <%= partner_service_area_text(@partner) %>.</p>
<% end %>
<%= render_component "address",
address: @partner.address
%>

<%= render AddressComponent.new(address: @partner.address) %>

<% unless @partner.accessibility_info_html.blank? %>
<details id='accessibility-info'>
Expand Down Expand Up @@ -97,9 +96,7 @@
<div class="gi gi__1-2">
<h3 class="udl udl--fw allcaps h4">Address</h3>
<div class="small">
<%= render_component "address",
address: place.address
%>
<%= render AddressComponent.new(address: place.address) %>
</div>
<h3 class="udl udl--fw allcaps h4">Contact</h3>
<div class="small">
Expand Down
20 changes: 20 additions & 0 deletions test/components/address_component_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

require 'test_helper'
require 'view_component/test_case'

class AddressComponentTest < ViewComponent::TestCase
setup do
# TODO: once we can make the event factory use a local calendar
# instead of one that makes outgoing HTTP calls, switch to using
# the event factory here. Or remove this TODO once
# we have an integration test
@address = create(:address)
@raw_location = 'Unformatted Address, Ungeolocated Lane, Manchester'
end

def test_component_renders_address
render_inline(AddressComponent.new(address: @address, raw_location: @raw_location))
assert_text '123 Moss Ln E, Manchester, Manchester, M15 5DD'
end
end

0 comments on commit 4a78e5f

Please sign in to comment.