-
-
Save jimmycuadra/c3234352b3c4776ce132 to your computer and use it in GitHub Desktop.
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
# ... | |
config.active_record.observers = :comment_observer | |
# ... |
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
class CommentObserver < ActiveRecord::Observer | |
def after_create(comment) | |
Notification.new_comment(comment).deliver | |
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
require 'spec_helper' | |
describe CommentObserver do | |
it "sends a notification mail after a new comment is created" do | |
@comment = Factory.build(:comment) | |
Notification.should_receive(:new_comment).with(@comment) | |
@comment.save | |
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
class Notification < ActionMailer::Base | |
def new_comment(comment) | |
@comment = comment | |
mail(:to => ENV["ADMIN_EMAIL"], :from => ENV["ADMIN_EMAIL"], :subject => "New comment from #{@comment.name} on #{@comment.post.title}") | |
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
require "spec_helper" | |
describe Notification do | |
describe "#new_comment" do | |
before(:each) do | |
ENV["ADMIN_EMAIL"] = 'admin@example.com' | |
@comment = Factory(:comment) | |
@email = Notification.new_comment(@comment).deliver | |
end | |
it "queues a message for new comments" do | |
ActionMailer::Base.deliveries.should_not be_empty | |
end | |
it "sends email to and from the admin" do | |
@email.to.should include(ENV["ADMIN_EMAIL"]) | |
@email.from.should include(ENV["ADMIN_EMAIL"]) | |
end | |
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
Notification | |
#new_comment | |
queues a message for new comments | |
sends email to and from the admin | |
CommentObserver | |
sends a notification mail after a new comment is created (FAILED - 1) | |
Failures: | |
1) CommentObserver sends a notification mail after a new comment is created | |
Failure/Error: @comment.save | |
NoMethodError: | |
undefined method `deliver' for nil:NilClass | |
# ./app/models/comment_observer.rb:3:in `after_create' | |
# ./spec/models/comment_observer_spec.rb:7:in `block (2 levels) in <top (required)>' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment