Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: Migrate the AddressComponent to using ViewComponent #2638

Merged
merged 12 commits into from
Nov 18, 2024
Merged
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

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

cant work out how to get rid of this but i think more faff than its worth to remove from the commit now lol

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) %>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@lenikadali i found one more address just fyi! otherwise happy to merge if you are?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hahaha beat me to it 😄

Yes please merge 🙌

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh no theres an test fail now are you ok to fix the test?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure why that is. Locally it doesn't fail though. Let me see 🤞

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Realized I was mixing the integration and controller tests 😅

Can't figure out why it is failing right now. Will look at it tomorrow with a fresher head.


<% 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
Loading