Skip to content

Commit

Permalink
Add Heya::Campaigns::Step#in_segment?
Browse files Browse the repository at this point in the history
Exposing this method through Step is useful for unit testing segments
without having to run the scheduler. cc #31
  • Loading branch information
joshuap committed Feb 9, 2020
1 parent 0916150 commit c83c02a
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/heya/campaigns/scheduler.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def self.process(campaign, step, user)
ActiveRecord::Base.transaction do
return if CampaignReceipt.where(user: user, step_gid: step.gid).exists?

if Heya.in_segments?(user, user.class.__heya_default_segment, campaign.segment, step.segment)
if step.in_segment?(user)
now = Time.now.utc
Queries::CampaignMembershipsForUpdate.call(campaign, user).update_all(last_sent_at: now)
CampaignReceipt.create!(user: user, step_gid: step.gid, sent_at: now)
Expand Down
4 changes: 4 additions & 0 deletions lib/heya/campaigns/step.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@ def self.find(id)
def gid
to_gid(app: "heya").to_s
end

def in_segment?(user)
Heya.in_segments?(user, user.class.__heya_default_segment, campaign.segment, segment)
end
end
end
end
25 changes: 25 additions & 0 deletions test/lib/heya/campaigns/step_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require "test_helper"

module Heya
module Campaigns
class StepTest < ActiveSupport::TestCase
test "it checks segment for user" do
campaign = Class.new(Base) {
step :one, segment: ->(c) { c.traits["name"] != "Jack" }
user_type "Contact"
def self.name
"TestCampaign"
end
}
step = campaign.steps.first
contact = contacts(:one)

assert step.in_segment?(contact)

contact.traits["name"] = "Jack"

refute step.in_segment?(contact)
end
end
end
end

0 comments on commit c83c02a

Please sign in to comment.