Class: Msg::Sending

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/msg/sending.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.add_receiver_hooks(klass, options) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'app/models/msg/sending.rb', line 21

def self.add_receiver_hooks(klass, options)
  key = klass.to_s.underscore

  define_method  :"#{key}_group" do
    self.receiving_group if self.receiving_class == key
  end

  # class_group= is called from the SendingsController and given the _name_ of a messaging group,
  # we get the designated proc from the class messaging rules, then call it to get an array of receivers
  #
  define_method  :"#{key}_group=" do |name|
    self.receiving_class = key
    self.receiving_group = name
    self.receivers = klass.messaging_groups[name.to_sym].call
  end
  
  define_method  :"#{key}_receiver_ids" do
    self.envelopes.map(&:receiver_id) if self.receiving_class == key
  end

  # class_receiver_ids= is called from the SendingsController and given a list of receiver ids of this class.
  # they are instantiated and passed to receivers=
  #
  define_method :"#{key}_receiver_ids=" do |ids|
    self.receivers = klass.find(ids)
  end
end

Instance Method Details

#receivers=(receivers) ⇒ Object



7
8
9
10
11
# File 'app/models/msg/sending.rb', line 7

def receivers=(receivers)
  receivers.each do |rec|
    self.envelopes.build(:receiver => rec, :sending => self)
  end
end

#statisticsObject



13
14
15
16
17
18
19
# File 'app/models/msg/sending.rb', line 13

def statistics
  sent = envelopes.count || 0
  bounced = envelopes.bounced.count || 0
  read = envelopes.opened.count || 0
  unread = sent-read-bounced
  [unread, read, bounced]
end