diff --git a/models/discussion.rb b/models/discussion.rb index f35189fd..b5d0757e 100644 --- a/models/discussion.rb +++ b/models/discussion.rb @@ -22,6 +22,8 @@ class Discussion #this is the slug for the url key :slug, String + key :subscribed_users, Array + timestamps! before_save :make_slug @@ -55,6 +57,10 @@ def forum_description end end + def create_subscription! email + subscribed_users << email + end + private def make_slug self.slug = self.title.to_slug diff --git a/models/reply.rb b/models/reply.rb index d6682546..27859625 100644 --- a/models/reply.rb +++ b/models/reply.rb @@ -13,8 +13,14 @@ class Reply #we need to make sure we have an author validate_on_create :author_check + after_save :send_subscription_notice + private + def send_subscription_notice + _root_document.create_subscription! :author_email + end + def author_check if author.nil? errors.add(:author, "Someone must have written this reply!") diff --git a/spec/acceptance/programs_spec.rb b/spec/acceptance/programs_spec.rb index ad527f97..e0f92a02 100644 --- a/spec/acceptance/programs_spec.rb +++ b/spec/acceptance/programs_spec.rb @@ -15,7 +15,7 @@ page.should have_content "Program created" page.should have_content "Hello world" - page.should have_content "By steve" + page.should have_content "By #{@hacker.username}" page.should have_content "puts" end diff --git a/spec/acceptance/subscription_spec.rb b/spec/acceptance/subscription_spec.rb new file mode 100644 index 00000000..9b234959 --- /dev/null +++ b/spec/acceptance/subscription_spec.rb @@ -0,0 +1,55 @@ +require File.dirname(__FILE__) + '/acceptance_helper' + +feature "Subscriptions" do + + scenario "Subscribing to a thread" do + +# Given I've commented in a thread + thread = Factory(:discussion) + me = Factory(:hacker) + reply = Factory(:reply, :author => me.username, :author_email => me.email) + thread.replies << reply + + Pony.should_receive(:deliver) do |mail| + mail.to.should == [ me.email ] + mail.body.to_s.should =~ /\/forums\/#{thread.forum}\/#{thread.slug}/ + end + +# When someone else makes a comment + somebody = Factory(:hacker) + log_in somebody + visit "/forums/#{thread.forum}/#{thread.slug}" + click_link "Reply" + fill_in "Body", :with => "Here's my take on things: Dream big!" + click_button "Create Reply" + +# Then I should receive an email +# (see pony block above) + +# And it should have a link to that thread +# (see pony block above) + + end + +# Scenario: Unsubscribing from a thread +# Given I've subscribed to a thread +# And I'm on the page for that thread +# When I click the unsubscribe link +# And someone makes a comment +# Then I should not receive an email + +# Scenario: Subscribing to a forum +# Given I'm on the index page for a forum +# When I click the subscription link +# And someone makes a new thread in that forum +# Then I should receive an email +# And it should have a link to that forum + +# Scenario: Unsubscribing from a forum +# Given I've subscribed to a forum +# And I'm on the index page for that forum +# When I click the unsubscribe link +# And someone makes a new thread in that forum +# Then I should not receive an email + +end diff --git a/spec/discussion_spec.rb b/spec/discussion_spec.rb new file mode 100644 index 00000000..f8a8669d --- /dev/null +++ b/spec/discussion_spec.rb @@ -0,0 +1,14 @@ +describe Discussion do + + describe "#create_subscription!" do + + it "adds a new email to the subscription list" do + discussion = Factory(:discussion) + discussion.subscribed_users.count.should == 0 + discussion.create_subscription! "somebody@example.com" + discussion.subscribed_users.count.should == 1 + end + + end + +end diff --git a/spec/factories.rb b/spec/factories.rb index 0aae7e2d..5728e81c 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -6,9 +6,13 @@ "user#{n}@example.com" end +Factory.sequence :username do |n| + "user#{n}" +end + #this factory defines a hacker Factory.define :hacker do |u| - u.username "steve" + u.username { Factory.next(:username)} u.email { Factory.next(:email) } u.password "foobar" #we need to set the password_confirmation to the same value as the password diff --git a/spec/reply_spec.rb b/spec/reply_spec.rb new file mode 100644 index 00000000..14d94527 --- /dev/null +++ b/spec/reply_spec.rb @@ -0,0 +1,15 @@ +describe Reply do + + describe "subscriptions" do + it "adds one to its Discussion upon creation" do + email = "someone@example.com" + @discussion = Factory(:discussion) + @discussion.should_receive(:create_subscription!) + @discussion.replies << Factory(:reply, :author_email => email) + @discussion.save + end + + it "triggers an email to others upon creation" + end + +end