MS IZ refactored querying

This commit is contained in:
zhitomirskiyi 2010-10-26 17:05:38 -07:00
parent acdbbe4dfc
commit fa484e95e4
8 changed files with 58 additions and 11 deletions

View file

@ -30,7 +30,7 @@ class ApplicationController < ActionController::Base
@aspects = current_user.aspects
@aspects_dropdown_array = current_user.aspects.collect{|x| [x.to_s, x.id]}
@friends = current_user.friends.map{|c| c.person}
@friends = current_user.person_objects
end
end

View file

@ -47,7 +47,7 @@ class AspectsController < ApplicationController
unless @aspect
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404
else
@friends = @aspect.people.map{|c| c.person}
@friends = @aspect.person_objects
@posts = current_user.visible_posts( :by_members_of => @aspect ).paginate :per_page => 15, :order => 'created_at DESC'
respond_with @aspect
end

View file

@ -30,6 +30,11 @@ class Aspect
posts.detect{|x| x.person.id == id }
end
def person_objects
person_ids = people.map{|x| x.person_id}
Person.all(:id.in => person_ids)
end
def as_json(opts = {})
{
:aspect => {

View file

@ -15,4 +15,5 @@ class Contact
key :aspect_ids, Array, :typecast => 'ObjectId'
many :aspects, :in => :aspect_ids, :class_name => 'Aspect'
validates_presence_of :aspects
end

View file

@ -236,7 +236,7 @@ class User
push_to_hub(post) if post.respond_to?(:public) && post.public
push_to_people(post, target_contacts.map{|c|c.person})
push_to_people(post, self.person_objects(target_contacts))
end
def push_to_people(post, people)
@ -289,7 +289,8 @@ class User
if owns? comment.post
comment.post_creator_signature = comment.sign_with_key(encryption_key)
comment.save
push_to_people comment, contacts_in_aspects(aspects_with_post(comment.post.id)).map{|c|c.person}
aspects = aspects_with_post(comment.post_id)
push_to_people(comment, people_in_aspects(aspects))
elsif owns? comment
comment.save
push_to_people comment, [comment.post.person]
@ -303,7 +304,7 @@ class User
post.unsocket_from_uid(self.id, :aspect_ids => aspect_ids) if post.respond_to? :unsocket_from_uid
retraction = Retraction.for(post)
push_to_people retraction, contacts_in_aspects(aspects_with_post(post.id)).map{|c| c.person}
push_to_people retraction, people_in_aspects(aspects_with_post(post.id))
retraction
end

View file

@ -33,7 +33,18 @@ module Diaspora
end
def friends_not_in_aspect( aspect )
Contact.all(:user_id => self.id, :aspect_ids.ne => aspect._id).map{|c| c.person}
person_ids = Contact.all(:user_id => self.id, :aspect_ids.ne => aspect._id).collect{|x| x.person_id }
Person.all(:id.in => person_ids)
end
def person_objects(contacts = self.friends)
person_ids = contacts.collect{|x| x.person_id}
Person.all(:id.in => person_ids)
end
def people_in_aspects(aspects)
person_ids = contacts_in_aspects(aspects).collect{|x| x.person_id}
Person.all(:id.in => person_ids)
end
def aspect_by_id( id )
@ -45,6 +56,7 @@ module Diaspora
self.aspects.find_all_by_post_ids( id.to_id )
end
def aspects_with_person person
contact_for(person).aspects
end

View file

@ -22,7 +22,7 @@ describe AspectsController do
it "assigns @friends to all the user's friends" do
Factory.create :person
get :index
assigns[:friends].should == @user.friends.map{|c| c.person}
assigns[:friends].should == @user.person_objects
end
end

View file

@ -61,19 +61,47 @@ describe User do
let!(:user) {Factory :user}
let!(:first_aspect) {user.aspect(:name => 'bruisers')}
let!(:second_aspect) {user.aspect(:name => 'losers')}
let!(:user4) { Factory.create(:user_with_aspect)}
before do
friend_users(user, first_aspect, user4, user4.aspects.first)
friend_users(user, second_aspect, user2, user2.aspects.first)
end
describe '#friends_not_in_aspect' do
it 'finds the people who are not in the given aspect' do
user4 = Factory.create(:user_with_aspect)
friend_users(user, first_aspect, user4, user4.aspects.first)
friend_users(user, second_aspect, user2, user2.aspects.first)
people = user.friends_not_in_aspect(first_aspect)
people.should == [user2.person]
end
end
describe '#person_objects' do
it 'returns "person" objects for all of my friends' do
people = user.person_objects
people.size.should == 2
[user4.person, user2.person].each{ |p| people.should include p }
end
it 'should return people objects given a collection of contacts' do
target_contacts = [user.contact_for(user2.person)]
people = user.person_objects(target_contacts)
people.should == [user2.person]
end
end
describe '#people_in_aspects' do
it 'should return people objects for a users friend in each aspect' do
people = user.people_in_aspects([first_aspect])
people.should == [user4.person]
people = user.people_in_aspects([second_aspect])
people.should == [user2.person]
end
end
end
describe '#albums_by_aspect' do
let!(:first_aspect) {user2.aspect(:name => 'bruisers')}
let!(:second_aspect) {user2.aspect(:name => 'losers')}