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
3 changes: 3 additions & 0 deletions app/assets/javascripts/categories.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,10 @@ $(() => {

$('.js-category-select').each((_i, el) => {
const $tgt = $(el);
/** @type {HTMLElement|undefined} */
const modal = el.closest('.modal--container') || void 0;
$tgt.select2({
dropdownParent: modal,
ajax: {
url: '/categories',
headers: { 'Accept': 'application/json' },
Expand Down
10 changes: 9 additions & 1 deletion app/controllers/categories_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,15 @@ class CategoriesController < ApplicationController
before_action :verify_view_access, except: [:index, :homepage, :new, :create, :post_types]

def index
@categories = Category.accessible_to(current_user).all.order(sequence: :asc, id: :asc)
@categories = if params[:term].present?
Category.search(params[:term])
else
Category.all
end

@categories = @categories.accessible_to(current_user)
.order(sequence: :asc, id: :asc)

respond_to do |format|
format.html
format.json do
Expand Down
17 changes: 17 additions & 0 deletions test/controllers/categories_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,23 @@ class CategoriesControllerTest < ActionController::TestCase
assert_not_nil assigns(:categories)
end

test ':index should correctly search categories' do
get :index
assert_response(:success)
@all_categories = assigns(:categories)
assert_not_nil @all_categories
assert @all_categories.any?

get :index, params: { term: 'meta' }
assert_response(:success)
@search_categories = assigns(:categories)
assert_not_nil @search_categories
assert @search_categories.any?
assigns(@search_categories.all? { |c| c.name.downcase.match?('meta') })

assert_not_equal @all_categories.size, @search_categories.size
end

test 'homepage should show categories in the correct order' do
get :homepage
assert_not_nil assigns(:header_categories)
Expand Down
Loading