Skip to content

Commit

Permalink
Get list & check for user(s) created by invite irrespective of status
Browse files Browse the repository at this point in the history
  • Loading branch information
swapnilchincholkar committed Apr 17, 2015
1 parent 3d64d78 commit 8b70e41
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
7 changes: 5 additions & 2 deletions README.rdoc
Original file line number Diff line number Diff line change
Expand Up @@ -256,9 +256,11 @@ You can add :skip_invitation to attributes hash if skip_invitation is added to a
User.invite!(:email => "new_user@example.com", :name => "John Doe", :skip_invitation => true)
# => the record will be created, but the invitation email will not be sent

Skip_invitation skips sending the email, but sets invitation_token, so invited_to_sign_up? on the
Skip_invitation skips sending the email, but sets invitation_token, so <tt>invited_to_sign_up?</tt> on the
resulting user returns true.

To check if a perticular user is created by invitation, irrespective to state of invitation one can use <tt>created_by_invite?</tt>

**Warning**

When using skip_invitation you must send the email with the user object instance that generated the tokens, as
Expand Down Expand Up @@ -297,8 +299,9 @@ The callbacks support all options and arguments available to the standard callba

A pair of scopes to find those users that have accepted, and those that have not accepted, invitations are defined:

User.invitation_accepted # => returns all Users for whom the invitation_accepted_at attribute is not nil
User.invitation_accepted # => returns all Users for whom the invitation_accepted_at attribute is not nil
User.invitation_not_accepted # => returns all Users for whom the invitation_accepted_at attribute is nil
User.created_by_invite # => returns all Users who are created by invitations, irrespective to invitation status

== Integration in a Rails application

Expand Down
7 changes: 7 additions & 0 deletions lib/devise_invitable/model.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,11 @@ module Invitable

scope :no_active_invitation, lambda { where(:invitation_token => nil) }
if defined?(Mongoid) && defined?(Mongoid::Document) && self < Mongoid::Document
scope :created_by_invite, lambda { where(:invitation_sent_at.ne => nil) }
scope :invitation_not_accepted, lambda { where(:invitation_accepted_at => nil, :invitation_token.ne => nil) }
scope :invitation_accepted, lambda { where(:invitation_accepted_at.ne => nil) }
else
scope :created_by_invite, lambda { where(arel_table[:invitation_sent_at].not_eq(nil)) }
scope :invitation_not_accepted, lambda { where(arel_table[:invitation_token].not_eq(nil)).where(:invitation_accepted_at => nil) }
scope :invitation_accepted, lambda { where(arel_table[:invitation_accepted_at].not_eq(nil)) }

Expand Down Expand Up @@ -85,6 +87,11 @@ def accept_invitation!
end
end

# Verify wheather a user is created by invitation, irrespective to invitation status
def created_by_invite?
invitation_sent_at.present?
end

# Verifies whether a user has been invited or not
def invited_to_sign_up?
persisted? && invitation_token.present?
Expand Down
25 changes: 22 additions & 3 deletions test/models/invitable_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -431,6 +431,21 @@ def setup
user.reload
assert !user.valid_password?('new_password')
end

test 'should check if created by invitation' do
user = User.invite!(:email => "valid@email.com")
assert user.created_by_invite?

invited_user = User.accept_invitation!(
:invitation_token => Thread.current[:token],
:password => 'new_password',
:password_confirmation => 'new_password',
:username => 'a'
)
user.reload
assert user.created_by_invite?
end


test 'should set other attributes on accepting invitation' do
user = new_user(:password => nil, :password_confirmation => nil)
Expand Down Expand Up @@ -611,19 +626,23 @@ def assert_callbacks_status(user, fired)
assert !user.errors.empty?
end

test "should count accepted and not accepted invitations" do
test "should count invited, created_by_invite, accepted and not accepted invitations" do
assert_equal 0, User.invitation_not_accepted.count
assert_equal 0, User.invitation_accepted.count
assert_equal 0, User.created_by_invite.count

User.invite!(:email => "invalid@email.com")
User.invite!(:email => "another_invalid@email.com")
user = User.invite!(:email => "valid@email.com")

assert_equal 2, User.invitation_not_accepted.count
assert_equal 3, User.invitation_not_accepted.count
assert_equal 0, User.invitation_accepted.count
assert_equal 3, User.created_by_invite.count

user.accept_invitation!
assert_equal 1, User.invitation_not_accepted.count
assert_equal 2, User.invitation_not_accepted.count
assert_equal 1, User.invitation_accepted.count
assert_equal 3, User.created_by_invite.count
end

test "should preserve return values of Devise::Recoverable#reset_password!" do
Expand Down

0 comments on commit 8b70e41

Please sign in to comment.