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
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ gem "active_storage_validations", "~> 3.0"

group :development, :test do
gem "better_errors"
gem "brakeman", "~> 7.1.1", require: false
gem "brakeman", "~> 7.1.2", require: false
gem "bundler-audit", require: false
gem "capybara", "~> 3.36"
gem "dotenv-rails"
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ GEM
debug_inspector (>= 1.2.0)
bootsnap (1.18.6)
msgpack (~> 1.2)
brakeman (7.1.1)
brakeman (7.1.2)
racc
builder (3.3.0)
bundler-audit (0.9.2)
Expand Down Expand Up @@ -458,7 +458,7 @@ DEPENDENCIES
better_errors
binding_of_caller
bootsnap
brakeman (~> 7.1.1)
brakeman (~> 7.1.2)
bundler-audit
capybara (~> 3.36)
cocoon (~> 1.2.6)
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,17 @@ def index
per_page = params[:number_of_items_per_page].presence || 25
@category_types = CategoryType.order(:name)

unpaginated = Category.joins(:category_type)
filtered = unpaginated.category_type_id(params[:category_type_id])
unfiltered = Category.joins(:category_type)
filtered = unfiltered.category_type_id(params[:category_type_id])
.category_name(params[:category_name])
.published_search(params[:published_search])
.order("metadata.name ASC, categories.name ASC")
@categories = filtered.paginate(page: params[:page], per_page: per_page)

@count_display = if @categories.total_entries == unpaginated.count
unpaginated.count
@count_display = if filtered.count == unfiltered.count
unfiltered.count
else
"#{@categories.total_entries}/#{unpaginated.count}"
"#{filtered.count}/#{unfiltered.count}"
end
end

Expand Down
17 changes: 10 additions & 7 deletions app/controllers/community_news_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,28 @@ class CommunityNewsController < ApplicationController

def index
per_page = params[:number_of_items_per_page].presence || 25
unpaginated = current_user.super_user? ? CommunityNews.all : Community_news.published
filtered = unpaginated.search_by_params(params)
@community_news = filtered.paginate(page: params[:page], per_page: per_page)
unfiltered = current_user.super_user? ? CommunityNews.all : Community_news.published
filtered = unfiltered.search_by_params(params)
@community_news = filtered.paginate(page: params[:page], per_page: per_page).decorate

@count_display = if @community_news.total_entries == unpaginated.count
unpaginated.count
@count_display = if filtered.count == unfiltered.count
unfiltered.count
else
"#{@community_news.total_entries}/#{unpaginated.count}"
"#{filtered.count}/#{unfiltered.count}"
end
end

def show
@community_news = @community_news.decorate
end

def new
@community_news = CommunityNews.new
@community_news = CommunityNews.new.decorate
set_form_variables
end

def edit
@community_news = @community_news.decorate
set_form_variables
end

Expand All @@ -33,6 +35,7 @@ def create
redirect_to community_news_index_path,
notice: "Community news was successfully created."
else
@community_news = @community_news.decorate
set_form_variables
render :new, status: :unprocessable_content
end
Expand Down
11 changes: 6 additions & 5 deletions app/controllers/resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,22 @@ def index
.includes(:main_image, :gallery_images, :attachments)
filtered = unpaginated.search_by_params(params)
.by_created
@resources = filtered.paginate(page: params[:page], per_page: per_page)
@resources = filtered.paginate(page: params[:page], per_page: per_page).decorate

@count_display = if @resources.total_entries == unpaginated.count
@count_display = if filtered.count == unpaginated.count
unpaginated.count
else
"#{@resources.total_entries}/#{unpaginated.count}"
"#{filtered.count}/#{unpaginated.count}"
end
@sortable_fields = Resource::PUBLISHED_KINDS
end

def stories
@stories = Resource.story.paginate(page: params[:page], per_page: 6)
@stories = Resource.story.paginate(page: params[:page], per_page: 6).decorate
end

def new
@resource = Resource.new
@resource = Resource.new.decorate
set_form_variables
end

Expand All @@ -47,6 +47,7 @@ def create
if @resource.save
redirect_to resources_path
else
@resource = @resource.decorate
set_form_variables
flash[:alert] = "Unable to save #{@resource.title.titleize}"
render :new
Expand Down
10 changes: 5 additions & 5 deletions app/controllers/sectors_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@ class SectorsController < ApplicationController

def index
per_page = params[:number_of_items_per_page].presence || 25
unpaginated = Sector.all
filtered = unpaginated.sector_name(params[:sector_name])
unfiltered = Sector.all
filtered = unfiltered.sector_name(params[:sector_name])
.published_search(params[:published_search])
.order(:name)
@sectors = filtered.paginate(page: params[:page], per_page: per_page)

@count_display = if @sectors.total_entries == unpaginated.count
unpaginated.count
@count_display = if filtered.count == unfiltered.count
unfiltered.count
else
"#{@sectors.total_entries}/#{unpaginated.count}"
"#{filtered.count}/#{unfiltered.count}"
end
end

Expand Down
13 changes: 9 additions & 4 deletions app/controllers/stories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,24 +7,27 @@ def index
filtered = unpaginated.includes(:windows_type, :project, :workshop, :created_by, :updated_by)
.search_by_params(params)
.order(created_at: :desc)
@stories = filtered.paginate(page: params[:page], per_page: per_page)
@stories = filtered.paginate(page: params[:page], per_page: per_page).decorate

@count_display = if @stories.total_entries == unpaginated.count
@count_display = if filtered.count == unpaginated.count
unpaginated.count
else
"#{@stories.total_entries}/#{unpaginated.count}"
"#{filtered.count}/#{unpaginated.count}"
end
end

def show
@story = @story.decorate
end

def new
@story = Story.new
@story = Story.new.decorate
@story = @story.decorate
set_form_variables
end

def edit
@story = @story.decorate
set_form_variables
end

Expand All @@ -34,6 +37,7 @@ def create
if @story.save
redirect_to stories_path, notice: "Story was successfully created."
else
@story = @story.decorate
set_form_variables
render :new, status: :unprocessable_content
end
Expand All @@ -43,6 +47,7 @@ def update
if @story.update(story_params.except(:images))
redirect_to stories_path, notice: "Story was successfully updated.", status: :see_other
else
@story = @story.decorate
set_form_variables
render :edit, status: :unprocessable_content
end
Expand Down
17 changes: 6 additions & 11 deletions app/decorators/application_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,12 @@
class ApplicationDecorator < Draper::Decorator
delegate_all

def object_link_target
if object.respond_to?(:link_target)
object.link_target
else
object_default_link_target
end
def link_target
h.polymorphic_path(object)
end

private

def object_default_link_target
h.polymorphic_path(object)
def external_link?
false
end
end

end
6 changes: 6 additions & 0 deletions app/decorators/community_news_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
class CommunityNewsDecorator < ApplicationDecorator
include ::Linkable

def detail(length: nil)
length ? body&.truncate(length) : body
end

def external_url
object.reference_url
end

def inactive?
!published?
end
Expand Down
Original file line number Diff line number Diff line change
@@ -1,36 +1,28 @@
# app/decorators/concerns/linkable.rb
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
if external_link?
normalized_url(external_url)
else
default_link_target
end
external_link? ? normalized_url(external_url) : super
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"
raise NotImplementedError, "#{self.class.name} must define #external_url"
end

private

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

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
Expand All @@ -41,10 +33,6 @@ def valid_external_url?(value)
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}"
Expand Down
5 changes: 5 additions & 0 deletions app/decorators/resource_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class ResourceDecorator < ApplicationDecorator
include ::Linkable

def detail(length: nil)
length ? text&.truncate(length) : text # TODO - rename field
end

def external_url
object.url
end

def main_image_url
if main_image&.file&.attached?
Rails.application.routes.url_helpers.url_for(main_image.file)
Expand Down
5 changes: 5 additions & 0 deletions app/decorators/story_decorator.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,14 @@
class StoryDecorator < ApplicationDecorator
include ::Linkable

def detail(length: 50)
body&.truncate(length)
end

def external_url
object.website_url
end

def inactive?
!published?
end
Expand Down
5 changes: 1 addition & 4 deletions app/models/community_news.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class CommunityNews < ApplicationRecord
include Linkable, TagFilterable, WindowsTypeFilterable
include TagFilterable, WindowsTypeFilterable

belongs_to :project, optional: true
belongs_to :windows_type, optional: true
Expand Down Expand Up @@ -52,7 +52,4 @@ def self.search_by_params(params)
community_news
end

def external_url
reference_url
end
end
6 changes: 1 addition & 5 deletions app/models/resource.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Resource < ApplicationRecord
include Linkable, TagFilterable, WindowsTypeFilterable
include TagFilterable, WindowsTypeFilterable
include Rails.application.routes.url_helpers

PUBLISHED_KINDS = ["Handout", "Scholarship", "Template", "Toolkit", "Form"]
Expand Down Expand Up @@ -109,10 +109,6 @@ def name
title || id
end

def external_url
url
end

def download_attachment
main_image || gallery_images.first || attachments.first
end
Expand Down
6 changes: 1 addition & 5 deletions app/models/story.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Story < ApplicationRecord
include Linkable, TagFilterable, WindowsTypeFilterable
include TagFilterable, WindowsTypeFilterable

belongs_to :created_by, class_name: "User"
belongs_to :updated_by, class_name: "User"
Expand Down Expand Up @@ -62,10 +62,6 @@ def name
title
end

def external_url
website_url
end

def organization_name
project&.name
end
Expand Down
Loading