Move json hash from users controller to NicknameUnavailable

This commit is contained in:
Einar Jonsson 2013-08-25 22:41:17 +00:00
parent 982b763216
commit 9085cec232
3 changed files with 24 additions and 14 deletions

View File

@ -217,15 +217,8 @@ class UsersController < ApplicationController
end end
rescue ActiveRecord::StatementInvalid rescue ActiveRecord::StatementInvalid
render json: { success: false, message: I18n.t("login.something_already_taken") } render json: { success: false, message: I18n.t("login.something_already_taken") }
rescue DiscourseHub::NicknameUnavailable rescue DiscourseHub::NicknameUnavailable=> e
render json: { success: false, render json: e.response_message
message: I18n.t(
"login.errors",
errors:I18n.t(
"login.not_available", suggestion: UserNameSuggester.suggest(params[:username])
)
)
}
rescue RestClient::Forbidden rescue RestClient::Forbidden
render json: { errors: [I18n.t("discourse_hub.access_token_problem")] } render json: { errors: [I18n.t("discourse_hub.access_token_problem")] }
end end

View File

@ -3,7 +3,24 @@ require_dependency 'version'
module DiscourseHub module DiscourseHub
class NicknameUnavailable < RuntimeError; end class NicknameUnavailable < RuntimeError
def initialize(nickname)
@nickname = nickname
end
def response_message
{
success: false,
message: I18n.t(
"login.errors",
errors:I18n.t(
"login.not_available", suggestion: UserNameSuggester.suggest(@nickname)
)
)
}
end
end
def self.nickname_available?(nickname) def self.nickname_available?(nickname)
json = get('/users/nickname_available', {nickname: nickname}) json = get('/users/nickname_available', {nickname: nickname})
@ -20,7 +37,7 @@ module DiscourseHub
if json.has_key?('success') if json.has_key?('success')
true true
else else
raise NicknameUnavailable # TODO: report ALL the errors raise NicknameUnavailable.new(nickname) # TODO: report ALL the errors
end end
end end
@ -34,7 +51,7 @@ module DiscourseHub
if json.has_key?('success') if json.has_key?('success')
true true
else else
raise NicknameUnavailable # TODO: report ALL the errors raise NicknameUnavailable.new(new_nickname) # TODO: report ALL the errors
end end
end end
@ -93,4 +110,4 @@ module DiscourseHub
def self.accepts def self.accepts
[:json, 'application/vnd.discoursehub.v1'] [:json, 'application/vnd.discoursehub.v1']
end end
end end

View File

@ -477,7 +477,7 @@ describe UsersController do
context 'when nickname is unavailable in DiscourseHub' do context 'when nickname is unavailable in DiscourseHub' do
before do before do
SiteSetting.stubs(:call_discourse_hub?).returns(true) SiteSetting.stubs(:call_discourse_hub?).returns(true)
DiscourseHub.stubs(:register_nickname).raises(DiscourseHub::NicknameUnavailable) DiscourseHub.stubs(:register_nickname).raises(DiscourseHub::NicknameUnavailable.new(@user.name))
end end
let(:create_params) {{ let(:create_params) {{
name: @user.name, name: @user.name,