-
-
Notifications
You must be signed in to change notification settings - Fork 58
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Use inheritance to obey the Open/Closed Principle
* Uses a subclass to add unsubscribeable behavior * Demonstrates when subclasses are ugly
- Loading branch information
Showing
7 changed files
with
66 additions
and
25 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
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,13 @@ | ||
class UnsubscribeableInvitation < Invitation | ||
def deliver | ||
unless unsubscribed? | ||
super | ||
end | ||
end | ||
|
||
private | ||
|
||
def unsubscribed? | ||
Unsubscribe.where(email: recipient_email).exists? | ||
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
25 changes: 25 additions & 0 deletions
25
example_app/spec/models/unsubscribeable_invitation_spec.rb
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,25 @@ | ||
require 'spec_helper' | ||
|
||
describe UnsubscribeableInvitation, '#deliver' do | ||
include Rails.application.routes.url_helpers | ||
self.default_url_options = ActionMailer::Base.default_url_options | ||
|
||
it 'sends email notifications to new users' do | ||
invitation = deliver_invitation | ||
|
||
find_email(invitation.recipient_email). | ||
should have_body_text(invitation.message) | ||
end | ||
|
||
it 'sends nothing to users that have unsubscribed' do | ||
unsubscribe = create(:unsubscribe) | ||
deliver_invitation(recipient_email: unsubscribe.email) | ||
|
||
find_email(unsubscribe.email).should be_nil | ||
end | ||
|
||
def deliver_invitation(overrides = {}) | ||
InvitationDeliverer.new(UnsubscribeableInvitation). | ||
deliver_invitation(overrides) | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
class InvitationDeliverer | ||
include FactoryGirl::Syntax::Methods | ||
|
||
def initialize(factory) | ||
@factory = factory | ||
end | ||
|
||
def deliver_invitation(overrides = {}) | ||
attributes = { | ||
message: 'hello', | ||
survey: create(:survey) | ||
}.merge(overrides) | ||
invitation_attributes = create(:invitation, attributes).attributes | ||
|
||
@factory.new(invitation_attributes).tap do |invitation| | ||
invitation.deliver | ||
end | ||
end | ||
end |