Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 7 additions & 5 deletions app/decorators/application_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
class ApplicationDecorator < Draper::Decorator
delegate_all

def link_target
object.respond_to?(:website_url) && object.website_url.present? ?
object.website_url :
default_link_target
def object_link_target
if object.respond_to?(:link_target)
object.link_target
else
object_default_link_target
end
end

private

def default_link_target
def object_default_link_target
h.polymorphic_path(object)
end
end
6 changes: 0 additions & 6 deletions app/decorators/resource_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ def detail(length: nil)
length ? text&.truncate(length) : text # TODO - rename field
end

def featured_url
return "" if url.nil?
url.empty? ? h.resource_path(resource) : url
end


def main_image_url
if main_image&.file&.attached?
Rails.application.routes.url_helpers.url_for(main_image.file)
Expand Down
4 changes: 4 additions & 0 deletions app/models/community_news.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,4 +51,8 @@ def self.search_by_params(params)
community_news = community_news.published_search(params[:published_search]) if params[:published_search].present?
community_news
end

def external_url
reference_url
end
end
41 changes: 40 additions & 1 deletion app/models/concerns/linkable.rb
Original file line number Diff line number Diff line change
@@ -1,13 +1,52 @@
module Linkable
extend ActiveSupport::Concern

included do
# Override this in each model with something like:
# def internal_url
# Rails.application.routes.url_helpers.story_path(self)
# end
end

def link_target
website_url.presence || default_link_target
if external_link?
normalized_url(external_url)
else
default_link_target
end
end

def external_link?
external_url.present? && valid_external_url?(external_url)
end

# Models must implement this method.
def external_url
raise NotImplementedError, "Models including Linkable must define #external_url"
end

private

def valid_external_url?(value)
return false if value.blank?

# Only normalize *naked domains*, not scheme-bearing strings
if value =~ /\A[\w.-]+\.[a-z]{2,}/i
value = "https://#{value}" unless value =~ /\Ahttps?:\/\//i
end

uri = URI.parse(value)
uri.host.present? && %w[http https].include?(uri.scheme&.downcase)
rescue URI::InvalidURIError
false
end

def default_link_target
Rails.application.routes.url_helpers.polymorphic_path(self)
end

def normalized_url(value)
return "" if value.blank?
value =~ /\Ahttp(s)?:\/\// ? value : "https://#{value}"
end
end
2 changes: 1 addition & 1 deletion app/models/event.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Event < ApplicationRecord
include Linkable, TagFilterable, WindowsTypeFilterable
include TagFilterable, WindowsTypeFilterable

belongs_to :created_by, class_name: "User", optional: true
has_many :bookmarks, as: :bookmarkable, dependent: :destroy
Expand Down
2 changes: 1 addition & 1 deletion app/models/facilitator.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Facilitator < ApplicationRecord
include Linkable, TagFilterable, WindowsTypeFilterable
include TagFilterable, WindowsTypeFilterable

belongs_to :created_by, class_name: "User"
belongs_to :updated_by, class_name: "User"
Expand Down
2 changes: 1 addition & 1 deletion app/models/project.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Project < ApplicationRecord
include Linkable, TagFilterable, WindowsTypeFilterable
include TagFilterable, WindowsTypeFilterable

belongs_to :project_status
belongs_to :project_obligation, optional: true
Expand Down
2 changes: 1 addition & 1 deletion app/models/quote.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Quote < ApplicationRecord
include Linkable, TagFilterable, WindowsTypeFilterable
include TagFilterable, WindowsTypeFilterable

belongs_to :workshop, optional: true
has_many :bookmarks, as: :bookmarkable, dependent: :destroy
Expand Down
4 changes: 4 additions & 0 deletions app/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,10 @@ def name
title || id
end

def external_url
url
end

def download_attachment
main_image || gallery_images.first || attachments.first
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/story.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,10 @@ def name
title
end

def external_url
website_url
end

def organization_name
project&.name
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/workshop.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Workshop < ApplicationRecord
include Linkable, TagFilterable, WindowsTypeFilterable
include TagFilterable, WindowsTypeFilterable
include Rails.application.routes.url_helpers

belongs_to :windows_type
Expand Down
6 changes: 3 additions & 3 deletions app/views/community_news/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<%= link_to (image_tag news.main_image.file,
class: "w-full rounded shadow-sm border border-gray-200",
alt: news.title),
community_news_path(news) %>
news.link_target, target: "_blank", rel: "noopener" %>
<% else %>
<svg class="w-6 h-6 text-gray-400"
fill="currentColor" viewBox="0 0 20 18"></svg>
Expand All @@ -52,7 +52,7 @@
<!-- TITLE + BADGES -->
<span class="inline-flex items-center gap-2">
<%= link_to title_with_badges(news, show_hidden_badge: current_user.super_user?).html_safe,
community_news_path(news),
news.link_target, target: "_blank", rel: "noopener",
class: "font-bold hover:text-indigo-800 hover:underline" %>
</span>
</td>
Expand All @@ -78,7 +78,7 @@
<% end %>

<%= link_to "View",
community_news_path(news),
news.link_target, target: "_blank", rel: "noopener",
class: "btn btn-secondary-outline" %>
</div>
</td>
Expand Down
5 changes: 5 additions & 0 deletions app/views/community_news/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@
<%= link_to "Edit", edit_community_news_path(@community_news),
class: "btn btn-secondary-outline admin-only bg-blue-100" %>
<% end %>

<% if @community_news.external_url.present? %>
<%= link_to "Website", safe_url(@community_news.external_url), target: "_blank", rel: "noopener",
class: "btn btn-secondary-outline" %>
<% end %>
</div>

<!-- Header Row -->
Expand Down
3 changes: 2 additions & 1 deletion app/views/dashboard/_community_news_item.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
</div>

<!-- CARD BODY (clickable) -->
<%= link_to community_news_path(news),

<%= link_to news.link_target, target: "_blank", rel: "noopener",
class: "flex flex-row items-start gap-4 block z-10" do %>

<!-- LEFT: TITLE + BODY -->
Expand Down
2 changes: 1 addition & 1 deletion app/views/dashboard/_story.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
</div>

<!-- CLICKABLE CARD -->
<%= link_to story_path(story),
<%= link_to story.link_target, target: "_blank", rel: "noopener",
class: "flex flex-col md:flex-row md:items-center gap-4 cursor-pointer block" do %>

<!-- LEFT: TEXT (leave right padding equal to bookmark width) -->
Expand Down
4 changes: 2 additions & 2 deletions app/views/resources/_resource_card.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
">
<!-- IMAGE -->
<div class="relative w-32 h-32">
<%= link_to resource_path(resource.object), class: "block w-full h-full" do %>
<%= link_to resource.link_target, target: "_blank", rel: "noopener", class: "block w-full h-full" do %>

<!-- fallback icon -->
<div class="absolute inset-0 flex items-center justify-center text-gray-700 resource-icon">
Expand All @@ -46,7 +46,7 @@
<!-- RIGHT SIDE -->
<div class="flex-1 min-w-0 pl-4 pr-10 pb-12 mt-2 relative">
<div class="mt-3 mb-2">
<%= link_to resource_path(resource.object),
<%= link_to resource.link_target, target: "_blank", rel: "noopener",
class: "inline-flex min-w-0 max-w-full text-lg font-semibold
text-gray-900 leading-tight hover:underline" do %>
<%= title_with_badges(resource, show_hidden_badge: current_user.super_user?).html_safe %>
Expand Down
5 changes: 5 additions & 0 deletions app/views/resources/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,11 @@
class: "admin-only bg-blue-100 btn btn-primary-outline") %>
<% end %>

<% if @resource.external_url.present? %>
<%= link_to "Website", safe_url(@resource.external_url), target: "_blank", rel: "noopener",
class: "btn btn-secondary-outline" %>
<% end %>

<%= link_to ("<span title='Download #{@resource.kind} image'>" +
" Download <span class='fa fa-download'></span></span>").html_safe,
resource_download_path(@resource),
Expand Down
2 changes: 1 addition & 1 deletion app/views/stories/_story_card.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<%# app/views/stories/_story_card.html.erb %>
<%# locals: story:, href: story_path(story) by default, show_meta: true, show_excerpt: true %>
<% href = local_assigns.fetch(:href, story_path(story)) %>
<% href = local_assigns.fetch(:href, story.link_target, target: "_blank", rel: "noopener") %>
<% show_meta = local_assigns.fetch(:show_meta, true) %>
<% show_excerpt = local_assigns.fetch(:show_excerpt, true) %>

Expand Down
3 changes: 2 additions & 1 deletion app/views/stories/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@
<div class="flex items-center justify-between mb-6">
<h1 class="text-2xl font-semibold text-gray-900">Edit <%= @story.class.model_name.human %></h1>
<div class="text-right text-end space-x-2">
<%= link_to "Website", safe_url(@story.website_url), class: "btn btn-secondary-outline" if @story.website_url %>
<%= link_to "Website", safe_url(@story.external_url), target: "_blank", rel: "noopener",
class: "btn btn-secondary-outline" if @story.external_url %>
<%= link_to "View",
story_path(@story),
class: "btn btn-secondary-outline" %>
Expand Down
7 changes: 4 additions & 3 deletions app/views/stories/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<%= link_to (image_tag story.main_image.file,
class: "w-full rounded shadow-sm border border-gray-200",
alt: story.title),
story_path(story) %>
story.link_target, target: "_blank", rel: "noopener" %>
<% else %>
<svg class="w-6 h-6 text-gray-400"
fill="currentColor" viewBox="0 0 20 18"></svg>
Expand All @@ -57,7 +57,7 @@
<!-- TITLE + BADGES -->
<span class="inline-flex items-center gap-2">
<%= link_to title_with_badges(story, show_hidden_badge: current_user.super_user?).html_safe,
story_path(story),
story.link_target, target: "_blank", rel: "noopener",
class: "font-bold hover:text-indigo-800 hover:underline" %>
</span>
</td>
Expand All @@ -75,7 +75,8 @@
</td>
<td class="px-4 py-2 text-sm text-gray-500"><%= story.updated_at.strftime("%b %d, %Y") %></td>
<td class="px-4 py-2 text-sm text-gray-500">
<%= link_to "View", story_path(story), class: "btn btn-secondary-outline" %>
<%= link_to "View", story.link_target, target: "_blank", rel: "noopener",
class: "btn btn-secondary-outline" %>
<% if current_user.super_user? %>
<%= link_to "Edit", edit_story_path(story),
class: "admin-only bg-blue-100 btn btn-secondary-outline" %>
Expand Down
4 changes: 2 additions & 2 deletions app/views/stories/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@
<%= render "bookmarks/editable_bookmark_button", resource: @story %>
</span>

<% if @story.website_url.present? %>
<%= link_to "Website", safe_url(@story.website_url),
<% if @story.external_url.present? %>
<%= link_to "Website", safe_url(@story.external_url), target: "_blank", rel: "noopener",
class: "btn btn-secondary-outline" %>
<% end %>

Expand Down
4 changes: 2 additions & 2 deletions app/views/taggings/_tagged_item_card.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@

<!-- IMAGE / ICON -->
<div class="relative w-18 h-18">
<%= link_to item.link_target, class: "block w-full h-full" do %>
<%= link_to item.object_link_target, class: "block w-full h-full" do %>

<% if item.class == ResourceDecorator %>
<div class="absolute inset-0 flex items-center justify-center text-gray-700 resource-icon">
Expand Down Expand Up @@ -57,7 +57,7 @@

<!-- TITLE -->
<div class="mt-3 mb-2">
<%= link_to item.link_target,
<%= link_to item.object_link_target,
class: "inline-flex min-w-0 max-w-full text-lg font-semibold
text-gray-900 leading-tight hover:underline" do %>
<%= item.title.truncate(50) %>
Expand Down
Loading
Loading