Skip to content

Commit 806dd65

Browse files
authored
Merge pull request #1779 from codidact/0valt/794/homepage-fix
Fix for the homepage runtime error when no categories are configured as such
2 parents a78615d + 1a51589 commit 806dd65

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

app/controllers/categories_controller.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ def show
2121

2222
def homepage
2323
@category = Category.where(is_homepage: true).first
24+
25+
unless @category.present?
26+
redirect_to categories_path
27+
return
28+
end
29+
2430
update_last_visit(@category)
2531
set_list_posts
2632
render :show
@@ -199,6 +205,9 @@ def set_list_posts
199205
@posts = @posts.paginate(page: params[:page], per_page: 50).order(sort_param)
200206
end
201207

208+
# Updates last visit cache for a given category
209+
# @param category [Category] category to update
210+
# @return [Boolean] whether the cache entry is deleted
202211
def update_last_visit(category)
203212
return if current_user.blank?
204213

app/models/category.rb

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,12 @@ class Category < ApplicationRecord
1515

1616
validates :name, uniqueness: { scope: [:community_id], case_sensitive: false }
1717

18+
# Is the category set as the homepage?
19+
# @return [Boolean] check result
20+
def homepage?
21+
is_homepage == true
22+
end
23+
1824
# Can anyone view the category (even if not logged in)?
1925
# @return [Boolean] check result
2026
def public?

test/controllers/categories_controller_test.rb

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,22 @@ class CategoriesControllerTest < ActionController::TestCase
99
assert_not_nil assigns(:categories)
1010
end
1111

12+
test ':homepage should correctly show the homepage category' do
13+
get :homepage
14+
assert_response(:success)
15+
@category = assigns(:category)
16+
assert_not_nil @category
17+
assert @category.homepage?
18+
end
19+
20+
test ':homepage should redirect to the categories list if there is no default category' do
21+
Category.where(is_homepage: true).destroy_all
22+
23+
get :homepage
24+
assert_response(:found)
25+
assert_redirected_to(categories_path)
26+
end
27+
1228
test 'should correctly show public categories' do
1329
public_categories = categories.select(&:public?)
1430

0 commit comments

Comments
 (0)