Skip to content

Commit

Permalink
fix update (timeline, replies, hashtags) pagination to sort THEN pagi…
Browse files Browse the repository at this point in the history
…nate, not sort in the view. added pagination and nav buttons to the update display in the user profile page.
  • Loading branch information
wolfwood committed Mar 24, 2011
1 parent 4691e17 commit e12c0a8
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 7 deletions.
2 changes: 1 addition & 1 deletion models/update.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def self.hashtag_search(tag, opts)
:page => opts[:page],
:per_page => opts[:per_page]
}
where(:text => /##{tag}/).paginate(popts)
where(:text => /##{tag}/).order(['created_at', 'descending']).paginate(popts)
end

def self.hot_updates
Expand Down
4 changes: 2 additions & 2 deletions models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -107,15 +107,15 @@ def timeline(opts)
:page => opts[:page],
:per_page => opts[:per_page]
}
Update.where(:author_id => following.map(&:author_id)).paginate(popts)
Update.where(:author_id => following.map(&:author_id)).order(['created_at', 'descending']).paginate(popts)
end

def at_replies(opts)
popts = {
:page => opts[:page],
:per_page => opts[:per_page]
}
Update.where(:author_id => following.map(&:author_id)).where(:text => /^@#{username} /).paginate(popts)
Update.where(:author_id => following.map(&:author_id)).where(:text => /^@#{username} /).order(['created_at', 'descending']).paginate(popts)
end

key :status
Expand Down
20 changes: 20 additions & 0 deletions rstatus.rb
Original file line number Diff line number Diff line change
Expand Up @@ -240,8 +240,28 @@ class Rstatus < Sinatra::Base

# show user profile
get "/users/:slug" do
params[:page] ||= 1
params[:per_page] ||= 20
params[:page] = params[:page].to_i
params[:per_page] = params[:per_page].to_i

user = User.first :username => params[:slug]
@author = user.author
#XXX: the following doesn't work for some reasond
# @updates = user.feed.updates.sort{|a, b| b.created_at <=> a.created_at}.paginate(:page => params[:page], :per_page => params[:per_page])

#XXX: this is not webscale
@updates = Update.where(:feed_id => user.feed.id).order(['created_at', 'descending']).paginate(:page => params[:page], :per_page => params[:per_page])

@next_page = nil
@prev_page = nil

@next_page = "?#{Rack::Utils.build_query :page => params[:page] + 1}"

if params[:page] > 1
@prev_page = "?#{Rack::Utils.build_query :page => params[:page] - 1}"
end

haml :"users/show"
end

Expand Down
2 changes: 1 addition & 1 deletion views/dashboard.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

!= haml :_navigation
#timeline.updates
!= haml :_updates, :locals => {:updates => @updates.to_a.sort{|a, b| b.created_at <=> a.created_at }}
!= haml :_updates, :locals => {:updates => @updates}
%nav.pagination
- unless @prev_page.nil?
%a.button{:href => @prev_page, :id => "prev_button"}
Expand Down
2 changes: 1 addition & 1 deletion views/hashtags.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

!= haml :_navigation
#timeline.updates
!= haml :_updates, :locals => {:updates => current_user.timeline.to_a.sort{|a, b| b.created_at <=> a.created_at }}
!= haml :_updates, :locals => {:updates => current_user.timeline}
- unless @prev_page.nil?
%a.button{:href => @prev_page, :id => "prev_button"}
&laquo; Previous
Expand Down
2 changes: 1 addition & 1 deletion views/replies.haml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

!= haml :_navigation
#replies.updates
!= haml :_updates, :locals => {:updates => @replies.to_a.sort{|a, b| b.created_at <=> a.created_at }}
!= haml :_updates, :locals => {:updates => @replies}
- unless @prev_page.nil?
%a.button{:href => @prev_page, :id => "prev_button"}
&laquo; Previous
Expand Down
10 changes: 9 additions & 1 deletion views/users/show.haml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@
.follow-status You are not following #{@author.username}

#updates.updates
!= haml :_updates, :locals => {:updates => @author.feed.updates.to_a.sort{|a, b| b.created_at <=> a.created_at }}
!= haml :_updates, :locals => {:updates => @updates}

- unless @prev_page.nil?
%a.button{:href => @prev_page, :id => "prev_button"}
&laquo; Previous

- unless @next_page.nil?
%a.button{:href => @next_page, :id => "next_button"}
Next &raquo;

- content_for :javascript do
%script{:src=>"/js/users.js"}

0 comments on commit e12c0a8

Please sign in to comment.