Class: Ecoportal::API::Internal::Person

Inherits:
V1::Person show all
Defined in:
lib/ecoportal/api/internal/person.rb

Constant Summary

Constants inherited from V1::Person

V1::Person::VALID_EMAIL_REGEX, V1::Person::VALID_TAG_REGEX

Instance Attribute Summary collapse

Attributes inherited from V1::Person

#contractor_organization_id, #details, #external_id, #id, #name, #supervisor_id

Attributes inherited from Common::BaseModel

#_key, #_parent

Instance Method Summary collapse

Methods inherited from V1::Person

#add_details, #email=, #filter_tags, #filter_tags=, #supervisor, #supervisor=

Methods inherited from Common::BaseModel

#dirty?, #doc, embeds_one, #initial_doc, #original_doc, passthrough, #print_pretty, #replace_doc!, #replace_original_doc!, #to_json

Methods included from Common::BaseClass

#class_resolver, #redef_without_warning, #resolve_class

Constructor Details

#initialize(doc = {}, *args, **kargs, &block) ⇒ Person

Returns a new instance of Person.



10
11
12
13
# File 'lib/ecoportal/api/internal/person.rb', line 10

def initialize(doc = {}, *args, **kargs, &block)
  super(doc, *args, **kargs, &block)
  @is_new = @prev_is_new = (@doc)
end

Instance Attribute Details

#accountAccount?

the account of the person or nil if missing.

Returns:

  • (Account, nil)

    the current value of account



5
6
7
# File 'lib/ecoportal/api/internal/person.rb', line 5

def 
  @account
end

Instance Method Details

#account_added?(value = :original) ⇒ Boolean

Returns if the account has been added to doc.

Returns:

  • (Boolean)

    if the account has been added to doc



41
42
43
44
# File 'lib/ecoportal/api/internal/person.rb', line 41

def (value = :original)
  ref_doc = (value == :original) ? original_doc : initial_doc
  !! && !ref_doc["account"]
end

#account_removed?(value = :original) ⇒ Boolean

Returns if the account has been removed from doc.

Returns:

  • (Boolean)

    if the account has been removed from doc



47
48
49
50
# File 'lib/ecoportal/api/internal/person.rb', line 47

def (value = :original)
  ref_doc = (value == :original) ? original_doc : initial_doc
  ! && !!ref_doc["account"]
end

#add_accountObject

Note:

if the person exists, and does not have an account, an this will send an invite.

Note:

this will not change the account properties of this person.

Adds an empty account to the person.



88
89
90
# File 'lib/ecoportal/api/internal/person.rb', line 88

def 
  self. = {}
end

#as_jsonObject



52
53
54
# File 'lib/ecoportal/api/internal/person.rb', line 52

def as_json
  super.update("account" => &.as_json)
end

#as_update(ref = :last, ignore: []) ⇒ Object



56
57
58
# File 'lib/ecoportal/api/internal/person.rb', line 56

def as_update(ref = :last, ignore: [])
  super(ref, ignore: ignore | ["user_id", "permissions_merged", "prefilter"])
end

#consolidate!Object



15
16
17
18
19
# File 'lib/ecoportal/api/internal/person.rb', line 15

def consolidate!
  @prev_is_new = @is_new
  @is_new = false
  super
end

#new?(value = :original) ⇒ Boolean

Note:
  • original_doc should technically hold the model as it is on server side
  • Once this person has been successfullyupdated, #consolidate! should be called.
  • The above sets a copy of doc as original_doc (meaning: no pending changes)
  • Therefore we can safely assume that this is a new record if the original_doc does not have details nor account (as all people should have either to exist).
  • However, to the purpose of getting a clean as_update, original_doc is filled with details right on creation. For this reason, a workaround is necessary via @is_new

Returns whether or not this entry is being created now (i.e. non existent on server).

Returns:

  • (Boolean)

    whether or not this entry is being created now (i.e. non existent on server)



35
36
37
38
# File 'lib/ecoportal/api/internal/person.rb', line 35

def new?(value = :original)
  return @is_new if value == :original
  (initial_doc)
end

#reset!(*args) ⇒ Object



21
22
23
24
# File 'lib/ecoportal/api/internal/person.rb', line 21

def reset!(*args)
  @is_new = @prev_is_new
  super(*args)
end