Skip to content

Commit

Permalink
Merge pull request #784 from alphagov/surface-other-results
Browse files Browse the repository at this point in the history
Show other results from GOV.UK in a scoped search
  • Loading branch information
evilstreak committed Apr 13, 2015
2 parents 40b266d + a933fc9 commit 1387536
Show file tree
Hide file tree
Showing 11 changed files with 348 additions and 141 deletions.
14 changes: 14 additions & 0 deletions app/assets/stylesheets/views/_search.scss
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,20 @@ main.search {
}
}

li.descoped-results {
border-left: 4px solid $govuk-blue;
padding: $gutter-half 0 0 $gutter-half;
margin-bottom: $gutter;

.descope-message {
@include core-16;
}

ol {
padding: $gutter-half 0 0;
}
}

h3 {
@include core-24;
margin: 0;
Expand Down
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
53 changes: 53 additions & 0 deletions app/presenters/scoped_search_results_presenter.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
class ScopedSearchResultsPresenter < SearchResultsPresenter

def to_hash
super.merge({
is_scoped?: true,
scope_title: scope_title,
unscoped_results_any?: unscoped_results.any?,
unscoped_result_count: result_count_string(unscoped_result_count),
})
end

private

attr_reader :unscoped_results

def filter_fields
[]
end

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

def results
result_list = search_response["results"].map { |result| build_scoped_result(result).to_hash }

if unscoped_results.any?
insertion_point = [result_list.count, 3].min
unscoped_results_sublist = { results: unscoped_results, is_multiple_results: true }

result_list.insert(insertion_point, unscoped_results_sublist)
end
result_list
end

def unscoped_result_count
search_response["unscoped_results"]["total"]
end

def unscoped_results
@unscoped_results ||= build_result_presenters

end

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

def build_scoped_result(result)
ScopedResult.new(search_parameters, result)
end

end
20 changes: 10 additions & 10 deletions app/presenters/search_result.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,18 @@ def self.result_accessor(*keys)
end
end

result_accessor :link, :title, :format, :es_score
result_accessor :link, :title, :format, :es_score, :section, :subsection, :subsubsection

# Avoid the mundanity of creating these all by hand by making
# dynamic method and accessors.
%w(section subsection subsubsection).each do |key|
define_method "formatted_#{key}_name" do
mapped_name(send(key)) || humanized_name(send(key))
end
def formatted_section_name
mapped_name(section) || humanized_name(section)
end

define_method key do
result[key]
end
def formatted_subsection_name
mapped_name(subsection) || humanized_name(subsection)
end

def formatted_subsubsection_name
mapped_name(subsubsection) || humanized_name(subsubsection)
end

# External links have a truncated version of their URLs displayed on the
Expand Down
48 changes: 15 additions & 33 deletions app/presenters/search_results_presenter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ 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 },
results: results,
filter_fields: filter_fields,
debug_score: search_parameters.debug_score,
has_next_page?: has_next_page?,
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) }
search_response["results"].map { |result| build_result(result).to_hash }
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
85 changes: 85 additions & 0 deletions app/views/search/_result.mustache
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
<li{{#external}} class="external"{{/external}}>
<h3><a href="{{link}}" {{#external}}rel="external"{{/external}}>{{title}}</a></h3>

{{#debug_score}}
<p class="debug-link">
{{link}}
</p>
<p class="debug-info">
<span>Score: {{es_score}}</span>
<span>Format: {{#government}}government{{/government}} {{format}}</span>
</p>
{{/debug_score}}

{{#external}}
<p class="meta">
<span class="visuallyhidden">Part of </span>
<span class="url">{{ display_link }}</span>
</p>
{{/external}}

{{#section}}
<p class="meta crumbtrail">
<span class="visuallyhidden">Part of </span>
<span class="section">{{formatted_section_name}}</span>
{{#formatted_subsection_name}}
<span class="visuallyhidden">, </span>
<span class="subsection">{{ formatted_subsection_name }}</span>
{{/formatted_subsection_name}}
{{#formatted_subsubsection_name}}
<span class="visuallyhidden">, </span>
<span class="subsubsection">{{ formatted_subsubsection_name }}</span>
{{/formatted_subsubsection_name}}
</p>
{{/section}}

{{#metadata_any?}}
<ul class="attributes">
{{#metadata}}
<li> {{{.}}} </li>
{{/metadata}}
</ul>
{{/metadata_any?}}

{{#historic?}}
{{#government_name}}
<p class="historic">
First published during the {{government_name}}
</p>
{{/government_name}}
{{/historic?}}

<p>{{description}}</p>

{{#sections_present?}}
<ul class="sections">
{{#sections}}
<li><a href="{{link}}#{{hash}}">{{title}}</a></li>
{{/sections}}
</ul>
{{/sections_present?}}

{{#examples_present?}}
<ul class="examples">
{{#examples}}
<li>
<h4><a href="{{link}}">{{title}}</a></h4>
<p>{{description}}</p>
</li>
{{/examples}}

{{#suggested_filter_present?}}
<li>
<h4 class="see-all"><a href="{{suggested_filter_link}}">{{suggested_filter_title}}</a><h4>
</li>
{{/suggested_filter_present?}}
</ul>
{{/examples_present?}}

{{^examples_present?}}
{{#suggested_filter_present?}}
<h4 class="see-all"><a href="{{suggested_filter_link}}">{{suggested_filter_title}}</a><h4>
{{/suggested_filter_present?}}
{{/examples_present?}}

</li>
Loading

0 comments on commit 1387536

Please sign in to comment.