From cec2d27204f13528dd917aa5fa1b62c36ccacc08 Mon Sep 17 00:00:00 2001 From: danielgrippi Date: Mon, 12 Dec 2011 13:45:54 -0800 Subject: [PATCH] ported likes logic for the stream over from old branch --- app/models/post.rb | 3 +++ app/views/templates/stream_element.ujs | 14 ++++++++++++++ lib/stream/base.rb | 24 ++++++++++++++++++++---- spec/lib/stream/base_spec.rb | 24 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 4 deletions(-) diff --git a/app/models/post.rb b/app/models/post.rb index 205170f13..c8e86cc6a 100644 --- a/app/models/post.rb +++ b/app/models/post.rb @@ -9,6 +9,8 @@ class Post < ActiveRecord::Base include Diaspora::Commentable include Diaspora::Shareable + attr_accessor :user_like + # NOTE API V1 to be extracted acts_as_api api_accessible :backbone do |t| @@ -27,6 +29,7 @@ class Post < ActiveRecord::Base t.add :object_url t.add :root t.add :o_embed_cache + t.add :user_like end xml_attr :provider_display_name diff --git a/app/views/templates/stream_element.ujs b/app/views/templates/stream_element.ujs index 3f2bbc565..208431f3b 100644 --- a/app/views/templates/stream_element.ujs +++ b/app/views/templates/stream_element.ujs @@ -49,6 +49,20 @@ <%= public === true ? "Public" : "Limited" %> - + + + <% if(user_like !== null) { %> + + Unlike + + <% } else { %> + + <% } %> + + ยท + Comment diff --git a/lib/stream/base.rb b/lib/stream/base.rb index 89b24eaba..9631521b8 100644 --- a/lib/stream/base.rb +++ b/lib/stream/base.rb @@ -46,9 +46,11 @@ class Stream::Base Post.scoped end - # @return [ActiveRecord::Relation] + # @return [Array] 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 # @return [ActiveRecord::Association] AR association of people within stream's given aspects @@ -63,7 +65,7 @@ class Stream::Base I18n.translate('aspects.selected_contacts.view_all_contacts') end - # @return [String] + # @return [String] def contacts_title 'change me in lib/base_stream.rb!' def contacts_title 'change me in lib/base_stream.rb!' end @@ -109,7 +111,21 @@ class Stream::Base @order ||= 'created_at' 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] def publisher_opts {} diff --git a/spec/lib/stream/base_spec.rb b/spec/lib/stream/base_spec.rb index 6fcd5004b..29c8e7b12 100644 --- a/spec/lib/stream/base_spec.rb +++ b/spec/lib/stream/base_spec.rb @@ -4,12 +4,36 @@ describe Stream::Base do before do @stream = Stream::Base.new(alice) end + describe '#contacts_link' do it 'should default to your contacts page' do @stream.contacts_link.should =~ /contacts/ 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 before do @person = Factory(:person)