Skip to content

Commit

Permalink
We don't really need to query all of Updates for a user's page... not…
Browse files Browse the repository at this point in the history
… sure if this gets any performance benefits but it feels nicer.
  • Loading branch information
carols10cents committed Sep 10, 2011
1 parent ff9958a commit 45317ae
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
4 changes: 2 additions & 2 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ def show
redirect_to "/users/#{user.username}"
end
end
@author = user.author
@updates = Update.where(:feed_id => user.feed.id).order(['created_at', 'descending'])
@author = user.author
@updates = user.updates
@updates = @updates.paginate(:page => params[:page], :per_page => params[:per_page])
set_pagination_buttons(@updates)

Expand Down
4 changes: 4 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ def feed
self.author.feed
end

def updates
self.author.feed.updates.sort(:created_at.desc)
end

# Before a user is created, we will generate some RSA keys
def generate_rsa_pair
key = RSA::KeyPair.generate(2048)
Expand Down
17 changes: 17 additions & 0 deletions test/acceptance/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,23 @@
assert_equal url, page.current_url
end

it "has the user's updates on the page in reverse chronological order" do
u = Factory(:user)
update1 = Factory(:update,
:text => "This is a message posted yesterday",
:author => u.author,
:created_at => 1.day.ago)
update2 = Factory(:update,
:text => "This is a message posted last week",
:author => u.author,
:created_at => 1.week.ago)
u.feed.updates << update1
u.feed.updates << update2

visit "/users/#{u.username}"
assert_match /#{update1.text}.*#{update2.text}/m, page.body
end

it "has a link to edit your own profile" do
u = Factory(:user)
a = Factory(:authorization, :user => u)
Expand Down

0 comments on commit 45317ae

Please sign in to comment.