Skip to content

Commit

Permalink
Fix #203, keep invitation info on registering only for invitable reso…
Browse files Browse the repository at this point in the history
…urces
  • Loading branch information
scambra committed Apr 23, 2012
1 parent a5d20f7 commit f9255b7
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 15 deletions.
31 changes: 18 additions & 13 deletions lib/devise_invitable/controllers/registrations.rb
@@ -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
Expand Down
13 changes: 12 additions & 1 deletion test/functional/registrations_controller_test.rb
@@ -1,4 +1,5 @@
require 'test_helper'
require 'model_tests_helper'

class Devise::RegistrationsControllerTest < ActionController::TestCase
def setup
Expand Down Expand Up @@ -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
Expand All @@ -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
2 changes: 1 addition & 1 deletion test/rails_app/app/models/admin.rb
Expand Up @@ -15,6 +15,6 @@ class Admin < PARENT_MODEL_CLASS



devise :database_authenticatable, :validatable
devise :database_authenticatable, :validatable, :registerable
include DeviseInvitable::Inviter
end
3 changes: 3 additions & 0 deletions test/test_helper.rb
Expand Up @@ -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.