Skip to content

Instantly share code, notes, and snippets.

@colllin
Last active December 24, 2015 12:29
Show Gist options
  • Select an option

  • Save colllin/6798463 to your computer and use it in GitHub Desktop.

Select an option

Save colllin/6798463 to your computer and use it in GitHub Desktop.
Rails - basic filtering & sorting
class EventsController < ApplicationController
def index
# start with default filtering (usually no filtering)
@events = Event.all
# custom (user-chosen) filtering
if params[:filters].present?
# you can check for all the different filter values you want to allow. first is future/current/past
if params[:filters][:when] == 'now'
@events = @events.where(['start_time < ?', DateTime.now]).where(['end_time > ?', DateTime.now])
elsif params[:filters][:when] == 'future'
@events = @events.where(['start_time > ?', DateTime.now])
elsif params[:filters][:when] == 'past'
@events = @events.where(['end_time < ?', DateTime.now])
end
# zip
if params[:filters][:zip].to_i.present?
# hardcode distance to 25 miles for now
# need to add a custom method or custom scope to Event class named 'near',
# or change this code to use a function you got from a gem
@events = @events.near(params[:filters][:zip].to_i, 25)
end
end
# sorting
if params[:sort].present?
# sort in reverse order if the sort param starts with a hyphen
# allows you to create links like ....&sort=name... or ....&sort=-name.... for reverse order by name
if params[:sort].start_with?('-')
sortby = params[:sort].sub(/^-/, '') + ' DESC'
end
# then add the sort param to the events query
@events = @events.order(sortby)
else
# default sort
@events = @events.order('created_at DESC')
end
# check out the will_paginate gem if you want to enable pagination (page=1, page=2, etc)
@events = @events.to_a
end
end
<% params[:filters] ||= {} %>
<form class="form-inline">
<fieldset>
<legend>Filters</legend>
<div class="pull-right">
<input type="text" placeholder="zip" name="filters[zip]" value="<%= params[:filters][:zip] %>">
<input type="submit" value="Apply">
</div>
<input type="hidden" name="filters[when]" value="<%= params[:filters][:when] || '' %>">
<div class="btn-group">
<%= link_to 'Past', events_url(filters: params[:filters].merge(when: 'past')), class: "btn btn-inverse #{'active' if params[:filters][:when] == 'past'}" %>
<%= link_to 'Current', events_url(filters: params[:filters].merge(when: 'now')), class: "btn btn-inverse #{'active' if params[:filters][:when] == 'now'}" %>
<%= link_to 'Future', events_url(filters: params[:filters].merge(when: 'future')), class: "btn btn-inverse #{'active' if params[:filters][:when] == 'future'}" %>
<%= link_to 'All', events_url(filters: params[:filters].merge(when: nil)), class: "btn btn-inverse #{'active' if params[:filters][:when].blank?}" %>
</div>
<input type="hidden" name="filters[sort]" value="<%= params[:filters][:sort] || '' %>">
<div class="btn-group">
<%= link_to 'Newest First', events_url(filters: params[:filters].merge(sort: '-start_time')), class: "btn btn-inverse #{'active' if params[:filters][:sort] == '-start_time'}" %>
<%= link_to 'Oldest First', events_url(filters: params[:filters].merge(sort: 'start_time')), class: "btn btn-inverse #{'active' if params[:filters][:sort] == 'start_time'}" %>
</div>
</fieldset>
</form>
@glreese12

Copy link
Copy Markdown

Thanks for your help on this; I got everything working. I had to modify the sort section to include the filter parameter.

if params[:filters][:sort].present?
# sort in reverse order if the sort param starts with a hyphen
# allows you to create links like ....&sort=name... or ....&sort=-name.... for reverse order by name
if params[:filters][:sort].start_with?('-')
sortby = params[:filters][:sort].sub(/^-/, '') + ' Desc'
logger.info sortby
# then add the sort param to the events query
@events = @events.order(sortby)
else
# default sort
@events = @events.order('StartDate')
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment