Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

send emails through resque #68

Merged
5 commits merged into from

1 participant

This page is out of date. Refresh to see the latest.
View
23 app/models/active_member_notificator.rb
@@ -0,0 +1,23 @@
+# This class is a Resque job to send email notifications to the active members.
+# Also it provides a helper to create and enqueue it.
+class ActiveMemberNotificator
+ @queue = :active_member_notificator
+
+ # This will be called by a worker when a job needs to be processed. It will
+ # send the given notification to all the current active members.
+ # @param [String, Symbol] notification The message that will be sended to {Notifications}.
+ # @param [Fixnum] motion_id The id of the motion that triggered the notification.
+ def self.perform(notification, motion_id)
+ motion = Motion.find(motion_id)
+ Membership.members_active_at(Time.now).each do |member|
+ Notifications.send(notification, motion, member).deliver
+ end
+ end
+
+ # Helper to create and enqueue this Resque job.
+ # @param [String, Symbol] notification The message that will be sended to {Notifications}.
+ # @param [Motion] motion_id The motion that triggered the notification.
+ def self.deliver(notification, motion)
+ Resque.enqueue(self, notification, motion.id)
+ end
+end
View
13 app/models/motion.rb
@@ -361,17 +361,16 @@ def schedule_updates
state.schedule_updates
end
- # @todo Refactor to send e-mail via queue
def send_email_on_create
- Membership.members_active_at(Time.now).each do |member|
- Notifications.motion_created(self, member).deliver
- end
+ send_email :motion_created
end
def send_email_on_state_change
- Membership.members_active_at(Time.now).each do |member|
- Notifications.motion_state_changed(self, member).deliver
- end
+ send_email :motion_state_changed
+ end
+
+ def send_email(notification)
+ ActiveMemberNotificator.deliver(notification, self)
end
def assign_state
View
2  db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20101231011732) do
+ActiveRecord::Schema.define(:version => 20101231011731) do
create_table "conflicts", :force => true do |t|
t.string "title"
View
23 spec/mailers/notifications_spec.rb
@@ -26,7 +26,7 @@
end
end
- describe "motion state changed" do
+ describe "motion_state_changed" do
before do
@motion = Factory.stub(:motion, :state_name => "discussing")
@member = Factory.stub(:member, :email => "member@email.com")
@@ -43,9 +43,24 @@
mail.subject.should include(@motion.title)
end
- it "should say a motion was created" do
- mail = Notifications.motion_state_changed(@motion, @member)
- mail.subject.should include(I18n.t("notifications.motion_state_changed.subjects.discussing"))
+ context "when the motion is in the discussing state" do
+ it "should say a motion has changed its state to discussing" do
+ motion = Factory.stub(:discussing_motion)
+ mail = Notifications.motion_state_changed(motion, @member)
+ mail.subject.should include(I18n.t("notifications.motion_state_changed.subjects.discussing"))
+ end
+ end
+
+ context "when the motion is in the voting state" do
+ it "should say a motion has changed its state to voting" do
+ motion = Factory.stub(:voting_motion)
+ mail = Notifications.motion_state_changed(motion, @member)
+ mail.subject.should include(I18n.t("notifications.motion_state_changed.subjects.voting"))
+ end
+ end
+
+ context "when the motion is in the closed state" do
+ it "should say a motion has changed its state to closed"
end
end
end
View
33 spec/models/active_member_notificator_spec.rb
@@ -0,0 +1,33 @@
+require 'spec_helper'
+
+describe ActiveMemberNotificator do
+ describe ".perform" do
+ before(:each) do
+ @mock_mail = mock('mock mail', :deliver => true)
+
+ @member_1 = Factory.stub(:member, :email => "member1@email.com")
+ @member_2 = Factory.stub(:member, :email => "member2@email.com")
+ Membership.stub(:members_active_at).and_return([@member_1, @member_2])
+
+ @motion = Factory.stub(:motion)
+ end
+
+ it "knows how to notify each member when a new motion is created" do
+ Motion.should_receive(:find).with(1).and_return(@motion)
+
+ Notifications.should_receive(:motion_created).with(@motion, @member_1).and_return(@mock_mail)
+ Notifications.should_receive(:motion_created).with(@motion, @member_2).and_return(@mock_mail)
+
+ ActiveMemberNotificator.perform(:motion_created, 1)
+ end
+
+ it "knows how to notify each member when a motion chages its state" do
+ Motion.should_receive(:find).with(1).and_return(@motion)
+
+ Notifications.should_receive(:motion_state_changed).with(@motion, @member_1).and_return(@mock_mail)
+ Notifications.should_receive(:motion_state_changed).with(@motion, @member_2).and_return(@mock_mail)
+
+ ActiveMemberNotificator.perform(:motion_state_changed, 1)
+ end
+ end
+end
View
50 spec/models/motion_spec.rb
@@ -160,50 +160,16 @@
end
describe "email notifications" do
- before(:each) do
- @member_1 = Factory.stub(:member, :email => "member1@email.com")
- @member_2 = Factory.stub(:member, :email => "member2@email.com")
- Membership.stub(:members_active_at).and_return([@member_1, @member_2])
-
- ActionMailer::Base.deliveries = []
- end
-
- describe "when a motion is created" do
- it "should send a notification to all members" do
- Factory.create(:motion)
- ActionMailer::Base.deliveries.should have(2).emails
- end
-
- it "should notify members the motion was created" do
- Factory.create(:motion)
- sample_message = ActionMailer::Base.deliveries.first
- sample_message.subject.should include(I18n.t('notifications.motion_created.subject'))
- end
+ it "should send a notification to all active members when a motion is created" do
+ motion = Factory.build(:motion)
+ ActiveMemberNotificator.should_receive(:deliver).with(:motion_created, motion)
+ motion.save
end
- describe "when a motion's state changes" do
- before do
- @motion = Factory.create(:motion)
- ActionMailer::Base.deliveries = []
- end
-
- # state_name == discussing
- it "should send a notification to all members" do
- @motion.discussing!
- ActionMailer::Base.deliveries.should have(2).emails
- end
-
- it "should notify members the motion has been seconded" do
- @motion.discussing!
- sample_message = ActionMailer::Base.deliveries.first
- sample_message.subject.should include(I18n.t('notifications.motion_state_changed.subjects.discussing'))
- end
-
- it "should notify members the motion has entered voting" do
- @motion.voting!
- sample_message = ActionMailer::Base.deliveries.first
- sample_message.subject.should include(I18n.t('notifications.motion_state_changed.subjects.voting'))
- end
+ it "should send a notification to all active members when a motion enters the discussing state" do
+ motion = Factory.create(:motion)
+ ActiveMemberNotificator.should_receive(:deliver).with(:motion_state_changed, motion)
+ motion.discussing!
end
describe "when a motion passes" do
Something went wrong with that request. Please try again.