Permalink
Browse files

Restructure and refactor example

This will be a demo along rather than a code along
  • Loading branch information...
Antony Donovan
Antony Donovan committed Nov 5, 2015
1 parent 27b71db commit 937a9818889c755bc21d9a85352a3f7017087c2d
@@ -1 +1 @@
2.2.2
2.2.3
33 Gemfile
@@ -1,35 +1,28 @@
# Look for ruby gems here
source 'https://rubygems.org'

# Set required ruby version (better for heroku)
ruby '2.2.2'
# Set required ruby version (for heroku)
ruby '2.2.3'

gem 'rails', '4.2.3'
gem 'rails', '4.2.4'

# Minimize rails kitchen sink approach
# Reduce rails kitchen sink approach
gem 'rails-api'

# PostgreSQL database
# Cross-Origin Resource Sharing
gem 'rack-cors', require: 'rack/cors'

# Use PostgreSQL database
gem 'pg'

# To use ActiveModel has_secure_password
# Use ActiveModel has_secure_password
gem 'bcrypt', '~> 3.1.7'

# Helpo with JSON serialization
# Help with JSON serialization
gem 'active_model_serializers'

# Use pry over irb for rails console
group :development do
# Use pry over irb for rails console
gem 'pry-rails'
gem 'byebug'
end

# To use Jbuilder templates for JSON
# gem 'jbuilder'

# Use unicorn as the app server
# gem 'unicorn'

# Deploy with Capistrano
# gem 'capistrano', :group => :development

# To use debugger
# gem 'ruby-debug19', :require => 'ruby-debug'
@@ -1,104 +1,106 @@
GEM
remote: https://rubygems.org/
specs:
actionmailer (4.2.3)
actionpack (= 4.2.3)
actionview (= 4.2.3)
activejob (= 4.2.3)
actionmailer (4.2.4)
actionpack (= 4.2.4)
actionview (= 4.2.4)
activejob (= 4.2.4)
mail (~> 2.5, >= 2.5.4)
rails-dom-testing (~> 1.0, >= 1.0.5)
actionpack (4.2.3)
actionview (= 4.2.3)
activesupport (= 4.2.3)
actionpack (4.2.4)
actionview (= 4.2.4)
activesupport (= 4.2.4)
rack (~> 1.6)
rack-test (~> 0.6.2)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
actionview (4.2.3)
activesupport (= 4.2.3)
actionview (4.2.4)
activesupport (= 4.2.4)
builder (~> 3.1)
erubis (~> 2.7.0)
rails-dom-testing (~> 1.0, >= 1.0.5)
rails-html-sanitizer (~> 1.0, >= 1.0.2)
active_model_serializers (0.9.3)
activemodel (>= 3.2)
activejob (4.2.3)
activesupport (= 4.2.3)
activejob (4.2.4)
activesupport (= 4.2.4)
globalid (>= 0.3.0)
activemodel (4.2.3)
activesupport (= 4.2.3)
activemodel (4.2.4)
activesupport (= 4.2.4)
builder (~> 3.1)
activerecord (4.2.3)
activemodel (= 4.2.3)
activesupport (= 4.2.3)
activerecord (4.2.4)
activemodel (= 4.2.4)
activesupport (= 4.2.4)
arel (~> 6.0)
activesupport (4.2.3)
activesupport (4.2.4)
i18n (~> 0.7)
json (~> 1.7, >= 1.7.7)
minitest (~> 5.1)
thread_safe (~> 0.3, >= 0.3.4)
tzinfo (~> 1.1)
arel (6.0.2)
arel (6.0.3)
bcrypt (3.1.10)
builder (3.2.2)
byebug (6.0.2)
coderay (1.1.0)
erubis (2.7.0)
globalid (0.3.5)
globalid (0.3.6)
activesupport (>= 4.1.0)
i18n (0.7.0)
json (1.8.3)
loofah (2.0.2)
loofah (2.0.3)
nokogiri (>= 1.5.9)
mail (2.6.3)
mime-types (>= 1.16, < 3)
method_source (0.8.2)
mime-types (2.6.1)
mime-types (2.6.2)
mini_portile (0.6.2)
minitest (5.7.0)
minitest (5.8.2)
nokogiri (1.6.6.2)
mini_portile (~> 0.6.0)
pg (0.18.2)
pry (0.10.1)
pg (0.18.3)
pry (0.10.3)
coderay (~> 1.1.0)
method_source (~> 0.8.1)
slop (~> 3.4)
pry-rails (0.3.4)
pry (>= 0.9.10)
rack (1.6.4)
rack-cors (0.4.0)
rack-test (0.6.3)
rack (>= 1.0)
rails (4.2.3)
actionmailer (= 4.2.3)
actionpack (= 4.2.3)
actionview (= 4.2.3)
activejob (= 4.2.3)
activemodel (= 4.2.3)
activerecord (= 4.2.3)
activesupport (= 4.2.3)
rails (4.2.4)
actionmailer (= 4.2.4)
actionpack (= 4.2.4)
actionview (= 4.2.4)
activejob (= 4.2.4)
activemodel (= 4.2.4)
activerecord (= 4.2.4)
activesupport (= 4.2.4)
bundler (>= 1.3.0, < 2.0)
railties (= 4.2.3)
railties (= 4.2.4)
sprockets-rails
rails-api (0.4.0)
actionpack (>= 3.2.11)
railties (>= 3.2.11)
rails-deprecated_sanitizer (1.0.3)
activesupport (>= 4.2.0.alpha)
rails-dom-testing (1.0.6)
rails-dom-testing (1.0.7)
activesupport (>= 4.2.0.beta, < 5.0)
nokogiri (~> 1.6.0)
rails-deprecated_sanitizer (>= 1.0.1)
rails-html-sanitizer (1.0.2)
loofah (~> 2.0)
railties (4.2.3)
actionpack (= 4.2.3)
activesupport (= 4.2.3)
railties (4.2.4)
actionpack (= 4.2.4)
activesupport (= 4.2.4)
rake (>= 0.8.7)
thor (>= 0.18.1, < 2.0)
rake (10.4.2)
slop (3.6.0)
sprockets (3.2.0)
rack (~> 1.0)
sprockets-rails (2.3.2)
sprockets (3.4.0)
rack (> 1, < 3)
sprockets-rails (2.3.3)
actionpack (>= 3.0)
activesupport (>= 3.0)
sprockets (>= 2.8, < 4.0)
@@ -113,9 +115,11 @@ PLATFORMS
DEPENDENCIES
active_model_serializers
bcrypt (~> 3.1.7)
byebug
pg
pry-rails
rails (= 4.2.3)
rack-cors
rails (= 4.2.4)
rails-api

BUNDLED WITH
File renamed without changes.
@@ -1,23 +1,38 @@
#
class ApplicationController < ActionController::API
include ActionController::HttpAuthentication::Token::ControllerMethods
include ActionController::Serialization

rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
# Defaults for API requests
before_action :api_request_settings
def api_request_settings
request.format = :json
end

# Use Token Authentication
include ActionController::HttpAuthentication::Token::ControllerMethods
before_action :authenticate

private

attr_reader :current_user

def authenticate
authenticate_or_request_with_http_token do |token, _options|
@current_user = User.find_by token: token
@current_user = authenticate_or_request_with_http_token do |token, _opts|
User.find_by token: token
end
end
# Controllers can use this to authorize actions
attr_reader :current_user

# Require SSL for deployed applications
force_ssl if: :ssl_configured?
def ssl_configured?
!Rails.env.development?
end

# Use enhanced JSON serialization
include ActionController::Serialization

# return 404 for failed search by id
rescue_from ActiveRecord::RecordNotFound, with: :record_not_found
def record_not_found
render json: { message: 'Not Found' }, status: :not_found
end

# Restrict visibility of these methods
private :authenticate, :current_user, :record_not_found
private :ssl_configured?, :api_request_settings
end
@@ -0,0 +1,43 @@
#
class AuthController < ApplicationController
skip_before_action :authenticate, only: [:login, :register]

# POST /login
def login
@user = User.authenticate credentials[:email], credentials[:password]
if @user
render json: @user, serializer: LoginUserSerializer, root: 'user'
else
head :unauthorized
end
end

# POST /register
def register
@user = User.create(credentials)

if @user.valid?
render json: @user, status: :created, location: @user
else
render json: @user.errors, status: :unprocessable_entity
end
end

# DELETE /logout/1
def logout
if current_user == User.find(params[:id])
current_user.logout
head :no_content
else
head :unauthorized
end
end

def credentials
params.require(:credentials).permit(:email,
:password,
:password_confirmation)
end

private :credentials
end
@@ -1,24 +1,24 @@
#
class BooksController < OpenReadController
before_action :set_book, only: [:show, :update, :destroy]
before_action :set_book, only: [:update, :destroy]

# GET /books
# GET /books.json
def index
@books = Book.all

render json: @books
end

# GET /books/1
# GET /books/1.json
def show
@book = Book.find(params[:id])

render json: @book
end

# POST /books
# POST /books.json
def create
@book = Book.new(book_params)
@book = current_user.books.new(book_params)

if @book.save
render json: @book, status: :created, location: @book
@@ -27,11 +27,8 @@ def create
end
end

# PATCH/PUT /books/1
# PATCH/PUT /books/1.json
# PATCH /books/1
def update
@book = Book.find(params[:id])

if @book.update(book_params)
head :no_content
else
@@ -40,15 +37,14 @@ def update
end

# DELETE /books/1
# DELETE /books/1.json
def destroy
@book.destroy

head :no_content
end

def set_book
@book = Book.find(params[:id])
@book = current_user.books.find(params[:id])
end

def book_params
@@ -1,18 +1,18 @@
# Allow READ_ACTIONS access without authentication
class OpenReadController < ApplicationController
# for access to token_and_options method
TokenAuth = ActionController::HttpAuthentication::Token
# Allow unauthenticated access to these actions
READ_ACTIONS = [:index, :show]
skip_before_action :authenticate, only: READ_ACTIONS
before_action :set_current_user, only: READ_ACTIONS

private

# set current_user if unset and token present
# but set current_user if a token is present
before_action :set_current_user, only: READ_ACTIONS
def set_current_user
return if current_user
tok_opts = TokenAuth.token_and_options(request)
@current_user =
User.find_by(token: tok_opts.first) if tok_opts
# for access to authenticate method
t = ActionController::HttpAuthentication::Token
@current_user = t.authenticate(self) do |token, _opts|
User.find_by(token: token)
end
end

private :set_current_user
end
Oops, something went wrong.

0 comments on commit 937a981

Please sign in to comment.