Skip to content

Commit

Permalink
FIX: inviting a user again after the first invite expires will create…
Browse files Browse the repository at this point in the history
… a new invite
  • Loading branch information
nlalonde committed Jan 21, 2014
1 parent e2c361f commit 1dbc1c5
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,9 @@
{{else}}
<td>{{unbound email}}</td>
<td colspan='6'>
{{#if expired}}
{{i18n user.invited.expired}}
{{/if}}
{{#if rescinded}}
{{i18n user.invited.rescinded}}
{{else}}
Expand Down
10 changes: 9 additions & 1 deletion app/models/invite.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,15 @@ def redeem
# Return the previously existing invite if already exists. Returns nil if the invite can't be created.
def self.invite_by_email(email, invited_by, topic=nil)
lower_email = Email.downcase(email)
invite = Invite.with_deleted.where('invited_by_id = ? and email = ?', invited_by.id, lower_email).first
invite = Invite.with_deleted
.where('invited_by_id = ? and email = ?', invited_by.id, lower_email)
.order('created_at DESC')
.first

if invite && invite.expired?
invite.destroy
invite = nil
end

if invite.blank?
invite = Invite.create(invited_by: invited_by, email: lower_email)
Expand Down
6 changes: 5 additions & 1 deletion app/serializers/invite_serializer.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
class InviteSerializer < ApplicationSerializer

attributes :email, :created_at, :redeemed_at
attributes :email, :created_at, :redeemed_at, :expired
has_one :user, embed: :objects, serializer: InvitedUserSerializer

def include_email?
!object.redeemed?
end

def expired
object.expired?
end

end
1 change: 1 addition & 0 deletions config/locales/client.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ en:
pending: "Pending Invites"
topics_entered: "Topics Viewed"
posts_read_count: "Posts Read"
expired: "This invite has expired."
rescind: "Remove Invitation"
rescinded: "Invite removed"
time_read: "Read Time"
Expand Down
9 changes: 9 additions & 0 deletions spec/models/invite_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,15 @@
it 'matches case sensitively for the local part' do
topic.invite_by_email(inviter, 'ICEKING@adventuretime.ooo').should_not == @invite
end

it 'returns a new invite if the other has expired' do
SiteSetting.stubs(:invite_expiry_days).returns(1)
@invite.created_at = 2.days.ago
@invite.save
new_invite = topic.invite_by_email(inviter, 'iceking@adventuretime.ooo')
new_invite.should_not == @invite
new_invite.should_not be_expired
end
end

context 'when adding to another topic' do
Expand Down

0 comments on commit 1dbc1c5

Please sign in to comment.