Class: Heya::Campaigns::Base
- Inherits:
-
Object
- Object
- Heya::Campaigns::Base
- Extended by:
- ActiveSupport::DescendantsTracker
- Includes:
- ActiveSupport::Rescuable, GlobalID::Identification, Singleton
- Defined in:
- lib/heya/campaigns/base.rb
Overview
Base provides a Ruby DSL for building campaign sequences. Multiple actions are supported; the default is email.
Instance Attribute Summary collapse
-
#steps ⇒ Object
Returns the value of attribute steps.
Class Method Summary collapse
- .default(**params) ⇒ Object
- .find(_id) ⇒ Object
- .inherited(campaign) ⇒ Object
- .segment(arg = nil, &block) ⇒ Object
- .step(name, **opts, &block) ⇒ Object
- .user_type(value = nil) ⇒ Object
Instance Method Summary collapse
- #add(user, restart: false, concurrent: false, send_now: true) ⇒ Object
-
#gid ⇒ Object
Returns String GlobalID.
- #handle_exception(exception) ⇒ Object
-
#initialize ⇒ Base
constructor
A new instance of Base.
- #remove(user) ⇒ Object
- #user_class ⇒ Object
Constructor Details
#initialize ⇒ Base
Returns a new instance of Base.
17 18 19 |
# File 'lib/heya/campaigns/base.rb', line 17 def initialize self.steps = [] end |
Instance Attribute Details
#steps ⇒ Object
Returns the value of attribute steps.
73 74 75 |
# File 'lib/heya/campaigns/base.rb', line 73 def steps @steps end |
Class Method Details
.default(**params) ⇒ Object
103 104 105 |
# File 'lib/heya/campaigns/base.rb', line 103 def default(**params) self.__defaults = __defaults.merge(params).freeze end |
.find(_id) ⇒ Object
97 98 99 |
# File 'lib/heya/campaigns/base.rb', line 97 def find(_id) instance end |
.inherited(campaign) ⇒ Object
91 92 93 94 95 |
# File 'lib/heya/campaigns/base.rb', line 91 def inherited(campaign) Heya.register_campaign(campaign) Heya.unregister_campaign(campaign.superclass) super end |
.segment(arg = nil, &block) ⇒ Object
115 116 117 118 119 120 121 |
# File 'lib/heya/campaigns/base.rb', line 115 def segment(arg = nil, &block) if block self.__segments = ([block] | __segments).freeze elsif arg self.__segments = ([arg] | __segments).freeze end end |
.step(name, **opts, &block) ⇒ Object
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/heya/campaigns/base.rb', line 123 def step(name, **opts, &block) if block opts[:block] ||= block opts[:action] ||= Actions::Block end opts = STEP_ATTRS .merge(Heya.config.campaigns.) .merge(__defaults) .merge(opts) attrs = opts.select { |k, _| STEP_ATTRS.key?(k) } attrs[:id] = "#{self.name}/#{name}" attrs[:name] = name.to_s attrs[:campaign] = instance attrs[:position] = steps.size attrs[:params] = opts.reject { |k, _| STEP_ATTRS.key?(k) }.stringify_keys step = Step.new(**attrs) method_name = :"#{step.name.underscore}" raise "Invalid step name: #{step.name}\n Step names must not conflict with method names on Heya::Campaigns::Base" if respond_to?(method_name) define_singleton_method method_name do |user| step.action.new(user: user, step: step).build end steps << step step end |
Instance Method Details
#add(user, restart: false, concurrent: false, send_now: true) ⇒ Object
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/heya/campaigns/base.rb', line 29 def add(user, restart: false, concurrent: false, send_now: true) return false unless Heya.in_segments?(user, *__segments) membership = CampaignMembership.where(user: user, campaign_gid: gid) if membership.exists? return false unless restart membership.delete_all end receipts = CampaignReceipt .where(user: user, step_gid: steps.map(&:gid)) receipts.delete_all if restart last_sent_at = receipts.maximum(:created_at) receipt_gids = receipts.map(&:step_gid) if (step = steps.find { |s| receipt_gids.exclude?(s.gid) }) membership.create! do |m| m.concurrent = concurrent m.step_gid = step.gid m.last_sent_at = last_sent_at end if send_now && step.wait == 0 Scheduler.new.run(user: user) end end true end |
#gid ⇒ Object
Returns String GlobalID.
25 26 27 |
# File 'lib/heya/campaigns/base.rb', line 25 def gid to_gid(app: "heya").to_s end |
#handle_exception(exception) ⇒ Object
69 70 71 |
# File 'lib/heya/campaigns/base.rb', line 69 def handle_exception(exception) rescue_with_handler(exception) || raise(exception) end |
#remove(user) ⇒ Object
60 61 62 63 |
# File 'lib/heya/campaigns/base.rb', line 60 def remove(user) CampaignMembership.where(user: user, campaign_gid: gid).delete_all true end |
#user_class ⇒ Object
65 66 67 |
# File 'lib/heya/campaigns/base.rb', line 65 def user_class @user_class ||= self.class.user_type.constantize end |