-
Notifications
You must be signed in to change notification settings - Fork 0
UX: show complete URL path if website domain is same as instance domain #5
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
base: url-handling-pre
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -40,6 +40,7 @@ def self.untrusted_attributes(*attrs) | |
| :bio_cooked, | ||
| :created_at, | ||
| :website, | ||
| :website_name, | ||
| :profile_background, | ||
| :card_background, | ||
| :location, | ||
|
|
@@ -133,6 +134,26 @@ def website | |
| object.user_profile.website | ||
| 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 | ||
| end | ||
|
Comment on lines
+137
to
+151
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion | 🟠 Major Consider refactoring to reduce complexity. The method has high cyclomatic complexity (8/7) and perceived complexity (9/8), making it harder to understand and maintain. Consider extracting helper methods or simplifying the conditional logic. Here's a refactored version that's more readable: def website_name
return unless website.present?
begin
uri = URI(website.to_s)
website_host = uri.host
return unless website_host
path = format_path(uri.path)
should_include_path?(website_host) ? "#{website_host}#{path}" : website_host
rescue URI::InvalidURIError
nil
end
end
private
def should_include_path?(website_host)
discourse_host = Discourse.current_hostname
return true if website_host == discourse_host
return true if domains_share_parent?(website_host, discourse_host)
false
end
def domains_share_parent?(website_host, discourse_host)
# Check if discourse is a subdomain of website
return true if discourse_host.ends_with?(".#{website_host}")
# Check if both are subdomains of the same parent (e.g., www.example.com vs forum.example.com)
website_parts = website_host.split('.')
discourse_parts = discourse_host.split('.')
if website_parts.length == discourse_parts.length && website_parts.length > 2
website_parts[1..-1] == discourse_parts[1..-1]
else
false
end
end
def format_path(path)
path.present? && path != '/' ? path : ''
endAs per coding guidelines (based on RuboCop Metrics violations). 🧰 Tools🪛 RuboCop (1.81.7)[convention] 137-151: Assignment Branch Condition size for (Metrics/AbcSize) [convention] 137-151: Cyclomatic complexity for (Metrics/CyclomaticComplexity) [convention] 137-151: Perceived complexity for (Metrics/PerceivedComplexity) [convention] 138-138: Avoid using (Style/RescueModifier) 🤖 Prompt for AI Agents |
||
|
|
||
| def include_website_name | ||
| website.present? | ||
| end | ||
|
|
||
| def card_image_badge_id | ||
| object.user_profile.card_image_badge.try(:id) | ||
| end | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replace rescue modifier with proper error handling.
Using
rescuein modifier form is not idiomatic Ruby and can hide unexpected errors. It's better to use a proper rescue block or validate the input before parsing.Apply this diff to use proper error handling:
As per coding guidelines (based on RuboCop Style/RescueModifier).
🧰 Tools
🪛 RuboCop (1.81.7)
[convention] 138-138: Avoid using
rescuein its modifier form.(Style/RescueModifier)
🤖 Prompt for AI Agents