From 6ae36ea49271801a96401c8a2da986e87c9206f3 Mon Sep 17 00:00:00 2001 From: joshua stein Date: Mon, 3 Sep 2012 17:21:41 -0500 Subject: [PATCH] route /newest/:user to show all stories by that user --- app/controllers/home_controller.rb | 45 +++++++++++++++++++++++------- app/views/home/index.html.erb | 11 ++++---- app/views/users/show.html.erb | 3 +- config/routes.rb | 2 ++ 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/app/controllers/home_controller.rb b/app/controllers/home_controller.rb index 50ff482..e997a5f 100644 --- a/app/controllers/home_controller.rb +++ b/app/controllers/home_controller.rb @@ -2,7 +2,8 @@ class HomeController < ApplicationController STORIES_PER_PAGE = 25 def index - @stories = find_stories_for_user_and_tag_and_newest(@user, nil, false) + @stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user, + nil, false, nil) @rss_link ||= "" @@ -17,7 +18,8 @@ class HomeController < ApplicationController end def newest - @stories = find_stories_for_user_and_tag_and_newest(@user, nil, true) + @stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user, + nil, true, nil) @title = "Newest Stories" @cur_url = "/newest" @@ -33,9 +35,25 @@ class HomeController < ApplicationController end end + def newest_by_user + for_user = User.find_by_username!(params[:user]) + + @stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user, + nil, false, for_user.id) + + @title = "Newest Stories by #{for_user.username}" + @cur_url = "/newest/#{for_user.username}" + + @newest = true + @for_user = for_user.username + + render :action => "index" + end + def tagged @tag = Tag.find_by_tag!(params[:tag]) - @stories = find_stories_for_user_and_tag_and_newest(@user, @tag, false) + @stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user, + @tag, false, nil) @title = @tag.description.blank?? @tag.tag : @tag.description @cur_url = tag_url(@tag.tag) @@ -52,7 +70,8 @@ class HomeController < ApplicationController end private - def find_stories_for_user_and_tag_and_newest(user, tag = nil, newest = false) + def find_stories_for_user_and_tag_and_newest_and_by_user(user, tag = nil, + newest = false, by_user = nil) @page = 1 if params[:page].to_i > 0 @page = params[:page].to_i @@ -60,23 +79,26 @@ private # guest views have caching, but don't bother for logged-in users if user - stories, @show_more = _find_stories_for_user_and_tag_and_newest(user, - tag, newest) + stories, @show_more = + _find_stories_for_user_and_tag_and_newest_and_by_user(user, tag, + newest, by_user) else stories, @show_more = Rails.cache.fetch("stories tag:" << - "#{tag ? tag.tag : ""} new:#{newest} page:#{@page.to_i}", + "#{tag ? tag.tag : ""} new:#{newest} page:#{@page.to_i} by:#{by_user}", :expires_in => 45) do - _find_stories_for_user_and_tag_and_newest(user, tag, newest) + _find_stories_for_user_and_tag_and_newest_and_by_user(user, tag, + newest, by_user) end end stories end - def _find_stories_for_user_and_tag_and_newest(user, tag = nil, newest = false) + def _find_stories_for_user_and_tag_and_newest_and_by_user(user, tag = nil, + newest = false, by_user = nil) conds = [ "is_expired = 0 " ] - if user && !newest + if user && !(newest || by_user) # exclude downvoted items conds[0] << "AND stories.id NOT IN (SELECT story_id FROM votes " << "WHERE user_id = ? AND vote < 0 AND comment_id IS NULL) " @@ -87,6 +109,9 @@ private conds[0] << "AND stories.id IN (SELECT taggings.story_id FROM " << "taggings WHERE taggings.tag_id = ?)" conds.push tag.id + elsif by_user + conds[0] << "AND stories.user_id = ?" + conds.push by_user elsif user conds[0] += " AND taggings.tag_id NOT IN (SELECT tag_id FROM " << "tag_filters WHERE user_id = ?)" diff --git a/app/views/home/index.html.erb b/app/views/home/index.html.erb index f18e484..8f31095 100644 --- a/app/views/home/index.html.erb +++ b/app/views/home/index.html.erb @@ -5,16 +5,17 @@ diff --git a/app/views/users/show.html.erb b/app/views/users/show.html.erb index 0352e54..8d98567 100644 --- a/app/views/users/show.html.erb +++ b/app/views/users/show.html.erb @@ -27,7 +27,8 @@ - <%= @showing_user.stories_submitted_count %> + <%= + @showing_user.stories_submitted_count %>
diff --git a/config/routes.rb b/config/routes.rb index 6ebc1c6..944c975 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -6,6 +6,8 @@ Lobsters::Application.routes.draw do get "/newest(.format)" => "home#newest" get "/newest/page/:page" => "home#newest" + get "/newest/:user" => "home#newest_by_user" + get "/newest/:user/page/:page" => "home#newest_by_user" get "/threads" => "comments#threads" get "/threads/:user" => "comments#threads"