Skip to content
Open
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
3 changes: 2 additions & 1 deletion app/controllers/tags_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,14 @@ def index

def sectors
authorize! Sector, to: :tags_index?
@sectors = Sector.published.order(:name)
@sectors = Sector.published.has_taggings.order(:name)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@RJV333 this PR should include this scope being added to Sector, right?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that scope already exists actually. We just weren't using it.

end

def categories
authorize! Category, to: :tags_index?
@categories_by_type = Category
.published
.has_taggings
.joins(:category_type)
.select("categories.*, category_types.name AS category_type_name")
.order("category_types.name, categories.position, categories.name")
Expand Down
1 change: 1 addition & 0 deletions app/models/category.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class Category < ApplicationRecord
after_destroy :expire_categories_cache

# Scopes
scope :has_taggings, -> { joins(:categorizable_items).distinct }
scope :category_type_id, ->(category_type_id) {
category_type_id.present? ? where(category_type_id: category_type_id) : all }
scope :category_name, ->(category_name) {
Expand Down
22 changes: 22 additions & 0 deletions spec/requests/tags_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@
let!(:sector) { create(:sector, :published, name: "Youth") }
let!(:category_type) { create(:category_type, name: "Theme") }
let!(:category) { create(:category, :published, name: "Healing", category_type: category_type) }
let!(:workshop) { create(:workshop) }

before do
create(:sectorable_item, sector: sector, sectorable: workshop)
create(:categorizable_item, category: category, categorizable: workshop)
end

describe "as a regular user" do
let(:user) { create(:user) }
Expand Down Expand Up @@ -36,6 +42,22 @@
expect(response.body).to include("Healing")
end

it "only shows sectors that have at least one sectorable_item" do
unlinked_sector = create(:sector, :published, name: "Unlinked Sector")
get tags_sectors_path
expect(response).to have_http_status(:ok)
expect(response.body).to include("Youth")
expect(response.body).not_to include("Unlinked Sector")
end

it "only shows categories that have at least one categorizable_item" do
unlinked_category = create(:category, :published, name: "Unlinked Category", category_type: category_type)
get tags_categories_path
expect(response).to have_http_status(:ok)
expect(response.body).to include("Healing")
expect(response.body).not_to include("Unlinked Category")
end

it "does NOT show admin-only controls" do
get tags_path
expect(response).to have_http_status(:ok)
Expand Down