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
|
||||
@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
|
||||
|
||||
if current_user.getting_started == true
|
||||
|
|
@ -60,7 +62,8 @@ class AspectsController < ApplicationController
|
|||
@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_count = @posts.count
|
||||
@post_hashes = hashes_for_posts @posts
|
||||
@post_count = @posts.count
|
||||
|
||||
respond_with @aspect
|
||||
end
|
||||
|
|
@ -130,4 +133,21 @@ class AspectsController < ApplicationController
|
|||
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
|
||||
|
|
|
|||
|
|
@ -86,6 +86,13 @@ class StatusMessagesController < ApplicationController
|
|||
|
||||
def show
|
||||
@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
|
||||
end
|
||||
end
|
||||
|
|
|
|||
|
|
@ -13,7 +13,17 @@ module SocketsHelper
|
|||
begin
|
||||
user = User.find_by_id uid
|
||||
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
|
||||
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
|
||||
|
|
|
|||
|
|
@ -71,6 +71,19 @@ class Comment
|
|||
def signature_valid?
|
||||
verify_signature(creator_signature, person)
|
||||
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
|
||||
|
|
|
|||
|
|
@ -150,6 +150,15 @@ class Person
|
|||
}
|
||||
}
|
||||
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
|
||||
|
||||
def clean_url
|
||||
|
|
|
|||
|
|
@ -13,9 +13,9 @@
|
|||
.span-15
|
||||
= render 'aspects/no_contacts_message', :aspect => @aspect, :contact_count => @contacts.count
|
||||
= 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
|
||||
= will_paginate @posts
|
||||
|
|
|
|||
|
|
@ -18,9 +18,9 @@
|
|||
.span-15.last
|
||||
= render 'shared/publisher', :aspect => @aspect
|
||||
= 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
|
||||
= will_paginate @posts
|
||||
|
|
|
|||
|
|
@ -3,8 +3,8 @@
|
|||
-# the COPYRIGHT file.
|
||||
|
||||
%ul.comments{:id => post_id, :class => ("hidden" if defined?(hidden) && hidden)}
|
||||
- for comment in comments
|
||||
= render 'comments/comment', :comment => comment, :person => comment.person
|
||||
- for comment_hash in comment_hashes
|
||||
= render 'comments/comment', comment_hash
|
||||
%li.comment.show
|
||||
= render 'comments/new_comment', :post_id => post_id
|
||||
|
||||
|
|
|
|||
|
|
@ -3,5 +3,5 @@
|
|||
-# the COPYRIGHT file.
|
||||
|
||||
%ul{:class => 'stream', :id => 'main_stream'}
|
||||
- for post in @posts
|
||||
= render 'shared/stream_element', :post => post, :aspects => @aspects
|
||||
- for post_hash in @post_hashes
|
||||
= render 'shared/stream_element', post_hash.merge(:aspects => @aspects)
|
||||
|
|
|
|||
|
|
@ -4,11 +4,11 @@
|
|||
|
||||
%li.message{:data=>{:guid=>post.id}}
|
||||
|
||||
= person_image_link(post.person)
|
||||
= person_image_link(person)
|
||||
|
||||
.content
|
||||
.from
|
||||
= link_to post.person.real_name, post.person
|
||||
= link_to person.real_name, person
|
||||
|
||||
- if current_user.owns?(post)
|
||||
.aspect
|
||||
|
|
@ -29,7 +29,6 @@
|
|||
.info
|
||||
%span.time= link_to(how_long_ago(post), object_path(post))
|
||||
|
||||
= comment_toggle(post.comments.count)
|
||||
|
||||
= render "comments/comments", :post_id => post.id, :comments => post.comments, :hidden => (post.comments.count == 0)
|
||||
= comment_toggle(comments.length)
|
||||
= render "comments/comments", :post_id => post.id, :comment_hashes => comments, :hidden => (comments.length == 0)
|
||||
|
||||
|
|
|
|||
|
|
@ -22,4 +22,4 @@
|
|||
%h4{:style=>"margin-bottom:5px;"}= t('_comments')
|
||||
%div{:class => 'stream show', :id => 'status_message_stream'}
|
||||
%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
|
||||
it "assigns @contacts to all the user's contacts" do
|
||||
Factory.create :person
|
||||
begin
|
||||
get :index
|
||||
rescue Exception => e
|
||||
raise e.original_exception
|
||||
end
|
||||
assigns[:contacts].should == @user.contacts
|
||||
end
|
||||
context 'performance' do
|
||||
|
|
|
|||
|
|
@ -214,9 +214,6 @@ describe Aspect do
|
|||
before do
|
||||
@message = user2.post(:status_message, :message => "Hey Dude", :to => aspect2.id)
|
||||
aspect.reload
|
||||
@post_count = aspect.posts.count
|
||||
@post_count1 = aspect1.posts.count
|
||||
|
||||
user.reload
|
||||
end
|
||||
|
||||
|
|
|
|||
|
|
@ -20,6 +20,40 @@ describe Comment do
|
|||
comment.errors.full_messages.should include "Diaspora handle and person handle must match"
|
||||
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
|
||||
before do
|
||||
@status = user.post(:status_message, :message => "hello", :to => aspect.id)
|
||||
|
|
|
|||
Loading…
Reference in a new issue