Class: CommunicationEvent

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

Overview

create_table :communication_events do |t|

t.integer  :from_contact_mechanism_id
t.string   :from_contact_mechanism_type

t.integer  :to_contact_mechanism_id
t.string   :to_contact_mechanism_type

t.string   :short_description
t.integer  :case_id
t.datetime :start_at
t.datetime :end_at
t.string   :notes
t.string   :external_identifier
t.string   :external_id_source

t.timestamps

end

add_index :communication_events, :status_type_id add_index :communication_events, :case_id add_index :communication_events, [:to_contact_mechanism_id, :to_contact_mechanism_type], :name => ‘to_contact_mech_idx’ add_index :communication_events, [:from_contact_mechanism_id, :from_contact_mechanism_type], :name => ‘from_contact_mech_idx’

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.scope_by_party(party, options = {}) ⇒ ActiveRecord::Relation

scope by party

or an array of Party ids

Parameters:

  • party (Integer | Party | Array)

    either a id of Party record, a Party record, an array of Party records

  • options (Hash) (defaults to: {})

    options to apply to this scope

Options Hash (options):

  • :role_types (Array)

    role types to include in the scope

Returns:

  • (ActiveRecord::Relation)


46
47
48
49
50
51
52
53
54
# File 'app/models/communication_event.rb', line 46

def scope_by_party(party, options={})
  if options[:role_types]
    joins(communication_event_pty_roles: :role_type)
    .where(communication_event_pty_roles: {party_id: party, role_type_id: options[:role_types]})

  else
    joins(:communication_event_pty_roles).where(communication_event_pty_roles: {party_id: party})
  end
end

Instance Method Details

#add_from_party(party) ⇒ Object



100
101
102
# File 'app/models/communication_event.rb', line 100

def add_from_party(party)
  add_party(party, 'communication_events_from')
end

#add_party(party, role_type) ⇒ Object

Add party with role type



118
119
120
121
122
123
124
# File 'app/models/communication_event.rb', line 118

def add_party(party, role_type)
  if role_type.is_a? String
    role_type = RoleType.iid(role_type)
  end

  communication_event_pty_roles.create(party: party, role_type: role_type)
end

#add_to_party(party) ⇒ Object



108
109
110
# File 'app/models/communication_event.rb', line 108

def add_to_party(party)
  add_party(party, 'communication_events_to')
end

#from_partiesObject



67
68
69
70
# File 'app/models/communication_event.rb', line 67

def from_parties
  parties.joins(communication_event_pty_roles: :role_type)
  .where(role_types: {internal_identifier: 'communication_events_from'})
end

#from_partyObject

Helper method if this communication event only has one from party



88
89
90
# File 'app/models/communication_event.rb', line 88

def from_party
  from_parties.first
end

#from_party=(party) ⇒ Object

Helper method if this communication event only has one from party



94
95
96
97
98
# File 'app/models/communication_event.rb', line 94

def from_party=(party)
  remove_from_parties

  add_party(party, 'communication_events_from')
end

#partiesObject



57
58
59
60
# File 'app/models/communication_event.rb', line 57

def parties
  Party.joins(:communication_event_pty_roles)
  .where(communication_event_pty_roles: {communication_event_id: self.id})
end

#remove_from_partiesObject



104
105
106
# File 'app/models/communication_event.rb', line 104

def remove_from_parties
  remove_parties('communication_events_from')
end

#remove_parties(role_type) ⇒ Object



126
127
128
129
130
131
132
133
134
135
# File 'app/models/communication_event.rb', line 126

def remove_parties(role_type)
  if role_type.is_a? String
    role_type = RoleType.iid(role_type)
  end

  communication_event_pty_roles.joins(:role_type)
  .where(role_types: {id: role_type}).each do |communication_event_pty_role|
    communication_event_pty_role.destroy
  end
end

#remove_to_partiesObject



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

def remove_to_parties
  remove_parties('communication_events_to')
end

#to_partiesObject



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

def to_parties
  parties.joins(communication_event_pty_roles: :role_type)
  .where(role_types: {internal_identifier: 'communication_events_to'})
end

#to_partyObject

Helper method if this communication event only has one to party



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

def to_party
  to_parties.first
end

#to_party=(party) ⇒ Object

Helper method if this communication event only has one to party



80
81
82
83
84
# File 'app/models/communication_event.rb', line 80

def to_party=(party)
  remove_to_parties

  add_party(party, 'communication_events_to')
end

#to_sObject Also known as: to_label



137
138
139
# File 'app/models/communication_event.rb', line 137

def to_s
  "#{short_description}"
end