From 882fa84de96a11ae01fcfafb341d77ba0a7784bc Mon Sep 17 00:00:00 2001 From: Liam Morley Date: Tue, 26 Oct 2021 15:05:30 -0400 Subject: [PATCH] Unify how we sort SQM Categories --- app/controllers/dashboard_controller.rb | 2 +- app/controllers/home_controller.rb | 2 +- app/controllers/sqm_categories_controller.rb | 2 +- app/models/sqm_category.rb | 3 ++- spec/controllers/dashboard_controller_spec.rb | 16 ++++++++++++++++ spec/controllers/home_controller_spec.rb | 12 ++++++++++++ .../sqm_categories_controller_spec.rb | 17 +++++++++++++++++ spec/support/basic_auth_helper.rb | 7 +++++++ 8 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 spec/controllers/dashboard_controller_spec.rb create mode 100644 spec/controllers/home_controller_spec.rb create mode 100644 spec/controllers/sqm_categories_controller_spec.rb create mode 100644 spec/support/basic_auth_helper.rb diff --git a/app/controllers/dashboard_controller.rb b/app/controllers/dashboard_controller.rb index 867fa2d6..fa4bd1ee 100644 --- a/app/controllers/dashboard_controller.rb +++ b/app/controllers/dashboard_controller.rb @@ -7,7 +7,7 @@ class DashboardController < SqmApplicationController .sort .reverse - @category_presenters = SqmCategory.all.map { |sqm_category| CategoryPresenter.new(category: sqm_category) } + @category_presenters = SqmCategory.sorted.map { |sqm_category| CategoryPresenter.new(category: sqm_category) } end private diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index b1e94b75..a05e39f6 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -3,6 +3,6 @@ class HomeController < ActionController::Base @districts = District.all.order(:name) @schools = School.all - @categories = SqmCategory.all.order(:sort_index).map { |category| CategoryPresenter.new(category: category) } + @categories = SqmCategory.sorted.map { |category| CategoryPresenter.new(category: category) } end end diff --git a/app/controllers/sqm_categories_controller.rb b/app/controllers/sqm_categories_controller.rb index bcfe8b24..5d44d520 100644 --- a/app/controllers/sqm_categories_controller.rb +++ b/app/controllers/sqm_categories_controller.rb @@ -1,7 +1,7 @@ class SqmCategoriesController < SqmApplicationController def show - @categories = SqmCategory.all.order(:sort_index).map { |category| CategoryPresenter.new(category: category) } + @categories = SqmCategory.sorted.map { |category| CategoryPresenter.new(category: category) } @category = CategoryPresenter.new(category: SqmCategory.find_by_slug(params[:id])) end diff --git a/app/models/sqm_category.rb b/app/models/sqm_category.rb index 503820ca..f4237e84 100644 --- a/app/models/sqm_category.rb +++ b/app/models/sqm_category.rb @@ -1,7 +1,8 @@ class SqmCategory < ActiveRecord::Base include FriendlyId - friendly_id :name, use: [:slugged] + scope :sorted, ->() { order(:sort_index) } + has_many :subcategories end diff --git a/spec/controllers/dashboard_controller_spec.rb b/spec/controllers/dashboard_controller_spec.rb new file mode 100644 index 00000000..cf615823 --- /dev/null +++ b/spec/controllers/dashboard_controller_spec.rb @@ -0,0 +1,16 @@ +require 'rails_helper' + +describe DashboardController, type: :controller do + include BasicAuthHelper + let(:school) { create(:school) } + let(:district) { create(:district) } + let!(:categories) { + [create(:sqm_category, name: 'Second', sort_index: 2), create(:sqm_category, name: 'First', sort_index: 1)] + } + + it 'fetches categories sorted by sort_index' do + login_as district + get :index, params: { school_id: school.to_param, district_id: district.to_param } + expect(assigns(:category_presenters).map(&:name)).to eql ['First', 'Second'] + end +end diff --git a/spec/controllers/home_controller_spec.rb b/spec/controllers/home_controller_spec.rb new file mode 100644 index 00000000..d9ec07f8 --- /dev/null +++ b/spec/controllers/home_controller_spec.rb @@ -0,0 +1,12 @@ +require 'rails_helper' + +describe HomeController, type: :controller do + let!(:categories) { + [create(:sqm_category, name: 'Second', sort_index: 2), create(:sqm_category, name: 'First', sort_index: 1)] + } + + it 'fetches categories sorted by sort_index' do + get :index + expect(assigns(:categories).map(&:name)).to eql ['First', 'Second'] + end +end diff --git a/spec/controllers/sqm_categories_controller_spec.rb b/spec/controllers/sqm_categories_controller_spec.rb new file mode 100644 index 00000000..9098771e --- /dev/null +++ b/spec/controllers/sqm_categories_controller_spec.rb @@ -0,0 +1,17 @@ +require 'rails_helper' + +describe SqmCategoriesController, type: :controller do + include BasicAuthHelper + let(:school) { create(:school) } + let(:district) { create(:district) } + let!(:categories) { + [create(:sqm_category, name: 'Second', sort_index: 2), create(:sqm_category, name: 'First', sort_index: 1)] + } + + it 'fetches categories sorted by sort_index' do + login_as district + category = categories.first + get :show, params: { id: category.to_param, school_id: school.to_param, district_id: district.to_param } + expect(assigns(:categories).map(&:name)).to eql ['First', 'Second'] + end +end diff --git a/spec/support/basic_auth_helper.rb b/spec/support/basic_auth_helper.rb new file mode 100644 index 00000000..71fd021f --- /dev/null +++ b/spec/support/basic_auth_helper.rb @@ -0,0 +1,7 @@ +module BasicAuthHelper + def login_as(district) + user = district.name.downcase + pw = "#{district.name.downcase}!" + request.env['HTTP_AUTHORIZATION'] = ActionController::HttpAuthentication::Basic.encode_credentials(user, pw) + end +end