story pagination
This commit is contained in:
parent
b128c818c9
commit
6e17272e9d
|
@ -442,6 +442,17 @@ div.story_content {
|
|||
margin-right: 40px;
|
||||
}
|
||||
|
||||
div.morelink {
|
||||
margin-top: 1.5em;
|
||||
margin-left: 40px;
|
||||
font-size: 10pt;
|
||||
}
|
||||
div.morelink a {
|
||||
color: #666;
|
||||
font-weight: bold;
|
||||
text-decoration: none;
|
||||
}
|
||||
|
||||
div.story_text {
|
||||
font-size: 10pt;
|
||||
margin-bottom: 1.5em;
|
||||
|
|
|
@ -63,6 +63,9 @@
|
|||
margin-left: 36px;
|
||||
margin-right: 20px;
|
||||
}
|
||||
div.morelink {
|
||||
margin-left: 36px;
|
||||
}
|
||||
li.story div.byline {
|
||||
font-size: 10pt;
|
||||
}
|
||||
|
|
|
@ -26,51 +26,4 @@ class ApplicationController < ActionController::Base
|
|||
return false
|
||||
end
|
||||
end
|
||||
|
||||
def find_stories_for_user_and_tag_and_newest(user, tag = nil, newest = false)
|
||||
stories = []
|
||||
|
||||
conds = [ "is_expired = 0 AND is_moderated = 0 " ]
|
||||
|
||||
if user && !newest
|
||||
# exclude downvoted items
|
||||
conds[0] << "AND stories.id NOT IN (SELECT story_id FROM votes " <<
|
||||
"WHERE user_id = ? AND vote < 0) "
|
||||
conds.push user.id
|
||||
end
|
||||
|
||||
if tag
|
||||
conds[0] << "AND taggings.tag_id = ?"
|
||||
conds.push tag.id
|
||||
stories = Story.find(:all, :conditions => conds,
|
||||
:include => [ :user, { :taggings => :tag } ], :limit => 30,
|
||||
:order => (newest ? "stories.created_at DESC" : "hotness"))
|
||||
else
|
||||
if user
|
||||
conds[0] += " AND taggings.tag_id NOT IN (SELECT tag_id FROM " <<
|
||||
"tag_filters WHERE user_id = ?)"
|
||||
conds.push @user.id
|
||||
end
|
||||
|
||||
stories = Story.find(:all, :conditions => conds,
|
||||
:include => [ :user, { :taggings => :tag } ], :limit => 30,
|
||||
:order => (newest ? "stories.created_at DESC" : "hotness"))
|
||||
end
|
||||
|
||||
# TODO: figure out a better sorting algorithm for newest, including some
|
||||
# older stories that got one or two votes
|
||||
|
||||
if user
|
||||
votes = Vote.votes_by_user_for_stories_hash(user.id,
|
||||
stories.map{|s| s.id })
|
||||
|
||||
stories.each do |s|
|
||||
if votes[s.id]
|
||||
s.vote = votes[s.id]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
stories
|
||||
end
|
||||
end
|
||||
|
|
|
@ -1,4 +1,6 @@
|
|||
class HomeController < ApplicationController
|
||||
STORIES_PER_PAGE = 20
|
||||
|
||||
def index
|
||||
@stories = find_stories_for_user_and_tag_and_newest(@user, nil, false)
|
||||
|
||||
|
@ -48,4 +50,62 @@ class HomeController < ApplicationController
|
|||
format.rss { render :action => "rss", :layout => false }
|
||||
end
|
||||
end
|
||||
|
||||
private
|
||||
def find_stories_for_user_and_tag_and_newest(user, tag = nil, newest = false)
|
||||
conds = [ "is_expired = 0 AND is_moderated = 0 " ]
|
||||
|
||||
if user && !newest
|
||||
# exclude downvoted items
|
||||
conds[0] << "AND stories.id NOT IN (SELECT story_id FROM votes " <<
|
||||
"WHERE user_id = ? AND vote < 0) "
|
||||
conds.push user.id
|
||||
end
|
||||
|
||||
if tag
|
||||
conds[0] << "AND taggings.tag_id = ?"
|
||||
conds.push tag.id
|
||||
elsif user
|
||||
conds[0] += " AND taggings.tag_id NOT IN (SELECT tag_id FROM " <<
|
||||
"tag_filters WHERE user_id = ?)"
|
||||
conds.push @user.id
|
||||
end
|
||||
|
||||
@page = 1
|
||||
if params[:page].to_i > 0
|
||||
@page = params[:page].to_i
|
||||
end
|
||||
|
||||
stories = Story.find(
|
||||
:all,
|
||||
:conditions => conds,
|
||||
:include => [ :user, { :taggings => :tag } ],
|
||||
:limit => STORIES_PER_PAGE + 1,
|
||||
:offset => ((@page - 1) * STORIES_PER_PAGE),
|
||||
:order => (newest ? "stories.created_at DESC" : "hotness")
|
||||
)
|
||||
|
||||
@show_more = false
|
||||
|
||||
if stories.count > STORIES_PER_PAGE
|
||||
@show_more = true
|
||||
stories.pop
|
||||
end
|
||||
|
||||
# TODO: figure out a better sorting algorithm for newest, including some
|
||||
# older stories that got one or two votes
|
||||
|
||||
if user
|
||||
votes = Vote.votes_by_user_for_stories_hash(user.id,
|
||||
stories.map{|s| s.id })
|
||||
|
||||
stories.each do |s|
|
||||
if votes[s.id]
|
||||
s.vote = votes[s.id]
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
stories
|
||||
end
|
||||
end
|
||||
|
|
|
@ -2,3 +2,19 @@
|
|||
<%= render :partial => "stories/listdetail", :collection => @stories,
|
||||
:as => :story %>
|
||||
</ol>
|
||||
|
||||
<div class="morelink">
|
||||
<% if @page && @page > 1 %>
|
||||
<a href="<%= @tag ? "/t/#{@tag.tag}" : (@newest ? "/newest" : "")
|
||||
%><%= @page == 2 ? "" : "/page/#{@page - 1}" %>"><< Page
|
||||
<%= @page - 1 %></a>
|
||||
<% end %>
|
||||
|
||||
<% if @show_more %>
|
||||
<% if @page && @page > 1 %>
|
||||
|
|
||||
<% end %>
|
||||
<a href="<%= @tag ? "/t/#{@tag.tag}" : (@newest ? "/newest" : "")
|
||||
%>/page/<%= @page + 1 %>">Page <%= @page + 1 %> >></a>
|
||||
</div>
|
||||
<% end %>
|
||||
|
|
|
@ -2,7 +2,10 @@ Lobsters::Application.routes.draw do
|
|||
root :to => "home#index",
|
||||
:protocol => (Rails.env == "production" ? "https://" : "http://")
|
||||
|
||||
get "/page/:page" => "home#index"
|
||||
|
||||
get "/newest(.format)" => "home#newest"
|
||||
get "/newest/page/:page" => "home#newest"
|
||||
|
||||
get "/threads" => "comments#threads"
|
||||
|
||||
|
@ -20,7 +23,8 @@ Lobsters::Application.routes.draw do
|
|||
match "/login/set_new_password" => "login#set_new_password",
|
||||
:as => "set_new_password"
|
||||
|
||||
match "/t/:tag" => "home#tagged", :as => "tag"
|
||||
match "/t/:tag" => "home#tagged"
|
||||
match "/t/:tag/page/:page" => "home#tagged"
|
||||
|
||||
resources :stories do
|
||||
post "upvote"
|
||||
|
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended to check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(:version => 20120705145520) do
|
||||
ActiveRecord::Schema.define(:version => 20120706221602) do
|
||||
|
||||
create_table "comments", :force => true do |t|
|
||||
t.datetime "created_at", :null => false
|
||||
|
@ -79,6 +79,7 @@ ActiveRecord::Schema.define(:version => 20120705145520) do
|
|||
end
|
||||
|
||||
add_index "stories", ["hotness"], :name => "hotness_idx"
|
||||
add_index "stories", ["is_expired", "is_moderated"], :name => "is_idxes"
|
||||
add_index "stories", ["url"], :name => "url"
|
||||
|
||||
create_table "tag_filters", :force => true do |t|
|
||||
|
@ -88,6 +89,8 @@ ActiveRecord::Schema.define(:version => 20120705145520) do
|
|||
t.integer "tag_id"
|
||||
end
|
||||
|
||||
add_index "tag_filters", ["user_id", "tag_id"], :name => "user_tag_idx"
|
||||
|
||||
create_table "taggings", :force => true do |t|
|
||||
t.integer "story_id", :null => false
|
||||
t.integer "tag_id", :null => false
|
||||
|
|
Loading…
Reference in a new issue