diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js
index 52d176c41..a3d4d1b7c 100644
--- a/app/assets/javascripts/application.js
+++ b/app/assets/javascripts/application.js
@@ -38,7 +38,7 @@
//= require votes
//= require annotatable
//= require advanced_search
-
+//= require registration_form
var initialize_modules = function() {
App.Comments.initialize();
@@ -52,6 +52,7 @@ var initialize_modules = function() {
App.IeAlert.initialize();
App.Annotatable.initialize();
App.AdvancedSearch.initialize();
+ App.RegistrationForm.initialize();
};
$(function(){
diff --git a/app/assets/javascripts/registration_form.js.coffee b/app/assets/javascripts/registration_form.js.coffee
new file mode 100644
index 000000000..13233b68d
--- /dev/null
+++ b/app/assets/javascripts/registration_form.js.coffee
@@ -0,0 +1,25 @@
+App.RegistrationForm =
+
+ initialize: ->
+
+ registrationForm = $("form#new_user[action=\"/users\"]")
+ usernameInput = $("input#user_username")
+
+ clearUsernameMessage = ->
+ $("small").remove()
+
+ showUsernameMessage = (response) ->
+ klass = if response.available then "error no-error" else "error error"
+ usernameInput.after $("#{response.message}")
+
+ validateUsername = (username) ->
+ request = $.get "/user/registrations/check_username?username=#{username}"
+ request.done (response) ->
+ showUsernameMessage(response)
+
+
+ if registrationForm.length > 0
+ usernameInput.on "focusout", ->
+ clearUsernameMessage()
+ username = usernameInput.val();
+ validateUsername(username) if username != ""
diff --git a/app/assets/stylesheets/layout.scss b/app/assets/stylesheets/layout.scss
index 26aeebfcc..a98e8f256 100644
--- a/app/assets/stylesheets/layout.scss
+++ b/app/assets/stylesheets/layout.scss
@@ -847,6 +847,11 @@ span.error, small.error {
color: $alert-color;
}
+span.error, small.no-error {
+ background: $success-bg;
+ color: $success-color;
+}
+
.error small.error {
background: $alert-bg;
color: $alert-color;
diff --git a/app/controllers/users/registrations_controller.rb b/app/controllers/users/registrations_controller.rb
index a707d7e75..d14cff907 100644
--- a/app/controllers/users/registrations_controller.rb
+++ b/app/controllers/users/registrations_controller.rb
@@ -36,6 +36,14 @@ class Users::RegistrationsController < Devise::RegistrationsController
end
end
+ def check_username
+ if User.find_by_username params[:username]
+ render json: {available: false, message: t("devise_views.users.registrations.new.username_is_not_available")}
+ else
+ render json: {available: true, message: t("devise_views.users.registrations.new.username_is_available")}
+ end
+ end
+
private
def sign_up_params
diff --git a/config/locales/devise_views.en.yml b/config/locales/devise_views.en.yml
index 62cc1a2a7..2cb15ffef 100755
--- a/config/locales/devise_views.en.yml
+++ b/config/locales/devise_views.en.yml
@@ -116,6 +116,8 @@ en:
terms: By registering you accept the %{terms}
terms_link: terms and conditions of use
title: Register
+ username_is_available: "Username available"
+ username_is_not_available: "Username already in use"
username_label: Username
username_note: Name that appears next to your posts
success:
diff --git a/config/locales/devise_views.es.yml b/config/locales/devise_views.es.yml
index a5df0e4dc..51dd9b452 100644
--- a/config/locales/devise_views.es.yml
+++ b/config/locales/devise_views.es.yml
@@ -116,8 +116,10 @@ es:
terms: Al registrarte aceptas las %{terms}
terms_link: condiciones de uso
title: Registrarse
+ username_is_available: "Nombre de usuario disponible"
+ username_is_not_available: "Nombre de usuario ya existente"
username_label: Nombre de usuario
- username_note: Nombre que aparecerá en tus publicaciones
+ username_note: Nombre que aparecerá en tus publicacionesß
success:
back_to_index: Entendido, volver a la página principal
instructions_1_html: Por favor revisa tu correo electrónico - te hemos enviado un enlace para confirmar tu cuenta.
diff --git a/config/routes.rb b/config/routes.rb
index 8228aade6..9c7146dcf 100644
--- a/config/routes.rb
+++ b/config/routes.rb
@@ -19,7 +19,7 @@ Rails.application.routes.draw do
devise_scope :user do
patch '/user/confirmation', to: 'users/confirmations#update', as: :update_user_confirmation
-
+ get '/user/registrations/check_username', to: 'users/registrations#check_username'
get 'users/sign_up/success', to: 'users/registrations#success'
get 'users/registrations/delete_form', to: 'users/registrations#delete_form'
delete 'users/registrations', to: 'users/registrations#delete'
diff --git a/spec/controllers/users/registrations_controller_spec.rb b/spec/controllers/users/registrations_controller_spec.rb
new file mode 100644
index 000000000..ea73428f5
--- /dev/null
+++ b/spec/controllers/users/registrations_controller_spec.rb
@@ -0,0 +1,34 @@
+require 'rails_helper'
+
+describe Users::RegistrationsController do
+
+ describe "POST check_username" do
+
+ before(:each) do
+ @request.env["devise.mapping"] = Devise.mappings[:user]
+ end
+
+ context "when username is available" do
+ it "should return true with no error message" do
+ get :check_username, username: "available username"
+
+ data = JSON.parse response.body, symbolize_names: true
+ expect(data[:available]).to be true
+ expect(data[:message]).to eq I18n.t("devise_views.users.registrations.new.username_is_available")
+ end
+ end
+
+ context "when username is not available" do
+ it "should return false with an error message" do
+ user = create(:user)
+ get :check_username, username: user.username
+
+ data = JSON.parse response.body, symbolize_names: true
+ expect(data[:available]).to be false
+ expect(data[:message]).to eq I18n.t("devise_views.users.registrations.new.username_is_not_available")
+ end
+ end
+
+ end
+
+end
\ No newline at end of file
diff --git a/spec/features/registration_form_spec.rb b/spec/features/registration_form_spec.rb
new file mode 100644
index 000000000..875e2b344
--- /dev/null
+++ b/spec/features/registration_form_spec.rb
@@ -0,0 +1,27 @@
+require 'rails_helper'
+
+feature 'Registration form' do
+
+ scenario 'username is not available', :js do
+ user = create(:user)
+
+ visit new_user_registration_path
+ expect(page).to_not have_content I18n.t("devise_views.users.registrations.new.username_is_not_available")
+
+ fill_in "user_username", with: user.username
+ check 'user_terms_of_service'
+
+ expect(page).to have_content I18n.t("devise_views.users.registrations.new.username_is_not_available")
+ end
+
+ scenario 'username is available', :js do
+ visit new_user_registration_path
+ expect(page).to_not have_content I18n.t("devise_views.users.registrations.new.username_is_available")
+
+ fill_in "user_username", with: "available username"
+ check 'user_terms_of_service'
+
+ expect(page).to have_content I18n.t("devise_views.users.registrations.new.username_is_available")
+ end
+
+end