From 35a135106e5157632ede368f5da533b1ea4357c1 Mon Sep 17 00:00:00 2001 From: junv Date: Tue, 11 Mar 2014 17:55:25 +0800 Subject: [PATCH] add client side registration feature --- app/assets/javascripts/api/v1/registrations.js | 2 ++ app/assets/stylesheets/api/v1/registrations.css | 4 ++++ .../api/v1/registrations_controller.rb | 17 +++++++++++++++++ app/controllers/api/v1/sessions_controller.rb | 1 - app/helpers/api/v1/registrations_helper.rb | 2 ++ config/routes.rb | 1 + .../api/v1/registrations_controller_spec.rb | 5 +++++ .../helpers/api/v1/registrations_helper_spec.rb | 15 +++++++++++++++ 8 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 app/assets/javascripts/api/v1/registrations.js create mode 100644 app/assets/stylesheets/api/v1/registrations.css create mode 100644 app/controllers/api/v1/registrations_controller.rb create mode 100644 app/helpers/api/v1/registrations_helper.rb create mode 100644 spec/controllers/api/v1/registrations_controller_spec.rb create mode 100644 spec/helpers/api/v1/registrations_helper_spec.rb diff --git a/app/assets/javascripts/api/v1/registrations.js b/app/assets/javascripts/api/v1/registrations.js new file mode 100644 index 0000000..dee720f --- /dev/null +++ b/app/assets/javascripts/api/v1/registrations.js @@ -0,0 +1,2 @@ +// Place all the behaviors and hooks related to the matching controller here. +// All this logic will automatically be available in application.js. diff --git a/app/assets/stylesheets/api/v1/registrations.css b/app/assets/stylesheets/api/v1/registrations.css new file mode 100644 index 0000000..afad32d --- /dev/null +++ b/app/assets/stylesheets/api/v1/registrations.css @@ -0,0 +1,4 @@ +/* + Place all the styles related to the matching controller here. + They will automatically be included in application.css. +*/ diff --git a/app/controllers/api/v1/registrations_controller.rb b/app/controllers/api/v1/registrations_controller.rb new file mode 100644 index 0000000..68cfd97 --- /dev/null +++ b/app/controllers/api/v1/registrations_controller.rb @@ -0,0 +1,17 @@ +class Api::V1::RegistrationsController < ApplicationController + before_filter :allow_cors + + respond_to :json + def create + + user = User.new(params[:user]) + if user.save + render :json=> user.as_json(:auth_token=>user.auth_keys.last.value, :email=>user.email), :status=>201 + return + else + warden.custom_failure! + render :json=> user.errors, :status=>422 + end + end + +end diff --git a/app/controllers/api/v1/sessions_controller.rb b/app/controllers/api/v1/sessions_controller.rb index 3893610..4e37ad3 100644 --- a/app/controllers/api/v1/sessions_controller.rb +++ b/app/controllers/api/v1/sessions_controller.rb @@ -12,7 +12,6 @@ class Api::V1::SessionsController < Devise::SessionsController token: current_user.auth_keys.last.value, avatar: current_user.avatar, name: current_user.preferred_name ? current_user.preferred_name : '匿名' - } } diff --git a/app/helpers/api/v1/registrations_helper.rb b/app/helpers/api/v1/registrations_helper.rb new file mode 100644 index 0000000..5e0f475 --- /dev/null +++ b/app/helpers/api/v1/registrations_helper.rb @@ -0,0 +1,2 @@ +module Api::V1::RegistrationsHelper +end diff --git a/config/routes.rb b/config/routes.rb index 589a143..d4ca298 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -50,6 +50,7 @@ Libr::Application.routes.draw do namespace :v1 do devise_scope :user do resources :sessions, :only => [:create, :destroy, :failure] + resources :registrations, :only => [:create] end get 'locations/detail' => 'locations#get_location' diff --git a/spec/controllers/api/v1/registrations_controller_spec.rb b/spec/controllers/api/v1/registrations_controller_spec.rb new file mode 100644 index 0000000..197f69a --- /dev/null +++ b/spec/controllers/api/v1/registrations_controller_spec.rb @@ -0,0 +1,5 @@ +require 'spec_helper' + +describe Api::V1::RegistrationsController do + +end diff --git a/spec/helpers/api/v1/registrations_helper_spec.rb b/spec/helpers/api/v1/registrations_helper_spec.rb new file mode 100644 index 0000000..c55ea53 --- /dev/null +++ b/spec/helpers/api/v1/registrations_helper_spec.rb @@ -0,0 +1,15 @@ +require 'spec_helper' + +# Specs in this file have access to a helper object that includes +# the Api::V1::RegistrationsHelper. For example: +# +# describe Api::V1::RegistrationsHelper do +# describe "string concat" do +# it "concats two strings with spaces" do +# expect(helper.concat_strings("this","that")).to eq("this that") +# end +# end +# end +describe Api::V1::RegistrationsHelper do + pending "add some examples to (or delete) #{__FILE__}" +end