Created
February 12, 2010 16:31
-
-
Save AndrewO/302716 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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