This repository has been archived by the owner on Jan 30, 2018. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Extract EventInviter from Invitations controller
* Add validation for for invitation text to Invitation https://trello.com/c/WXLLlx9n
- Loading branch information
Jessie Young
committed
Aug 7, 2013
1 parent
452ac3a
commit 99a2c5e
Showing
8 changed files
with
155 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
class EventInviter | ||
def initialize(options = {}) | ||
@current_user = options[:current_user] | ||
@event = options[:event] | ||
@invitation_text = options[:invitation_text] | ||
@invitee_emails = options[:invitee_emails] | ||
end | ||
|
||
def valid? | ||
invitations.map(&:valid?).exclude? false | ||
end | ||
|
||
def send_invitations | ||
invitations.map do |invitation| | ||
invitation.invite | ||
end | ||
end | ||
|
||
def invalid_invitation_errors | ||
invalid_invitations = invitations.select(&:invalid?) | ||
invalid_invitations.first.errors.full_messages.join(', ') | ||
end | ||
|
||
private | ||
|
||
def invitations | ||
@invitations ||= @invitee_emails.map do |email| | ||
build_invitation_for(email) | ||
end | ||
end | ||
|
||
def build_invitation_for(email) | ||
Invitation.new( | ||
event: @event, | ||
invitation_text: @invitation_text, | ||
invitee: find_or_create_user(email), | ||
sender: @current_user | ||
) | ||
end | ||
|
||
def find_or_create_user(email) | ||
InviteeBuilder.new(email.strip, @event).find_user_by_email_or_create_guest | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require 'spec_helper' | ||
|
||
describe EventInviter, '#valid?' do | ||
it 'returns true if all invitations are valid' do | ||
current_user = build_stubbed(:user) | ||
event = build_stubbed(:event) | ||
invitation_text = 'Example text' | ||
invitee_emails = ['example1@example.com', 'example2@example.com'] | ||
options = { | ||
current_user: current_user, | ||
event: event, | ||
invitation_text: invitation_text, | ||
invitee_emails: invitee_emails | ||
} | ||
|
||
valid = EventInviter.new(options).valid? | ||
|
||
expect(valid).to be true | ||
end | ||
|
||
it 'returns false if all invitations are not valid' do | ||
current_user = build_stubbed(:user) | ||
event = build_stubbed(:event) | ||
invitation_text = 'Example text' | ||
invitee_emails = ['example1@example.com', 'example@example.com'] | ||
options = { | ||
current_user: current_user, | ||
event: event, | ||
invitee_emails: invitee_emails | ||
} | ||
|
||
valid = EventInviter.new(options).valid? | ||
|
||
expect(valid).to be false | ||
end | ||
end | ||
|
||
describe EventInviter, '#send_invitations' do | ||
it 'calls invite on each invitation' do | ||
invitation_stub = stub('invitation', :invite) | ||
Invitation.stubs(:new).returns(invitation_stub) | ||
current_user = build_stubbed(:user) | ||
event = build_stubbed(:event) | ||
invitation_text = 'Example text' | ||
invitee_emails = ['example1@example.com', 'example2@example.com'] | ||
options = { | ||
current_user: current_user, | ||
event: event, | ||
invitation_text: invitation_text, | ||
invitee_emails: invitee_emails | ||
} | ||
|
||
EventInviter.new(options).send_invitations | ||
|
||
expect(Invitation).to have_received(:new).twice | ||
expect(invitation_stub).to have_received(:invite).twice | ||
end | ||
end | ||
|
||
describe EventInviter, '#invalid_invitation_errors' do | ||
it 'returns the errors belonging to the first invalid invitation' do | ||
current_user = build_stubbed(:user) | ||
event = build_stubbed(:event) | ||
invitee_emails = ['example1@example.com', 'example2@example.com'] | ||
options = { | ||
current_user: current_user, | ||
event: event, | ||
invitee_emails: invitee_emails | ||
} | ||
|
||
errors = EventInviter.new(options).invalid_invitation_errors | ||
|
||
expect(errors).to eq "Invitation text can't be blank" | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters