digest interfaces returning empty objects (except for id) interface needed correction

This commit is contained in:
Sam 2013-08-28 10:44:13 +10:00
parent b6028c39d7
commit c0f610daf6
2 changed files with 15 additions and 16 deletions

View File

@ -5,15 +5,14 @@ module Jobs
recurrence { daily.hour_of_day(6) } recurrence { daily.hour_of_day(6) }
def execute(args) def execute(args)
target_users.each do |u| target_user_ids.each do |user_id|
Jobs.enqueue(:user_email, type: :digest, user_id: u.id) Jobs.enqueue(:user_email, type: :digest, user_id: user_id)
end end
end end
def target_users def target_user_ids
# Users who want to receive emails and haven't been emailed in the last day # Users who want to receive emails and haven't been emailed in the last day
query = User.select(:id) query = User.where(email_digests: true, active: true)
.where(email_digests: true, active: true)
.where("COALESCE(last_emailed_at, '2010-01-01') <= CURRENT_TIMESTAMP - ('1 DAY'::INTERVAL * digest_after_days)") .where("COALESCE(last_emailed_at, '2010-01-01') <= CURRENT_TIMESTAMP - ('1 DAY'::INTERVAL * digest_after_days)")
.where("COALESCE(last_seen_at, '2010-01-01') <= CURRENT_TIMESTAMP - ('1 DAY'::INTERVAL * digest_after_days)") .where("COALESCE(last_seen_at, '2010-01-01') <= CURRENT_TIMESTAMP - ('1 DAY'::INTERVAL * digest_after_days)")
@ -22,7 +21,7 @@ module Jobs
query = query.where("approved OR moderator OR admin") query = query.where("approved OR moderator OR admin")
end end
query query.pluck(:id)
end end
end end

View File

@ -10,33 +10,33 @@ describe Jobs::EnqueueDigestEmails do
let!(:user_no_digests) { Fabricate(:active_user, email_digests: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) } let!(:user_no_digests) { Fabricate(:active_user, email_digests: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) }
it "doesn't return users with email disabled" do it "doesn't return users with email disabled" do
Jobs::EnqueueDigestEmails.new.target_users.include?(user_no_digests).should be_false Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_no_digests.id).should be_false
end end
end end
context 'unapproved users' do context 'unapproved users' do
Given!(:unapproved_user) { Fabricate(:active_user, approved: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) } Given!(:unapproved_user) { Fabricate(:active_user, approved: false, last_emailed_at: 8.days.ago, last_seen_at: 10.days.ago) }
When { SiteSetting.stubs(:must_approve_users?).returns(true) } When { SiteSetting.stubs(:must_approve_users?).returns(true) }
Then { expect(Jobs::EnqueueDigestEmails.new.target_users.include?(unapproved_user)).to eq(false) } Then { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(false) }
# As a moderator # As a moderator
And { unapproved_user.update_column(:moderator, true) } And { unapproved_user.update_column(:moderator, true) }
And { expect(Jobs::EnqueueDigestEmails.new.target_users.include?(unapproved_user)).to eq(true) } And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) }
# As an admin # As an admin
And { unapproved_user.update_attributes(admin: true, moderator: false) } And { unapproved_user.update_attributes(admin: true, moderator: false) }
And { expect(Jobs::EnqueueDigestEmails.new.target_users.include?(unapproved_user)).to eq(true) } And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) }
# As an approved user # As an approved user
And { unapproved_user.update_attributes(admin: false, moderator: false, approved: true ) } And { unapproved_user.update_attributes(admin: false, moderator: false, approved: true ) }
And { expect(Jobs::EnqueueDigestEmails.new.target_users.include?(unapproved_user)).to eq(true) } And { expect(Jobs::EnqueueDigestEmails.new.target_user_ids.include?(unapproved_user.id)).to eq(true) }
end end
context 'recently emailed' do context 'recently emailed' do
let!(:user_emailed_recently) { Fabricate(:active_user, last_emailed_at: 6.days.ago) } let!(:user_emailed_recently) { Fabricate(:active_user, last_emailed_at: 6.days.ago) }
it "doesn't return users who have been emailed recently" do it "doesn't return users who have been emailed recently" do
Jobs::EnqueueDigestEmails.new.target_users.include?(user_emailed_recently).should be_false Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_emailed_recently.id).should be_false
end end
end end
@ -44,7 +44,7 @@ describe Jobs::EnqueueDigestEmails do
let!(:inactive_user) { Fabricate(:user) } let!(:inactive_user) { Fabricate(:user) }
it "doesn't return users who have been emailed recently" do it "doesn't return users who have been emailed recently" do
Jobs::EnqueueDigestEmails.new.target_users.include?(inactive_user).should be_false Jobs::EnqueueDigestEmails.new.target_user_ids.include?(inactive_user.id).should be_false
end end
end end
@ -53,7 +53,7 @@ describe Jobs::EnqueueDigestEmails do
let!(:user_visited_today) { Fabricate(:active_user, last_seen_at: 6.days.ago) } let!(:user_visited_today) { Fabricate(:active_user, last_seen_at: 6.days.ago) }
it "doesn't return users who have been emailed recently" do it "doesn't return users who have been emailed recently" do
Jobs::EnqueueDigestEmails.new.target_users.include?(user_visited_today).should be_false Jobs::EnqueueDigestEmails.new.target_user_ids.include?(user_visited_today.id).should be_false
end end
end end
@ -62,7 +62,7 @@ describe Jobs::EnqueueDigestEmails do
let!(:user) { Fabricate(:active_user) } let!(:user) { Fabricate(:active_user) }
it "returns the user" do it "returns the user" do
Jobs::EnqueueDigestEmails.new.target_users.should == [user] Jobs::EnqueueDigestEmails.new.target_user_ids.should == [user.id]
end end
end end
@ -73,7 +73,7 @@ describe Jobs::EnqueueDigestEmails do
let(:user) { Fabricate(:user) } let(:user) { Fabricate(:user) }
before do before do
Jobs::EnqueueDigestEmails.any_instance.expects(:target_users).returns([user]) Jobs::EnqueueDigestEmails.any_instance.expects(:target_user_ids).returns([user.id])
end end
it "enqueues the digest email job" do it "enqueues the digest email job" do