Class: Party

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#create_relationship(description, to_party_id, reln_type) ⇒ Object

Creates a new PartyRelationship for this particular party instance.



137
138
139
140
141
142
143
144
# File 'app/models/party.rb', line 137

def create_relationship(description, to_party_id, reln_type)
  PartyRelationship.create(:description => description,
                           :relationship_type => reln_type,
                           :party_id_from => id,
                           :from_role => reln_type.valid_from_role,
                           :party_id_to => to_party_id,
                           :to_role => reln_type.valid_to_role)
end

#relationships ⇒ Object (readonly)

Gathers all party relationships that contain this particular party id in either the from or to side of the relationship.



111
112
113
# File 'app/models/party.rb', line 111

def relationships
  @relationships
end

Class Method Details

.scope_by_dba_organization(dba_organization) ⇒ ActiveRecord::Relation

scope by dba organization

scope by

Parameters:

  • dba_organization (Party, Array) —

    dba organization to scope by or Array of dba organizations to

Returns:

  • (ActiveRecord::Relation)


28
29
30
31
32
# File 'app/models/party.rb', line 28

def scope_by_dba_organization(dba_organization)
  joins("inner join party_relationships on party_relationships.role_type_id_to ='#{RoleType.iid('dba_org').id}'
         and party_relationships.party_id_from = parties.id")
  .where({party_relationships: {party_id_to: dba_organization}})
end

.with_role_types(role_types) ⇒ Object

Scopes parties by passed role types

Parameters:

  • role_types (Array, RoleType) —

    Array of RoleTypes to scope by



37
38
39
# File 'app/models/party.rb', line 37

def with_role_types(role_types)
  joins('inner join party_roles on party_roles.party_id = parties.id').where('party_roles.role_type_id' => role_types)
end

Instance Method Details

#add_role_type(role) ⇒ Object



157
158
159
160
161
# File 'app/models/party.rb', line 157

def add_role_type(role)
  role = role.is_a?(RoleType) ? role : RoleType.iid(role)

  PartyRole.create(party: self, role_type: role)
end

#child_dba_organizations(dba_orgs = []) ⇒ Array

Get any child DBA Organizations related this party

Parameters:

  • dba_orgs (Array) (defaults to: []) —

    Array of current DBA Organizations to add to

Returns:

  • (Array) —

    Child DBA Organizations



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'app/models/party.rb', line 54

def child_dba_organizations(dba_orgs=[])
  dba_org = RoleType.iid('dba_org')

  PartyRelationship.joins('inner join parties on parties.id = party_relationships.party_id_from')
  .joins('inner join party_roles on party_roles.party_id = party_relationships.party_id_from')
  .where('party_id_to' => self.id)
  .where('party_relationships.role_type_id_to' => dba_org)
  .where('party_roles.role_type_id' => dba_org).each do |party_reln|

    dba_orgs.push(party_reln.from_party)
    if !dba_orgs.collect(&:id).include? party_reln.from_party.id
      party_reln.from_party.child_dba_organizations(dba_orgs)
    end

  end

  dba_orgs.uniq
end

#dba_organization ⇒ Party

helper method to get dba_organization related to this party

Returns:

  • (Party) —

    DBA Organization



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

def dba_organization
  find_related_parties_with_role('dba_org').first
end

#destroy_business_party ⇒ Object

Callbacks



147
148
149
150
151
# File 'app/models/party.rb', line 147

def destroy_business_party
  if self.business_party
    self.business_party.destroy
  end
end

#destroy_party_relationships ⇒ Object



153
154
155
# File 'app/models/party.rb', line 153

def destroy_party_relationships
  PartyRelationship.destroy_all("party_id_from = #{id} or party_id_to = #{id}")
end


123
124
125
126
127
# File 'app/models/party.rb', line 123

def find_related_parties_with_role(role_type_iid)
  Party.joins(:party_roles).joins("inner join party_relationships on (party_id_from = #{id} and parties.id = party_relationships.party_id_to)")
  .where(PartyRole.arel_table[:role_type_id].eq(RoleType.iid(role_type_iid).id))
  .where(Party.arel_table[:id].not_eq(id))
end

#find_relationships_by_type(relationship_type_iid) ⇒ Object



129
130
131
132
133
# File 'app/models/party.rb', line 129

def find_relationships_by_type(relationship_type_iid)
  PartyRelationship.includes(:relationship_type).
    where('party_id_from = ? or party_id_to = ?', id, id).
    where('relationship_types.internal_identifier' => relationship_type_iid.to_s)
end

#from_relationships ⇒ Object



119
120
121
# File 'app/models/party.rb', line 119

def from_relationships
  @relationships ||= PartyRelationship.where('party_id_from = ?', id)
end

#has_role_type?(*passed_roles) ⇒ Boolean

Returns:

  • (Boolean)


163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'app/models/party.rb', line 163

def has_role_type?(*passed_roles)
  result = false
  passed_roles.flatten!
  passed_roles.each do |role|
    role_iid = role.is_a?(RoleType) ? role.internal_identifier : role.to_s

    PartyRole.where(party_id: self.id).each do |party_role|
      result = true if (party_role.role_type.internal_identifier == role_iid)
      break if result
    end

  end
  result
end

#parent_dba_organizations(dba_orgs = []) ⇒ Array

Get any parent DBA Organizations related this party

Parameters:

  • dba_orgs (Array) (defaults to: []) —

    Array of current DBA Organizations to add to

Returns:

  • (Array) —

    Parent DBA Organizations



97
98
99
100
101
102
103
104
105
106
107
# File 'app/models/party.rb', line 97

def parent_dba_organizations(dba_orgs=[])
  PartyRelationship.
    where('party_id_from = ?', id).
  where('role_type_id_to' => RoleType.iid('dba_org')).each do |party_reln|

    dba_orgs.push(party_reln.to_party)
    party_reln.to_party.parent_dba_organizations(dba_orgs)
  end

  dba_orgs.uniq
end

#parties_in_relationships(parties = [], deep = false) ⇒ Array

Get all parties that have a relationship to this party. If deep is passed then of those parties do the same all the way down the relationship chain

Parameters:

  • parties (Array) (defaults to: []) —

    Array of currently found related parties

Returns:

  • (Array) —

    Array of related parties



78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'app/models/party.rb', line 78

def parties_in_relationships(parties=[], deep=false)
  PartyRelationship.where('party_id_to' => self.id).each do |party_reln|

    parties.push(party_reln.from_party) unless parties.include?(party_reln.from_party)
    if deep and !parties.collect(&:id).include? party_reln.from_party.id
      party_reln.from_party.parties_in_relationships(parties, deep)
    else
      parties
    end

  end

  parties.uniq
end

#to_data_hash ⇒ Object

convert party record to hash of data



189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'app/models/party.rb', line 189

def to_data_hash
  data = to_hash(only: [
                   :id,
                   :description,
                   :created_at,
                   :updated_at
                 ],
                 business_party_type: business_party.class.name
                 )

  # get business party data
  if business_party
    if business_party.is_a?(Individual)
      data.merge!({
                    first_name: business_party.current_first_name,
                    last_name: business_party.current_last_name,
                    middle_name: business_party.current_middle_name,
                    gender: business_party.gender
      })
    else
      data.merge!({
                    tax_id_number: business_party.tax_id_number
      })
    end
  end

  data
end

#to_label ⇒ Object

Alias for to_s



179
180
181
# File 'app/models/party.rb', line 179

def to_label
  to_s
end

#to_relationships ⇒ Object



115
116
117
# File 'app/models/party.rb', line 115

def to_relationships
  @relationships ||= PartyRelationship.where('party_id_to = ?', id)
end

#to_s ⇒ Object



183
184
185
# File 'app/models/party.rb', line 183

def to_s
  "#{description}"
end