Class: Heya::Campaigns::Base

Inherits:
Object
  • Object
show all
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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBase

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

#stepsObject

Returns the value of attribute steps.



80
81
82
# File 'lib/heya/campaigns/base.rb', line 80

def steps
  @steps
end

Class Method Details

.default(**params) ⇒ Object



110
111
112
# File 'lib/heya/campaigns/base.rb', line 110

def default(**params)
  self.__defaults = __defaults.merge(params).freeze
end

.find(_id) ⇒ Object



104
105
106
# File 'lib/heya/campaigns/base.rb', line 104

def find(_id)
  instance
end

.inherited(campaign) ⇒ Object



98
99
100
101
102
# File 'lib/heya/campaigns/base.rb', line 98

def inherited(campaign)
  Heya.register_campaign(campaign)
  Heya.unregister_campaign(campaign.superclass)
  super
end

.segment(arg = nil, &block) ⇒ Object



122
123
124
125
126
127
128
# File 'lib/heya/campaigns/base.rb', line 122

def segment(arg = nil, &block)
  if block_given?
    self.__segments = ([block] | __segments).freeze
  elsif arg
    self.__segments = ([arg] | __segments).freeze
  end
end

.step(name, **opts, &block) ⇒ Object



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
# File 'lib/heya/campaigns/base.rb', line 130

def step(name, **opts, &block)
  if block_given?
    opts[:block] ||= block
    opts[:action] ||= Actions::Block
  end

  opts =
    STEP_ATTRS
      .merge(Heya.config.campaigns.default_options)
      .merge(__defaults)
      .merge(opts)

  attrs = opts.select { |k, _| STEP_ATTRS.key?(k) }
  attrs[:params] = opts.reject { |k, _| STEP_ATTRS.key?(k) }.stringify_keys
  attrs[:id] = "#{self.name}/#{name}"
  attrs[:name] = name.to_s
  attrs[:position] = steps.size
  attrs[:campaign] = instance

  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

.user_type(value = nil) ⇒ Object



114
115
116
117
118
119
120
# File 'lib/heya/campaigns/base.rb', line 114

def user_type(value = nil)
  if value.present?
    self.__user_type = value.is_a?(String) ? value.to_s : value.name
  end

  __user_type || Heya.config.user_type
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
# 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

  if restart
    CampaignReceipt
      .where(user: user, step_gid: steps.map(&:gid))
      .delete_all
  end

  membership.create! do |m|
    m.concurrent = concurrent
  end

  if send_now && (step = steps.first) && step.wait <= 0
    Scheduler.process(self, step, user)
  end

  true
end

#gidObject

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



76
77
78
# File 'lib/heya/campaigns/base.rb', line 76

def handle_exception(exception)
  rescue_with_handler(exception) || raise(exception)
end

#remove(user) ⇒ Object



55
56
57
58
# File 'lib/heya/campaigns/base.rb', line 55

def remove(user)
  CampaignMembership.where(user: user, campaign_gid: gid).delete_all
  true
end

#user_classObject



72
73
74
# File 'lib/heya/campaigns/base.rb', line 72

def user_class
  @user_class ||= self.class.user_type.constantize
end

#usersObject



60
61
62
63
64
65
66
67
68
69
70
# File 'lib/heya/campaigns/base.rb', line 60

def users
  base_class = user_class.base_class
  user_class
    .joins(
      sanitize_sql_array([
        "inner join heya_campaign_memberships on heya_campaign_memberships.user_type = ? and heya_campaign_memberships.user_id = #{base_class.table_name}.id and heya_campaign_memberships.campaign_gid = ?",
        base_class.name,
        gid
      ])
    ).all
end