Rewrote ContactsController#index, thoughts?

This commit is contained in:
Dan Hansen
2011-07-17 15:44:07 -07:00
parent 7ad80bb358
commit 6a11784a2e
3 changed files with 37 additions and 27 deletions

View File

@@ -6,22 +6,29 @@ class ContactsController < ApplicationController
before_filter :authenticate_user!
def index
@aspect = :manage
if params[:a_id]
@aspect_ = current_user.aspects.find(params["a_id"])
@contacts = @aspect_.contacts.includes(:aspects, :person => :profile).order('profiles.last_name ASC').paginate(:page => params[:page], :per_page => 25)
elsif params[:aspect_ids] && request.format == "json"
@people = Person.joins(:contacts => :aspect_memberships).
where(:contacts => {:user_id => current_user.id},
:aspect_memberships => {:aspect_id => params[:aspect_ids]})
render :json => @people.includes(:profile).to_json
elsif params[:set] == "only_sharing"
@contacts = current_user.contacts.only_sharing.includes(:aspects, :person => :profile).order('profiles.last_name ASC').paginate(:page => params[:page], :per_page => 25)
elsif params[:set] != "all"
@contacts = current_user.contacts.receiving.includes(:aspects, :person => :profile).order('profiles.last_name ASC').paginate(:page => params[:page], :per_page => 25)
@contacts = case params[:set]
when "only_sharing"
current_user.contacts.only_sharing
when "all"
current_user.contacts
else
@contacts = current_user.contacts.includes(:aspects, :person => :profile).order('profiles.last_name ASC').paginate(:page => params[:page], :per_page => 25)
if params[:a_id]
@aspect = current_user.aspects.find(params[:a_id])
@aspect.contacts
else
current_user.contacts.receiving
end
end
respond_to do |format|
format.html { @contacts = sort_and_paginate_profiles(@contacts) }
format.json {
@people = Person.joins(:contacts => :aspect_memberships).
where(:contacts => { :user_id => current_user.id },
:aspect_memberships => { :aspect_id => params[:aspect_ids] })
render :json => @people.includes(:profile).to_json
}
end
end
@@ -30,4 +37,12 @@ class ContactsController < ApplicationController
render :layout => false
end
private
def sort_and_paginate_profiles contacts
contacts.
includes(:aspects, :person => :profile).
order('profiles.last_name ASC').
paginate(:page => params[:page], :per_page => 25)
end
end