Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

useful error messages on invalid usage of "/compare/[users...]"

  • Loading branch information...
commit 84dbfb871251310ea6d754d8c6b5cea84b52b959 1 parent df03cb3
@mislav authored
View
20 app/controllers/users_controller.rb
@@ -42,10 +42,24 @@ def unfollow
end
def compare
- users = params[:users].split('+', 2).map {|name| find_user name }
+ user_names = params[:users].split('+')
- @compare = User::Compare.new(*users)
- fresh_when etag: session_cache_key(@compare)
+ if user_names.size != 2
+ render 'shared/error', status: 400, locals: {
+ error: "Nice try. You can only compare 2 users at a time."
+ }
+ else
+ users = user_names.map {|name| find_user name }
+
+ if users.all?
+ @compare = User::Compare.new(*users)
+ fresh_when etag: session_cache_key(@compare)
+ else
+ user_missing = user_names[users.index(nil)]
+ @message = "Could not find user #{user_missing.inspect}."
+ render 'shared/not_found', status: 404
+ end
+ end
end
protected
View
7 app/views/shared/error.html.erb
@@ -2,6 +2,9 @@
<% body_class 'error' %>
<article>
<h1>There has been an error</h1>
-
+ <% if error.respond_to? :message %>
<p>The message of the error is: <%= error.message %></p>
-</article>
+ <% else %>
+ <p><%= error %></p>
+ <% end %>
+</article>
View
31 spec/requests/users_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe "Users" do
+ describe "GET /compare/[users...]" do
+ before do
+ [User, Movie].each { |model| model.collection.remove }
+ @mislav = User.create username: 'mislav'
+ @ivana = User.create username: 'ivana'
+ end
+
+ it "compares two users" do
+ get compare_path('ivana+mislav')
+ response.status.should be(200)
+ end
+
+ it "can't compare one user" do
+ get compare_path('ivana')
+ response.status.should be(400)
+ end
+
+ it "can't compare three users" do
+ get compare_path('ivana+mislav+another')
+ response.status.should be(400)
+ end
+
+ it "handles user not found" do
+ get compare_path('ivana+idontexist')
+ response.status.should be(404)
+ end
+ end
+end
Please sign in to comment.
Something went wrong with that request. Please try again.