From 5a8d032dd00d1a0d85bfb8791c1f9da755c68513 Mon Sep 17 00:00:00 2001 From: Nelson Jovel Date: Fri, 7 Jan 2022 15:37:32 +0100 Subject: [PATCH] Set up bullet gem. Implement bullet gem suggestions. --- Gemfile | 1 + Gemfile.lock | 7 ++++++- app/controllers/home_controller.rb | 2 +- app/controllers/sqm_application_controller.rb | 4 ++-- app/models/category.rb | 2 +- app/presenters/category_presenter.rb | 2 +- app/presenters/subcategory_presenter.rb | 16 ++++++++-------- config/environments/development.rb | 10 ++++++++++ config/environments/test.rb | 6 ++++++ spec/presenters/category_presenter_spec.rb | 6 +++--- 10 files changed, 39 insertions(+), 17 deletions(-) diff --git a/Gemfile b/Gemfile index 78eed45a..1b394bde 100644 --- a/Gemfile +++ b/Gemfile @@ -70,6 +70,7 @@ group :development do gem 'seed_dump' gem 'spring' gem 'spring-watcher-listen', '~> 2.0.0' + gem 'bullet' end group 'test' do diff --git a/Gemfile.lock b/Gemfile.lock index a62594e7..9076fb38 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -78,6 +78,9 @@ GEM bootsnap (1.9.1) msgpack (~> 1.0) builder (3.2.4) + bullet (7.0.0) + activesupport (>= 3.0.0) + uniform_notifier (~> 1.11) byebug (11.1.3) capybara (3.35.3) addressable @@ -261,6 +264,7 @@ GEM concurrent-ruby (~> 1.0) uglifier (4.2.0) execjs (>= 0.3.0, < 3) + uniform_notifier (1.14.2) warden (1.2.9) rack (>= 2.0.9) web-console (4.1.0) @@ -283,6 +287,7 @@ DEPENDENCIES activerecord-import apparition! bootsnap + bullet byebug capybara cssbundling-rails @@ -320,4 +325,4 @@ RUBY VERSION ruby 3.0.2p107 BUNDLED WITH - 2.2.26 + 2.2.32 diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index a1966c25..7460eb4d 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,7 +1,7 @@ class HomeController < ApplicationController def index @districts = District.all.order(:name) - @schools = School.all.order(:name) + @schools = School.all.includes([:district]).order(:name) @categories = Category.sorted.map { |category| CategoryPresenter.new(category: category) } end diff --git a/app/controllers/sqm_application_controller.rb b/app/controllers/sqm_application_controller.rb index bdbd63d8..1a4beb5b 100644 --- a/app/controllers/sqm_application_controller.rb +++ b/app/controllers/sqm_application_controller.rb @@ -12,8 +12,8 @@ class SqmApplicationController < ApplicationController def set_schools_and_districts @district = District.find_by_slug district_slug @districts = District.all.order(:name) - @school = School.find_by_slug school_slug - @schools = School.where(district: @district).order(:name) + @school = School.find_by_slug(school_slug) + @schools = School.includes([:district]).where(district: @district).order(:name) @academic_year = AcademicYear.find_by_range params[:year] @has_empty_dataset = Measure.none_meet_threshold? school: @school, academic_year: @academic_year end diff --git a/app/models/category.rb b/app/models/category.rb index 3b3d34b6..48b7ef53 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -2,7 +2,7 @@ class Category < ActiveRecord::Base include FriendlyId friendly_id :name, use: [:slugged] - scope :sorted, -> { order(:sort_index) } + scope :sorted, -> { order(:sort_index)} has_many :subcategories has_many :measures, through: :subcategories diff --git a/app/presenters/category_presenter.rb b/app/presenters/category_presenter.rb index 31a10836..db6190aa 100644 --- a/app/presenters/category_presenter.rb +++ b/app/presenters/category_presenter.rb @@ -56,7 +56,7 @@ class CategoryPresenter end def subcategories(academic_year:, school:) - @category.subcategories.sort_by(&:subcategory_id).map do |subcategory| + @category.subcategories.includes([:measures]).sort_by(&:subcategory_id).map do |subcategory| SubcategoryPresenter.new( subcategory: subcategory, academic_year: academic_year, diff --git a/app/presenters/subcategory_presenter.rb b/app/presenters/subcategory_presenter.rb index 45de0c12..0eefb1ce 100644 --- a/app/presenters/subcategory_presenter.rb +++ b/app/presenters/subcategory_presenter.rb @@ -31,7 +31,7 @@ class SubcategoryPresenter end def measure_presenters - @subcategory.measures.sort_by(&:measure_id).map do |measure| + @subcategory.measures.includes([:admin_data_items]).sort_by(&:measure_id).map do |measure| MeasurePresenter.new(measure: measure, academic_year: @academic_year, school: @school) end end @@ -39,15 +39,15 @@ class SubcategoryPresenter private def scale - Scale.new( - watch_low_benchmark: measures.map(&:watch_low_benchmark).average, - growth_low_benchmark: measures.map(&:growth_low_benchmark).average, - approval_low_benchmark: measures.map(&:approval_low_benchmark).average, - ideal_low_benchmark: measures.map(&:ideal_low_benchmark).average - ) + Scale.new( + watch_low_benchmark: measures.map(&:watch_low_benchmark).average, + growth_low_benchmark: measures.map(&:growth_low_benchmark).average, + approval_low_benchmark: measures.map(&:approval_low_benchmark).average, + ideal_low_benchmark: measures.map(&:ideal_low_benchmark).average + ) end def measures - @measures ||= @subcategory.measures.order(:measure_id) + @measures ||= @subcategory.measures.includes([:admin_data_items]).order(:measure_id) end end diff --git a/config/environments/development.rb b/config/environments/development.rb index 66df51f6..e53f1908 100644 --- a/config/environments/development.rb +++ b/config/environments/development.rb @@ -1,4 +1,14 @@ Rails.application.configure do + config.after_initialize do + Bullet.enable = true + Bullet.alert = true + Bullet.bullet_logger = true + Bullet.console = true + # Bullet.growl = true + Bullet.rails_logger = true + Bullet.add_footer = true + end + # Settings specified here will take precedence over those in config/application.rb. # In the development environment your application's code is reloaded on diff --git a/config/environments/test.rb b/config/environments/test.rb index 0cb24249..1dee5d8f 100644 --- a/config/environments/test.rb +++ b/config/environments/test.rb @@ -4,6 +4,12 @@ # and recreated between test runs. Don't rely on the data there! Rails.application.configure do + # config.after_initialize do + # Bullet.enable = true + # Bullet.bullet_logger = true + # Bullet.raise = true # raise an error if n+1 query occurs + # end + # Settings specified here will take precedence over those in config/application.rb. config.cache_classes = false diff --git a/spec/presenters/category_presenter_spec.rb b/spec/presenters/category_presenter_spec.rb index 13280206..c8536b5b 100644 --- a/spec/presenters/category_presenter_spec.rb +++ b/spec/presenters/category_presenter_spec.rb @@ -2,10 +2,10 @@ require 'rails_helper' describe CategoryPresenter do let(:category_presenter) do - subcategory1 = Subcategory.new(name: 'A subcategory', subcategory_id: '1') - subcategory2 = Subcategory.new(name: 'Another subcategory', subcategory_id: '2') + subcategory1 = Subcategory.create(name: 'A subcategory', subcategory_id: '1') + subcategory2 = Subcategory.create(name: 'Another subcategory', subcategory_id: '2') - category = Category.new(name: 'Some Category', subcategories: [subcategory1, subcategory2], + category = Category.create(name: 'Some Category', subcategories: [subcategory1, subcategory2], description: 'A description for some Category', short_description: 'A short description for some Category', category_id: '1') return CategoryPresenter.new(category: category) end