Skip to content

Commit

Permalink
Extract scoped search to separate presenter
Browse files Browse the repository at this point in the history
Since out code for scoped searches is getting a bit more involved with
the inclusion of un-scoped results, let's extract this functionality
to a new presenter `scoped_search_results_presenter`.

This commit just extracts the existing functionality into a new
presenter, no new features are added.
  • Loading branch information
Alice Bartlett committed Apr 9, 2015
1 parent 5b7049b commit 804af74
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 32 deletions.
8 changes: 7 additions & 1 deletion app/controllers/search_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,13 @@ def index
search_response = search_client.search(search_params)

@search_term = search_params.search_term
@results = SearchResultsPresenter.new(search_response, search_params)

if (search_response["scope"].present?)
@results = ScopedSearchResultsPresenter.new(search_response, search_params)
else
@results = SearchResultsPresenter.new(search_response, search_params)
end

@facets = search_response["facets"]
@spelling_suggestion = @results.spelling_suggestion

Expand Down
19 changes: 19 additions & 0 deletions app/presenters/scoped_search_results_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
class ScopedSearchResultsPresenter < SearchResultsPresenter

def to_hash
super.merge({
is_scoped?: true,
scope_title: scope_title,
})
end

private

def filter_fields
end

def scope_title
search_response[:scope][:title]
end

end
44 changes: 13 additions & 31 deletions app/presenters/search_results_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def to_hash
{
query: search_parameters.search_term,
result_count: result_count,
result_count_string: result_count_string,
result_count_string: result_count_string(result_count),
results_any?: results.any?,
results: results.map { |result| result.to_hash },
filter_fields: filter_fields,
Expand All @@ -29,25 +29,19 @@ def to_hash
previous_page_link: previous_page_link,
previous_page_label: previous_page_label,
first_result_number: (search_parameters.start + 1),
is_scoped?: is_scoped?,
scope_title: scope_title,
}
end

def filter_fields
if is_scoped?
[]
else
search_response["facets"].map do |field, value|
external = SearchParameters::external_field_name(field)
facet_params = search_parameters.filter(external)
facet = SearchFacetPresenter.new(value, facet_params)
{
field: external,
field_title: FACET_TITLES.fetch(field, field),
options: facet.to_hash,
}
end
search_response["facets"].map do |field, value|
external = SearchParameters::external_field_name(field)
facet_params = search_parameters.filter(external)
facet = SearchFacetPresenter.new(value, facet_params)
{
field: external,
field_title: FACET_TITLES.fetch(field, field),
options: facet.to_hash,
}
end
end

Expand All @@ -72,18 +66,16 @@ def result_count
search_response["total"].to_i
end

def result_count_string
pluralize(number_with_delimiter(result_count), "result")
def result_count_string(count)
pluralize(number_with_delimiter(count), "result")
end

def results
search_response["results"].map { |result| build_result(result) }
end

def build_result(result)
if is_scoped?
ScopedResult.new(search_parameters, result)
elsif result["document_type"] == "group"
if result["document_type"] == "group"
GroupResult.new(search_parameters, result)
elsif result["document_type"] && result["document_type"] != "edition"
NonEditionResult.new(search_parameters, result)
Expand Down Expand Up @@ -127,16 +119,6 @@ def previous_page_label
end
end

def is_scoped?
search_response[:scope].present?
end

def scope_title
if is_scoped?
search_response[:scope][:title]
end
end

private

attr_reader :search_parameters, :search_response
Expand Down

0 comments on commit 804af74

Please sign in to comment.