diff --git a/app/models/communication_channel.rb b/app/models/communication_channel.rb index e3b7a15a463..44ff767d170 100644 --- a/app/models/communication_channel.rb +++ b/app/models/communication_channel.rb @@ -27,6 +27,7 @@ class CommunicationChannel < ActiveRecord::Base has_many :pseudonyms belongs_to :user has_many :notification_policies, :dependent => :destroy + has_many :delayed_messages has_many :messages belongs_to :access_token diff --git a/app/models/notification_policy.rb b/app/models/notification_policy.rb index 36f060d899c..4e59d28e13a 100644 --- a/app/models/notification_policy.rb +++ b/app/models/notification_policy.rb @@ -20,6 +20,7 @@ class NotificationPolicy < ActiveRecord::Base belongs_to :notification belongs_to :communication_channel + has_many :delayed_messages attr_accessible :notification, :communication_channel, :frequency, :notification_id, :communication_channel_id diff --git a/lib/notification_message_creator.rb b/lib/notification_message_creator.rb index c49c6ab985b..505a2ee0ee8 100644 --- a/lib/notification_message_creator.rb +++ b/lib/notification_message_creator.rb @@ -112,8 +112,7 @@ class NotificationMessageCreator def build_fallback_for(user) fallback_channel = immediate_channels_for(user).sort_by(&:path_type).first fallback_policy = fallback_channel.notification_policies.by('daily').where(:notification_id => nil).first - fallback_policy ||= NotificationPolicy.new(:communication_channel => fallback_channel, - :frequency => 'daily') + fallback_policy ||= fallback_channel.notification_policies.create!(frequency: 'daily') build_summary_for(user, fallback_policy) end @@ -125,15 +124,14 @@ class NotificationMessageCreator def build_summary_for(user, policy) message = user.messages.build(message_options_for(user)) message.parse!('summary') - delayed_message = DelayedMessage.new(:notification => @notification, - :notification_policy => policy, - :frequency => policy.frequency, - :communication_channel_id => policy.communication_channel_id, - :root_account_id => message.context_root_account.try(:id), - :linked_name => 'work on this link!!!', - :name_of_topic => message.subject, - :link => message.url, - :summary => message.body) + delayed_message = policy.delayed_messages.build(:notification => @notification, + :frequency => policy.frequency, + :communication_channel_id => policy.communication_channel_id, + :root_account_id => message.context_root_account.try(:id), + :linked_name => 'work on this link!!!', + :name_of_topic => message.subject, + :link => message.url, + :summary => message.body) delayed_message.context = @asset delayed_message.save! if Rails.env.test? delayed_message diff --git a/spec/lib/notification_message_creator_spec.rb b/spec/lib/notification_message_creator_spec.rb index 43ad89cfda2..c26bcd8fc91 100644 --- a/spec/lib/notification_message_creator_spec.rb +++ b/spec/lib/notification_message_creator_spec.rb @@ -388,15 +388,19 @@ describe NotificationMessageCreator do end end - it "should create policies on the user's shard" do + it "should create policies and summary messages on the user's shard" do @shard1.activate do @user = User.create! - @user.communication_channels.create!(path: "user@example.com").confirm! + @cc = @user.communication_channels.create!(path: "user@example.com") + @cc.confirm! end notification_model(category: 'TestWeekly') - creator = NotificationMessageCreator.new(@notification, nil) - creator.expects(:too_many_messages_for?).returns(false) - creator.send(:delayed_policies_for, @user).first.shard.should == @shard1 + Message.any_instance.stubs(:get_template).returns('template') + @cc.notification_policies.should be_empty + @cc.delayed_messages.should be_empty + NotificationMessageCreator.new(@notification, @user, :to_list => @user).create_message + @cc.notification_policies.should_not be_empty + @cc.delayed_messages.reload.should_not be_empty end end end