Skip to content

Commit db2e9ed

Browse files
authored
Change links to refer to external_links, if present (#584)
* Change links to refer to external_link, if present for Story, CommunityNews, Resource * Only implement link_target w Linkable, otherwise use object_link_target via decorator (called from taggings index)
1 parent b3521c3 commit db2e9ed

24 files changed

+195
-52
lines changed
Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
11
class ApplicationDecorator < Draper::Decorator
22
delegate_all
33

4-
def link_target
5-
object.respond_to?(:website_url) && object.website_url.present? ?
6-
object.website_url :
7-
default_link_target
4+
def object_link_target
5+
if object.respond_to?(:link_target)
6+
object.link_target
7+
else
8+
object_default_link_target
9+
end
810
end
911

1012
private
1113

12-
def default_link_target
14+
def object_default_link_target
1315
h.polymorphic_path(object)
1416
end
1517
end

app/decorators/resource_decorator.rb

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,6 @@ def detail(length: nil)
44
length ? text&.truncate(length) : text # TODO - rename field
55
end
66

7-
def featured_url
8-
return "" if url.nil?
9-
url.empty? ? h.resource_path(resource) : url
10-
end
11-
12-
137
def main_image_url
148
if main_image&.file&.attached?
159
Rails.application.routes.url_helpers.url_for(main_image.file)

app/models/community_news.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,4 +51,8 @@ def self.search_by_params(params)
5151
community_news = community_news.published_search(params[:published_search]) if params[:published_search].present?
5252
community_news
5353
end
54+
55+
def external_url
56+
reference_url
57+
end
5458
end

app/models/concerns/linkable.rb

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,52 @@
11
module Linkable
22
extend ActiveSupport::Concern
33

4+
included do
5+
# Override this in each model with something like:
6+
# def internal_url
7+
# Rails.application.routes.url_helpers.story_path(self)
8+
# end
9+
end
10+
411
def link_target
5-
website_url.presence || default_link_target
12+
if external_link?
13+
normalized_url(external_url)
14+
else
15+
default_link_target
16+
end
17+
end
18+
19+
def external_link?
20+
external_url.present? && valid_external_url?(external_url)
21+
end
22+
23+
# Models must implement this method.
24+
def external_url
25+
raise NotImplementedError, "Models including Linkable must define #external_url"
626
end
727

828
private
929

30+
def valid_external_url?(value)
31+
return false if value.blank?
32+
33+
# Only normalize *naked domains*, not scheme-bearing strings
34+
if value =~ /\A[\w.-]+\.[a-z]{2,}/i
35+
value = "https://#{value}" unless value =~ /\Ahttps?:\/\//i
36+
end
37+
38+
uri = URI.parse(value)
39+
uri.host.present? && %w[http https].include?(uri.scheme&.downcase)
40+
rescue URI::InvalidURIError
41+
false
42+
end
43+
1044
def default_link_target
1145
Rails.application.routes.url_helpers.polymorphic_path(self)
1246
end
47+
48+
def normalized_url(value)
49+
return "" if value.blank?
50+
value =~ /\Ahttp(s)?:\/\// ? value : "https://#{value}"
51+
end
1352
end

app/models/event.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Event < ApplicationRecord
2-
include Linkable, TagFilterable, WindowsTypeFilterable
2+
include TagFilterable, WindowsTypeFilterable
33

44
belongs_to :created_by, class_name: "User", optional: true
55
has_many :bookmarks, as: :bookmarkable, dependent: :destroy

app/models/facilitator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Facilitator < ApplicationRecord
2-
include Linkable, TagFilterable, WindowsTypeFilterable
2+
include TagFilterable, WindowsTypeFilterable
33

44
belongs_to :created_by, class_name: "User"
55
belongs_to :updated_by, class_name: "User"

app/models/project.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Project < ApplicationRecord
2-
include Linkable, TagFilterable, WindowsTypeFilterable
2+
include TagFilterable, WindowsTypeFilterable
33

44
belongs_to :project_status
55
belongs_to :project_obligation, optional: true

app/models/quote.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Quote < ApplicationRecord
2-
include Linkable, TagFilterable, WindowsTypeFilterable
2+
include TagFilterable, WindowsTypeFilterable
33

44
belongs_to :workshop, optional: true
55
has_many :bookmarks, as: :bookmarkable, dependent: :destroy

app/models/resource.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@ def name
109109
title || id
110110
end
111111

112+
def external_url
113+
url
114+
end
115+
112116
def download_attachment
113117
main_image || gallery_images.first || attachments.first
114118
end

app/models/story.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,10 @@ def name
6262
title
6363
end
6464

65+
def external_url
66+
website_url
67+
end
68+
6569
def organization_name
6670
project&.name
6771
end

0 commit comments

Comments
 (0)