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
1 1
api.array :results, api_meta(:total_count => @result_count, :offset => @offset, :limit => @limit) do
2
  @results.each do |result|
2
  @results.each do |result, rank|
3 3
    api.result do
4 4
      api.id          result.id
5 5
      api.title       result.event_title
......
7 7
      api.url         url_for(result.event_url(:only_path => false))
8 8
      api.description result.event_description
9 9
      api.datetime    result.event_datetime
10
      api.rank        rank
10 11
    end
11 12
  end
12 13
end
app/views/search/index.html.erb
41 41
    </div>
42 42
    <h3><%= l(:label_result_plural) %> (<%= @result_count %>)</h3>
43 43
    <dl id="search-results">
44
      <% @results.each do |e| %>
44
      <% @results.each do |e, rank| %>
45 45
        <dt class="<%= e.event_type %>">
46 46
          <%= content_tag('span', e.project, :class => 'project') unless @project == e.project %>
47
          <%= link_to(highlight_tokens(e.event_title.truncate(255), @tokens), e.event_url) %>
47
          <%= link_to(highlight_tokens(e.event_title.truncate(255), @tokens), e.event_url, :data => { :rank => rank }) %>
48 48
        </dt>
49 49
        <dd><span class="description"><%= highlight_tokens(e.event_description, @tokens) %></span>
50 50
        <span class="author"><%= format_time(e.event_datetime) %></span></dd>
lib/redmine/search.rb
82 82
      # Returns the results for the given offset and limit
83 83
      def results(offset, limit)
84 84
        result_ids_to_load = result_ids[offset, limit] || []
85
  
85

  
86 86
        results_by_scope = Hash.new {|h,k| h[k] = []}
87 87
        result_ids_to_load.group_by(&:first).each do |scope, scope_and_ids|
88 88
          klass = scope.singularize.camelcase.constantize
89 89
          results_by_scope[scope] += klass.search_results_from_ids(scope_and_ids.map(&:last))
90 90
        end
91
  
92
        result_ids_to_load.map do |scope, id|
93
          results_by_scope[scope].detect {|record| record.id == id}
91

  
92
        result_ids_to_load.map do |scope, rank, id|
93
          [results_by_scope[scope].detect {|record| record.id == id}, rank]
94 94
        end.compact
95 95
      end
96 96

  
......
129 129
        end
130 130
        # sort results, higher rank and id first
131 131
        ret.sort! {|a,b| b.last <=> a.last}
132
        # only keep ids now that results are sorted
133
        ret.map! {|scope, r| [scope, r.last]}
132
        # [[scope, rank, id], ...]
133
        ret.map! {|scope, r| [scope, r].flatten }
134 134
        ret
135 135
      end
136 136
    end
(1-1/2)