Skip to content

Commit

Permalink
Add block yielding to RegistrationsController
Browse files Browse the repository at this point in the history
Allows you to make super simple additions to the
RegistrationsController without having to reimplement entire pieces of
functionality by passing a block to super.
  • Loading branch information
sgwilym committed Apr 9, 2015
1 parent dae8106 commit e6a0f88
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 1 deletion.
3 changes: 3 additions & 0 deletions app/controllers/devise_token_auth/registrations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ def create
# override email confirmation, must be sent manually from ctrl
resource_class.skip_callback("create", :after, :send_on_create_confirmation_instructions)
if @resource.save
yield @resource if block_given?

unless @resource.confirmed?
# user will require email authentication
Expand Down Expand Up @@ -93,6 +94,7 @@ def update
if @resource

if @resource.update_attributes(account_update_params)
yield @resource if block_given?
render json: {
status: 'success',
data: @resource.as_json
Expand All @@ -114,6 +116,7 @@ def update
def destroy
if @resource
@resource.destroy
yield @resource if block_given?

render json: {
status: 'success',
Expand Down
43 changes: 43 additions & 0 deletions test/controllers/custom/custom_registrations_controller_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
require 'test_helper'

class Custom::RegistrationsControllerTest < ActionDispatch::IntegrationTest

describe Custom::RegistrationsController do

setup do
@create_params = {
email: Faker::Internet.email,
password: "secret123",
password_confirmation: "secret123",
confirm_success_url: Faker::Internet.url,
unpermitted_param: '(x_x)'
}

@existing_user = nice_users(:confirmed_email_user)
@auth_headers = @existing_user.create_new_auth_token
@client_id = @auth_headers['client']

# ensure request is not treated as batch request
age_token(@existing_user, @client_id)
end

test "yield resource to block on create success" do
post '/nice_user_auth', @create_params
assert @controller.create_block_called?, "create failed to yield resource to provided block"
end

test "yield resource to block on update success" do
put '/nice_user_auth', {
nickname: "Ol' Sunshine-face",
}, @auth_headers
assert @controller.update_block_called?, "update failed to yield resource to provided block"
end

test "yield resource to block on destroy success" do
delete '/nice_user_auth', @auth_headers
assert @controller.destroy_block_called?, "update failed to yield resource to provided block"
end

end

end
33 changes: 33 additions & 0 deletions test/dummy/app/controllers/custom/registrations_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
class Custom::RegistrationsController < DeviseTokenAuth::RegistrationsController

def create
super do |resource|
@create_block_called = true
end
end

def update
super do |resource|
@update_block_called = true
end
end

def destroy
super do |resource|
@destroy_block_called = true
end
end

def create_block_called?
@create_block_called == true
end

def update_block_called?
@update_block_called == true
end

def destroy_block_called?
@destroy_block_called == true
end

end
7 changes: 7 additions & 0 deletions test/dummy/app/models/nice_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
class NiceUser < ActiveRecord::Base
# Include default devise modules.
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :trackable, :validatable,
:confirmable, :omniauthable
include DeviseTokenAuth::Concerns::User
end
4 changes: 4 additions & 0 deletions test/dummy/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
token_validations: 'overrides/token_validations'
}

mount_devise_token_auth_for 'NiceUser', at: 'nice_user_auth', controllers: {
registrations: 'custom/registrations'
}

mount_devise_token_auth_for 'OnlyEmailUser', at: 'only_email_auth', skip: [:omniauth_callbacks]

mount_devise_token_auth_for 'UnregisterableUser', at: 'unregisterable_user_auth', skip: [:registrations]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
class DeviseTokenAuthCreateNiceUsers < ActiveRecord::Migration
def change
create_table(:nice_users) do |t|
## Required
t.string :provider, :null => false
t.string :uid, :null => false, :default => ""

## Database authenticatable
t.string :encrypted_password, :null => false, :default => ""

## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at

## Rememberable
t.datetime :remember_created_at

## Trackable
t.integer :sign_in_count, :default => 0, :null => false
t.datetime :current_sign_in_at
t.datetime :last_sign_in_at
t.string :current_sign_in_ip
t.string :last_sign_in_ip

## Confirmable
t.string :confirmation_token
t.datetime :confirmed_at
t.datetime :confirmation_sent_at
t.string :unconfirmed_email # Only if using reconfirmable

## Lockable
# t.integer :failed_attempts, :default => 0, :null => false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at

## User Info
t.string :name
t.string :nickname
t.string :image
t.string :email

## Tokens
t.text :tokens

t.timestamps
end

add_index :nice_users, :email
add_index :nice_users, [:uid, :provider], :unique => true
add_index :nice_users, :reset_password_token, :unique => true
# add_index :nice_users, :confirmation_token, :unique => true
# add_index :nice_users, :unlock_token, :unique => true
end
end
31 changes: 30 additions & 1 deletion test/dummy/db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20141222053502) do
ActiveRecord::Schema.define(version: 20150409095712) do

create_table "evil_users", force: :cascade do |t|
t.string "email"
Expand Down Expand Up @@ -77,6 +77,35 @@
add_index "mangs", ["reset_password_token"], name: "index_mangs_on_reset_password_token", unique: true
add_index "mangs", ["uid", "provider"], name: "index_mangs_on_uid_and_provider", unique: true

create_table "nice_users", force: :cascade do |t|
t.string "provider", null: false
t.string "uid", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.integer "sign_in_count", default: 0, null: false
t.datetime "current_sign_in_at"
t.datetime "last_sign_in_at"
t.string "current_sign_in_ip"
t.string "last_sign_in_ip"
t.string "confirmation_token"
t.datetime "confirmed_at"
t.datetime "confirmation_sent_at"
t.string "unconfirmed_email"
t.string "name"
t.string "nickname"
t.string "image"
t.string "email"
t.text "tokens"
t.datetime "created_at"
t.datetime "updated_at"
end

add_index "nice_users", ["email"], name: "index_nice_users_on_email"
add_index "nice_users", ["reset_password_token"], name: "index_nice_users_on_reset_password_token", unique: true
add_index "nice_users", ["uid", "provider"], name: "index_nice_users_on_uid_and_provider", unique: true

create_table "only_email_users", force: :cascade do |t|
t.string "provider", null: false
t.string "uid", default: "", null: false
Expand Down
29 changes: 29 additions & 0 deletions test/fixtures/nice_users.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<% timestamp = DateTime.parse(2.weeks.ago.to_s).to_time.strftime("%F %T") %>
<% @email = Faker::Internet.email %>
confirmed_email_user:
uid: "<%= @email %>"
email: "<%= @email %>"
provider: 'email'
confirmed_at: '<%= timestamp %>'
created_at: '<%= timestamp %>'
updated_at: '<%= timestamp %>'
encrypted_password: <%= User.new.send(:password_digest, 'secret123') %>

<% @fb_email = Faker::Internet.email %>
duplicate_email_facebook_user:
uid: "<%= Faker::Number.number(10) %>"
email: "<%= @fb_email %>"
provider: 'facebook'
created_at: '<%= timestamp %>'
updated_at: '<%= timestamp %>'
confirmed_at: '<%= timestamp %>'
encrypted_password: <%= User.new.send(:password_digest, 'secret123') %>

<% @unconfirmed_email = Faker::Internet.email %>
unconfirmed_email_user:
uid: "<%= @unconfirmed_email %>"
email: "<%= @unconfirmed_email %>"
provider: 'email'
created_at: '<%= timestamp %>'
updated_at: '<%= timestamp %>'
encrypted_password: <%= User.new.send(:password_digest, 'secret123') %>

0 comments on commit e6a0f88

Please sign in to comment.