Skip to content

Commit

Permalink
Fixes #9167: Fix chained scopes when using scoped_search in API
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
ehelms committed Feb 2, 2015
1 parent 3247cc6 commit 25dda4c
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
12 changes: 9 additions & 3 deletions app/controllers/katello/api/v2/api_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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?('.')

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down

0 comments on commit 25dda4c

Please sign in to comment.