Module: MessageTrain::InstanceMethods::GeneralMethods

Defined in:
lib/message_train/instance_methods.rb

Overview

Included by InstanceMethods when message_train mixin is run

Constant Summary collapse

BOX_DEFINITIONS =
{
  in: :with_untrashed_to,
  sent: :with_untrashed_by,
  all: :with_untrashed_for,
  drafts: :with_drafts_by,
  trash: :with_trashed_for,
  ignored: :ignored
}.freeze
BOX_OMISSION_DEFINITIONS =
{
  trash: :without_trashed_for,
  drafts: :with_ready_for,
  ignored: :unignored
}.freeze

Instance Method Summary collapse

Instance Method Details

#all_conversations(*args) ⇒ Object



89
90
91
92
93
94
# File 'lib/message_train/instance_methods.rb', line 89

def all_conversations(*args)
  participant = args[0] || self
  results = MessageTrain::Conversation.with_messages_through(self)
  return [] if results.empty?
  results.with_messages_for(participant)
end

#all_messages(*args) ⇒ Object



96
97
98
99
100
101
# File 'lib/message_train/instance_methods.rb', line 96

def all_messages(*args)
  participant = args[0] || self
  results = MessageTrain::Message.with_receipts_through(self)
  return [] if results.empty?
  results.with_receipts_for(participant)
end

#allows_access_by?(recipient) ⇒ Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/message_train/instance_methods.rb', line 45

def allows_access_by?(recipient)
  allows_receiving_by?(recipient) || allows_sending_by?(recipient)
end

#allows_receiving_by?(recipient) ⇒ Boolean

Returns:

  • (Boolean)


49
50
51
52
# File 'lib/message_train/instance_methods.rb', line 49

def allows_receiving_by?(recipient)
  return false if valid_recipients.empty?
  valid_recipients.include? recipient
end

#allows_sending_by?(sender) ⇒ Boolean

Returns:

  • (Boolean)


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

def allows_sending_by?(sender)
  valid_senders.include? sender
end

#boxes_for_participant(participant) ⇒ Object



78
79
80
81
82
83
84
85
86
87
# File 'lib/message_train/instance_methods.rb', line 78

def boxes_for_participant(participant)
  original_order = [:in, :sent, :all, :drafts, :trash, :ignored]
  divisions = [:all, :trash]
  if respond_to?(:messages) || allows_sending_by?(participant)
    divisions += [:sent, :drafts]
  end
  divisions += [:in, :ignored] if allows_receiving_by?(participant)
  divisions.sort_by! { |x| original_order.index x }
  divisions.collect { |d| MessageTrain::Box.new(self, d, participant) }
end

#conversations(*args) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/message_train/instance_methods.rb', line 59

def conversations(*args)
  division = args[0] || :in
  participant = args[1] || self
  definition_method = BOX_DEFINITIONS[division]
  return if definition_method.nil?
  conversations = defined_conversations(definition_method, participant)
  BOX_OMISSION_DEFINITIONS.each do |division_key, omission_method|
    next if division_key == division # Because not to be omitted
    conversations = conversations.send(omission_method, participant)
  end
  conversations
end

#defined_conversations(definition, participant) ⇒ Object



72
73
74
75
76
# File 'lib/message_train/instance_methods.rb', line 72

def defined_conversations(definition, participant)
  MessageTrain::Conversation.with_messages_through(self)
                            .without_deleted_for(participant)
                            .send(definition, participant)
end

#message_train_subscriptionsObject



115
116
117
118
119
120
121
# File 'lib/message_train/instance_methods.rb', line 115

def message_train_subscriptions
  subscriptions = [self_subscription]
  subscriptions += collective_boxes.values.map do |boxes|
    boxes.map { |box| message_train_subscription(box) }
  end
  subscriptions.flatten.compact
end

#path_partObject



27
28
29
30
31
# File 'lib/message_train/instance_methods.rb', line 27

def path_part
  return unless self.class.collective?
  # This must mean it's a collective
  "#{self.class.table_name}:#{message_train_slug}"
end

#self_collectionObject



54
55
56
57
# File 'lib/message_train/instance_methods.rb', line 54

def self_collection
  # This turns a single record into an active record collection.
  self.class.where(id: id)
end

#unsubscribe_from(from) ⇒ Object



111
112
113
# File 'lib/message_train/instance_methods.rb', line 111

def unsubscribe_from(from)
  unsubscribes.find_or_create_by(from: from)
end

#unsubscribed_from?(from) ⇒ Boolean

Returns:

  • (Boolean)


107
108
109
# File 'lib/message_train/instance_methods.rb', line 107

def unsubscribed_from?(from)
  unsubscribed_from_all? || unsubscribes.where(from: from).exists?
end

#unsubscribed_from_all?Boolean

Returns:

  • (Boolean)


103
104
105
# File 'lib/message_train/instance_methods.rb', line 103

def unsubscribed_from_all?
  unsubscribes.where(from: nil).exists?
end

#valid_recipientsObject



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

def valid_recipients
  send(self.class.valid_recipients_method)
end

#valid_sendersObject



33
34
35
# File 'lib/message_train/instance_methods.rb', line 33

def valid_senders
  send(self.class.valid_senders_method)
end