Skip to content

Commit 9d3670c

Browse files
authored
Merge pull request #1704 from codidact/0valt/categories
Fix for the "0 available post types" case when clicking on the "Create Post" button
2 parents 0d7ae3e + 8279bc2 commit 9d3670c

5 files changed

Lines changed: 39 additions & 11 deletions

File tree

app/controllers/categories_controller.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,9 +127,11 @@ def rss_feed
127127
end
128128

129129
def post_types
130-
@post_types = @category.post_types.where(is_top_level: true)
130+
@post_types = @category.top_level_post_types
131131
if @post_types.one?
132132
redirect_to new_category_post_path(post_type: @post_types.first, category: @category)
133+
elsif @post_types.empty? && current_user&.admin?
134+
redirect_to edit_category_post_types_path(@category, no_return: '1')
133135
end
134136
end
135137

app/models/category.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def public?
2222
trust_level <= 0
2323
end
2424

25+
def top_level_post_types
26+
post_types.where(is_top_level: true)
27+
end
28+
2529
def new_posts_for?(user)
2630
key = "#{community_id}/#{user.id}/#{id}/last_visit"
2731
Rails.cache.fetch key, expires_in: 5.minutes do

app/views/categories/category_post_types.html.erb

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,17 @@
1-
<p class="has-font-size-caption">
2-
<%= link_to edit_category_path(@category) do %>
3-
&laquo; Back to category edit
4-
<% end %>
5-
</p>
1+
<%#
2+
View for managing allowed category post types
3+
4+
Parameters:
5+
params[:no_return] : whether to suppress the return link
6+
%>
7+
8+
<% unless params[:no_return] == '1' %>
9+
<p class="has-font-size-caption">
10+
<%= link_to edit_category_path(@category) do %>
11+
&laquo; Back to category edit
12+
<% end %>
13+
</p>
14+
<% end %>
615
<h1>Allowed post types for <%= @category.name %></h1>
716
<p class="has-font-size-caption">
817
Only post types listed here are allowed to be posted in this category. Not all will be displayed as available options

app/views/categories/post_types.html.erb

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
1-
<h1>What kind of post?</h1>
1+
<% header_title = @post_types.any? ? 'What kind of post?' : 'No allowed post types'
2+
header_subtitle = @post_types.any? \
3+
? 'This category has more than one type of post available. Pick a post type to get started.'
4+
: 'This category does not have any post types available.'
5+
%>
6+
7+
<h1><%= header_title %></h1>
28
<p class="has-font-size-larger has-color-tertiary-500">
3-
This category has more than one type of post available. Pick a post type to get started.
9+
<%= header_subtitle %>
410
</p>
511

612
<% @post_types.each do |pt| %>

app/views/layouts/_header.html.erb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -282,9 +282,16 @@ $(() => {
282282
<% end %>
283283
<% end %>
284284
<div class="category-header--nav-separator"></div>
285-
<%= link_to category_post_types_path(current_cat.id),
286-
class: 'category-header--nav-item is-button' do %>
287-
<%= current_cat.button_text.present? ? current_cat.button_text : 'Create Post' %>
285+
<% button_text = current_cat.button_text.present? ? current_cat.button_text : 'Create Post' %>
286+
<% if current_cat&.top_level_post_types.any? || admin? %>
287+
<%= link_to category_post_types_path(current_cat.id),
288+
class: "category-header--nav-item is-button" do %>
289+
<%= button_text %>
290+
<% end %>
291+
<% else %>
292+
<%= button_tag button_text, class: "button is-muted is-outlined",
293+
disabled: true,
294+
title: "The category doesn't have any allowed post types" %>
288295
<% end %>
289296
</div>
290297
</div>

0 commit comments

Comments
 (0)