Class: Message

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

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.functional(function, group = nil) ⇒ Object



66
67
68
69
70
71
72
73
# File 'app/models/message.rb', line 66

def self.functional(function, group=nil)
  messages = for_function(function)
  if group
    messages.belonging_to(group).first
  else
    messages.ungrouped.first
  end
end

Instance Method Details

#active_readersObject



50
51
52
# File 'app/models/message.rb', line 50

def active_readers
  possible_readers.active
end

#administrative?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'app/models/message.rb', line 77

def administrative?
  has_function?
end

#deliver(readers) ⇒ Object



94
95
96
97
98
99
100
101
# File 'app/models/message.rb', line 94

def deliver(readers)
  failures = []
  readers.each do |reader|
    failures.push(reader) unless deliver_to(reader)
  end
  self.published!
  failures
end

#deliver_to(reader, sender = nil) ⇒ Object



103
104
105
106
107
108
109
110
# File 'app/models/message.rb', line 103

def deliver_to(reader, sender=nil)
  ReaderNotifier.deliver_message(reader, self, sender)
  record_delivery(reader)
  true
rescue => e
  Rails.logger.warn "@@  delivery failed: #{e.inspect}"
  raise
end

#delivered?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'app/models/message.rb', line 54

def delivered?
  deliveries.any?
end

#delivered_to?(reader) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'app/models/message.rb', line 116

def delivered_to?(reader)
  recipients.include?(reader)
end

#delivery_to(reader) ⇒ Object



120
121
122
# File 'app/models/message.rb', line 120

def delivery_to(reader)
  deliveries.to_reader(reader).first if delivered_to?(reader)
end

#filtered_bodyObject



30
31
32
# File 'app/models/message.rb', line 30

def filtered_body
  filter.filter(body)
end

#functionObject



63
64
65
# File 'app/models/message.rb', line 63

def function
  MessageFunction[self.function_id]
end

#has_function?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/models/message.rb', line 74

def has_function?
  !function.nil?
end

#inactive_readersObject



46
47
48
# File 'app/models/message.rb', line 46

def inactive_readers
  possible_readers.inactive
end

#possible_readersObject



34
35
36
# File 'app/models/message.rb', line 34

def possible_readers
  permitted_readers
end

#preview(reader = nil) ⇒ Object



58
59
60
61
# File 'app/models/message.rb', line 58

def preview(reader=nil)
  reader ||= possible_readers.first || Reader.for_user(Reader.current)
  ReaderNotifier.create_message(reader, self)
end

#published!Object



90
91
92
# File 'app/models/message.rb', line 90

def published!
  status = Status[:published]
end

#published?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/message.rb', line 87

def published?
  status == Status[:published]
end

#record_delivery(reader) ⇒ Object



112
113
114
# File 'app/models/message.rb', line 112

def record_delivery(reader)
  MessageReader.find_or_create_by_message_id_and_reader_id(self.id, reader.id).update_attribute(:sent_at, Time.now)
end

#statusObject



81
82
83
# File 'app/models/message.rb', line 81

def status
  Status.find(self.status_id)
end

#status=(value) ⇒ Object



84
85
86
# File 'app/models/message.rb', line 84

def status=(value)
  self.status_id = value.id
end

#undelivered_readersObject



38
39
40
41
42
43
44
# File 'app/models/message.rb', line 38

def undelivered_readers
  if recipients.any?
    possible_readers.except(recipients)
  else
    possible_readers
  end
end