Skip to content

Commit 5eb67ed

Browse files
authored
Merge pull request #2033 from codidact/0valt/2032/category-select-fix
Fix category change select dropdown
2 parents e88f0ab + f0aa6f2 commit 5eb67ed

3 files changed

Lines changed: 29 additions & 1 deletion

File tree

app/assets/javascripts/categories.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ $(() => {
4545

4646
$('.js-category-select').each((_i, el) => {
4747
const $tgt = $(el);
48+
/** @type {HTMLElement|undefined} */
49+
const modal = el.closest('.modal--container') || void 0;
4850
$tgt.select2({
51+
dropdownParent: modal,
4952
ajax: {
5053
url: '/categories',
5154
headers: { 'Accept': 'application/json' },

app/controllers/categories_controller.rb

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,15 @@ class CategoriesController < ApplicationController
55
before_action :verify_view_access, except: [:index, :homepage, :new, :create, :post_types]
66

77
def index
8-
@categories = Category.accessible_to(current_user).all.order(sequence: :asc, id: :asc)
8+
@categories = if params[:term].present?
9+
Category.search(params[:term])
10+
else
11+
Category.all
12+
end
13+
14+
@categories = @categories.accessible_to(current_user)
15+
.order(sequence: :asc, id: :asc)
16+
917
respond_to do |format|
1018
format.html
1119
format.json do

test/controllers/categories_controller_test.rb

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

12+
test ':index should correctly search categories' do
13+
get :index
14+
assert_response(:success)
15+
@all_categories = assigns(:categories)
16+
assert_not_nil @all_categories
17+
assert @all_categories.any?
18+
19+
get :index, params: { term: 'meta' }
20+
assert_response(:success)
21+
@search_categories = assigns(:categories)
22+
assert_not_nil @search_categories
23+
assert @search_categories.any?
24+
assigns(@search_categories.all? { |c| c.name.downcase.match?('meta') })
25+
26+
assert_not_equal @all_categories.size, @search_categories.size
27+
end
28+
1229
test 'homepage should show categories in the correct order' do
1330
get :homepage
1431
assert_not_nil assigns(:header_categories)

0 commit comments

Comments
 (0)