Merge branch 'microweb10-registration_username_ajax_validation'
This commit is contained in:
@@ -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(){
|
||||
|
||||
25
app/assets/javascripts/registration_form.js.coffee
Normal file
25
app/assets/javascripts/registration_form.js.coffee
Normal file
@@ -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 $("<small class=\"#{klass}\" style=\"margin-top: -16px;\">#{response.message}</small>")
|
||||
|
||||
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 != ""
|
||||
@@ -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;
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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:
|
||||
|
||||
@@ -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 <b>revisa tu correo electrónico</b> - te hemos enviado un <b>enlace para confirmar tu cuenta</b>.
|
||||
|
||||
@@ -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'
|
||||
|
||||
34
spec/controllers/users/registrations_controller_spec.rb
Normal file
34
spec/controllers/users/registrations_controller_spec.rb
Normal file
@@ -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
|
||||
27
spec/features/registration_form_spec.rb
Normal file
27
spec/features/registration_form_spec.rb
Normal file
@@ -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
|
||||
Reference in New Issue
Block a user