Class: DripperProxy

Inherits:
Object
  • Object
show all
Defined in:
lib/dripper_mail.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}, &block) ⇒ DripperProxy

Returns a new instance of DripperProxy.



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/dripper_mail.rb', line 43

def initialize(opts={}, &block)
  @scopes = []
  # if there's a parent, initialize all values to the parent values first
  # then override with children
  [:model, :mailer, :action, :wait, :wait_until].each do |method|
    parent = opts[:parent]
    if parent
      instance_variable_set "@#{method}", parent.send(method)
    end
  end

  #overwrite any defined options
  opts.each { |k,v| instance_variable_set("@#{k}", v) }
  if opts[:scope]
    @scopes << opts[:scope] 
  end

  @children = []
  instance_eval(&block) if block
  # only include complete ones in the registry
  if self.action && self.mailer && self.model
    Dripper.registry << self
  end
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



36
37
38
# File 'lib/dripper_mail.rb', line 36

def action
  @action
end

#childrenObject

Returns the value of attribute children.



39
40
41
# File 'lib/dripper_mail.rb', line 39

def children
  @children
end

#mailerObject

Returns the value of attribute mailer.



35
36
37
# File 'lib/dripper_mail.rb', line 35

def mailer
  @mailer
end

#modelObject

Returns the value of attribute model.



34
35
36
# File 'lib/dripper_mail.rb', line 34

def model
  @model
end

#parentObject

Returns the value of attribute parent.



38
39
40
# File 'lib/dripper_mail.rb', line 38

def parent
  @parent
end

#scopeObject

Returns the value of attribute scope.



37
38
39
# File 'lib/dripper_mail.rb', line 37

def scope
  @scope
end

#waitObject

Returns the value of attribute wait.



40
41
42
# File 'lib/dripper_mail.rb', line 40

def wait
  @wait
end

#wait_untilObject

Returns the value of attribute wait_until.



41
42
43
# File 'lib/dripper_mail.rb', line 41

def wait_until
  @wait_until
end

Instance Method Details

#dripper(opts = {}, &block) ⇒ Object



68
69
70
71
# File 'lib/dripper_mail.rb', line 68

def dripper(opts={}, &block)
  proxy = DripperProxy.new opts.merge(parent: self), &block
  @children << proxy
end

#execute(item = nil) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/dripper_mail.rb', line 109

def execute(item = nil)

  dripper_action = Dripper::Action.find_by(action: self.action.to_s, mailer: self.mailer.to_s)
  puts self.action.to_s
  puts scoped_recs(item).to_sql
  scoped_recs(item).each do |obj|

    # instantiate the mailer and run the code
    mailer_obj = self.mailer.to_s.classify.constantize
    mail_obj = mailer_obj.send self.action, obj
    if mail_obj
      if self.wait
        if self.wait.respond_to? :call
          wait_date = self.wait.call
        else
          wait_date = self.wait
        end
        mail_obj.deliver_later(wait: wait_date)
      elsif self.wait_until
        if self.wait_until.respond_to? :call
          wait_date = self.wait_until.call
        else
          wait_date = self.wait_until
        end
        mail_obj.deliver_later(wait_until: wait_date)
      else
        mail_obj.deliver_now
      end

      # insert a row
      Dripper::Message.create!(dripper_action_id: dripper_action.id, drippable: obj)
    end

  end


end

#registerObject



73
74
75
76
77
78
# File 'lib/dripper_mail.rb', line 73

def register
  # don't register until migrations have completed
  if ActiveRecord::Base.connection.data_source_exists? 'dripper_actions'
    Dripper::Action.where(action: self.action.to_s, mailer: self.mailer.to_s).first_or_create
  end
end

#scoped_recs(item = nil) ⇒ Object



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/dripper_mail.rb', line 80

def scoped_recs(item = nil)

  dripper_action = Dripper::Action.find_by(action: self.action.to_s, mailer: self.mailer.to_s)

  all_recs = self.model.to_s.classify.constantize.send(:all)

    # only send if we haven't already
  already_sent = Dripper::Message
    .where(drippable_type: self.model.to_s.classify.to_s, dripper_action_id: dripper_action.id)
    .select(:drippable_id)

  final_scope = all_recs
    .where.not(id: already_sent)
    .where("#{self.model.to_s.classify.constantize.table_name}.created_at >= ?", dripper_action.created_at.change(usec: 0))

  # merge all the scopes
  @scopes.each do |s|
    final_scope = final_scope.merge s
  end

  if item
    final_scope = final_scope.where(id: item.id)
  end


  return final_scope

end