diff --git a/app/controllers/analytics/sparkline_controller.rb b/app/controllers/analytics/sparkline_controller.rb index 54495447f..0dff6b01f 100644 --- a/app/controllers/analytics/sparkline_controller.rb +++ b/app/controllers/analytics/sparkline_controller.rb @@ -13,7 +13,7 @@ def vega_specification def vega_data @scores = [] - @current_user.cases_involved_with.not_archived.recent.limit(8).each do |kase| + recent_cases(8).each do |kase| @scores << kase.scores.sampled(kase.id, 100) end @scores.flatten! diff --git a/app/controllers/api/v1/cases/dropdown_controller.rb b/app/controllers/api/v1/cases/dropdown_controller.rb index 8ff92168b..e1663f85e 100644 --- a/app/controllers/api/v1/cases/dropdown_controller.rb +++ b/app/controllers/api/v1/cases/dropdown_controller.rb @@ -4,41 +4,11 @@ module Api module V1 module Cases class DropdownController < Api::ApiController - # rubocop:disable Metrics/MethodLength def index - # Using joins/includes will not return the proper list in the - # correct order because rails refuses to include the - # `case_metadata`.`last_viewed_at` column in the SELECT statement - # which will then cause the ordering not to work properly. - # So instead, we have this beauty! - sql = " - SELECT DISTINCT `cases`.`id`, `case_metadata`.`last_viewed_at` - FROM `cases` - LEFT OUTER JOIN `case_metadata` ON `case_metadata`.`case_id` = `cases`.`id` - LEFT OUTER JOIN `teams_cases` ON `teams_cases`.`case_id` = `cases`.`id` - LEFT OUTER JOIN `teams` ON `teams`.`id` = `teams_cases`.`team_id` - LEFT OUTER JOIN `teams_members` ON `teams_members`.`team_id` = `teams`.`id` - LEFT OUTER JOIN `users` ON `users`.`id` = `teams_members`.`member_id` - WHERE (`teams_members`.`member_id` = #{current_user.id} OR `cases`.`owner_id` = #{current_user.id}) - AND (`cases`.`archived` = false OR `cases`.`archived` IS NULL) - ORDER BY `case_metadata`.`last_viewed_at` DESC, `cases`.`id` DESC - LIMIT 3 - " - - results = ActiveRecord::Base.connection.execute(sql) - - case_ids = [] - results.each do |row| - case_ids << row.first.to_i - end - - # map to objects - @cases = Case.includes(:tries).where(id: [ case_ids ]) - @cases = @cases.sort_by { |x| case_ids.index x.id } + @cases = recent_cases 3 respond_with @cases end - # rubocop:enable Metrics/MethodLength end end end diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index c5b7900c0..8dd3ad79f 100755 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -14,6 +14,7 @@ class ApplicationController < ActionController::Base before_action :set_current_user before_action :require_login before_action :check_current_user_locked! + before_action :set_recent_cases # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. diff --git a/app/controllers/concerns/authentication/current_case_manager.rb b/app/controllers/concerns/authentication/current_case_manager.rb index 3471cc2d8..f3c0056a6 100644 --- a/app/controllers/concerns/authentication/current_case_manager.rb +++ b/app/controllers/concerns/authentication/current_case_manager.rb @@ -36,6 +36,50 @@ def find_case set_case end + def set_recent_cases + @recent_cases = recent_cases(3) + end + + # rubocop:disable Metrics/MethodLength + def recent_cases count + if current_user + # Using joins/includes will not return the proper list in the + # correct order because rails refuses to include the + # `case_metadata`.`last_viewed_at` column in the SELECT statement + # which will then cause the ordering not to work properly. + # So instead, we have this beauty! + sql = " + SELECT DISTINCT `cases`.`id`, `case_metadata`.`last_viewed_at` + FROM `cases` + LEFT OUTER JOIN `case_metadata` ON `case_metadata`.`case_id` = `cases`.`id` + LEFT OUTER JOIN `teams_cases` ON `teams_cases`.`case_id` = `cases`.`id` + LEFT OUTER JOIN `teams` ON `teams`.`id` = `teams_cases`.`team_id` + LEFT OUTER JOIN `teams_members` ON `teams_members`.`team_id` = `teams`.`id` + LEFT OUTER JOIN `users` ON `users`.`id` = `teams_members`.`member_id` + WHERE (`teams_members`.`member_id` = #{current_user.id} OR `cases`.`owner_id` = #{current_user.id}) + AND (`cases`.`archived` = false OR `cases`.`archived` IS NULL) + ORDER BY `case_metadata`.`last_viewed_at` DESC, `cases`.`id` DESC + LIMIT #{count} + " + + results = ActiveRecord::Base.connection.execute(sql) + + case_ids = [] + results.each do |row| + case_ids << row.first.to_i + end + + # map to objects + # cases = Case.includes(:tries).where(id: [ case_ids ]) + cases = Case.where(id: [ case_ids ]) + cases = cases.sort_by { |x| case_ids.index x.id } + else + cases = [] + end + cases + end + # rubocop:enable Metrics/MethodLength + def case_with_all_the_bells_whistles if current_user @case = current_user diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 859d2e361..140315de1 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -1,15 +1,13 @@ # frozen_string_literal: true class HomeController < ApplicationController - # rubocop:disable Metrics/AbcSize # rubocop:disable Metrics/MethodLength # rubocop:disable Metrics/CyclomaticComplexity # rubocop:disable Metrics/PerceivedComplexity def show @cases = @current_user.cases_involved_with.not_archived.with_counts - - @most_recent_cases = @current_user.cases_involved_with.not_archived.recent.limit(4).with_counts.sort_by(&:case_name) + @most_recent_cases = recent_cases(4).sort_by { |c| c.case_name.downcase } # Run the prophet! @prophet_case_data = {} @@ -61,10 +59,9 @@ def show # Homepage is too slow so we have to cut some stuff out ;-( # candidate_cases = @cases.select { |kase| kase.scores.scored.count.positive? } - @cases # @grouped_cases = candidate_cases.group_by { |kase| kase.case_name.split(':').first } # @grouped_cases = @grouped_cases.select { |_key, value| value.count > 1 } - end + end # rubocop:enable Metrics/AbcSize # rubocop:enable Metrics/MethodLength # rubocop:enable Metrics/CyclomaticComplexity diff --git a/app/models/case.rb b/app/models/case.rb index fe33d9a4d..a36a85d9c 100644 --- a/app/models/case.rb +++ b/app/models/case.rb @@ -107,12 +107,6 @@ class Case < ApplicationRecord scope :public_cases, -> { where(public: true) } - # return cases sorted by recently either updated or viewed by the user - scope :recent, -> { - left_outer_joins(:metadata) - .order(Arel.sql('`case_metadata`.`last_viewed_at` DESC, `cases`.`updated_at` DESC')) - } - # load up the queries count for the case, alternative to counter_cache scope :with_counts, -> { select <<~SQL.squish diff --git a/app/views/home/_case.html.erb b/app/views/home/_case.html.erb index 7d079eb15..c5038fd7b 100644 --- a/app/views/home/_case.html.erb +++ b/app/views/home/_case.html.erb @@ -1,6 +1,6 @@ <%= kase.case_name %> - <%= kase.queries_count %> + <%= kase.queries.count %> <% unless kase.last_score.blank? %> <%= number_with_precision(kase.last_score.score, precision: 2) %> diff --git a/app/views/layouts/_header.html.erb b/app/views/layouts/_header.html.erb index 663231885..52f6a20a6 100644 --- a/app/views/layouts/_header.html.erb +++ b/app/views/layouts/_header.html.erb @@ -19,7 +19,7 @@ RECENT CASES
  • - <% @current_user.cases_involved_with.not_archived.recent.limit(3).each do |kase| %> + <% @recent_cases.each do |kase| %>
  • <%= link_to kase.case_name, case_core_path(kase, kase.last_try_number), class: 'cases-nav-listing' %>
  • <% end %>