Project

General

Profile

Feature #25198 » display-rank-as-data-attribute.diff

okkez _, 2017-02-26 09:24

View differences:

app/views/search/index.api.rsb
api.array :results, api_meta(:total_count => @result_count, :offset => @offset, :limit => @limit) do
@results.each do |result|
@results.each do |result, rank|
api.result do
api.id result.id
api.title result.event_title
......
api.url url_for(result.event_url(:only_path => false))
api.description result.event_description
api.datetime result.event_datetime
api.rank rank
end
end
end
app/views/search/index.html.erb
</div>
<h3><%= l(:label_result_plural) %> (<%= @result_count %>)</h3>
<dl id="search-results">
<% @results.each do |e| %>
<% @results.each do |e, rank| %>
<dt class="<%= e.event_type %>">
<%= content_tag('span', e.project, :class => 'project') unless @project == e.project %>
<%= link_to(highlight_tokens(e.event_title.truncate(255), @tokens), e.event_url) %>
<%= link_to(highlight_tokens(e.event_title.truncate(255), @tokens), e.event_url, :data => { :rank => rank }) %>
</dt>
<dd><span class="description"><%= highlight_tokens(e.event_description, @tokens) %></span>
<span class="author"><%= format_time(e.event_datetime) %></span></dd>
lib/redmine/search.rb
# Returns the results for the given offset and limit
def results(offset, limit)
result_ids_to_load = result_ids[offset, limit] || []
results_by_scope = Hash.new {|h,k| h[k] = []}
result_ids_to_load.group_by(&:first).each do |scope, scope_and_ids|
klass = scope.singularize.camelcase.constantize
results_by_scope[scope] += klass.search_results_from_ids(scope_and_ids.map(&:last))
end
result_ids_to_load.map do |scope, id|
results_by_scope[scope].detect {|record| record.id == id}
result_ids_to_load.map do |scope, rank, id|
[results_by_scope[scope].detect {|record| record.id == id}, rank]
end.compact
end
......
end
# sort results, higher rank and id first
ret.sort! {|a,b| b.last <=> a.last}
# only keep ids now that results are sorted
ret.map! {|scope, r| [scope, r.last]}
# [[scope, rank, id], ...]
ret.map! {|scope, r| [scope, r].flatten }
ret
end
end
(1-1/2)