Skip to content

Commit

Permalink
Rearranging UsersController#show to have guard conditions rather than…
Browse files Browse the repository at this point in the history
… buried early returns

I'm sure Gary could do more with this, but this is a start?
  • Loading branch information
carols10cents committed Sep 10, 2011
1 parent 45317ae commit 5b1c40a
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 17 deletions.
31 changes: 14 additions & 17 deletions app/controllers/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,26 +14,23 @@ def index
end

def show
set_params_page

user = User.first :username => params[:id]
user = User.first :username => /^#{Regexp.escape(params[:id])}$/i

if user.nil?
#check for a case insensitive match and then redirect to the correct address
username = Regexp.escape(params[:id])
user = User.first :username => /^#{username}$/i
if user.nil?
render :file => "#{Rails.root}/public/404.html", :status => 404
return
else
redirect_to "/users/#{user.username}"
end
end
@author = user.author
@updates = user.updates
@updates = @updates.paginate(:page => params[:page], :per_page => params[:per_page])
set_pagination_buttons(@updates)
render :file => "#{Rails.root}/public/404.html", :status => 404
elsif user.username != params[:id] # case difference
redirect_to "/users/#{user.username}"
else
set_params_page
@author = user.author
@updates = user.updates
@updates = @updates.paginate(:page => params[:page], :per_page => params[:per_page])

headers['Link'] = "<#{user_xrd_path(user.author.username)}>; rel=\"lrdd\"; type=\"application/xrd+xml\""
set_pagination_buttons(@updates)

headers['Link'] = "<#{user_xrd_path(user.author.username)}>; rel=\"lrdd\"; type=\"application/xrd+xml\""
end
end

def edit
Expand Down
5 changes: 5 additions & 0 deletions test/acceptance/profile_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
assert_match /#{update1.text}.*#{update2.text}/m, page.body
end

it "404s if the user doesnt exist" do
visit "/users/nonexistent"
assert_match "The page you were looking for doesn't exist.", 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 5b1c40a

Please sign in to comment.