Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add pagination #24

Closed
wants to merge 2 commits into from

3 participants

@jgrevich

I saw that the pagination was available in the original SearchEngine model and decided to implement it further. This is still a bit 'first draftish' and could be refined. I can provide patches for the other branches if desired.

@vaicine

This works well. Many thanks!

@jgrevich

Glad to hear. I'll try to expedite the process of creating patches for each branch and some tests... any one else want to lend a hand? :)

@parndt
Owner

As this is a new feature can you craft this against the master branch please? Apologies for the delay.

@parndt parndt closed this
@jgrevich

@parndt Will do, I'll try to get that wrapped up this weekend.

@kdbaker kdbaker referenced this pull request from a commit
@kdbaker kdbaker Update search_controller.rb
Updating per #24 by jgrevich
059d171
@kdbaker kdbaker referenced this pull request from a commit
@kdbaker kdbaker Update search_engine.rb
Updating per #24 by jgrevich
21fe514
@kdbaker kdbaker referenced this pull request from a commit
@kdbaker kdbaker Update show.html.erb
Updating per #24 by jgrevich
4039b3a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Aug 21, 2012
  1. @jgrevich
  2. @jgrevich

    removed unnecessary logging.

    jgrevich authored
This page is out of date. Refresh to see the latest.
View
6 app/controllers/refinery/search_controller.rb
@@ -3,7 +3,11 @@ class SearchController < ::ApplicationController
# Display search results given the query supplied
def show
- @results = Refinery::SearchEngine.search(params[:query], params[:page])
+ query = params[:query]
+ @page_number = params[:page].nil? ? 1 : params[:page].to_i
+ @search = Refinery::SearchEngine.search(query, @page_number)
+ @results = @search[:results]
+ @count = @search[:count]
present(@page = Refinery::Page.find_by_link_url("/search"))
end
View
14 app/models/refinery/search_engine.rb
@@ -5,14 +5,22 @@ class SearchEngine
RESULTS_LIMIT = 10
# Perform search over the specified models
- def self.search(query, page = 1)
+ def self.search(query, page = 1, per = RESULTS_LIMIT)
+ page = 1 if page.nil?
results = []
+ offset = page.to_i*per
Refinery.searchable_models.each do |model|
- results << model.limit(RESULTS_LIMIT).with_query(query)
+ results << model.with_query(query)
end if query.present?
- results.flatten[0..(RESULTS_LIMIT - 1)]
+ count = results.flatten.count
+ start = -per + offset
+ finish = start + per-1
+ results = results.flatten[start..(finish)]
+ pages = (count.to_f/per.to_f).ceil
+
+ results = { results: results, count: count, pages: pages, per: per }
end
end
View
27 app/views/refinery/search/show.html.erb
@@ -1,19 +1,24 @@
+
<% content_for :body_content_title do %>
<%= t('.search_results_for', :what => params[:query]) %>
<% end %>
<% content_for :body_content_left do %>
- <ul id="search_results">
- <% @results.each do |result| %>
- <li>
- <span class='result_type'>
- <%= result_type result %>
- </span>
- <%= link_to result_title(result).html_safe, result_url(result) %>
- </li>
- <% end %>
- </ul>
-
+<section id="search_results">
+ <header><h1><%= @count %> Results for '<%=h params[:query] %>'</h1></header>
+ <ul>
+ <% @results.each do |result| %>
+ <li <%= "class='last'" if result == @results.last %>>
+ <span class='result_type'><%= result_type result %></span>
+ <%= link_to result_title(result).html_safe, result_url(result) %>
+ </li>
+ <% end if @results%>
+ </ul>
+ <div class='pagination'>
+ <%= link_to "Previous", search_url(:query => params[:query], :page => (@page_number-1)) unless @page_number == nil || @page_number < 2 %>
+ <%= link_to "Next", search_url(:query => params[:query], :page => (@page_number > 1 ? @page_number+1 : 2)) unless @page_number >= @search[:pages].to_i %>
+ </div>
+</section>
<% end %>
<%= render :partial => "/refinery/content_page" %>
Something went wrong with that request. Please try again.