Skip to content

Commit

Permalink
Re-add ERB component file, Rubocop and test fixes
Browse files Browse the repository at this point in the history
Re-added the ERB component file so that there's a clear
separation of concerns. This also allows us to keep the code
easy to read; in case the template grows bigger, it will still
be readable.

Fixed the Rails/OutputSafety cop by moving the sanitize call
to within the template instead of trying to do it with the component

Also switched to using the `address` factory instead of a string
value; the value the component expects should be an instance of
the Address model and consequently the `all_address_lines` method
can be called on it.
  • Loading branch information
lenikadali committed Nov 15, 2024
1 parent 9a530b1 commit f3b5f50
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 11 deletions.
3 changes: 3 additions & 0 deletions app/components/address_component.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<p class="place_info__address" property="address" typeof="PostalAddress">
<%= sanitize formatted_address %>
</p>
10 changes: 2 additions & 8 deletions app/components/address_component.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
# frozen_string_literal: true

class AddressComponent < ViewComponent::Base
erb_template <<~ERB
<p class="place_info__address" property="address" typeof="PostalAddress">
<%= formatted_address %>
</p>
ERB

def initialize(address:, raw_location: nil)
super
@address = address
Expand All @@ -16,11 +10,11 @@ def initialize(address:, raw_location: nil)
def formatted_address
if @address.present?
address_lines = @address.all_address_lines.map(&:strip)
return address_lines.join(', <br>').html_safe # rubocop:disable Rails/OutputSafety
return address_lines.join(", #{tag.br}")
end

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

rescue URI::InvalidURIError
raw_location
Expand Down
10 changes: 7 additions & 3 deletions test/components/address_component_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@

class AddressComponentTest < ViewComponent::TestCase
setup do
@address = '123 Moss Ln E'
# 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 'Unformatted Address, Ungeolocated Lane, Manchester'
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 f3b5f50

Please sign in to comment.