MS IZ refactored querying
This commit is contained in:
parent
acdbbe4dfc
commit
fa484e95e4
8 changed files with 58 additions and 11 deletions
|
|
@ -30,7 +30,7 @@ class ApplicationController < ActionController::Base
|
||||||
|
|
||||||
@aspects = current_user.aspects
|
@aspects = current_user.aspects
|
||||||
@aspects_dropdown_array = current_user.aspects.collect{|x| [x.to_s, x.id]}
|
@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
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -47,7 +47,7 @@ class AspectsController < ApplicationController
|
||||||
unless @aspect
|
unless @aspect
|
||||||
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404
|
render :file => "#{Rails.root}/public/404.html", :layout => false, :status => 404
|
||||||
else
|
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'
|
@posts = current_user.visible_posts( :by_members_of => @aspect ).paginate :per_page => 15, :order => 'created_at DESC'
|
||||||
respond_with @aspect
|
respond_with @aspect
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -30,6 +30,11 @@ class Aspect
|
||||||
posts.detect{|x| x.person.id == id }
|
posts.detect{|x| x.person.id == id }
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def person_objects
|
||||||
|
person_ids = people.map{|x| x.person_id}
|
||||||
|
Person.all(:id.in => person_ids)
|
||||||
|
end
|
||||||
|
|
||||||
def as_json(opts = {})
|
def as_json(opts = {})
|
||||||
{
|
{
|
||||||
:aspect => {
|
:aspect => {
|
||||||
|
|
|
||||||
|
|
@ -15,4 +15,5 @@ class Contact
|
||||||
key :aspect_ids, Array, :typecast => 'ObjectId'
|
key :aspect_ids, Array, :typecast => 'ObjectId'
|
||||||
many :aspects, :in => :aspect_ids, :class_name => 'Aspect'
|
many :aspects, :in => :aspect_ids, :class_name => 'Aspect'
|
||||||
validates_presence_of :aspects
|
validates_presence_of :aspects
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -236,7 +236,7 @@ class User
|
||||||
|
|
||||||
push_to_hub(post) if post.respond_to?(:public) && post.public
|
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
|
end
|
||||||
|
|
||||||
def push_to_people(post, people)
|
def push_to_people(post, people)
|
||||||
|
|
@ -289,7 +289,8 @@ class User
|
||||||
if owns? comment.post
|
if owns? comment.post
|
||||||
comment.post_creator_signature = comment.sign_with_key(encryption_key)
|
comment.post_creator_signature = comment.sign_with_key(encryption_key)
|
||||||
comment.save
|
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
|
elsif owns? comment
|
||||||
comment.save
|
comment.save
|
||||||
push_to_people comment, [comment.post.person]
|
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
|
post.unsocket_from_uid(self.id, :aspect_ids => aspect_ids) if post.respond_to? :unsocket_from_uid
|
||||||
retraction = Retraction.for(post)
|
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
|
retraction
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -33,7 +33,18 @@ module Diaspora
|
||||||
end
|
end
|
||||||
|
|
||||||
def friends_not_in_aspect( aspect )
|
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
|
end
|
||||||
|
|
||||||
def aspect_by_id( id )
|
def aspect_by_id( id )
|
||||||
|
|
@ -45,6 +56,7 @@ module Diaspora
|
||||||
self.aspects.find_all_by_post_ids( id.to_id )
|
self.aspects.find_all_by_post_ids( id.to_id )
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
def aspects_with_person person
|
def aspects_with_person person
|
||||||
contact_for(person).aspects
|
contact_for(person).aspects
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -22,7 +22,7 @@ describe AspectsController do
|
||||||
it "assigns @friends to all the user's friends" do
|
it "assigns @friends to all the user's friends" do
|
||||||
Factory.create :person
|
Factory.create :person
|
||||||
get :index
|
get :index
|
||||||
assigns[:friends].should == @user.friends.map{|c| c.person}
|
assigns[:friends].should == @user.person_objects
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -61,19 +61,47 @@ describe User do
|
||||||
let!(:user) {Factory :user}
|
let!(:user) {Factory :user}
|
||||||
let!(:first_aspect) {user.aspect(:name => 'bruisers')}
|
let!(:first_aspect) {user.aspect(:name => 'bruisers')}
|
||||||
let!(:second_aspect) {user.aspect(:name => 'losers')}
|
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
|
describe '#friends_not_in_aspect' do
|
||||||
it 'finds the people who are not in the given 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 = user.friends_not_in_aspect(first_aspect)
|
||||||
people.should == [user2.person]
|
people.should == [user2.person]
|
||||||
end
|
end
|
||||||
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
|
end
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
describe '#albums_by_aspect' do
|
describe '#albums_by_aspect' do
|
||||||
let!(:first_aspect) {user2.aspect(:name => 'bruisers')}
|
let!(:first_aspect) {user2.aspect(:name => 'bruisers')}
|
||||||
let!(:second_aspect) {user2.aspect(:name => 'losers')}
|
let!(:second_aspect) {user2.aspect(:name => 'losers')}
|
||||||
Loading…
Reference in a new issue