Skip to content

Instantly share code, notes, and snippets.

@AndrewO
Created February 12, 2010 16:31
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AndrewO/302716 to your computer and use it in GitHub Desktop.
Save AndrewO/302716 to your computer and use it in GitHub Desktop.
class Label < ActiveRecord::Base
searchable do
text :user_name do |label|
label.user.name
end
string :tracking_number
string :status
date :created_at
end
end
class Admin::LabelsController < ApplicationController
def index
@search = Sunspot.new_search(Label)
@search.build do |q|
q.keywords(params[:keywords]) if params[:keywords].present?
q.with(:tracking_number, params[:tracking_number]) if params[:tracking_number].present?
q.with(:status, params[:status]) if params[:status].present?
q.order_by(:created_at, :desc)
q.facet :status
q.data_accessor_for(Label).include = {:collection => [:brigade, :team]}
end
respond_to do |format|
format.html { @search.execute! }
format.csv do
render :text => proc {|resp, output|
output.write FasterCSV.generate_line(
["User Name", "Tracking Number", "Status", "Created At"]
)
# Ugly, but given will_paginate's collection API, this seems to the best option.
page = 1
while page
@search.build do |q|
q.paginate(:per_page => 10000, :page => page)
end
@search.execute!
@search.hits.each do |hit|
output.write FasterCSV.generate_line([
[
:user_name
:status,
:tracking_number,
:created_at
].map {|col| hit.stored(col)}
])
end
page = @search.results.next_page
end
}
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment