Sha256: ac45f555204d58baab442939c241dfdb3d98b1877e2838261390bee7cea26e7d

Contents?: true

Size: 1.31 KB

Versions: 3

Compression:

Stored size: 1.31 KB

Contents

module DeviseInvitable::Controllers::Registrations
  def self.included(controller)
    controller.send :around_filter, :destroy_if_previously_invited, :only => :create
  end

  protected

  def destroy_if_previously_invited
    invitation_info = {}

    hash = params[resource_name]
    if resource_class.modules.include?(:invitable) && 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]
        resource.destroy
      end
    end

    # execute the action (create)
    yield
    # Note that the after_filter is executed at THIS position !

    # 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
    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.save!
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
devise_invitable-1.1.2 lib/devise_invitable/controllers/registrations.rb~
devise_invitable-1.1.1 lib/devise_invitable/controllers/registrations.rb~
devise_invitable-1.1.0 lib/devise_invitable/controllers/registrations.rb~