ported likes logic for the stream over from old branch

This commit is contained in:
danielgrippi 2011-12-12 13:45:54 -08:00 committed by Dennis Collinson
parent bc1ffd317e
commit cec2d27204
4 changed files with 61 additions and 4 deletions

View file

@ -9,6 +9,8 @@ class Post < ActiveRecord::Base
include Diaspora::Commentable include Diaspora::Commentable
include Diaspora::Shareable include Diaspora::Shareable
attr_accessor :user_like
# NOTE API V1 to be extracted # NOTE API V1 to be extracted
acts_as_api acts_as_api
api_accessible :backbone do |t| api_accessible :backbone do |t|
@ -27,6 +29,7 @@ class Post < ActiveRecord::Base
t.add :object_url t.add :object_url
t.add :root t.add :root
t.add :o_embed_cache t.add :o_embed_cache
t.add :user_like
end end
xml_attr :provider_display_name xml_attr :provider_display_name

View file

@ -49,6 +49,20 @@
<%= public === true ? "Public" : "Limited" %> <%= public === true ? "Public" : "Limited" %>
- -
</span> </span>
<span class="like_action">
<% if(user_like !== null) { %>
<a href="/posts/<%= id %>/likes/<%= user_like.id %>" class="unlike" data-method='delete' data-remote='true' rel='nofollow'>
Unlike
</a>
<% } else { %>
<a href="/posts/<%= id %>/likes?positive=true" class="like" data-method='post' data-remote='true' rel='nofollow'>
Like
</a>
<% } %>
</span>
·
<a href="#" class="focus_comment_textarea"> <a href="#" class="focus_comment_textarea">
Comment Comment
</a> </a>

View file

@ -46,9 +46,11 @@ class Stream::Base
Post.scoped Post.scoped
end end
# @return [ActiveRecord::Relation<Post>] # @return [Array<Post>]
def stream_posts def stream_posts
self.posts.for_a_stream(max_time, order, self.user) self.posts.for_a_stream(max_time, order, self.user).tap do |posts|
like_posts_for_stream!(posts) #some sql person could probably do this with joins.
end
end end
# @return [ActiveRecord::Association<Person>] AR association of people within stream's given aspects # @return [ActiveRecord::Association<Person>] AR association of people within stream's given aspects
@ -63,7 +65,7 @@ class Stream::Base
I18n.translate('aspects.selected_contacts.view_all_contacts') I18n.translate('aspects.selected_contacts.view_all_contacts')
end end
# @return [String] # @return [String] def contacts_title 'change me in lib/base_stream.rb!'
def contacts_title def contacts_title
'change me in lib/base_stream.rb!' 'change me in lib/base_stream.rb!'
end end
@ -109,7 +111,21 @@ class Stream::Base
@order ||= 'created_at' @order ||= 'created_at'
end end
private protected
# @return [void]
def like_posts_for_stream!(posts)
likes = Like.where(:target_id => posts.map(&:id), :target_type => "Post")
like_hash = likes.inject({}) do |hash, like|
hash[like.target_id] = like
hash
end
posts.each do |post|
post.user_like = like_hash[post.id]
end
end
# @return [Hash] # @return [Hash]
def publisher_opts def publisher_opts
{} {}

View file

@ -4,12 +4,36 @@ describe Stream::Base do
before do before do
@stream = Stream::Base.new(alice) @stream = Stream::Base.new(alice)
end end
describe '#contacts_link' do describe '#contacts_link' do
it 'should default to your contacts page' do it 'should default to your contacts page' do
@stream.contacts_link.should =~ /contacts/ @stream.contacts_link.should =~ /contacts/
end end
end end
describe '#stream_posts' do
it "should returns the posts.for_a_stream" do
posts = mock
@stream.stub(:posts).and_return(posts)
@stream.stub(:like_posts_for_stream!)
posts.should_receive(:for_a_stream).with(anything, anything, alice).and_return(posts)
@stream.stream_posts
end
context "when alice has liked some posts" do
before do
bob.post(:status_message, :text => "sup", :to => bob.aspects.first.id)
@liked_status = bob.posts.last
@like = Factory(:like, :target => @liked_status, :author => alice.person)
end
it "marks the posts as liked" do
@stream.stream_posts.first.user_like.id.should == @like.id
end
end
end
describe '.can_comment?' do describe '.can_comment?' do
before do before do
@person = Factory(:person) @person = Factory(:person)