From 25dda4c9d05a9f386eda118035ad18ce87f7f6f1 Mon Sep 17 00:00:00 2001 From: "Eric D. Helms" Date: Thu, 29 Jan 2015 09:10:17 -0500 Subject: [PATCH] Fixes #9167: Fix chained scopes when using scoped_search in API Scoped search claims to support being applied to a set of scopes via scopes.search_for. However, when multiple joins are used within the 'scopes' the last join wins in the prior format. To combat this, we apply the search_for first, which is itself a scope, and merge the scopes into that result. --- app/controllers/katello/api/v2/api_controller.rb | 12 +++++++++--- .../api/v2/content_view_filters_controller.rb | 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) diff --git a/app/controllers/katello/api/v2/api_controller.rb b/app/controllers/katello/api/v2/api_controller.rb index 03709ff8fce..63b78b2b910 100644 --- a/app/controllers/katello/api/v2/api_controller.rb +++ b/app/controllers/katello/api/v2/api_controller.rb @@ -45,6 +45,12 @@ class Api::V2::ApiController < ::Api::V2::BaseController param :object_root, String, :desc => N_("root-node of single-resource responses (optional)") param :root_name, String, :desc => N_("root-node of collection contained in responses (default: 'results')") + def resource_class + @resource_class ||= resource_name.classify.constantize + rescue NameError + @resource_class ||= "Katello::#{resource_name.classify}".constantize + end + def item_search(item_class, param_hash, options) fail "@search_service search not defined" if @search_service.nil? if param_hash[:order] @@ -79,10 +85,10 @@ def item_search(item_class, param_hash, options) } end - def scoped_search(query, default_sort_by, default_sort_order) + def scoped_search(query, default_sort_by, default_sort_order, resource = resource_class) total = query.count - sub_total = query.search_for(*search_options).count - query = query.search_for(*search_options) + sub_total = resource.search_for(*search_options).merge(query).count + query = resource.search_for(*search_options).merge(query) sort_attr = params[:sort_by] || default_sort_by sort_attr = "#{query.table_name}.#{sort_attr}" unless sort_attr.to_s.include?('.') diff --git a/app/controllers/katello/api/v2/content_view_filters_controller.rb b/app/controllers/katello/api/v2/content_view_filters_controller.rb index 5975ba7419b..ef860c630fb 100644 --- a/app/controllers/katello/api/v2/content_view_filters_controller.rb +++ b/app/controllers/katello/api/v2/content_view_filters_controller.rb @@ -98,7 +98,7 @@ def available_errata scoped = scoped.of_type(params[:types]) if params[:types] respond_for_index :template => '../errata/index', - :collection => scoped_search(scoped, 'issued', 'desc') + :collection => scoped_search(scoped, 'issued', 'desc', Erratum) end api :GET, "/content_views/:content_view_id/filters/:id/available_package_groups",