Skip to content

Commit

Permalink
Move counting to model where it belongs. Rails-ier response for counts
Browse files Browse the repository at this point in the history
  • Loading branch information
Bernerd Schaefer and Josh Graham authored and Pairing Workstation Sears committed May 25, 2010
1 parent 97817af commit 5c17ac2
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 9 deletions.
12 changes: 7 additions & 5 deletions app/controllers/clients_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
class ClientsController < ApplicationController
respond_to :html, :xml, :json

# GET /clients
# GET /clients.xml
# GET /clients.json
Expand All @@ -8,12 +9,13 @@ def index
respond_with(@clients)
end

def draft_timesheets_count
@clients = Client.all
@timesheets = @clients.map do |client|
{ :id => client.id, :draft_timesheets_count => client.draft_timesheets.count }
# GET /clients/counts
# GET /clients/counts.xml
# GET /clients/counts.json
def counts
respond_with(Client.all_with_counts, :root => 'clients') do |format|
format.html { redirect_to clients_path }
end
respond_with(@timesheets)
end

def recent
Expand Down
6 changes: 4 additions & 2 deletions app/models/client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,10 @@ class Client < ActiveRecord::Base

validates_presence_of :name

def draft_timesheets
timesheets.draft.to_a # TODO: remove .to_a when Rails to_json bug fixed
def self.all_with_counts
all.map do |client|
{ :id => client.id, :draft_timesheets_count => client.timesheets.draft.count }
end
end

end
2 changes: 1 addition & 1 deletion app/views/clients/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -27,4 +27,4 @@
<%= link_to 'New client', new_client_path -%>

<br/>
<%= link_to 'Update draft timesheets count', draft_timesheets_count_clients_path, :remote => true, :'data-type' => :json, :id => 'update_client_details' -%>
<%= link_to 'Update draft timesheets count', counts_clients_path, :remote => true, 'data-type' => :json, :id => 'update_draft_timesheets' -%>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
collection do
get :recent
get :newest
get :draft_timesheets_count
get :counts
end

resources :billing_codes
Expand Down

0 comments on commit 5c17ac2

Please sign in to comment.