Skip to content

Commit b3521c3

Browse files
authored
Add more searching (#583)
* Fix decotrators for recent_activity * Fix resources searching (published wasn't applying correctly when All was selected) * Add filtering to community_news index * Add another condition to displaying hidden badge * Change search label * Make community_news same width as resources and workshops * Fix resource published scope * Add searching to stories index
1 parent c0ed3e0 commit b3521c3

17 files changed

+148
-36
lines changed

app/controllers/community_news_controller.rb

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,14 @@ class CommunityNewsController < ApplicationController
44
def index
55
per_page = params[:number_of_items_per_page].presence || 25
66
unpaginated = current_user.super_user? ? CommunityNews.all : Community_news.published
7-
unpaginated = unpaginated.search_by_params(params)
8-
@community_news_count = unpaginated.count
9-
@community_news = unpaginated.paginate(page: params[:page], per_page: per_page)
7+
filtered = unpaginated.search_by_params(params)
8+
@community_news = filtered.paginate(page: params[:page], per_page: per_page)
9+
10+
@count_display = if @community_news.total_entries == unpaginated.count
11+
unpaginated.count
12+
else
13+
"#{@community_news.total_entries}/#{unpaginated.count}"
14+
end
1015
end
1116

1217
def show

app/controllers/resources_controller.rb

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
class ResourcesController < ApplicationController
22

33
def index
4+
per_page = params[:number_of_items_per_page].presence || 25
45
unpaginated = Resource.where(kind: Resource::PUBLISHED_KINDS) #TODO - #FIXME brittle
56
.includes(:main_image, :gallery_images, :attachments)
6-
.search_by_params(params)
7+
filtered = unpaginated.search_by_params(params)
78
.by_created
8-
@resources = unpaginated.paginate(page: params[:page], per_page: 24)
9+
@resources = filtered.paginate(page: params[:page], per_page: per_page)
910

10-
@resources_count = unpaginated.size
11+
@count_display = if @resources.total_entries == unpaginated.count
12+
unpaginated.count
13+
else
14+
"#{@resources.total_entries}/#{unpaginated.count}"
15+
end
1116
@sortable_fields = Resource::PUBLISHED_KINDS
12-
13-
respond_to do |format|
14-
format.html
15-
end
1617
end
1718

1819
def stories

app/controllers/stories_controller.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,16 @@ class StoriesController < ApplicationController
44
def index
55
per_page = params[:number_of_items_per_page].presence || 25
66
unpaginated = current_user.super_user? ? Story.all : Story.published
7-
unpaginated = unpaginated.search_by_params(params)
8-
@stories = unpaginated.includes(:windows_type, :project, :workshop, :created_by, :updated_by)
7+
filtered = unpaginated.includes(:windows_type, :project, :workshop, :created_by, :updated_by)
8+
.search_by_params(params)
99
.order(created_at: :desc)
10-
.paginate(page: params[:page], per_page: per_page)
10+
@stories = filtered.paginate(page: params[:page], per_page: per_page)
1111

12-
@stories_count = unpaginated.size
12+
@count_display = if @stories.total_entries == unpaginated.count
13+
unpaginated.count
14+
else
15+
"#{@stories.total_entries}/#{unpaginated.count}"
16+
end
1317
end
1418

1519
def show
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,9 @@
11
class CategoryDecorator < ApplicationDecorator
2+
def title
3+
name
4+
end
5+
6+
def detail
7+
"#{category_type.name}: #{name}"
8+
end
29
end

app/decorators/sector_decorator.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,6 @@ def title
55
end
66

77
def detail
8+
"Service population: #{name}"
89
end
910
end

app/helpers/title_display_helper.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,9 @@ def title_with_badges(record, font_size: "text-lg", record_title: nil,
55
fragments = []
66

77
# --- Hidden badge ---
8-
if show_hidden_badge && record.respond_to?(:inactive?) &&
9-
record.inactive? && controller_name != "dashboard"
8+
if show_hidden_badge && controller_name != "dashboard" && (
9+
record.respond_to?(:inactive?) && record.inactive? && controller_name != "dashboard" ||
10+
record.respond_to?(:published?) && !record.published?)
1011
fragments << content_tag(
1112
:span,
1213
content_tag(:i, "", class: "fa-solid fa-eye-slash mr-1") + " Hidden",

app/models/community_news.rb

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,21 @@ class CommunityNews < ApplicationRecord
3434
end
3535

3636
scope :featured, -> { where(featured: true) }
37-
scope :published, ->(published=nil) { published ? where(published: published) : where(published: true) }
3837
scope :category_names, ->(names) { tag_names(:categories, names) }
3938
scope :sector_names, ->(names) { tag_names(:sectors, names) }
39+
scope :community_news_name, ->(community_news_name) {
40+
community_news_name.present? ? where("community_news.name LIKE ?", "%#{community_news_name}%") : all }
41+
scope :published, ->(published=nil) {
42+
["true", "false"].include?(published) ? where(published: published) : where(published: true) }
43+
scope :published_search, ->(published_search) { published_search.present? ? published(published_search) : all }
4044

4145
def self.search_by_params(params)
4246
community_news = self.all
4347
community_news = community_news.search(params[:query]) if params[:query].present?
4448
community_news = community_news.sector_names(params[:sector_names]) if params[:sector_names].present?
4549
community_news = community_news.category_names(params[:category_names]) if params[:category_names].present?
4650
community_news = community_news.windows_type_name(params[:windows_type_name]) if params[:windows_type_name].present?
47-
community_news = community_news.published(params[:published]) if params[:published].present?
51+
community_news = community_news.published_search(params[:published_search]) if params[:published_search].present?
4852
community_news
4953
end
5054
end

app/models/resource.rb

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,16 @@ class Resource < ApplicationRecord
6262
scope :kind, -> (kind) { where("kind like ?", kind ) }
6363
scope :leader_spotlights, -> { kind("LeaderSpotlight") }
6464
scope :published_kinds, -> { where(kind: PUBLISHED_KINDS) }
65-
scope :published, -> (published=nil) { published.present? ?
66-
where(inactive: !published).published_kinds : where(inactive: false).published_kinds }
65+
scope :published, ->(published=nil) {
66+
if ["true", "false"].include?(published)
67+
result = where(inactive: published == "true" ? false : true)
68+
else
69+
result = where(inactive: false)
70+
end
71+
result.published_kinds
72+
}
73+
scope :published_search, ->(published_search=nil) { published_search.present? ? published(published_search) : published_kinds }
74+
6775
scope :recent, -> { published.by_created }
6876
scope :sector_impact, -> { where(kind: "SectorImpact") }
6977
scope :scholarship, -> { where(kind: "Scholarship") }
@@ -79,7 +87,7 @@ def self.search_by_params(params)
7987
resources = resources.windows_type_name(params[:windows_type_name]) if params[:windows_type_name].present?
8088
resources = resources.title(params[:title]) if params[:title].present?
8189
resources = resources.kind(params[:kind]) if params[:kind].present?
82-
resources = resources.published(params[:published]) if params[:published].present?
90+
resources = resources.published_search(params[:published_search]) if params[:published_search].present?
8391
resources = resources.featured(params[:featured]) if params[:featured].present?
8492
resources
8593
end

app/models/story.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,21 @@ class Story < ApplicationRecord
3939

4040
# Scopes
4141
scope :featured, -> { where(featured: true) }
42-
scope :published, ->(published=nil) { published ? where(published: published) : where(published: true) }
4342
scope :category_names, ->(names) { tag_names(:categories, names) }
4443
scope :sector_names, ->(names) { tag_names(:sectors, names) }
44+
scope :story_name, ->(story_name) {
45+
story_name.present? ? where("stories.name LIKE ?", "%#{story_name}%") : all }
46+
scope :published, ->(published=nil) {
47+
["true", "false"].include?(published) ? where(published: published) : where(published: true) }
48+
scope :published_search, ->(published_search) { published_search.present? ? published(published_search) : all }
4549

4650
def self.search_by_params(params)
4751
stories = self.all
4852
stories = stories.search(params[:query]) if params[:query].present?
4953
stories = stories.sector_names(params[:sector_names]) if params[:sector_names].present?
5054
stories = stories.category_names(params[:category_names]) if params[:category_names].present?
55+
stories = stories.story_name(params[:story_name]) if params[:story_name].present?
56+
stories = stories.published_search(params[:published_search]) if params[:published_search].present?
5157
stories = stories.windows_type_name(params[:windows_type_name]) if params[:windows_type_name].present?
5258
stories
5359
end

app/views/categories/_search_boxes.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919

2020
<!-- Name Search -->
2121
<div>
22-
<%= f.label :category_name, "Name Contains",
22+
<%= f.label :category_name, "Name contains",
2323
class: "block text-sm font-medium text-gray-700" %>
2424

2525
<%= f.text_field :category_name,

0 commit comments

Comments
 (0)