filter logic in aspects_controller#index
This commit is contained in:
parent
9411ba31b0
commit
79b7f62520
2 changed files with 61 additions and 21 deletions
|
|
@ -10,7 +10,16 @@ class AspectsController < ApplicationController
|
|||
respond_to :js
|
||||
|
||||
def index
|
||||
@posts = current_user.visible_posts(:_type => "StatusMessage").paginate :page => params[:page], :per_page => 15, :order => 'created_at DESC'
|
||||
if params[:a_ids]
|
||||
aspects = current_user.aspects_from_ids(params[:a_ids])
|
||||
else
|
||||
aspects = current_user.aspects
|
||||
end
|
||||
|
||||
post_ids = aspects.map!{|a| a.post_ids}.flatten!
|
||||
|
||||
@posts = Post.where(:id.in => post_ids, :_type => "StatusMessage").paginate :page => params[:page], :per_page => 15, :order => 'created_at DESC'
|
||||
|
||||
@post_hashes = hashes_for_posts @posts
|
||||
@contacts = Contact.all(:user_id => current_user.id, :pending => false)
|
||||
@aspect_hashes = hashes_for_aspects @aspects.all, @contacts, :limit => 8
|
||||
|
|
|
|||
|
|
@ -11,11 +11,11 @@ describe AspectsController do
|
|||
@user = make_user
|
||||
@user2 = make_user
|
||||
|
||||
@aspect = @user.aspects.create(:name => "lame-os")
|
||||
@aspect0 = @user.aspects.create(:name => "lame-os")
|
||||
@aspect1 = @user.aspects.create(:name => "another aspect")
|
||||
@aspect2 = @user2.aspects.create(:name => "party people")
|
||||
|
||||
connect_users(@user, @aspect, @user2, @aspect2)
|
||||
connect_users(@user, @aspect0, @user2, @aspect2)
|
||||
|
||||
@contact = @user.contact_for(@user2.person)
|
||||
@user.getting_started = false
|
||||
|
|
@ -35,6 +35,37 @@ describe AspectsController do
|
|||
end
|
||||
assigns[:contacts].should == @user.contacts
|
||||
end
|
||||
|
||||
context 'filtering' do
|
||||
before(:all) do
|
||||
@posts = []
|
||||
@users = []
|
||||
8.times do |n|
|
||||
user = make_user
|
||||
@users << user
|
||||
aspect = user.aspects.create(:name => 'people')
|
||||
connect_users(@user, @aspect0, user, aspect)
|
||||
post = @user.post(:status_message, :message => "hello#{n}", :to => eval("@aspect#{(n%2)}.id"))
|
||||
@posts << post
|
||||
end
|
||||
end
|
||||
|
||||
it "returns all posts" do
|
||||
get :index
|
||||
assigns(:posts).length.should == 8
|
||||
end
|
||||
|
||||
it "returns posts filtered by a single aspect" do
|
||||
get :index, :a_ids => [@aspect1.id.to_s]
|
||||
assigns(:posts).length.should == 4
|
||||
end
|
||||
|
||||
it "returns posts from filtered aspects" do
|
||||
get :index, :a_ids => [@aspect0.id.to_s, @aspect1.id.to_s]
|
||||
assigns(:posts).length.should == 8
|
||||
end
|
||||
end
|
||||
|
||||
context 'performance' do
|
||||
before do
|
||||
require 'benchmark'
|
||||
|
|
@ -44,7 +75,7 @@ describe AspectsController do
|
|||
user = make_user
|
||||
@users << user
|
||||
aspect = user.aspects.create(:name => 'people')
|
||||
connect_users(@user, @aspect, user, aspect)
|
||||
connect_users(@user, @aspect0, user, aspect)
|
||||
post = @user.post(:status_message, :message => "hello#{n}", :to => @aspect1.id)
|
||||
@posts << post
|
||||
user.comment "yo#{post.message}", :on => post
|
||||
|
|
@ -61,13 +92,13 @@ describe AspectsController do
|
|||
|
||||
describe "#show" do
|
||||
it "succeeds" do
|
||||
get :show, 'id' => @aspect.id.to_s
|
||||
get :show, 'id' => @aspect0.id.to_s
|
||||
response.should be_success
|
||||
end
|
||||
it "assigns aspect, aspect_contacts, and posts" do
|
||||
get :show, 'id' => @aspect.id.to_s
|
||||
get :show, 'id' => @aspect0.id.to_s
|
||||
assigns(:aspect).should == @aspect
|
||||
achash = @controller.send(:hashes_for_contacts, @aspect.contacts).first
|
||||
achash = @controller.send(:hashes_for_contacts, @aspect0.contacts).first
|
||||
assigns(:aspect_contacts).first[:contact].should == achash[:contact]
|
||||
assigns(:aspect_contacts).first[:person].should == achash[:person]
|
||||
assigns(:posts).should == []
|
||||
|
|
@ -77,7 +108,7 @@ describe AspectsController do
|
|||
@user.send_contact_request_to(make_user.person, @aspect)
|
||||
@user.contacts.count.should == 2
|
||||
|
||||
get :show, 'id' => @aspect.id.to_s
|
||||
get :show, 'id' => @aspect0.id.to_s
|
||||
contacts = assigns(:contacts)
|
||||
contacts.count.should == 1
|
||||
contacts.first.should == @contact
|
||||
|
|
@ -85,10 +116,10 @@ describe AspectsController do
|
|||
it "paginates" do
|
||||
16.times { |i| @user2.post(:status_message, :to => @aspect2.id, :message => "hi #{i}") }
|
||||
|
||||
get :show, 'id' => @aspect.id.to_s
|
||||
get :show, 'id' => @aspect0.id.to_s
|
||||
assigns(:posts).count.should == 15
|
||||
|
||||
get :show, 'id' => @aspect.id.to_s, 'page' => '2'
|
||||
get :show, 'id' => @aspect0.id.to_s, 'page' => '2'
|
||||
assigns(:posts).count.should == 1
|
||||
end
|
||||
end
|
||||
|
|
@ -175,7 +206,7 @@ describe AspectsController do
|
|||
@person = Factory.create(:person)
|
||||
@opts = {
|
||||
:person_id => @person.id,
|
||||
:from => @aspect.id,
|
||||
:from => @aspect0.id,
|
||||
:to =>
|
||||
{:to => @aspect1.id}
|
||||
}
|
||||
|
|
@ -216,39 +247,39 @@ describe AspectsController do
|
|||
@user.reload
|
||||
@hashes = @controller.send(:hashes_for_aspects, @user.aspects, @user.contacts, :limit => 9)
|
||||
@hash = @hashes.first
|
||||
@aspect = @user.aspects.first
|
||||
@aspect0 = @user.aspects.first
|
||||
end
|
||||
it 'has aspects' do
|
||||
@hashes.length.should == 2
|
||||
@hash[:aspect].should == @aspect
|
||||
end
|
||||
it 'has a contact_count' do
|
||||
@hash[:contact_count].should == @aspect.contacts.count
|
||||
@hash[:contact_count].should == @aspect0.contacts.count
|
||||
end
|
||||
it 'takes a limit on contacts returned' do
|
||||
@hash[:contacts].count.should == 9
|
||||
end
|
||||
it 'has a person in each hash' do
|
||||
@aspect.contacts.map{|c| c.person}.include?(@hash[:contacts].first[:person]).should be_true
|
||||
@aspect0.contacts.map{|c| c.person}.include?(@hash[:contacts].first[:person]).should be_true
|
||||
end
|
||||
it "does not return the rsa key" do
|
||||
@hash[:contacts].first[:person].serialized_public_key.should be_nil
|
||||
end
|
||||
it 'has a contact in each hash' do
|
||||
@aspect.contacts.include?(@hash[:contacts].first[:contact]).should be_true
|
||||
@aspect0.contacts.include?(@hash[:contacts].first[:contact]).should be_true
|
||||
end
|
||||
end
|
||||
|
||||
describe "#update" do
|
||||
before do
|
||||
@aspect = @user.aspects.create(:name => "Bruisers")
|
||||
@aspect0 = @user.aspects.create(:name => "Bruisers")
|
||||
end
|
||||
it "doesn't overwrite random attributes" do
|
||||
new_user = Factory.create :user
|
||||
params = {"name" => "Bruisers"}
|
||||
params[:user_id] = new_user.id
|
||||
put('update', :id => @aspect.id, "aspect" => params)
|
||||
Aspect.find(@aspect.id).user_id.should == @user.id
|
||||
put('update', :id => @aspect0.id, "aspect" => params)
|
||||
Aspect.find(@aspect0.id).user_id.should == @user.id
|
||||
end
|
||||
end
|
||||
|
||||
|
|
@ -309,10 +340,10 @@ describe AspectsController do
|
|||
post 'remove_from_aspect',
|
||||
:format => 'js',
|
||||
:person_id => @user2.person.id,
|
||||
:aspect_id => @aspect.id
|
||||
:aspect_id => @aspect0.id
|
||||
response.should be_success
|
||||
@aspect.reload
|
||||
@aspect.contacts.include?(@contact).should be false
|
||||
@aspect0.reload
|
||||
@aspect0.contacts.include?(@contact).should be false
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
|||
Loading…
Reference in a new issue