From 6d68f96f39303726017f82009f1b3ca6b57da616 Mon Sep 17 00:00:00 2001 From: huang Date: Mon, 7 Mar 2016 17:21:53 +0800 Subject: [PATCH] V2 --- app/controllers/api/v2/base_controller.rb | 15 +++++++++++++++ app/controllers/api/v2/users_controller.rb | 9 +++++++++ app/helpers/api/v2/base_helper.rb | 2 ++ app/helpers/api/v2/users_helper.rb | 2 ++ app/views/api/v2/users/show.json.jbuilder | 3 +++ config/routes.rb | 2 +- ...307071054_add_authentication_token_to_users.rb | 4 ++++ spec/controllers/api/v2/base_controller_spec.rb | 5 +++++ spec/controllers/api/v2/users_controller_spec.rb | 5 +++++ 9 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 app/controllers/api/v2/base_controller.rb create mode 100644 app/controllers/api/v2/users_controller.rb create mode 100644 app/helpers/api/v2/base_helper.rb create mode 100644 app/helpers/api/v2/users_helper.rb create mode 100644 app/views/api/v2/users/show.json.jbuilder create mode 100644 db/migrate/20160307071054_add_authentication_token_to_users.rb create mode 100644 spec/controllers/api/v2/base_controller_spec.rb create mode 100644 spec/controllers/api/v2/users_controller_spec.rb diff --git a/app/controllers/api/v2/base_controller.rb b/app/controllers/api/v2/base_controller.rb new file mode 100644 index 000000000..adf214885 --- /dev/null +++ b/app/controllers/api/v2/base_controller.rb @@ -0,0 +1,15 @@ +class Api::V2::BaseController < ApplicationController + # disable the CSRF token + protect_from_forgery with: :null_session + + # disable cookies (no set-cookies header in response) + before_action :destroy_session + + # disable the CSRF token + skip_before_action :verify_authenticity_token + + def destroy_session + request.session_options[:skip] = true + end + +end diff --git a/app/controllers/api/v2/users_controller.rb b/app/controllers/api/v2/users_controller.rb new file mode 100644 index 000000000..ea0b7966c --- /dev/null +++ b/app/controllers/api/v2/users_controller.rb @@ -0,0 +1,9 @@ +class Api::V2::UsersController < ApplicationController + def show + @user = User.find(params[:id]) + + # 原文使用 Api::V1::UserSerializer + # 我们现在使用 app/views/api/v1/users/show.json.jbuilder + # render(json: Api::V1::UserSerializer.new(user).to_json) + end +end diff --git a/app/helpers/api/v2/base_helper.rb b/app/helpers/api/v2/base_helper.rb new file mode 100644 index 000000000..16515f590 --- /dev/null +++ b/app/helpers/api/v2/base_helper.rb @@ -0,0 +1,2 @@ +module Api::V2::BaseHelper +end diff --git a/app/helpers/api/v2/users_helper.rb b/app/helpers/api/v2/users_helper.rb new file mode 100644 index 000000000..1485a0233 --- /dev/null +++ b/app/helpers/api/v2/users_helper.rb @@ -0,0 +1,2 @@ +module Api::V2::UsersHelper +end diff --git a/app/views/api/v2/users/show.json.jbuilder b/app/views/api/v2/users/show.json.jbuilder new file mode 100644 index 000000000..2ef6aa156 --- /dev/null +++ b/app/views/api/v2/users/show.json.jbuilder @@ -0,0 +1,3 @@ +json.user do + json.(@user, :id, :email, :name, :activated, :admin, :created_at, :updated_at) +end \ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index 3627b3b0a..c0ca59f1c 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -29,7 +29,7 @@ RedmineApp::Application.routes.draw do mount Mobile::API => '/api' namespace :api do - namespace :v1 do + namespace :v2 do resources :users, only: [:index, :create, :show, :update, :destroy] # 原文有 microposts, 我们现在把它注释掉 # resources :microposts, only: [:index, :create, :show, :update, :destroy] diff --git a/db/migrate/20160307071054_add_authentication_token_to_users.rb b/db/migrate/20160307071054_add_authentication_token_to_users.rb new file mode 100644 index 000000000..1f5f6f086 --- /dev/null +++ b/db/migrate/20160307071054_add_authentication_token_to_users.rb @@ -0,0 +1,4 @@ +class AddAuthenticationTokenToUsers < ActiveRecord::Migration + def change + end +end diff --git a/spec/controllers/api/v2/base_controller_spec.rb b/spec/controllers/api/v2/base_controller_spec.rb new file mode 100644 index 000000000..894e71259 --- /dev/null +++ b/spec/controllers/api/v2/base_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Api::V2::BaseController, :type => :controller do + +end diff --git a/spec/controllers/api/v2/users_controller_spec.rb b/spec/controllers/api/v2/users_controller_spec.rb new file mode 100644 index 000000000..817a714f3 --- /dev/null +++ b/spec/controllers/api/v2/users_controller_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Api::V2::UsersController, :type => :controller do + +end