clean up Person scopes, and re-use one in contact controller to deprecate a controller method
This commit is contained in:
parent
7cef6a8d22
commit
c100f8bfdd
5 changed files with 21 additions and 20 deletions
|
|
@ -19,17 +19,14 @@ class ContactsController < ApplicationController
|
|||
current_user.contacts.receiving
|
||||
end
|
||||
end
|
||||
@contacts = @contacts.for_a_stream(params[:page])
|
||||
|
||||
respond_to do |format|
|
||||
format.html { @contacts = sort_and_paginate_profiles(@contacts) }
|
||||
format.mobile { @contacts = sort_and_paginate_profiles(@contacts) }
|
||||
format.json {
|
||||
@people = Person.for_json.joins(:contacts => :aspect_memberships).
|
||||
where(:contacts => { :user_id => current_user.id },
|
||||
:aspect_memberships => { :aspect_id => params[:aspect_ids] })
|
||||
|
||||
@people = Person.all_from_aspects(params[:aspect_ids], current_user).for_json
|
||||
render :json => @people.to_json
|
||||
}
|
||||
format.any{}
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -43,12 +40,4 @@ class ContactsController < ApplicationController
|
|||
@people = Person.community_spotlight
|
||||
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
|
||||
|
|
|
|||
|
|
@ -37,6 +37,12 @@ class Contact < ActiveRecord::Base
|
|||
where(:receiving => true)
|
||||
}
|
||||
|
||||
scope :for_a_stream, lambda { |page|
|
||||
includes(:aspects, :person => :profile).
|
||||
order('profiles.last_name ASC').
|
||||
paginate(:page => page, :per_page => 25)
|
||||
}
|
||||
|
||||
scope :only_sharing, lambda {
|
||||
sharing.where(:receiving => false)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -67,12 +67,18 @@ class Person < ActiveRecord::Base
|
|||
# @note user is passed in here defensively
|
||||
scope :all_from_aspects, lambda { |aspect_ids, user|
|
||||
joins(:contacts => :aspect_memberships).
|
||||
where(:contacts => {:user_id => user.id},
|
||||
:aspect_memberships => {:aspect_id => aspect_ids}).
|
||||
select("DISTINCT people.*")
|
||||
where(:contacts => {:user_id => user.id}, :aspect_memberships => {:aspect_id => aspect_ids})
|
||||
}
|
||||
|
||||
scope :in_aspects, lambda{|aspect_ids| joins(:contacts => {:aspect_memberships => :aspect}).where(Aspect.arel_table[:id].in(aspect_ids))}
|
||||
scope :unique_from_aspects, lambda{ |aspect_ids, user|
|
||||
all_from_aspects(aspect_ids, user).select('DISTINCT people.*')
|
||||
}
|
||||
|
||||
#not defensive
|
||||
scope :in_aspects, lambda { |aspect_ids|
|
||||
joins(:contacts => :aspect_memberships).
|
||||
where(:contacts => { :aspect_memberships => {:aspect_id => aspect_ids}})
|
||||
}
|
||||
|
||||
scope :profile_tagged_with, lambda{|tag_name| joins(:profile => :tags).where(:profile => {:tags => {:name => tag_name}}).where('profiles.searchable IS TRUE') }
|
||||
|
||||
|
|
|
|||
|
|
@ -48,7 +48,7 @@ class Stream::Aspect < Stream::Base
|
|||
|
||||
# @return [ActiveRecord::Association<Person>] AR association of people within stream's given aspects
|
||||
def people
|
||||
@people ||= Person.all_from_aspects(aspect_ids, user).includes(:profile)
|
||||
@people ||= Person.unique_from_aspects(aspect_ids, user).includes(:profile)
|
||||
end
|
||||
|
||||
# @return [String] URL
|
||||
|
|
|
|||
|
|
@ -92,7 +92,7 @@ describe Stream::Aspect do
|
|||
stream = Stream::Aspect.new(alice, [])
|
||||
|
||||
stream.stub(:aspect_ids).and_return(aspect_ids)
|
||||
Person.should_receive(:all_from_aspects).with(stream.aspect_ids, alice).and_return(stub(:includes => :profile))
|
||||
Person.should_receive(:unique_from_aspects).with(stream.aspect_ids, alice).and_return(stub(:includes => :profile))
|
||||
stream.people
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue