Permalink
Browse files

Fix #203, keep invitation info on registering only for invitable res…

…ources
  • Loading branch information...
scambra committed Apr 23, 2012
1 parent a5d20f7 commit f9255b7ccb34c23caa8a29e99b658cf4ac2c03df
@@ -1,35 +1,40 @@
module DeviseInvitable::Controllers::Registrations
def self.included(controller)
- controller.send :around_filter, :destroy_if_previously_invited, :only => :create
+ controller.send :around_filter, :keep_invitation_info, :only => :create
end
protected
def destroy_if_previously_invited
- invitation_info = {}
+ @invitation_info = {}
hash = params[resource_name]
if hash && hash[:email]
resource = resource_class.where(:email => hash[:email], :encrypted_password => '').first
if resource
- invitation_info[:invitation_sent_at] = resource[:invitation_sent_at]
- invitation_info[:invited_by_id] = resource[:invited_by_id]
- invitation_info[:invited_by_type] = resource[:invited_by_type]
+ @invitation_info[:invitation_sent_at] = resource[:invitation_sent_at]
+ @invitation_info[:invited_by_id] = resource[:invited_by_id]
+ @invitation_info[:invited_by_type] = resource[:invited_by_type]
resource.destroy
end
end
-
- # execute the action (create)
+ end
+
+ def keep_invitation_info
+ resource_invitable = resource_class.devise_modules.include?(:invitable)
+ destroy_if_previously_invited if resource_invitable
yield
- # Note that the after_filter is executed at THIS position !
-
+ reset_invitation_info if resource_invitable
+ end
+
+ def reset_invitation_info
# Restore info about the last invitation (for later reference)
# Reset the invitation_info only, if invited_by_id is still nil at this stage:
- resource = resource_class.where(:email => hash[:email], :invited_by_id => nil).first
+ resource = resource_class.where(:email => params[resource_name][:email], :invited_by_id => nil).first
if resource
- resource[:invitation_sent_at] = invitation_info[:invitation_sent_at]
- resource[:invited_by_id] = invitation_info[:invited_by_id]
- resource[:invited_by_type] = invitation_info[:invited_by_type]
+ resource[:invitation_sent_at] = @invitation_info[:invitation_sent_at]
+ resource[:invited_by_id] = @invitation_info[:invited_by_id]
+ resource[:invited_by_type] = @invitation_info[:invited_by_type]
resource.save!
end
end
@@ -1,4 +1,5 @@
require 'test_helper'
+require 'model_tests_helper'
class Devise::RegistrationsControllerTest < ActionController::TestCase
def setup
@@ -27,7 +28,7 @@ def setup
assert_blank @invitee.encrypted_password, "the password should be unset"
# sign_up the invitee
- post :create, :user => { :email => invitee_email, :password => "1password"}
+ post :create, :user => {:email => invitee_email, :password => "1password"}
@invitee = User.where(:email => invitee_email).first
assert_present @invitee.encrypted_password
@@ -36,4 +37,14 @@ def setup
assert_present @invitee.invited_by_id
assert_present @invitee.invited_by_type
end
+
+ test "not invitable resources can register" do
+ @request.env["devise.mapping"] = Devise.mappings[:admin]
+ invitee_email = "invitee@example.org"
+
+ post :create, :admin => {:email => invitee_email, :password => "1password"}
+
+ @invitee = Admin.where(:email => invitee_email).first
+ assert_present @invitee.encrypted_password
+ end
end
@@ -15,6 +15,6 @@ class Admin < PARENT_MODEL_CLASS
- devise :database_authenticatable, :validatable
+ devise :database_authenticatable, :validatable, :registerable
include DeviseInvitable::Inviter
end
View
@@ -18,3 +18,6 @@
class ActionDispatch::IntegrationTest
include Capybara::DSL
end
+class ActionController::TestCase
+ include Devise::TestHelpers
+end

0 comments on commit f9255b7

Please sign in to comment.