SimpleSearch

SimpleSearch privides search methods for your ActiveRecord models, by providing methods that allow you to search, group, and order. Main goal is to simplify all search related operations from url.

  • Searching

  • Paging

  • Grouping

  • Ordering

Getting Started

In your Gemfile:

gem "simple-search"  # Last officially released gem

In your controller:

def index
  @posts = Post.select('posts.*, comments.id as comment_id, comments.body as comment').joins(:comments)
  @posts = @posts.simplesearch(params)
  render :index
end

In your view:

<%= form_tag('/posts', :method=>:GET) do -%>
  ID &gt; <%= text_field_tag 'id_gt', params[:id_gt] %> <br/>
  Subject contains <%= text_field_tag 'subject_ct', params[:subject_ct] %> <br/>
  <%= submit_tag "Search" %>
<% end -%>

<style>
  #pages a {border:1px solid grey; display:inline-block; min-width:13px; text-align:center; text-decoration: none}
  #pages a:first-child, #pages a:last-child {background-color:grey}
  #pages a.current {color:white; background-color:#333}
</style>
<div id="pages">
  Pages : <%=raw page_urls(@posts, :num_pages=>5).join(" ") %>
</div>

<table>
  <tr>
    <th><%=order_link(:id)%></th>
    <th><%=order_link(:subject)%></th>
    <th><%=order_link(:body)%></th>
    <th><%=order_link(:comment_id)%></th>
    <th><%=order_link(:comment)%></th>
  </tr>
<% @posts.each do |post| %>
  <tr>
    <td><%=h post.id %></td>
    <td><%=h post.subject %></td>
    <td><%=h post.body %></td>
    <td><%=h post.comment_id %></td>
    <td><%=h post.comment %></td>
  </tr>
<% end %>
</table>

Search postfixes

  • _eq, equal to, =

  • _gt, greater than, >

  • _lt, less than, <

  • _le, less or equal to, <=

  • _ge, greater or equal to, <=

  • _in, includes, IN

    i.e.,  &id_in=1,2,3
    
  • _bt (alias of _between), between, BETWEEN

    i.e.,  &id_between=1,3
    
  • _sw (alias of _startswith), starts with, LIKE ‘key%’ (

  • _ew (alias of _endsswith), ends with, LIKE ‘%key’

  • _ct (alias of _contains,_like), contains, LIKE ‘%key%’

  • _nc (alias of _notcontains,_notlike), not contains, NOT LIKE ‘%key%’

  • _is, IS

    i.e.,  &id_is=null
    
  • _it (alias of _isnot), IS NOT

Sorting your result

  • order_by

    i.e.,  &order_by=id+asc
    

Paging

  • page , page number

    i.e. &page=1
    
  • page_by, number of rows in a page

    i.e. &page_by=10
    

Grouping

  • group_by, column to group

    i.e. &group_by=posts.id
    
  • Check out the latest master to make sure the feature hasn’t been implemented or the bug hasn’t been fixed yet.

  • Check out the issue tracker to make sure someone already hasn’t requested it and/or contributed it.

  • Fork the project.

  • Start a feature/bugfix branch.

  • Commit and push until you are happy with your contribution.

  • Make sure to add tests for it. This is important so I don’t break it in a future version unintentionally.

  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright © 2012 Allen Kim. See LICENSE.txt for further details.