Class: Dispatch

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/forge/app/models/dispatch.rb

Instance Method Summary collapse

Instance Method Details

#chart_dataObject



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/forge/app/models/dispatch.rb', line 38

def chart_data
  days = (self.sent_at.to_date..Date.today).to_a
  unsubscribes = DispatchUnsubscribe.find_by_sql("SELECT DATE(created_at) as date, COUNT(*) as total FROM dispatch_unsubscribes WHERE dispatch_id = #{self.id} GROUP BY DATE(created_at)").map { |u| {u.date => u.total} }
  opens = DispatchOpen.find_by_sql("SELECT DATE(created_at) as date, COUNT(*) as total FROM dispatch_opens WHERE dispatch_id = #{self.id} GROUP BY DATE(created_at)").map { |u| {u.date => u.total} }
  clicks = DispatchLinkClick.find_by_sql("SELECT DATE(dlc.created_at) as date, COUNT(*) as total FROM dispatch_link_clicks dlc, dispatch_links dl WHERE dlc.dispatch_link_id = dl.id AND dl.dispatch_id = #{self.id} GROUP BY DATE(dlc.created_at)").map { |u| {u.date => u.total} }
  [unsubscribes, opens, clicks].each do |items|
    (days - items.map{|i| i.keys[0]}).each {|day| items << {day => 0}}
  end
  
  return {
    :unsubscribes_over_time => unsubscribes.sort_by {|i| i.keys[0]}, 
    :opens_over_time => opens.sort_by {|i| i.keys[0]}, 
    :clicks_over_time => clicks.sort_by {|i| i.keys[0]},
    :sent_messages => self.sent_messages.count,
    :opened_messages => self.opens.count,
    :total_messages => self.messages.count,
    :days => days
  }
end

#deliver!(group_ids = []) ⇒ Object



20
21
22
23
24
25
26
27
# File 'lib/forge/app/models/dispatch.rb', line 20

def deliver!(group_ids = [])
  subscribers = group_ids.blank? ? Subscriber.all : SubscriberGroup.where(id: group_ids).to_a.map(&:subscribers).flatten.uniq
  subscribers.each {|s| 
    qd = QueuedDispatch.create(:subscriber => s, :dispatch => self)
    qd.send!
  }
  self.update_attributes(:sent_at => Time.now)
end

#display_contentObject



30
31
32
33
34
35
36
# File 'lib/forge/app/models/dispatch.rb', line 30

def display_content
  doc = Hpricot(self.content)
  self.dispatch_links.each do |dispatch_link|
    doc.search('a')[dispatch_link.position]["href"] = "#{MySettings.site_url}/dispatches/#{self.id}/links/#{dispatch_link.id}" if doc.search('a')[dispatch_link.position]
  end
  return doc.to_s
end