mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-18 20:59:39 +02:00
Fix: No comma when locality is empty (small fix) (#2111)
* Fix: No comma when locality is empty * better cleanup on address string * fix test to one-liner * add more testing
This commit is contained in:
parent
298e150f43
commit
6a21f26d2d
3 changed files with 41 additions and 7 deletions
|
@ -2,7 +2,7 @@ class Address < ApplicationRecord
|
|||
belongs_to :addressable, polymorphic: true
|
||||
|
||||
def to_s
|
||||
I18n.t("address.format",
|
||||
string = I18n.t("address.format",
|
||||
line1: line1,
|
||||
line2: line2,
|
||||
county: county,
|
||||
|
@ -11,5 +11,8 @@ class Address < ApplicationRecord
|
|||
country: country,
|
||||
postal_code: postal_code
|
||||
)
|
||||
|
||||
# Clean up the string to maintain I18n comma formatting
|
||||
string.split(",").map(&:strip).reject(&:empty?).join(", ")
|
||||
end
|
||||
end
|
||||
|
|
|
@ -8,8 +8,4 @@ en:
|
|||
locality: Locality
|
||||
postal_code: Postal Code
|
||||
region: Region
|
||||
format: |-
|
||||
%{line1}
|
||||
%{line2}
|
||||
%{locality}, %{region} %{postal_code}
|
||||
%{country}
|
||||
format: "%{line1} %{line2}, %{locality}, %{region} %{postal_code} %{country}"
|
|
@ -10,6 +10,41 @@ class AddressTest < ActiveSupport::TestCase
|
|||
postal_code: "94101"
|
||||
)
|
||||
|
||||
assert_equal "123 Main St\n\nSan Francisco, CA 94101\nUS", address.to_s
|
||||
assert_equal "123 Main St, San Francisco, CA 94101 US", address.to_s
|
||||
end
|
||||
|
||||
test "can print a formatted address with line2" do
|
||||
address = Address.new(
|
||||
line1: "123 Main St",
|
||||
line2: "Apt 1",
|
||||
locality: "San Francisco",
|
||||
region: "CA",
|
||||
country: "US",
|
||||
postal_code: "94101"
|
||||
)
|
||||
|
||||
assert_equal "123 Main St Apt 1, San Francisco, CA 94101 US", address.to_s
|
||||
end
|
||||
|
||||
test "can print empty when address is empty" do
|
||||
address = Address.new(
|
||||
line1: nil,
|
||||
line2: nil,
|
||||
locality: nil,
|
||||
region: nil,
|
||||
country: nil,
|
||||
postal_code: nil
|
||||
)
|
||||
|
||||
assert_equal "", address.to_s
|
||||
end
|
||||
|
||||
test "can strip extras commas and spaces" do
|
||||
address = Address.new(
|
||||
line1: "123 Main St ,",
|
||||
locality: " San Francisco, ",
|
||||
)
|
||||
|
||||
assert_equal "123 Main St, San Francisco", address.to_s
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue