Class: EgovUtils::Person

Inherits:
ApplicationRecord show all
Defined in:
app/models/egov_utils/person.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Person

Returns a new instance of Person.



22
23
24
25
26
27
28
# File 'app/models/egov_utils/person.rb', line 22

def initialize(attributes = {})
  if attributes&.fetch(:remote_id, nil).present?
    super(attributes.slice(:remote_id, :person_type))
  else
    super
  end
end

Class Method Details

.find_or_create_from_remote_id(create_attrs) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'app/models/egov_utils/person.rb', line 62

def self.find_or_create_from_remote_id(create_attrs)
  remote_id = create_attrs[:remote_id].presence
  person_type = create_attrs[:person_type].presence

  person = if remote_id.present?
    EgovUtils::Person.find_by(
      remote_id: remote_id.presence,
      person_type: person_type.presence
    )
  end
  return person if person

  if remote_id
    attributes =
      if person_type == 'natural'
        Iszr::NaturalPeople::SearchByRemoteId.run!(remote_id:)
      else
        Iszr::LegalPeople::SearchByRemoteId.run!(remote_id:)
      end.to_h

    person_entity =
      if person_type == 'natural'
        NaturalPerson.find_or_initialize_by(attributes.except(:remote_id))
      else
        LegalPerson.find_or_initialize_by(attributes.except(:remote_id))
      end

    if person_entity.persisted?
      person_entity.person.update!(remote_id:)
    else
      person_entity.build_person(remote_id:, person_type:)
      person_entity.save(validate: false)
    end
    person_entity.person
  else
    new(create_attrs).tap do
      _1.save(validate: false)
    end
  end
end

Instance Method Details

#person_entityObject



30
31
32
33
34
35
36
37
# File 'app/models/egov_utils/person.rb', line 30

def person_entity
  case person_type
  when 'natural'
    natural_person
  when 'legal'
    legal_person
  end
end

#person_entity=(entity) ⇒ Object



39
40
41
42
43
44
45
46
47
48
# File 'app/models/egov_utils/person.rb', line 39

def person_entity=(entity)
  case person_type
  when 'natural'
    self.natural_person = entity
    self.legal_person = nil
  when 'legal'
    self.legal_person = entity
    self.natural_person = nil
  end
end

#residenceObject



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

def residence
  addresses.last
end

#residence=(address) ⇒ Object



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

def residence=(address)
  addresses << address
end

#to_sObject



58
59
60
# File 'app/models/egov_utils/person.rb', line 58

def to_s
  person_entity.to_s
end