From bc1aef49990daf501c9b00dcf5e0aff65b1c4937 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Fri, 16 Sep 2011 18:19:34 -0700 Subject: [PATCH] MS IZ start to implement followed tags in the stream --- app/controllers/tag_followings_controller.rb | 6 ++ app/helpers/stream_helper.rb | 2 + app/views/aspects/index.html.haml | 4 +- config/routes.rb | 3 + lib/tag_stream.rb | 66 +++++++++++++++++++ .../tag_followings_controller_spec.rb | 15 ++++- 6 files changed, 93 insertions(+), 3 deletions(-) create mode 100644 lib/tag_stream.rb diff --git a/app/controllers/tag_followings_controller.rb b/app/controllers/tag_followings_controller.rb index fab373942..f28f02486 100644 --- a/app/controllers/tag_followings_controller.rb +++ b/app/controllers/tag_followings_controller.rb @@ -1,6 +1,12 @@ +require File.join(Rails.root, '/lib/tag_stream') class TagFollowingsController < ApplicationController before_filter :authenticate_user! + def index + @stream = TagStream.new(current_user) + + render 'aspects/index', :locals => {:posts => @stream.posts} + end # POST /tag_followings # POST /tag_followings.xml def create diff --git a/app/helpers/stream_helper.rb b/app/helpers/stream_helper.rb index 56133a90e..bcb16a112 100644 --- a/app/helpers/stream_helper.rb +++ b/app/helpers/stream_helper.rb @@ -10,6 +10,8 @@ module StreamHelper "/apps/1?#{{:max_time => @posts.last.created_at.to_i}.to_param}" elsif controller.instance_of?(PeopleController) person_path(@person, :max_time => @posts.last.created_at.to_i) + elsif controller.instance_of?(TagFollowingsController) + tag_followings_path(:max_time => @stream.posts.last.created_at.to_i) elsif controller.instance_of?(AspectsController) aspects_path(:max_time => @stream.posts.last.send(@stream.order.to_sym).to_i, :a_ids => @stream.aspect_ids) else diff --git a/app/views/aspects/index.html.haml b/app/views/aspects/index.html.haml index e3b7ca651..d2b59a43f 100644 --- a/app/views/aspects/index.html.haml +++ b/app/views/aspects/index.html.haml @@ -26,9 +26,9 @@ .span-13.append-1 #aspect_stream_container.stream_container - = render 'aspect_stream', :stream => @stream + = render 'aspects/aspect_stream', :stream => @stream .span-5.rightBar.last - = render 'selected_contacts', :stream => @stream + = render 'aspects/selected_contacts', :stream => @stream = render 'shared/right_sections' diff --git a/config/routes.rb b/config/routes.rb index e0381e15a..7fa599576 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -51,6 +51,9 @@ Diaspora::Application.routes.draw do post "tag_followings" => "tag_followings#create", :as => 'tag_tag_followings' delete "tag_followings" => "tag_followings#destroy" end + + # get "tag_followings" => "tag_followings#index", :as => 'tag_followings' + get 'tags/:name' => 'tags#show', :as => 'tag' resources :apps, :only => [:show] diff --git a/lib/tag_stream.rb b/lib/tag_stream.rb new file mode 100644 index 000000000..3ec31f303 --- /dev/null +++ b/lib/tag_stream.rb @@ -0,0 +1,66 @@ +# Copyright (c) 2010-2011, Diaspora Inc. This file is +# licensed under the Affero General Public License version 3 or later. See +# the COPYRIGHT file. + +class TagStream + + attr_reader :max_time, :order + + # @param user [User] + # @param inputted_aspect_ids [Array] Ids of aspects for given stream + # @param aspect_ids [Array] Aspects this stream is responsible for + # @opt max_time [Integer] Unix timestamp of stream's post ceiling + # @opt order [String] Order of posts (i.e. 'created_at', 'updated_at') + # @return [void] + def initialize(user, opts={}) + @tags = user.followed_tags + @tag_string = @tags.join(', '){|tag| tag.name}.to_sym + @user = user + @max_time = opts[:max_time] + @order = opts[:order] + end + + # Filters aspects given the stream's aspect ids on initialization and the user. + # Will disclude aspects from inputted aspect ids if user is not associated with their + # target aspects. + # + # @return [ActiveRecord::Association] Filtered aspects given the stream's user + def aspects + [@tag_string] + end + + # Maps ids into an array from #aspects + # + # @return [Array] Aspect ids + def aspect_ids + [] + end + + # @return [ActiveRecord::Association] AR association of posts + def posts + # NOTE(this should be something like Post.all_for_stream(@user, aspect_ids, {}) that calls visible_posts + @posts ||= StatusMessage.tagged_with([@tag_string], :any => true) + + + end + + # @return [ActiveRecord::Association] AR association of people within stream's given aspects + def people + @people ||= posts.map{|p| p.author}.uniq + end + + # The first aspect in #aspects, given the stream is not for all aspects, or #aspects size is 1 + # @note aspects.first is used for mobile. NOTE(this is a hack and should be fixed) + # @return [Aspect,Symbol] + def aspect + @tags_string + end + + # Determine whether or not the stream is displaying across + # all of the user's aspects. + # + # @return [Boolean] + def for_all_aspects? + true + end +end diff --git a/spec/controllers/tag_followings_controller_spec.rb b/spec/controllers/tag_followings_controller_spec.rb index d1619564b..374abe0b3 100644 --- a/spec/controllers/tag_followings_controller_spec.rb +++ b/spec/controllers/tag_followings_controller_spec.rb @@ -15,7 +15,20 @@ describe TagFollowingsController do sign_in :user, bob end - describe "POST create" do + describe 'index' do + + it 'assings new TagStream' do + get :index + assigns[:stream].should be_a TagStream + end + + it 'renders a view' do + get :index + response.body.should_not be_blank + end + end + + describe "create" do describe "with valid params" do it "creates a new TagFollowing" do expect {