Skip to content

Instantly share code, notes, and snippets.

@jparker
Created June 30, 2010 18:54
Show Gist options
  • Select an option

  • Save jparker/459069 to your computer and use it in GitHub Desktop.

Select an option

Save jparker/459069 to your computer and use it in GitHub Desktop.
Initial implementation (FLAWED)
%tr
%td= link_to "Details", billable
%td= h billable.client.name
%td= h billable.booking.date.to_s(:short)
%td= h billable.agency.name
%td= h billable.job_type.name
%td= h truncate(billable.vendor.name)
%td= h truncate(billable.project.name)
%td= number_to_currency billable.base_rate
%td= number_to_currency billable.gross_rate
%td= number_to_currency billable.commission_rate
%td= number_to_currency billable.outstanding
class Search
def initialize(query)
@query = query
end
def billables
# Billable.search parses the query and returns a named_scope with corresponding conditions
@billables ||= Billable.search(@query)
end
def sum(column)
billables.sum(column)
end
def needs_gross_invoice?
billables.any? { |b| b.needs_gross_invoice? }
end
def needs_commission_invoice?
billables.any? { |b| b.needs_commission_invoice? }
end
end
class SearchesController < ActionController::Base
# GET /search[?page=#]
def show
@search = session[:search]
@billables = @search.billables.paginate(:page => params[:page])
end
# POST /search
def create
session[:search] = Search.new(params[:query])
redirect_to search_path
end
end
= link_to_if @search.needs_gross_invoice?, new_search_gross_invoice_path
|
= link_to_if @search.needs_commission_invoice?, new_search_commission_invoice_path
%table
%thead
/ ...
%tfoot
%tr
%td{:colspan => ...} Totals
%td= number_to_currency @search.sum(:base_rate)
%td= number_to_currency @search.sum(:gross_rate)
%td= number_to_currency @search.sum(:commission_rate)
%td= number_to_currency @search.sum(:outstanding)
%tbody
= render @billables
= will_paginate @billables
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment