-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add block yielding to RegistrationsController
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
Showing
8 changed files
with
203 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
test/controllers/custom/custom_registrations_controller_test.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
33
test/dummy/app/controllers/custom/registrations_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
test/dummy/db/migrate/20150409095712_devise_token_auth_create_nice_users.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') %> |