Refactor views to not be so dumb.
This commit is contained in:
parent
7b3aac923a
commit
7ca124c002
14 changed files with 114 additions and 21 deletions
|
|
@ -10,6 +10,8 @@ class AspectsController < ApplicationController
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@posts = current_user.visible_posts(:_type => "StatusMessage").paginate :page => params[:page], :per_page => 15, :order => 'created_at DESC'
|
@posts = current_user.visible_posts(:_type => "StatusMessage").paginate :page => params[:page], :per_page => 15, :order => 'created_at DESC'
|
||||||
|
@post_hashes = hashes_for_posts @posts
|
||||||
|
|
||||||
@aspect = :all
|
@aspect = :all
|
||||||
|
|
||||||
if current_user.getting_started == true
|
if current_user.getting_started == true
|
||||||
|
|
@ -60,7 +62,8 @@ class AspectsController < ApplicationController
|
||||||
@aspect_contacts_count = @aspect_contacts.count
|
@aspect_contacts_count = @aspect_contacts.count
|
||||||
|
|
||||||
@posts = @aspect.posts.find_all_by__type("StatusMessage", :order => 'created_at desc').paginate :page => params[:page], :per_page => 15
|
@posts = @aspect.posts.find_all_by__type("StatusMessage", :order => 'created_at desc').paginate :page => params[:page], :per_page => 15
|
||||||
@posts_count = @posts.count
|
@post_hashes = hashes_for_posts @posts
|
||||||
|
@post_count = @posts.count
|
||||||
|
|
||||||
respond_with @aspect
|
respond_with @aspect
|
||||||
end
|
end
|
||||||
|
|
@ -130,4 +133,21 @@ class AspectsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def hashes_for_posts posts
|
||||||
|
comment_hash = Comment.hash_from_post_ids posts.map{|p| p.id}
|
||||||
|
person_hash = Person.from_post_comment_hash comment_hash
|
||||||
|
|
||||||
|
posts.map do |post|
|
||||||
|
{:post => post,
|
||||||
|
:person => post.person,
|
||||||
|
:comments => comment_hash[post.id].map do |comment|
|
||||||
|
{:comment => comment,
|
||||||
|
:person => person_hash[comment.person_id],
|
||||||
|
}
|
||||||
|
end,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -86,6 +86,13 @@ class StatusMessagesController < ApplicationController
|
||||||
|
|
||||||
def show
|
def show
|
||||||
@status_message = current_user.find_visible_post_by_id params[:id]
|
@status_message = current_user.find_visible_post_by_id params[:id]
|
||||||
|
comments_hash = Comment.hash_from_post_ids [@status_message.id]
|
||||||
|
person_hash = Person.from_post_comment_hash comments_hash
|
||||||
|
@comment_hashes = comments_hash[@status_message.id].map do |comment|
|
||||||
|
{:comment => comment,
|
||||||
|
:person => person_hash[comment.person_id]
|
||||||
|
}
|
||||||
|
end
|
||||||
respond_with @status_message
|
respond_with @status_message
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -13,7 +13,17 @@ module SocketsHelper
|
||||||
begin
|
begin
|
||||||
user = User.find_by_id uid
|
user = User.find_by_id uid
|
||||||
if object.is_a? Post
|
if object.is_a? Post
|
||||||
v = render_to_string(:partial => 'shared/stream_element', :locals => {:post => object, :current_user => user, :aspects => user.aspects})
|
post_hash = {:post => object,
|
||||||
|
:person => object.person,
|
||||||
|
:comments => object.comments.map{|c|
|
||||||
|
{:comment => c,
|
||||||
|
:person => c.person
|
||||||
|
}
|
||||||
|
},
|
||||||
|
:current_user => user,
|
||||||
|
:aspects => user.aspects,
|
||||||
|
}
|
||||||
|
v = render_to_string(:partial => 'shared/stream_element', :locals => post_hash)
|
||||||
elsif object.is_a? Person
|
elsif object.is_a? Person
|
||||||
v = render_to_string(:partial => type_partial(object), :locals => {:single_aspect_form => opts[:single_aspect_form], :person => object, :aspects => user.aspects, :current_user => user})
|
v = render_to_string(:partial => type_partial(object), :locals => {:single_aspect_form => opts[:single_aspect_form], :person => object, :aspects => user.aspects, :current_user => user})
|
||||||
elsif object.is_a? Comment
|
elsif object.is_a? Comment
|
||||||
|
|
|
||||||
|
|
@ -71,6 +71,19 @@ class Comment
|
||||||
def signature_valid?
|
def signature_valid?
|
||||||
verify_signature(creator_signature, person)
|
verify_signature(creator_signature, person)
|
||||||
end
|
end
|
||||||
|
def self.hash_from_post_ids post_ids
|
||||||
|
hash = {}
|
||||||
|
comments = self.on_posts(post_ids)
|
||||||
|
post_ids.each do |id|
|
||||||
|
hash[id] = []
|
||||||
|
end
|
||||||
|
comments.each do |comment|
|
||||||
|
hash[comment.post_id] << comment
|
||||||
|
end
|
||||||
|
hash.each_value {|comments| comments.sort!{|c1, c2| c1.created_at <=> c2.created_at }}
|
||||||
|
hash
|
||||||
|
end
|
||||||
|
scope :on_posts, lambda { |post_ids|
|
||||||
|
where(:post_id.in => post_ids)
|
||||||
|
}
|
||||||
end
|
end
|
||||||
|
|
|
||||||
|
|
@ -150,6 +150,15 @@ class Person
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def self.from_post_comment_hash(hash)
|
||||||
|
person_ids = hash.values.flatten.map{|c| c.person_id}.uniq
|
||||||
|
people = where(:id.in => person_ids)
|
||||||
|
people_hash = {}
|
||||||
|
people.each{|p| people_hash[p.id] = p}
|
||||||
|
people_hash
|
||||||
|
end
|
||||||
|
|
||||||
protected
|
protected
|
||||||
|
|
||||||
def clean_url
|
def clean_url
|
||||||
|
|
|
||||||
|
|
@ -13,9 +13,9 @@
|
||||||
.span-15
|
.span-15
|
||||||
= render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @contacts.count
|
= render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @contacts.count
|
||||||
= render 'shared/publisher', :aspect => @aspect
|
= render 'shared/publisher', :aspect => @aspect
|
||||||
= render 'aspects/no_posts_message', :post_count => @posts.count, :contact_count => @contacts.count
|
= render 'aspects/no_posts_message', :post_count => @post_hashes.length, :contact_count => @contacts.count
|
||||||
|
|
||||||
= render 'shared/stream', :posts => @posts
|
= render 'shared/stream', :posts => @post_hashes
|
||||||
|
|
||||||
#pagination
|
#pagination
|
||||||
= will_paginate @posts
|
= will_paginate @posts
|
||||||
|
|
|
||||||
|
|
@ -18,9 +18,9 @@
|
||||||
.span-15.last
|
.span-15.last
|
||||||
= render 'shared/publisher', :aspect => @aspect
|
= render 'shared/publisher', :aspect => @aspect
|
||||||
= render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @aspect_contacts_count, :options => false
|
= render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @aspect_contacts_count, :options => false
|
||||||
= render 'aspects/no_posts_message', :post_count => @posts_count, :contact_count=> @aspect_contacts_count
|
= render 'aspects/no_posts_message', :post_count => @post_count, :contact_count=> @aspect_contacts_count
|
||||||
|
|
||||||
= render 'shared/stream', :posts => @posts
|
= render 'shared/stream', :posts => @post_hashes
|
||||||
|
|
||||||
#pagination
|
#pagination
|
||||||
= will_paginate @posts
|
= will_paginate @posts
|
||||||
|
|
|
||||||
|
|
@ -3,8 +3,8 @@
|
||||||
-# the COPYRIGHT file.
|
-# the COPYRIGHT file.
|
||||||
|
|
||||||
%ul.comments{:id => post_id, :class => ("hidden" if defined?(hidden) && hidden)}
|
%ul.comments{:id => post_id, :class => ("hidden" if defined?(hidden) && hidden)}
|
||||||
- for comment in comments
|
- for comment_hash in comment_hashes
|
||||||
= render 'comments/comment', :comment => comment, :person => comment.person
|
= render 'comments/comment', comment_hash
|
||||||
%li.comment.show
|
%li.comment.show
|
||||||
= render 'comments/new_comment', :post_id => post_id
|
= render 'comments/new_comment', :post_id => post_id
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -3,5 +3,5 @@
|
||||||
-# the COPYRIGHT file.
|
-# the COPYRIGHT file.
|
||||||
|
|
||||||
%ul{:class => 'stream', :id => 'main_stream'}
|
%ul{:class => 'stream', :id => 'main_stream'}
|
||||||
- for post in @posts
|
- for post_hash in @post_hashes
|
||||||
= render 'shared/stream_element', :post => post, :aspects => @aspects
|
= render 'shared/stream_element', post_hash.merge(:aspects => @aspects)
|
||||||
|
|
|
||||||
|
|
@ -4,11 +4,11 @@
|
||||||
|
|
||||||
%li.message{:data=>{:guid=>post.id}}
|
%li.message{:data=>{:guid=>post.id}}
|
||||||
|
|
||||||
= person_image_link(post.person)
|
= person_image_link(person)
|
||||||
|
|
||||||
.content
|
.content
|
||||||
.from
|
.from
|
||||||
= link_to post.person.real_name, post.person
|
= link_to person.real_name, person
|
||||||
|
|
||||||
- if current_user.owns?(post)
|
- if current_user.owns?(post)
|
||||||
.aspect
|
.aspect
|
||||||
|
|
@ -29,7 +29,6 @@
|
||||||
.info
|
.info
|
||||||
%span.time= link_to(how_long_ago(post), object_path(post))
|
%span.time= link_to(how_long_ago(post), object_path(post))
|
||||||
|
|
||||||
= comment_toggle(post.comments.count)
|
= comment_toggle(comments.length)
|
||||||
|
= render "comments/comments", :post_id => post.id, :comment_hashes => comments, :hidden => (comments.length == 0)
|
||||||
= render "comments/comments", :post_id => post.id, :comments => post.comments, :hidden => (post.comments.count == 0)
|
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -22,4 +22,4 @@
|
||||||
%h4{:style=>"margin-bottom:5px;"}= t('_comments')
|
%h4{:style=>"margin-bottom:5px;"}= t('_comments')
|
||||||
%div{:class => 'stream show', :id => 'status_message_stream'}
|
%div{:class => 'stream show', :id => 'status_message_stream'}
|
||||||
%li.message{:data=>{:guid=>@status_message.id}}
|
%li.message{:data=>{:guid=>@status_message.id}}
|
||||||
= render "comments/comments", :post_id => @status_message.id, :comments => @status_message.comments
|
= render "comments/comments", :post_id => @status_message.id, :comment_hashes => @comment_hashes
|
||||||
|
|
|
||||||
|
|
@ -27,7 +27,11 @@ describe AspectsController do
|
||||||
describe "#index" do
|
describe "#index" do
|
||||||
it "assigns @contacts to all the user's contacts" do
|
it "assigns @contacts to all the user's contacts" do
|
||||||
Factory.create :person
|
Factory.create :person
|
||||||
|
begin
|
||||||
get :index
|
get :index
|
||||||
|
rescue Exception => e
|
||||||
|
raise e.original_exception
|
||||||
|
end
|
||||||
assigns[:contacts].should == @user.contacts
|
assigns[:contacts].should == @user.contacts
|
||||||
end
|
end
|
||||||
context 'performance' do
|
context 'performance' do
|
||||||
|
|
|
||||||
|
|
@ -214,9 +214,6 @@ describe Aspect do
|
||||||
before do
|
before do
|
||||||
@message = user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id)
|
@message = user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id)
|
||||||
aspect.reload
|
aspect.reload
|
||||||
@post_count = aspect.posts.count
|
|
||||||
@post_count1 = aspect1.posts.count
|
|
||||||
|
|
||||||
user.reload
|
user.reload
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -20,6 +20,40 @@ describe Comment do
|
||||||
comment.errors.full_messages.should include "Diaspora handle and person handle must match"
|
comment.errors.full_messages.should include "Diaspora handle and person handle must match"
|
||||||
end
|
end
|
||||||
|
|
||||||
|
describe '.hash_from_post_ids' do
|
||||||
|
before do
|
||||||
|
@hello = user.post(:status_message, :message => "Hello.", :to => aspect.id)
|
||||||
|
@hi = user.post(:status_message, :message => "hi", :to => aspect.id)
|
||||||
|
@lonely = user.post(:status_message, :message => "Hello?", :to => aspect.id)
|
||||||
|
|
||||||
|
@c11 = user2.comment "why so formal?", :on => @hello
|
||||||
|
@c21 = user2.comment "lol hihihi", :on => @hi
|
||||||
|
@c12 = user.comment "I simply felt like issuing a greeting. Do step off.", :on => @hello
|
||||||
|
@c22 = user.comment "stfu noob", :on => @hi
|
||||||
|
|
||||||
|
@c12.created_at = Time.now+10
|
||||||
|
@c12.save!
|
||||||
|
@c22.created_at = Time.now+10
|
||||||
|
@c22.save!
|
||||||
|
end
|
||||||
|
it 'returns an empty array for posts with no comments' do
|
||||||
|
Comment.hash_from_post_ids([@lonely.id]).should ==
|
||||||
|
{@lonely.id => []}
|
||||||
|
end
|
||||||
|
it 'returns a hash from posts to comments' do
|
||||||
|
Comment.hash_from_post_ids([@hello.id, @hi.id]).should ==
|
||||||
|
{@hello.id => [@c11, @c12],
|
||||||
|
@hi.id => [@c21, @c22]
|
||||||
|
}
|
||||||
|
end
|
||||||
|
it 'gets the people from the db' do
|
||||||
|
hash = Comment.hash_from_post_ids([@hello.id, @hi.id])
|
||||||
|
Person.from_post_comment_hash(hash).should == {
|
||||||
|
user.person.id => user.person,
|
||||||
|
user2.person.id => user2.person,
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
describe 'User#comment' do
|
describe 'User#comment' do
|
||||||
before do
|
before do
|
||||||
@status = user.post(:status_message, :message => "hello", :to => aspect.id)
|
@status = user.post(:status_message, :message => "hello", :to => aspect.id)
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue