simplify story finding
This commit is contained in:
parent
35f43ea10c
commit
8dbc0e4724
|
@ -11,8 +11,7 @@ class HomeController < ApplicationController
|
||||||
before_filter :find_user_from_rss_token, :only => [ :index, :newest ]
|
before_filter :find_user_from_rss_token, :only => [ :index, :newest ]
|
||||||
|
|
||||||
def index
|
def index
|
||||||
@stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user,
|
@stories = find_stories
|
||||||
nil, false, nil)
|
|
||||||
|
|
||||||
@rss_link ||= "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
|
@rss_link ||= "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
|
||||||
"title=\"RSS 2.0\" href=\"/rss" <<
|
"title=\"RSS 2.0\" href=\"/rss" <<
|
||||||
|
@ -35,18 +34,16 @@ class HomeController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def newest
|
def newest
|
||||||
@stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user,
|
@stories = find_stories({ :newest => true })
|
||||||
nil, true, nil)
|
|
||||||
|
|
||||||
@heading = @title = "Newest Stories"
|
@heading = @title = "Newest Stories"
|
||||||
@cur_url = "/newest"
|
@cur_url = "/newest"
|
||||||
|
@newest = true
|
||||||
|
|
||||||
@rss_link = "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
|
@rss_link = "<link rel=\"alternate\" type=\"application/rss+xml\" " <<
|
||||||
"title=\"RSS 2.0 - Newest Items\" href=\"/newest.rss" <<
|
"title=\"RSS 2.0 - Newest Items\" href=\"/newest.rss" <<
|
||||||
(@user ? "?token=#{@user.rss_token}" : "") << "\" />"
|
(@user ? "?token=#{@user.rss_token}" : "") << "\" />"
|
||||||
|
|
||||||
@newest = true
|
|
||||||
|
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { render :action => "index" }
|
format.html { render :action => "index" }
|
||||||
format.rss {
|
format.rss {
|
||||||
|
@ -61,24 +58,23 @@ class HomeController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
def newest_by_user
|
def newest_by_user
|
||||||
for_user = User.where(:username => params[:user]).first!
|
by_user = User.where(:username => params[:user]).first!
|
||||||
|
|
||||||
@stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user,
|
@stories = find_stories({ :by_user => by_user })
|
||||||
nil, false, for_user.id)
|
|
||||||
|
|
||||||
@heading = @title = "Newest Stories by #{for_user.username}"
|
@heading = @title = "Newest Stories by #{by_user.username}"
|
||||||
@cur_url = "/newest/#{for_user.username}"
|
@cur_url = "/newest/#{by_user.username}"
|
||||||
|
|
||||||
@newest = true
|
@newest = true
|
||||||
@for_user = for_user.username
|
@for_user = by_user.username
|
||||||
|
|
||||||
render :action => "index"
|
render :action => "index"
|
||||||
end
|
end
|
||||||
|
|
||||||
def tagged
|
def tagged
|
||||||
@tag = Tag.where(:tag => params[:tag]).first!
|
@tag = Tag.where(:tag => params[:tag]).first!
|
||||||
@stories = find_stories_for_user_and_tag_and_newest_and_by_user(@user,
|
|
||||||
@tag, false, nil)
|
@stories = find_stories({ :tag => @tag })
|
||||||
|
|
||||||
@heading = @title = @tag.description.blank?? @tag.tag : @tag.description
|
@heading = @title = @tag.description.blank?? @tag.tag : @tag.description
|
||||||
@cur_url = tag_url(@tag.tag)
|
@cur_url = tag_url(@tag.tag)
|
||||||
|
@ -115,8 +111,7 @@ class HomeController < ApplicationController
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
def find_stories_for_user_and_tag_and_newest_and_by_user(user, tag = nil,
|
def find_stories(how = {})
|
||||||
newest = false, by_user = nil)
|
|
||||||
@page = 1
|
@page = 1
|
||||||
if params[:page].to_i > 0
|
if params[:page].to_i > 0
|
||||||
@page = params[:page].to_i
|
@page = params[:page].to_i
|
||||||
|
@ -124,32 +119,28 @@ private
|
||||||
|
|
||||||
# guest views have caching, but don't bother for logged-in users or dev or
|
# guest views have caching, but don't bother for logged-in users or dev or
|
||||||
# when the user has tag filters
|
# when the user has tag filters
|
||||||
if Rails.env == "development" || user || tags_filtered_by_cookie.any?
|
if Rails.env == "development" || @user || tags_filtered_by_cookie.any?
|
||||||
stories, @show_more =
|
stories, @show_more = _find_stories(how)
|
||||||
_find_stories_for_user_and_tag_and_newest_and_by_user(user, tag,
|
|
||||||
newest, by_user)
|
|
||||||
else
|
else
|
||||||
stories, @show_more = Rails.cache.fetch("stories tag:" <<
|
stories, @show_more = Rails.cache.fetch("stories " <<
|
||||||
"#{tag ? tag.tag : ""} new:#{newest} page:#{@page.to_i} by:#{by_user}",
|
"tag:#{how[:tag].try(:tag)} new:#{how[:newest]} page:#{@page.to_i} " <<
|
||||||
:expires_in => 45) do
|
"by:#{how[:by_user].try(:id)}", :expires_in => 45) do
|
||||||
_find_stories_for_user_and_tag_and_newest_and_by_user(user, tag,
|
_find_stories(how)
|
||||||
newest, by_user)
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
stories
|
stories
|
||||||
end
|
end
|
||||||
|
|
||||||
def _find_stories_for_user_and_tag_and_newest_and_by_user(user, tag = nil,
|
def _find_stories(how)
|
||||||
newest = false, by_user = nil)
|
|
||||||
stories = Story.where(:is_expired => false)
|
stories = Story.where(:is_expired => false)
|
||||||
|
|
||||||
if user && !(newest || by_user)
|
if @user && !(how[:newest] || how[:by_user])
|
||||||
# exclude downvoted items
|
# exclude downvoted items
|
||||||
stories = stories.where(
|
stories = stories.where(
|
||||||
Story.arel_table[:id].not_in(
|
Story.arel_table[:id].not_in(
|
||||||
Vote.arel_table.where(
|
Vote.arel_table.where(
|
||||||
Vote.arel_table[:user_id].eq(user.id)
|
Vote.arel_table[:user_id].eq(@user.id)
|
||||||
).where(
|
).where(
|
||||||
Vote.arel_table[:vote].lt(0)
|
Vote.arel_table[:vote].lt(0)
|
||||||
).where(
|
).where(
|
||||||
|
@ -162,13 +153,13 @@ private
|
||||||
end
|
end
|
||||||
|
|
||||||
filtered_tag_ids = []
|
filtered_tag_ids = []
|
||||||
if user
|
if @user
|
||||||
filtered_tag_ids = @user.tag_filters.map{|tf| tf.tag_id }
|
filtered_tag_ids = @user.tag_filters.map{|tf| tf.tag_id }
|
||||||
else
|
else
|
||||||
filtered_tag_ids = tags_filtered_by_cookie.map{|t| t.id }
|
filtered_tag_ids = tags_filtered_by_cookie.map{|t| t.id }
|
||||||
end
|
end
|
||||||
|
|
||||||
if tag
|
if how[:tag]
|
||||||
stories = stories.where(
|
stories = stories.where(
|
||||||
Story.arel_table[:id].in(
|
Story.arel_table[:id].in(
|
||||||
Tagging.arel_table.where(
|
Tagging.arel_table.where(
|
||||||
|
@ -178,8 +169,8 @@ private
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
elsif by_user
|
elsif how[:by_user]
|
||||||
stories = stories.where(:user_id => by_user)
|
stories = stories.where(:user_id => how[:by_user].id)
|
||||||
elsif filtered_tag_ids.any?
|
elsif filtered_tag_ids.any?
|
||||||
stories = stories.where(
|
stories = stories.where(
|
||||||
Story.arel_table[:id].not_in(
|
Story.arel_table[:id].not_in(
|
||||||
|
@ -192,7 +183,7 @@ private
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
if newest && @page == 1
|
if how[:newest] && @page == 1
|
||||||
# try to help recently-submitted stories that didn't gain traction
|
# try to help recently-submitted stories that didn't gain traction
|
||||||
|
|
||||||
# grab the list of stories from the past n days, shifting out popular
|
# grab the list of stories from the past n days, shifting out popular
|
||||||
|
@ -224,7 +215,7 @@ private
|
||||||
).offset(
|
).offset(
|
||||||
(@page - 1) * STORIES_PER_PAGE
|
(@page - 1) * STORIES_PER_PAGE
|
||||||
).order(
|
).order(
|
||||||
newest ? "stories.created_at DESC" : "hotness"
|
how[:newest] ? "stories.created_at DESC" : "hotness"
|
||||||
).to_a
|
).to_a
|
||||||
|
|
||||||
show_more = false
|
show_more = false
|
||||||
|
@ -233,9 +224,8 @@ private
|
||||||
stories.pop
|
stories.pop
|
||||||
end
|
end
|
||||||
|
|
||||||
if user
|
if @user
|
||||||
votes = Vote.votes_by_user_for_stories_hash(user.id,
|
votes = Vote.votes_by_user_for_stories_hash(@user.id, stories.map(&:id))
|
||||||
stories.map{|s| s.id })
|
|
||||||
|
|
||||||
stories.each do |s|
|
stories.each do |s|
|
||||||
if votes[s.id]
|
if votes[s.id]
|
||||||
|
|
Loading…
Reference in a new issue