Skip to content

Commit

Permalink
fix for pagination buttons
Browse files Browse the repository at this point in the history
  • Loading branch information
burningTyger committed Apr 22, 2011
1 parent 0e817c9 commit 762b8a1
Show file tree
Hide file tree
Showing 9 changed files with 170 additions and 100 deletions.
12 changes: 2 additions & 10 deletions controllers/updates_controller.rb
Expand Up @@ -3,22 +3,14 @@ class Rstatus
# Ahh, the classic 'world' view.
get '/updates' do
@updates = Update.paginate( :page => params[:page], :per_page => params[:per_page] || 20, :order => :created_at.desc)

if @updates.next_page
@next_page = "?#{Rack::Utils.build_query :page => @updates.next_page}"
end

if @updates.previous_page
@prev_page = "?#{Rack::Utils.build_query :page => @updates.previous_page}"
end
set_pagination_buttons(@updates)

haml :world
end

# If you're POST-ing to /updates, it means you're making a new one. Woo-hoo!
# This is what it's all built for.
post '/updates' do

# XXX: This should really be put into a model. Fat controller here!
do_tweet = params[:tweet] == "1"
do_facebook = params[:facebook] == "1"
Expand All @@ -31,7 +23,7 @@ class Rstatus
# add entry to user's feed
current_user.feed.updates << u
unless u.valid?
flash[:notice] = u.errors.errors.values.join("\n")
flash[:notice] = u.errors.errors.values.join("\n")
else
current_user.feed.save
current_user.save
Expand Down
55 changes: 20 additions & 35 deletions controllers/users_controller.rb
Expand Up @@ -58,46 +58,34 @@ class Rstatus

get '/users' do
set_params_page

# Filter users by search params
if params[:search] && !params[:search].empty?
@users = User.where(:username => /#{params[:search]}/i)

# Filter users by letter
# Filter and sort users by letter
elsif params[:letter]
if params[:letter] == "other"
@users = User.where(:username => /^[^a-z0-9]/i)
elsif
elsif params[:letter].empty?
break
else
@users = User.where(:username => /^#{params[:letter][0].chr}/i)
end
else
@users = User
end

# Sort users alphabetically when filtering by letter
if params[:letter]
@users = @users.sort(:username)

#otherwise get all users and sort by creation date
else
@users = User
@users = @users.sort(:created_at.desc)
end

@users = @users.paginate(:page => params[:page], :per_page => params[:per_page])

@next_page = nil

# If this would just accept params as is I wouldn't have to do this
if params[:letter]
@next_page = "?#{Rack::Utils.build_query :page => params[:page] + 1, :letter => params[:letter]}"

if params[:page] > 1
@prev_page = "?#{Rack::Utils.build_query :page => params[:page] + 1, :letter => params[:letter]}"
else
@prev_page = nil
end
if params[:letter] && !params[:letter].empty?
set_pagination_buttons(@users, :letter => params[:letter])
else
set_next_prev_page
set_pagination_buttons(@users)
end

haml :"users/index"
end

Expand Down Expand Up @@ -153,10 +141,9 @@ class Rstatus
end
end
@author = user.author
@updates = Update.where(:feed_id => user.feed.id).order(['created_at', 'descending']).paginate(:page => params[:page], :per_page => params[:per_page])

@next_page = nil
set_next_prev_page
@updates = Update.where(:feed_id => user.feed.id).order(['created_at', 'descending'])
@updates = @updates.paginate(:page => params[:page], :per_page => params[:per_page])
set_pagination_buttons(@updates)

haml :"users/show"
end
Expand Down Expand Up @@ -196,6 +183,7 @@ class Rstatus
feed = User.first(:username => params[:username]).feed
redirect "/feeds/#{feed.id}.atom"
end

# Who do you think is a really neat person? This page will show it to the
# world, so pick wisely!
get '/users/:username/following' do
Expand All @@ -204,11 +192,9 @@ class Rstatus
feeds = User.first(:username => params[:username]).following

@user = User.first(:username => params[:username])
@users = feeds.paginate(:page => params[:page], :per_page => params[:per_page], :order => :id.desc).map{|f| f.author.user}

set_next_prev_page
@next_page = nil unless params[:page]*params[:per_page] < feeds.count

@users = feeds.paginate(:page => params[:page], :per_page => params[:per_page], :order => :id.desc)
set_pagination_buttons(@users)
@users = @users.map{|f| f.author.user}
title = ""
title << "#{@user.username} is following"

Expand All @@ -232,10 +218,9 @@ class Rstatus
feeds = User.first(:username => params[:username]).followers

@user = User.first(:username => params[:username])
@users = feeds.paginate(:page => params[:page], :per_page => params[:per_page], :order => :id.desc).map{|f| f.author.user}

set_next_prev_page
@next_page = nil unless params[:page]*params[:per_page] < feeds.count
@users = feeds.paginate(:page => params[:page], :per_page => params[:per_page], :order => :id.desc)
set_pagination_buttons(@users)
@users = @users.map{|f| f.author.user}

#build title
title = ""
Expand Down
17 changes: 11 additions & 6 deletions helpers.rb
Expand Up @@ -47,13 +47,18 @@ def set_params_page
# Similar to the set_params_page helper this one creates the links
# for the previous and the next page on all routes that display
# stuff on more than one page.
def set_next_prev_page
@next_page = "?#{Rack::Utils.build_query :page => params[:page] + 1}"
#If needed it can also take options for more parameters
def set_pagination_buttons(data, options = {})
return if data.nil?

if data.next_page
params = {:page => data.next_page}.merge(options)
@next_page = "?#{Rack::Utils.build_query params}"
end

if params[:page] > 1
@prev_page = "?#{Rack::Utils.build_query :page => params[:page] - 1}"
else
@prev_page = nil
if data.previous_page
params = {:page => data.previous_page}.merge(options)
@prev_page = "?#{Rack::Utils.build_query params}"
end
end
end
Expand Down
15 changes: 4 additions & 11 deletions models/user.rb
Expand Up @@ -148,23 +148,16 @@ def following? feed_url

timestamps!

#create a timeline for the current user, i.e. my updates and those I follow.
def timeline(params)
popts = {
:page => params[:page],
:per_page => params[:per_page]
}

following_plus_me = following.clone
following_plus_me << self.feed
Update.where(:author_id => following_plus_me.map(&:author_id)).order(['created_at', 'descending']).paginate(popts)
Update.where(:author_id => following_plus_me.map(&:author_id)).order(['created_at', 'descending'])
end

#Find all replies for this user.
def at_replies(params)
popts = {
:page => params[:page],
:per_page => params[:per_page]
}
Update.where(:text => /^@#{Regexp.quote(username)}\b/).order(['created_at', 'descending']).paginate(popts)
Update.where(:text => /^@#{Regexp.quote(username)}\b/).order(['created_at', 'descending'])
end

key :status
Expand Down
23 changes: 7 additions & 16 deletions rstatus.rb
Expand Up @@ -83,8 +83,8 @@ class Rstatus
get '/' do
if logged_in?
set_params_page
@updates = current_user.timeline(params)
set_next_prev_page if @updates.total_entries > params[:per_page]
@updates = current_user.timeline(params).paginate( :page => params[:page], :per_page => params[:per_page] || 20, :order => :created_at.desc)
set_pagination_buttons(@updates)

@timeline = true

Expand Down Expand Up @@ -119,8 +119,8 @@ class Rstatus
get '/replies' do
if logged_in?
set_params_page
@replies = current_user.at_replies(params)
set_next_prev_page if @replies.total_entries > params[:per_page]
@replies = current_user.at_replies(params).paginate( :page => params[:page], :per_page => params[:per_page] || 20, :order => :created_at.desc)
set_pagination_buttons(@replies)
haml :replies
else
haml :index, :layout => false
Expand All @@ -145,16 +145,7 @@ class Rstatus
@updates = []
if params[:q]
@updates = Update.filter(params[:q], :page => params[:page], :per_page => params[:per_page] || 20, :order => :created_at.desc)
end

@next_page = nil
@prev_page = nil

if !@updates.empty? && @updates.next_page
@next_page = "?#{Rack::Utils.build_query :page => @updates.next_page}"
end
if !@updates.empty? && @updates.previous_page
@prev_page = "?#{Rack::Utils.build_query :page => @updates.previous_page}"
set_pagination_buttons(@updates)
end
haml :search
end
Expand All @@ -170,8 +161,8 @@ class Rstatus
get "/hashtags/:tag" do
@hashtag = params[:tag]
set_params_page
@updates = Update.hashtag_search(@hashtag, params)
set_next_prev_page if @updates.total_entries > params[:per_page]
@updates = Update.hashtag_search(@hashtag, params).paginate( :page => params[:page], :per_page => params[:per_page] || 20, :order => :created_at.desc)
set_pagination_buttons(@updates)
@timeline = true
@update_text = params[:status]
haml :dashboard
Expand Down
32 changes: 15 additions & 17 deletions test/acceptance/following_test.rb
Expand Up @@ -82,38 +82,36 @@ def test_users_followers_in_order
assert_match /leopard.*zebra/m, page.body
end

def test_user_following_paginates
def test_user_following_outputs_json
u = Factory(:user)
a = Factory(:authorization, :user => u)

log_in(u, a.uid)

51.times do
u2 = Factory(:user)
u.follow! u2.feed.url
end

visit "/users/#{u.username}/following"
u2 = Factory(:user, :username => "user1")
u.follow! u2.feed.url

click_link "next_button"
visit "/users/#{u.username}/following.json"

assert_match "Previous", page.body
assert_match "Next", page.body
json = JSON.parse(page.body)
assert_equal "user1", json.last["username"]
end

def test_user_following_outputs_json
def test_user_followers_paginates_not
u = Factory(:user)
a = Factory(:authorization, :user => u)

log_in(u, a.uid)

u2 = Factory(:user, :username => "user1")
u.follow! u2.feed.url
5.times do
u2 = Factory(:user)
u2.follow! u.feed.url
end

visit "/users/#{u.username}/following.json"
visit "/users/#{u.username}/followers"

json = JSON.parse(page.body)
assert_equal "user1", json.last["username"]
refute_match "Previous", page.body
refute_match "Next", page.body
end

def test_user_followers_paginates
Expand All @@ -135,7 +133,7 @@ def test_user_followers_paginates
assert_match "Next", page.body
end

def test_following_displays_username_logged_in
def test_following_displays_username_logged_in
u = Factory(:user, :username => "dfnkt")
a = Factory(:authorization, :user => u)

Expand Down
42 changes: 42 additions & 0 deletions test/acceptance/update_test.rb
Expand Up @@ -142,4 +142,46 @@ def test_update_render_with_referral
assert_match page.body, /#{update2.text}/
assert_match page.body, /#{update.text}/
end

def test_updates_paginate_forward_only
30.times do
Factory(:update)
end

u = Factory(:user)
log_in_email(u)
visit "/updates"

refute_match "Previous", page.body
assert_match "Next", page.body
end

def test_updates_paginate_back_only
u = Factory(:user)
log_in_email(u)

30.times do
Factory(:update)
end

visit "/updates"
click_link "next_button"

assert_match "Previous", page.body
refute_match "Next", page.body
end

def test_updates_paginate
54.times do
Factory(:update)
end

u = Factory(:user)
log_in_email(u)
visit "/updates"
click_link "next_button"

assert_match "Previous", page.body
assert_match "Next", page.body
end
end

0 comments on commit 762b8a1

Please sign in to comment.