From c6f6b17f71a1d0b3ff479396b57e14ed8ba3d3db Mon Sep 17 00:00:00 2001 From: David McClure Date: Sat, 9 Apr 2016 04:52:55 -0700 Subject: [PATCH] UX: Show website path in website name for all domains Query parameters are still truncated in website name --- app/serializers/user_serializer.rb | 16 +++--------- spec/serializers/user_serializer_spec.rb | 33 +++++++++++++++++------- 2 files changed, 27 insertions(+), 22 deletions(-) diff --git a/app/serializers/user_serializer.rb b/app/serializers/user_serializer.rb index 143279ba8..ff498b7b3 100644 --- a/app/serializers/user_serializer.rb +++ b/app/serializers/user_serializer.rb @@ -144,19 +144,9 @@ class UserSerializer < BasicUserSerializer end def website_name - website_host = URI(website.to_s).host rescue nil - discourse_host = Discourse.current_hostname - return if website_host.nil? - if website_host == discourse_host - # example.com == example.com - website_host + URI(website.to_s).path - elsif (website_host.split('.').length == discourse_host.split('.').length) && discourse_host.split('.').length > 2 - # www.example.com == forum.example.com - website_host.split('.')[1..-1].join('.') == discourse_host.split('.')[1..-1].join('.') ? website_host + URI(website.to_s).path : website_host - else - # example.com == forum.example.com - discourse_host.ends_with?("." << website_host) ? website_host + URI(website.to_s).path : website_host - end + uri = URI(website.to_s) rescue nil + return if uri.nil? || uri.host.nil? + uri.host + uri.path end def include_website_name diff --git a/spec/serializers/user_serializer_spec.rb b/spec/serializers/user_serializer_spec.rb index 16320fdfb..0b79dd7cf 100644 --- a/spec/serializers/user_serializer_spec.rb +++ b/spec/serializers/user_serializer_spec.rb @@ -96,22 +96,37 @@ describe UserSerializer do expect(json[:website]).to eq 'http://example.com/user' end - context "has a website name" do - it "returns website host name when instance domain is not same as website domain" do - Discourse.stubs(:current_hostname).returns('discourse.org') - expect(json[:website_name]).to eq 'example.com' + it "returns complete website name with path" do + expect(json[:website_name]).to eq 'example.com/user' + end + + context "when website includes query parameters" do + before do + user.user_profile.website = 'http://example.com/user?ref=payme' end - it "returns complete website path when instance domain is same as website domain" do - Discourse.stubs(:current_hostname).returns('example.com') - expect(json[:website_name]).to eq 'example.com/user' + it "has a website with query params" do + expect(json[:website]).to eq 'http://example.com/user?ref=payme' end - it "returns complete website path when website domain is parent of instance domain" do - Discourse.stubs(:current_hostname).returns('forums.example.com') + it "has a website name without query params" do expect(json[:website_name]).to eq 'example.com/user' end end + + context "when website is not a valid url" do + before do + user.user_profile.website = 'invalid-url' + end + + it "has a website with the invalid url" do + expect(json[:website]).to eq 'invalid-url' + end + + it "has a nil website name" do + expect(json[:website_name]).to eq nil + end + end end context "with filled out bio" do