Class: Person
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- Person
- Includes:
- Housekeeping::Timestamps, Housekeeping::Users, Shared::AlternateValues, Shared::DataAttributes, Shared::Depictions, Shared::DwcOccurrenceHooks, Shared::HasPapertrail, Shared::Identifiers, Shared::IsData, Shared::Notes, Shared::OriginRelationship, Shared::SharedAcrossProjects, Shared::Tags
- Defined in:
- app/models/person.rb
Overview
A human. Data only, not users. There are two classes of people: vetted and unvetted. !! People are only related to data via Roles.
A vetted person
-
Has two or more roles
-
Has one or more annotations
An unvetted person
-
Has no or 1 role
-
Has no annotations
A unvetted person becomes automatically vetted when they have > 1 roles or they have an annotation associated with them.
Defined Under Namespace
Constant Summary collapse
- ALTERNATE_VALUES_FOR =
[:last_name, :first_name].freeze
- IGNORE_SIMILAR =
[:type, :cached].freeze
- IGNORE_IDENTICAL =
[:type, :first_name, :last_name, :prefix, :suffix].freeze
Instance Attribute Summary collapse
-
#cached ⇒ String
Full name.
-
#first name(name) ⇒ String
the first name, includes initials if the are provided.
-
#last_name ⇒ String
the last/family name.
-
#no_cached ⇒ true?
Set as true to prevent caching.
-
#no_namecase ⇒ true?
Set as true to prevent application of NameCase().
-
#suffix ⇒ String
string following the last/family name.
-
#type ⇒ String
Person::Vetted or Person::Unvetted.
-
#year_active_end ⇒ Integer
(rough) ending point of when person made scientific assertions that were dissemenated (i.e. could be seen by others).
-
#year_active_start ⇒ Integer
(rough) starting point of when person made scientific assertions that were dissemenated (i.e. could be seen by others).
-
#year_born ⇒ Integer
Born on.
-
#year_died ⇒ Integer
Year died.
Attributes included from Housekeeping::Users
Class Method Summary collapse
-
.parse_to_people(name_string) ⇒ Array
Of People return people for name strings.
-
.parser(name_string) ⇒ Array
Of Hashes use citeproc to parse strings see also github.com/SpeciesFileGroup/taxonworks/issues/1161.
-
.select_optimized(user_id, project_id, role_type = 'SourceAuthor') ⇒ Hash
Geographic_areas optimized for user selection.
-
.used_recently(user_id, role_type = 'SourceAuthor') ⇒ Scope
The max 10 most recently used (1 week, could parameterize) people.
Instance Method Summary collapse
- #activity_ended_after_started ⇒ Ignored protected
-
#bibtex_name ⇒ String
The person’s name in BibTeX format (von last, Jr, first).
-
#change_to_cached_attribute? ⇒ Boolean
protected
Difficult to anticipate what attributes will be cached in different models.
- #died_after_born ⇒ Ignored protected
- #dwc_occurrences ⇒ Object
-
#full_last_name ⇒ String
The person’s full last name including prefix & suffix (von last Jr).
- #hard_merge(person_id_to_destroy) ⇒ Object
- #is_author? ⇒ Boolean
- #is_collector? ⇒ Boolean
- #is_determiner? ⇒ Boolean
- #is_editor? ⇒ Boolean
- #is_georeferencer? ⇒ Boolean
-
#is_in_use? ⇒ Boolean
!! overwrites IsData#is_in_use?.
- #is_source? ⇒ Boolean
- #is_taxon_name_author? ⇒ Boolean
-
#merge_with(person_id) ⇒ Object
r_person is merged into l_person (self) !! the intent is to keep self and remove target.
- #name ⇒ String
-
#namecase_names ⇒ Object
Apply a “proper” case to all strings.
- #not_active_after_death ⇒ Ignored protected
- #not_active_before_birth ⇒ Ignored protected
- #not_balrog ⇒ Object protected
- #not_gandalf ⇒ Object protected
-
#orcid ⇒ Object
Return [String, nil] convenience, maybe a delegate: candidate.
- #role_counts(project_id) ⇒ Object
- #set_cached ⇒ Ignored protected
- #set_role_cached ⇒ Ignored protected
-
#set_type_if_blank ⇒ Ignored
protected
TODO: deprecate this, always set explicitly.
-
#update_role_cached ⇒ Object
protected
Integer the total objects updated.
-
#used_in_project?(project_id) ⇒ Boolean
Boolean whether or not this Person is linked to any data in the project.
-
#wikidata_id ⇒ Object
Return [String, nil] convenience, maybe a delegate: candidate.
Methods included from Shared::OriginRelationship
#new_objects, #old_objects, #reject_origin_relationships, #set_origin
Methods included from Shared::IsData
#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_users_projects?, #metamorphosize, #similar
Methods included from Shared::HasPapertrail
#attribute_updated, #attribute_updater
Methods included from Shared::Tags
#reject_tags, #tag_with, #tagged?, #tagged_with?
Methods included from Shared::Depictions
#has_depictions?, #image_array=, #reject_depictions, #reject_images
Methods included from Shared::Notes
#concatenated_notes_string, #reject_notes
Methods included from Shared::Identifiers
#dwc_occurrence_id, #identified?, #next_by_identifier, #previous_by_identifier, #reject_identifiers, #uri, #uuid
Methods included from Shared::DataAttributes
#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes
Methods included from Shared::AlternateValues
#all_values_for, #alternate_valued?
Methods included from Housekeeping::Users
#set_created_by_id, #set_updated_by_id
Methods inherited from ApplicationRecord
Instance Attribute Details
#cached ⇒ String
Returns full name.
51 52 53 54 55 56 57 58 59 60 61 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'app/models/person.rb', line 51 class Person < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Shared::AlternateValues include Shared::DataAttributes include Shared::Identifiers include Shared::Notes include Shared::Depictions include Shared::Tags include Shared::SharedAcrossProjects include Shared::HasPapertrail include Shared::DwcOccurrenceHooks include Shared::IsData include Shared::OriginRelationship ALTERNATE_VALUES_FOR = [:last_name, :first_name].freeze IGNORE_SIMILAR = [:type, :cached].freeze IGNORE_IDENTICAL = [:type, :first_name, :last_name, :prefix, :suffix].freeze # @return [true, nil] # set as true to prevent caching attr_accessor :no_cached # @return [true, nil] # set as true to prevent application of NameCase() attr_accessor :no_namecase validates_presence_of :last_name, :type validates :year_born, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_died, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_active_start, inclusion: {in: 0..Time.now.year, message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validates :year_active_end, inclusion: {in: 0..Time.now.year , message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validate :died_after_born validate :activity_ended_after_started validate :not_active_after_death validate :not_active_before_birth validate :not_gandalf validate :not_balrog before_validation :namecase_names, unless: Proc.new {|n| n.no_namecase } # TODO: remove this before_validation :set_type_if_blank after_save :set_cached, unless: Proc.new { |n| n.no_cached || errors.any? } validates :type, inclusion: { in: ['Person::Vetted', 'Person::Unvetted'], message: '%{value} is not a validly_published type'} has_one :user, dependent: :restrict_with_error, inverse_of: :person has_many :roles, dependent: :restrict_with_error, inverse_of: :person # before_remove: :set_cached_for_related has_many :author_roles, class_name: 'SourceAuthor', dependent: :restrict_with_error, inverse_of: :person #, before_remove: :set_cached_for_related has_many :collector_roles, class_name: 'Collector', dependent: :restrict_with_error, inverse_of: :person has_many :determiner_roles, class_name: 'Determiner', dependent: :restrict_with_error, inverse_of: :person has_many :editor_roles, class_name: 'SourceEditor', dependent: :restrict_with_error, inverse_of: :person has_many :georeferencer_roles, class_name: 'Georeferencer', dependent: :restrict_with_error, inverse_of: :person has_many :source_roles, class_name: 'SourceSource', dependent: :restrict_with_error, inverse_of: :person has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', dependent: :restrict_with_error, inverse_of: :person has_many :authored_sources, class_name: 'Source::Bibtex', through: :author_roles, source: :role_object, source_type: 'Source', inverse_of: :authors has_many :edited_sources, class_name: 'Source::Bibtex', through: :editor_roles, source: :role_object, source_type: 'Source', inverse_of: :editors has_many :human_sources, class_name: 'Source::Bibtex', through: :source_roles, source: :role_object, source_type: 'Source', inverse_of: :people has_many :authored_taxon_names, through: :taxon_name_author_roles, source: :role_object, source_type: 'TaxonName', class_name: 'Protonym', inverse_of: :taxon_name_authors has_many :collecting_events, through: :collector_roles, source: :role_object, source_type: 'CollectingEvent', inverse_of: :collectors has_many :georeferences, through: :georeferencer_roles, source: :role_object, source_type: 'Georeference', inverse_of: :georeference_authors has_many :taxon_determinations, through: :determiner_roles, source: :role_object, source_type: 'TaxonDetermination', inverse_of: :determiners # TODO: !? has_many :sources, through: :roles, source: :role_object, source_type: 'Source' # Editor or Author or Person has_many :collection_objects, through: :collecting_events scope :created_before, -> (time) { where('created_at < ?', time) } scope :with_role, -> (role) { includes(:roles).where(roles: {type: role}) } scope :ordered_by_last_name, -> { order(:last_name) } scope :used_in_project, -> (project_id) { joins(:roles).where( roles: { project_id: } ) } # Apply a "proper" case to all strings def namecase_names write_attribute(:last_name, NameCase(last_name)) if last_name && will_save_change_to_last_name? write_attribute(:first_name, NameCase(first_name)) if first_name && will_save_change_to_first_name? write_attribute(:prefix, NameCase(prefix)) if prefix && will_save_change_to_prefix? write_attribute(:suffix, NameCase(suffix)) if suffix && will_save_change_to_suffix? end # @return [Boolean] # !! overwrites IsData#is_in_use? def is_in_use? roles.reload.any? end # @return Boolean # whether or not this Person is linked to any data in the project def used_in_project?(project_id) Role.where(person_id: id, project_id:).any? || Source.joins(:project_sources, :roles).where(roles: {person_id: id}, project_sources: { project_id: }).any? end # @return [String] def name [first_name, prefix, last_name, suffix].compact.join(' ') end # @return [String] # The person's name in BibTeX format (von last, Jr, first) def bibtex_name out = '' out << prefix + ' ' if prefix.present? out << last_name if last_name.present? out << ', ' unless out.blank? || (first_name.blank? && suffix.blank?) out << suffix if suffix.present? out << ', ' unless out.end_with?(', ') || first_name.blank? || out.blank? out << first_name if first_name.present? out.strip end # @return [String] # The person's full last name including prefix & suffix (von last Jr) def full_last_name [prefix, last_name, suffix].compact.join(' ') end # Return [String, nil] # convenience, maybe a delegate: candidate def orcid identifiers.where(type: 'Identifier::Global::Orcid').first&.cached end # Return [String, nil] # convenience, maybe a delegate: candidate def wikidata_id identifiers.where(type: 'Identifier::Global::Wikidata').first&.cached end # @param [Integer] person_id # @return [Boolean] # true if all records updated, false if any one failed (all or none) # # No person is destroyed, see `hard_merge`. # # r_person is merged into l_person (self) # !! the intent is to keep self and remove target # def merge_with(person_id) return false if person_id == id if r_person = Person.find(person_id) # get the person to merge to into self begin ApplicationRecord.transaction do # !! Role.where(person_id: r_person.id).update(person_id: id) is BAAAD # !! It appends person_id: <old> to Role.where() in callbacks, breaking # !! Role#vet_person, etc. # update merge person's roles to old Role.where(person_id: r_person.id).each do |r| return false unless r.update(person_id: id) end roles.reload l_person_hash = annotations_hash if r_person.first_name.present? if first_name.blank? update(first_name: r_person.first_name) else if first_name != r_person.first_name # create a first_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.first_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'first_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.first_name, alternate_value_object_attribute: 'first_name', alternate_value_object_id: id) unless skip_av end end end if r_person.last_name.present? if last_name.blank? self.update(last_name: r_person.last_name) # NameCase() ? else if self.last_name != r_person.last_name # create a last_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.last_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'last_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.last_name, alternate_value_object_attribute: 'last_name', alternate_value_object_id: id) unless skip_av end end end r_person.annotations_hash.each do |r_kee, r_objects| r_objects.each do |r_o| skip = false l_test = l_person_hash[r_kee] if l_test.present? l_test.each do |l_o| # only look at same-type annotations # four types of annotations: # # data attributes, # # identifiers, # # notes, # # alternate values case r_kee when 'data attributes' if l_o.type == r_o.type && l_o.controlled_vocabulary_term_id == r_o.controlled_vocabulary_term_id && l_o.value == r_o.value && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'identifiers' if l_o.type == r_o.type && l_o.identifier == r_o.identifier && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'notes' if l_o.text == r_o.text && l_o.note_object_attribute == r_o.note.object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'alternate values' if l_o.value == r_o.value if l_o.type == r_o.type && l_o.alternate_value_object_attribute == r_o.alternate_value_object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end end end end skip end unless skip r_o.annotated_object = self r_o.save! end end end # TODO: handle prefix and suffix if false && prefix.blank? ## DD: do not change the name of verified person write_attribute(:prefix, r_person.prefix) else if r_person.prefix.present? # What to do when both have some content? end end if false && suffix.blank? ## DD: do not change the name of verified person self.suffix = r_person.suffix else if r_person.suffix.present? # What to do when both have some content? end end # TODO: handle years attributes if year_born.nil? self.year_born = r_person.year_born else unless r_person.year_born.nil? # What to do when both have some (different) numbers? end end if year_died.nil? self.year_died = r_person.year_died else unless r_person.year_died.nil? # What to do when both have some (different) numbers? end end if r_person.year_active_start # if not, r_person has nothing to contribute if self.year_active_start.nil? || (self.year_active_start > r_person.year_active_start) self.year_active_start = r_person.year_active_start end end if r_person.year_active_end # if not, r_person has nothing to contribute if self.year_active_end.nil? || (self.year_active_end < r_person.year_active_end) self.year_active_end = r_person.year_active_end end end # last thing to do in the transaction... # NO!!! - unless self.persisted? (all people are at this point persisted!) self.save! if self.changed? end rescue ActiveRecord::RecordInvalid return false end end true end def hard_merge(person_id_to_destroy) return false if id == person_id_to_destroy begin person_to_destroy = Person.find(person_id_to_destroy) Person.transaction do merge_with(person_to_destroy.id) person_to_destroy.destroy! end rescue ActiveRecord::RecordNotDestroyed return false rescue ActiveRecord::RecordInvalid return false rescue ActiveRecord::RecordNotFound return false end true end # @return [Boolean] def is_determiner? determiner_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_georeferencer? georeferencer_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_editor? editor_roles.any? end # @return [Boolean] def is_source? source_roles.any? end # @return [Boolean] def is_collector? collector_roles.any? end def role_counts(project_id) { in_project: self.roles.where(project_id:).group(:type).count, not_in_project: self.roles.where.not(project_id:).where.not(project_id: nil).group(:type).count, community: self.roles.where(project_id: nil).group(:type).count } end def dwc_occurrences ::Queries::DwcOccurrence::Filter.new(person_id: [id]).all end # @param [String] name_string # @return [Array] of Hashes # use citeproc to parse strings # see also https://github.com/SpeciesFileGroup/taxonworks/issues/1161 def self.parser(name_string) BibTeX::Entry.new(type: :book, author: name_string).parse_names.to_citeproc['author'] end # @param [String] name_string # @return [Array] of People # return people for name strings def self.parse_to_people(name_string) parser(name_string).collect { |n| Person::Unvetted.new( last_name: n['family'] ? NameCase(n['family']) : nil, first_name: n['given'] ? NameCase(n['given']) : nil, prefix: n['non-dropping-particle'] ? NameCase( n['non-dropping-particle']) : nil )} end # @param role_type [String] one of the Role types # @return [Scope] # the max 10 most recently used (1 week, could parameterize) people def self.used_recently(user_id, role_type = 'SourceAuthor') t = Role.arel_table p = Person.arel_table # i is a select manager i = t.project(t['person_id'], t['type'], t['updated_at']).from(t) .where(t['updated_at'].gt(1.week.ago)) .where(t['updated_by_id'].eq(user_id)) .where(t['type'].eq(role_type)) .order(t['updated_at'].desc) # z is a table alias z = i.as('recent_t') Person.joins( Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['person_id'].eq(p['id']))) ).pluck(:person_id).uniq end # @params Role [String] one the available roles # @return [Hash] geographic_areas optimized for user selection def self.select_optimized(user_id, project_id, role_type = 'SourceAuthor') r = used_recently(user_id, role_type) h = { quick: [], pinboard: Person.pinned_by(user_id).where(pinboard_items: {project_id:}).to_a, recent: [] } if r.empty? h[:quick] = Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a else h[:recent] = Person.where('"people"."id" IN (?)', r.first(10) ).to_a h[:quick] = ( Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a + Person.where('"people"."id" IN (?)', r.first(4) ).to_a ).uniq end h end protected # @return [Ignored] def died_after_born errors.add(:year_born, 'is older than died year') if year_born && year_died && year_born > year_died end # @return [Ignored] def activity_ended_after_started errors.add(:year_active_start, 'is older than died year') if year_active_start && year_active_end && year_active_start > year_active_end end # @return [Ignored] def not_active_after_death unless is_editor? || errors.add(:year_active_start, 'is older than year of death') if year_active_start && year_died && year_active_start > year_died errors.add(:year_active_end, 'is older than year of death') if year_active_end && year_died && year_active_end > year_died end true end # @return [Ignored] def not_active_before_birth errors.add(:year_active_start, 'is younger than than year of birth') if year_active_start && year_born && year_active_start < year_born errors.add(:year_active_end, 'is younger than year of birth') if year_active_end && year_born && year_active_end < year_born end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_gandalf errors.add(:base, 'fountain of eternal life does not exist yet') if year_born && year_died && year_died - year_born > 119 end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_balrog errors.add(:base, 'nobody is that active') if year_active_start && year_active_end && (year_active_end - year_active_start > 119) end # TODO: deprecate this, always set explicitly # @return [Ignored] def set_type_if_blank self.type = 'Person::Unvetted' if self.type.blank? end # @return [Ignored] def set_cached update_column(:cached, bibtex_name) set_role_cached end # @return [Ignored] def set_role_cached if change_to_cached_attribute? if roles.count > 25 delay(queue: :cache).update_role_cached else update_role_cached end end end # @return Integer # the total objects updated def update_role_cached # don't update the same object many times (e.g. CE of many COs?) updated = {} total = 0 roles.reload.find_each do |r| i = r.role_object.class.base_class.to_s + r.role_object.to_param next if updated[i] r.send(:set_cached) updated[i] = true total += 1 end total end # @return [Boolean] # Difficult to anticipate what # attributes will be cached in different models def change_to_cached_attribute? saved_change_to_last_name? || saved_change_to_prefix? || saved_change_to_suffix? end end |
#first name(name) ⇒ String
the first name, includes initials if the are provided
51 52 53 54 55 56 57 58 59 60 61 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'app/models/person.rb', line 51 class Person < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Shared::AlternateValues include Shared::DataAttributes include Shared::Identifiers include Shared::Notes include Shared::Depictions include Shared::Tags include Shared::SharedAcrossProjects include Shared::HasPapertrail include Shared::DwcOccurrenceHooks include Shared::IsData include Shared::OriginRelationship ALTERNATE_VALUES_FOR = [:last_name, :first_name].freeze IGNORE_SIMILAR = [:type, :cached].freeze IGNORE_IDENTICAL = [:type, :first_name, :last_name, :prefix, :suffix].freeze # @return [true, nil] # set as true to prevent caching attr_accessor :no_cached # @return [true, nil] # set as true to prevent application of NameCase() attr_accessor :no_namecase validates_presence_of :last_name, :type validates :year_born, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_died, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_active_start, inclusion: {in: 0..Time.now.year, message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validates :year_active_end, inclusion: {in: 0..Time.now.year , message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validate :died_after_born validate :activity_ended_after_started validate :not_active_after_death validate :not_active_before_birth validate :not_gandalf validate :not_balrog before_validation :namecase_names, unless: Proc.new {|n| n.no_namecase } # TODO: remove this before_validation :set_type_if_blank after_save :set_cached, unless: Proc.new { |n| n.no_cached || errors.any? } validates :type, inclusion: { in: ['Person::Vetted', 'Person::Unvetted'], message: '%{value} is not a validly_published type'} has_one :user, dependent: :restrict_with_error, inverse_of: :person has_many :roles, dependent: :restrict_with_error, inverse_of: :person # before_remove: :set_cached_for_related has_many :author_roles, class_name: 'SourceAuthor', dependent: :restrict_with_error, inverse_of: :person #, before_remove: :set_cached_for_related has_many :collector_roles, class_name: 'Collector', dependent: :restrict_with_error, inverse_of: :person has_many :determiner_roles, class_name: 'Determiner', dependent: :restrict_with_error, inverse_of: :person has_many :editor_roles, class_name: 'SourceEditor', dependent: :restrict_with_error, inverse_of: :person has_many :georeferencer_roles, class_name: 'Georeferencer', dependent: :restrict_with_error, inverse_of: :person has_many :source_roles, class_name: 'SourceSource', dependent: :restrict_with_error, inverse_of: :person has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', dependent: :restrict_with_error, inverse_of: :person has_many :authored_sources, class_name: 'Source::Bibtex', through: :author_roles, source: :role_object, source_type: 'Source', inverse_of: :authors has_many :edited_sources, class_name: 'Source::Bibtex', through: :editor_roles, source: :role_object, source_type: 'Source', inverse_of: :editors has_many :human_sources, class_name: 'Source::Bibtex', through: :source_roles, source: :role_object, source_type: 'Source', inverse_of: :people has_many :authored_taxon_names, through: :taxon_name_author_roles, source: :role_object, source_type: 'TaxonName', class_name: 'Protonym', inverse_of: :taxon_name_authors has_many :collecting_events, through: :collector_roles, source: :role_object, source_type: 'CollectingEvent', inverse_of: :collectors has_many :georeferences, through: :georeferencer_roles, source: :role_object, source_type: 'Georeference', inverse_of: :georeference_authors has_many :taxon_determinations, through: :determiner_roles, source: :role_object, source_type: 'TaxonDetermination', inverse_of: :determiners # TODO: !? has_many :sources, through: :roles, source: :role_object, source_type: 'Source' # Editor or Author or Person has_many :collection_objects, through: :collecting_events scope :created_before, -> (time) { where('created_at < ?', time) } scope :with_role, -> (role) { includes(:roles).where(roles: {type: role}) } scope :ordered_by_last_name, -> { order(:last_name) } scope :used_in_project, -> (project_id) { joins(:roles).where( roles: { project_id: } ) } # Apply a "proper" case to all strings def namecase_names write_attribute(:last_name, NameCase(last_name)) if last_name && will_save_change_to_last_name? write_attribute(:first_name, NameCase(first_name)) if first_name && will_save_change_to_first_name? write_attribute(:prefix, NameCase(prefix)) if prefix && will_save_change_to_prefix? write_attribute(:suffix, NameCase(suffix)) if suffix && will_save_change_to_suffix? end # @return [Boolean] # !! overwrites IsData#is_in_use? def is_in_use? roles.reload.any? end # @return Boolean # whether or not this Person is linked to any data in the project def used_in_project?(project_id) Role.where(person_id: id, project_id:).any? || Source.joins(:project_sources, :roles).where(roles: {person_id: id}, project_sources: { project_id: }).any? end # @return [String] def name [first_name, prefix, last_name, suffix].compact.join(' ') end # @return [String] # The person's name in BibTeX format (von last, Jr, first) def bibtex_name out = '' out << prefix + ' ' if prefix.present? out << last_name if last_name.present? out << ', ' unless out.blank? || (first_name.blank? && suffix.blank?) out << suffix if suffix.present? out << ', ' unless out.end_with?(', ') || first_name.blank? || out.blank? out << first_name if first_name.present? out.strip end # @return [String] # The person's full last name including prefix & suffix (von last Jr) def full_last_name [prefix, last_name, suffix].compact.join(' ') end # Return [String, nil] # convenience, maybe a delegate: candidate def orcid identifiers.where(type: 'Identifier::Global::Orcid').first&.cached end # Return [String, nil] # convenience, maybe a delegate: candidate def wikidata_id identifiers.where(type: 'Identifier::Global::Wikidata').first&.cached end # @param [Integer] person_id # @return [Boolean] # true if all records updated, false if any one failed (all or none) # # No person is destroyed, see `hard_merge`. # # r_person is merged into l_person (self) # !! the intent is to keep self and remove target # def merge_with(person_id) return false if person_id == id if r_person = Person.find(person_id) # get the person to merge to into self begin ApplicationRecord.transaction do # !! Role.where(person_id: r_person.id).update(person_id: id) is BAAAD # !! It appends person_id: <old> to Role.where() in callbacks, breaking # !! Role#vet_person, etc. # update merge person's roles to old Role.where(person_id: r_person.id).each do |r| return false unless r.update(person_id: id) end roles.reload l_person_hash = annotations_hash if r_person.first_name.present? if first_name.blank? update(first_name: r_person.first_name) else if first_name != r_person.first_name # create a first_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.first_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'first_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.first_name, alternate_value_object_attribute: 'first_name', alternate_value_object_id: id) unless skip_av end end end if r_person.last_name.present? if last_name.blank? self.update(last_name: r_person.last_name) # NameCase() ? else if self.last_name != r_person.last_name # create a last_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.last_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'last_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.last_name, alternate_value_object_attribute: 'last_name', alternate_value_object_id: id) unless skip_av end end end r_person.annotations_hash.each do |r_kee, r_objects| r_objects.each do |r_o| skip = false l_test = l_person_hash[r_kee] if l_test.present? l_test.each do |l_o| # only look at same-type annotations # four types of annotations: # # data attributes, # # identifiers, # # notes, # # alternate values case r_kee when 'data attributes' if l_o.type == r_o.type && l_o.controlled_vocabulary_term_id == r_o.controlled_vocabulary_term_id && l_o.value == r_o.value && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'identifiers' if l_o.type == r_o.type && l_o.identifier == r_o.identifier && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'notes' if l_o.text == r_o.text && l_o.note_object_attribute == r_o.note.object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'alternate values' if l_o.value == r_o.value if l_o.type == r_o.type && l_o.alternate_value_object_attribute == r_o.alternate_value_object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end end end end skip end unless skip r_o.annotated_object = self r_o.save! end end end # TODO: handle prefix and suffix if false && prefix.blank? ## DD: do not change the name of verified person write_attribute(:prefix, r_person.prefix) else if r_person.prefix.present? # What to do when both have some content? end end if false && suffix.blank? ## DD: do not change the name of verified person self.suffix = r_person.suffix else if r_person.suffix.present? # What to do when both have some content? end end # TODO: handle years attributes if year_born.nil? self.year_born = r_person.year_born else unless r_person.year_born.nil? # What to do when both have some (different) numbers? end end if year_died.nil? self.year_died = r_person.year_died else unless r_person.year_died.nil? # What to do when both have some (different) numbers? end end if r_person.year_active_start # if not, r_person has nothing to contribute if self.year_active_start.nil? || (self.year_active_start > r_person.year_active_start) self.year_active_start = r_person.year_active_start end end if r_person.year_active_end # if not, r_person has nothing to contribute if self.year_active_end.nil? || (self.year_active_end < r_person.year_active_end) self.year_active_end = r_person.year_active_end end end # last thing to do in the transaction... # NO!!! - unless self.persisted? (all people are at this point persisted!) self.save! if self.changed? end rescue ActiveRecord::RecordInvalid return false end end true end def hard_merge(person_id_to_destroy) return false if id == person_id_to_destroy begin person_to_destroy = Person.find(person_id_to_destroy) Person.transaction do merge_with(person_to_destroy.id) person_to_destroy.destroy! end rescue ActiveRecord::RecordNotDestroyed return false rescue ActiveRecord::RecordInvalid return false rescue ActiveRecord::RecordNotFound return false end true end # @return [Boolean] def is_determiner? determiner_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_georeferencer? georeferencer_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_editor? editor_roles.any? end # @return [Boolean] def is_source? source_roles.any? end # @return [Boolean] def is_collector? collector_roles.any? end def role_counts(project_id) { in_project: self.roles.where(project_id:).group(:type).count, not_in_project: self.roles.where.not(project_id:).where.not(project_id: nil).group(:type).count, community: self.roles.where(project_id: nil).group(:type).count } end def dwc_occurrences ::Queries::DwcOccurrence::Filter.new(person_id: [id]).all end # @param [String] name_string # @return [Array] of Hashes # use citeproc to parse strings # see also https://github.com/SpeciesFileGroup/taxonworks/issues/1161 def self.parser(name_string) BibTeX::Entry.new(type: :book, author: name_string).parse_names.to_citeproc['author'] end # @param [String] name_string # @return [Array] of People # return people for name strings def self.parse_to_people(name_string) parser(name_string).collect { |n| Person::Unvetted.new( last_name: n['family'] ? NameCase(n['family']) : nil, first_name: n['given'] ? NameCase(n['given']) : nil, prefix: n['non-dropping-particle'] ? NameCase( n['non-dropping-particle']) : nil )} end # @param role_type [String] one of the Role types # @return [Scope] # the max 10 most recently used (1 week, could parameterize) people def self.used_recently(user_id, role_type = 'SourceAuthor') t = Role.arel_table p = Person.arel_table # i is a select manager i = t.project(t['person_id'], t['type'], t['updated_at']).from(t) .where(t['updated_at'].gt(1.week.ago)) .where(t['updated_by_id'].eq(user_id)) .where(t['type'].eq(role_type)) .order(t['updated_at'].desc) # z is a table alias z = i.as('recent_t') Person.joins( Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['person_id'].eq(p['id']))) ).pluck(:person_id).uniq end # @params Role [String] one the available roles # @return [Hash] geographic_areas optimized for user selection def self.select_optimized(user_id, project_id, role_type = 'SourceAuthor') r = used_recently(user_id, role_type) h = { quick: [], pinboard: Person.pinned_by(user_id).where(pinboard_items: {project_id:}).to_a, recent: [] } if r.empty? h[:quick] = Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a else h[:recent] = Person.where('"people"."id" IN (?)', r.first(10) ).to_a h[:quick] = ( Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a + Person.where('"people"."id" IN (?)', r.first(4) ).to_a ).uniq end h end protected # @return [Ignored] def died_after_born errors.add(:year_born, 'is older than died year') if year_born && year_died && year_born > year_died end # @return [Ignored] def activity_ended_after_started errors.add(:year_active_start, 'is older than died year') if year_active_start && year_active_end && year_active_start > year_active_end end # @return [Ignored] def not_active_after_death unless is_editor? || errors.add(:year_active_start, 'is older than year of death') if year_active_start && year_died && year_active_start > year_died errors.add(:year_active_end, 'is older than year of death') if year_active_end && year_died && year_active_end > year_died end true end # @return [Ignored] def not_active_before_birth errors.add(:year_active_start, 'is younger than than year of birth') if year_active_start && year_born && year_active_start < year_born errors.add(:year_active_end, 'is younger than year of birth') if year_active_end && year_born && year_active_end < year_born end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_gandalf errors.add(:base, 'fountain of eternal life does not exist yet') if year_born && year_died && year_died - year_born > 119 end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_balrog errors.add(:base, 'nobody is that active') if year_active_start && year_active_end && (year_active_end - year_active_start > 119) end # TODO: deprecate this, always set explicitly # @return [Ignored] def set_type_if_blank self.type = 'Person::Unvetted' if self.type.blank? end # @return [Ignored] def set_cached update_column(:cached, bibtex_name) set_role_cached end # @return [Ignored] def set_role_cached if change_to_cached_attribute? if roles.count > 25 delay(queue: :cache).update_role_cached else update_role_cached end end end # @return Integer # the total objects updated def update_role_cached # don't update the same object many times (e.g. CE of many COs?) updated = {} total = 0 roles.reload.find_each do |r| i = r.role_object.class.base_class.to_s + r.role_object.to_param next if updated[i] r.send(:set_cached) updated[i] = true total += 1 end total end # @return [Boolean] # Difficult to anticipate what # attributes will be cached in different models def change_to_cached_attribute? saved_change_to_last_name? || saved_change_to_prefix? || saved_change_to_suffix? end end |
#last_name ⇒ String
the last/family name
51 52 53 54 55 56 57 58 59 60 61 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'app/models/person.rb', line 51 class Person < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Shared::AlternateValues include Shared::DataAttributes include Shared::Identifiers include Shared::Notes include Shared::Depictions include Shared::Tags include Shared::SharedAcrossProjects include Shared::HasPapertrail include Shared::DwcOccurrenceHooks include Shared::IsData include Shared::OriginRelationship ALTERNATE_VALUES_FOR = [:last_name, :first_name].freeze IGNORE_SIMILAR = [:type, :cached].freeze IGNORE_IDENTICAL = [:type, :first_name, :last_name, :prefix, :suffix].freeze # @return [true, nil] # set as true to prevent caching attr_accessor :no_cached # @return [true, nil] # set as true to prevent application of NameCase() attr_accessor :no_namecase validates_presence_of :last_name, :type validates :year_born, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_died, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_active_start, inclusion: {in: 0..Time.now.year, message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validates :year_active_end, inclusion: {in: 0..Time.now.year , message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validate :died_after_born validate :activity_ended_after_started validate :not_active_after_death validate :not_active_before_birth validate :not_gandalf validate :not_balrog before_validation :namecase_names, unless: Proc.new {|n| n.no_namecase } # TODO: remove this before_validation :set_type_if_blank after_save :set_cached, unless: Proc.new { |n| n.no_cached || errors.any? } validates :type, inclusion: { in: ['Person::Vetted', 'Person::Unvetted'], message: '%{value} is not a validly_published type'} has_one :user, dependent: :restrict_with_error, inverse_of: :person has_many :roles, dependent: :restrict_with_error, inverse_of: :person # before_remove: :set_cached_for_related has_many :author_roles, class_name: 'SourceAuthor', dependent: :restrict_with_error, inverse_of: :person #, before_remove: :set_cached_for_related has_many :collector_roles, class_name: 'Collector', dependent: :restrict_with_error, inverse_of: :person has_many :determiner_roles, class_name: 'Determiner', dependent: :restrict_with_error, inverse_of: :person has_many :editor_roles, class_name: 'SourceEditor', dependent: :restrict_with_error, inverse_of: :person has_many :georeferencer_roles, class_name: 'Georeferencer', dependent: :restrict_with_error, inverse_of: :person has_many :source_roles, class_name: 'SourceSource', dependent: :restrict_with_error, inverse_of: :person has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', dependent: :restrict_with_error, inverse_of: :person has_many :authored_sources, class_name: 'Source::Bibtex', through: :author_roles, source: :role_object, source_type: 'Source', inverse_of: :authors has_many :edited_sources, class_name: 'Source::Bibtex', through: :editor_roles, source: :role_object, source_type: 'Source', inverse_of: :editors has_many :human_sources, class_name: 'Source::Bibtex', through: :source_roles, source: :role_object, source_type: 'Source', inverse_of: :people has_many :authored_taxon_names, through: :taxon_name_author_roles, source: :role_object, source_type: 'TaxonName', class_name: 'Protonym', inverse_of: :taxon_name_authors has_many :collecting_events, through: :collector_roles, source: :role_object, source_type: 'CollectingEvent', inverse_of: :collectors has_many :georeferences, through: :georeferencer_roles, source: :role_object, source_type: 'Georeference', inverse_of: :georeference_authors has_many :taxon_determinations, through: :determiner_roles, source: :role_object, source_type: 'TaxonDetermination', inverse_of: :determiners # TODO: !? has_many :sources, through: :roles, source: :role_object, source_type: 'Source' # Editor or Author or Person has_many :collection_objects, through: :collecting_events scope :created_before, -> (time) { where('created_at < ?', time) } scope :with_role, -> (role) { includes(:roles).where(roles: {type: role}) } scope :ordered_by_last_name, -> { order(:last_name) } scope :used_in_project, -> (project_id) { joins(:roles).where( roles: { project_id: } ) } # Apply a "proper" case to all strings def namecase_names write_attribute(:last_name, NameCase(last_name)) if last_name && will_save_change_to_last_name? write_attribute(:first_name, NameCase(first_name)) if first_name && will_save_change_to_first_name? write_attribute(:prefix, NameCase(prefix)) if prefix && will_save_change_to_prefix? write_attribute(:suffix, NameCase(suffix)) if suffix && will_save_change_to_suffix? end # @return [Boolean] # !! overwrites IsData#is_in_use? def is_in_use? roles.reload.any? end # @return Boolean # whether or not this Person is linked to any data in the project def used_in_project?(project_id) Role.where(person_id: id, project_id:).any? || Source.joins(:project_sources, :roles).where(roles: {person_id: id}, project_sources: { project_id: }).any? end # @return [String] def name [first_name, prefix, last_name, suffix].compact.join(' ') end # @return [String] # The person's name in BibTeX format (von last, Jr, first) def bibtex_name out = '' out << prefix + ' ' if prefix.present? out << last_name if last_name.present? out << ', ' unless out.blank? || (first_name.blank? && suffix.blank?) out << suffix if suffix.present? out << ', ' unless out.end_with?(', ') || first_name.blank? || out.blank? out << first_name if first_name.present? out.strip end # @return [String] # The person's full last name including prefix & suffix (von last Jr) def full_last_name [prefix, last_name, suffix].compact.join(' ') end # Return [String, nil] # convenience, maybe a delegate: candidate def orcid identifiers.where(type: 'Identifier::Global::Orcid').first&.cached end # Return [String, nil] # convenience, maybe a delegate: candidate def wikidata_id identifiers.where(type: 'Identifier::Global::Wikidata').first&.cached end # @param [Integer] person_id # @return [Boolean] # true if all records updated, false if any one failed (all or none) # # No person is destroyed, see `hard_merge`. # # r_person is merged into l_person (self) # !! the intent is to keep self and remove target # def merge_with(person_id) return false if person_id == id if r_person = Person.find(person_id) # get the person to merge to into self begin ApplicationRecord.transaction do # !! Role.where(person_id: r_person.id).update(person_id: id) is BAAAD # !! It appends person_id: <old> to Role.where() in callbacks, breaking # !! Role#vet_person, etc. # update merge person's roles to old Role.where(person_id: r_person.id).each do |r| return false unless r.update(person_id: id) end roles.reload l_person_hash = annotations_hash if r_person.first_name.present? if first_name.blank? update(first_name: r_person.first_name) else if first_name != r_person.first_name # create a first_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.first_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'first_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.first_name, alternate_value_object_attribute: 'first_name', alternate_value_object_id: id) unless skip_av end end end if r_person.last_name.present? if last_name.blank? self.update(last_name: r_person.last_name) # NameCase() ? else if self.last_name != r_person.last_name # create a last_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.last_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'last_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.last_name, alternate_value_object_attribute: 'last_name', alternate_value_object_id: id) unless skip_av end end end r_person.annotations_hash.each do |r_kee, r_objects| r_objects.each do |r_o| skip = false l_test = l_person_hash[r_kee] if l_test.present? l_test.each do |l_o| # only look at same-type annotations # four types of annotations: # # data attributes, # # identifiers, # # notes, # # alternate values case r_kee when 'data attributes' if l_o.type == r_o.type && l_o.controlled_vocabulary_term_id == r_o.controlled_vocabulary_term_id && l_o.value == r_o.value && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'identifiers' if l_o.type == r_o.type && l_o.identifier == r_o.identifier && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'notes' if l_o.text == r_o.text && l_o.note_object_attribute == r_o.note.object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'alternate values' if l_o.value == r_o.value if l_o.type == r_o.type && l_o.alternate_value_object_attribute == r_o.alternate_value_object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end end end end skip end unless skip r_o.annotated_object = self r_o.save! end end end # TODO: handle prefix and suffix if false && prefix.blank? ## DD: do not change the name of verified person write_attribute(:prefix, r_person.prefix) else if r_person.prefix.present? # What to do when both have some content? end end if false && suffix.blank? ## DD: do not change the name of verified person self.suffix = r_person.suffix else if r_person.suffix.present? # What to do when both have some content? end end # TODO: handle years attributes if year_born.nil? self.year_born = r_person.year_born else unless r_person.year_born.nil? # What to do when both have some (different) numbers? end end if year_died.nil? self.year_died = r_person.year_died else unless r_person.year_died.nil? # What to do when both have some (different) numbers? end end if r_person.year_active_start # if not, r_person has nothing to contribute if self.year_active_start.nil? || (self.year_active_start > r_person.year_active_start) self.year_active_start = r_person.year_active_start end end if r_person.year_active_end # if not, r_person has nothing to contribute if self.year_active_end.nil? || (self.year_active_end < r_person.year_active_end) self.year_active_end = r_person.year_active_end end end # last thing to do in the transaction... # NO!!! - unless self.persisted? (all people are at this point persisted!) self.save! if self.changed? end rescue ActiveRecord::RecordInvalid return false end end true end def hard_merge(person_id_to_destroy) return false if id == person_id_to_destroy begin person_to_destroy = Person.find(person_id_to_destroy) Person.transaction do merge_with(person_to_destroy.id) person_to_destroy.destroy! end rescue ActiveRecord::RecordNotDestroyed return false rescue ActiveRecord::RecordInvalid return false rescue ActiveRecord::RecordNotFound return false end true end # @return [Boolean] def is_determiner? determiner_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_georeferencer? georeferencer_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_editor? editor_roles.any? end # @return [Boolean] def is_source? source_roles.any? end # @return [Boolean] def is_collector? collector_roles.any? end def role_counts(project_id) { in_project: self.roles.where(project_id:).group(:type).count, not_in_project: self.roles.where.not(project_id:).where.not(project_id: nil).group(:type).count, community: self.roles.where(project_id: nil).group(:type).count } end def dwc_occurrences ::Queries::DwcOccurrence::Filter.new(person_id: [id]).all end # @param [String] name_string # @return [Array] of Hashes # use citeproc to parse strings # see also https://github.com/SpeciesFileGroup/taxonworks/issues/1161 def self.parser(name_string) BibTeX::Entry.new(type: :book, author: name_string).parse_names.to_citeproc['author'] end # @param [String] name_string # @return [Array] of People # return people for name strings def self.parse_to_people(name_string) parser(name_string).collect { |n| Person::Unvetted.new( last_name: n['family'] ? NameCase(n['family']) : nil, first_name: n['given'] ? NameCase(n['given']) : nil, prefix: n['non-dropping-particle'] ? NameCase( n['non-dropping-particle']) : nil )} end # @param role_type [String] one of the Role types # @return [Scope] # the max 10 most recently used (1 week, could parameterize) people def self.used_recently(user_id, role_type = 'SourceAuthor') t = Role.arel_table p = Person.arel_table # i is a select manager i = t.project(t['person_id'], t['type'], t['updated_at']).from(t) .where(t['updated_at'].gt(1.week.ago)) .where(t['updated_by_id'].eq(user_id)) .where(t['type'].eq(role_type)) .order(t['updated_at'].desc) # z is a table alias z = i.as('recent_t') Person.joins( Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['person_id'].eq(p['id']))) ).pluck(:person_id).uniq end # @params Role [String] one the available roles # @return [Hash] geographic_areas optimized for user selection def self.select_optimized(user_id, project_id, role_type = 'SourceAuthor') r = used_recently(user_id, role_type) h = { quick: [], pinboard: Person.pinned_by(user_id).where(pinboard_items: {project_id:}).to_a, recent: [] } if r.empty? h[:quick] = Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a else h[:recent] = Person.where('"people"."id" IN (?)', r.first(10) ).to_a h[:quick] = ( Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a + Person.where('"people"."id" IN (?)', r.first(4) ).to_a ).uniq end h end protected # @return [Ignored] def died_after_born errors.add(:year_born, 'is older than died year') if year_born && year_died && year_born > year_died end # @return [Ignored] def activity_ended_after_started errors.add(:year_active_start, 'is older than died year') if year_active_start && year_active_end && year_active_start > year_active_end end # @return [Ignored] def not_active_after_death unless is_editor? || errors.add(:year_active_start, 'is older than year of death') if year_active_start && year_died && year_active_start > year_died errors.add(:year_active_end, 'is older than year of death') if year_active_end && year_died && year_active_end > year_died end true end # @return [Ignored] def not_active_before_birth errors.add(:year_active_start, 'is younger than than year of birth') if year_active_start && year_born && year_active_start < year_born errors.add(:year_active_end, 'is younger than year of birth') if year_active_end && year_born && year_active_end < year_born end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_gandalf errors.add(:base, 'fountain of eternal life does not exist yet') if year_born && year_died && year_died - year_born > 119 end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_balrog errors.add(:base, 'nobody is that active') if year_active_start && year_active_end && (year_active_end - year_active_start > 119) end # TODO: deprecate this, always set explicitly # @return [Ignored] def set_type_if_blank self.type = 'Person::Unvetted' if self.type.blank? end # @return [Ignored] def set_cached update_column(:cached, bibtex_name) set_role_cached end # @return [Ignored] def set_role_cached if change_to_cached_attribute? if roles.count > 25 delay(queue: :cache).update_role_cached else update_role_cached end end end # @return Integer # the total objects updated def update_role_cached # don't update the same object many times (e.g. CE of many COs?) updated = {} total = 0 roles.reload.find_each do |r| i = r.role_object.class.base_class.to_s + r.role_object.to_param next if updated[i] r.send(:set_cached) updated[i] = true total += 1 end total end # @return [Boolean] # Difficult to anticipate what # attributes will be cached in different models def change_to_cached_attribute? saved_change_to_last_name? || saved_change_to_prefix? || saved_change_to_suffix? end end |
#no_cached ⇒ true?
Returns set as true to prevent caching.
73 74 75 |
# File 'app/models/person.rb', line 73 def no_cached @no_cached end |
#no_namecase ⇒ true?
Returns set as true to prevent application of NameCase().
77 78 79 |
# File 'app/models/person.rb', line 77 def no_namecase @no_namecase end |
#suffix ⇒ String
string following the last/family name
51 52 53 54 55 56 57 58 59 60 61 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'app/models/person.rb', line 51 class Person < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Shared::AlternateValues include Shared::DataAttributes include Shared::Identifiers include Shared::Notes include Shared::Depictions include Shared::Tags include Shared::SharedAcrossProjects include Shared::HasPapertrail include Shared::DwcOccurrenceHooks include Shared::IsData include Shared::OriginRelationship ALTERNATE_VALUES_FOR = [:last_name, :first_name].freeze IGNORE_SIMILAR = [:type, :cached].freeze IGNORE_IDENTICAL = [:type, :first_name, :last_name, :prefix, :suffix].freeze # @return [true, nil] # set as true to prevent caching attr_accessor :no_cached # @return [true, nil] # set as true to prevent application of NameCase() attr_accessor :no_namecase validates_presence_of :last_name, :type validates :year_born, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_died, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_active_start, inclusion: {in: 0..Time.now.year, message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validates :year_active_end, inclusion: {in: 0..Time.now.year , message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validate :died_after_born validate :activity_ended_after_started validate :not_active_after_death validate :not_active_before_birth validate :not_gandalf validate :not_balrog before_validation :namecase_names, unless: Proc.new {|n| n.no_namecase } # TODO: remove this before_validation :set_type_if_blank after_save :set_cached, unless: Proc.new { |n| n.no_cached || errors.any? } validates :type, inclusion: { in: ['Person::Vetted', 'Person::Unvetted'], message: '%{value} is not a validly_published type'} has_one :user, dependent: :restrict_with_error, inverse_of: :person has_many :roles, dependent: :restrict_with_error, inverse_of: :person # before_remove: :set_cached_for_related has_many :author_roles, class_name: 'SourceAuthor', dependent: :restrict_with_error, inverse_of: :person #, before_remove: :set_cached_for_related has_many :collector_roles, class_name: 'Collector', dependent: :restrict_with_error, inverse_of: :person has_many :determiner_roles, class_name: 'Determiner', dependent: :restrict_with_error, inverse_of: :person has_many :editor_roles, class_name: 'SourceEditor', dependent: :restrict_with_error, inverse_of: :person has_many :georeferencer_roles, class_name: 'Georeferencer', dependent: :restrict_with_error, inverse_of: :person has_many :source_roles, class_name: 'SourceSource', dependent: :restrict_with_error, inverse_of: :person has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', dependent: :restrict_with_error, inverse_of: :person has_many :authored_sources, class_name: 'Source::Bibtex', through: :author_roles, source: :role_object, source_type: 'Source', inverse_of: :authors has_many :edited_sources, class_name: 'Source::Bibtex', through: :editor_roles, source: :role_object, source_type: 'Source', inverse_of: :editors has_many :human_sources, class_name: 'Source::Bibtex', through: :source_roles, source: :role_object, source_type: 'Source', inverse_of: :people has_many :authored_taxon_names, through: :taxon_name_author_roles, source: :role_object, source_type: 'TaxonName', class_name: 'Protonym', inverse_of: :taxon_name_authors has_many :collecting_events, through: :collector_roles, source: :role_object, source_type: 'CollectingEvent', inverse_of: :collectors has_many :georeferences, through: :georeferencer_roles, source: :role_object, source_type: 'Georeference', inverse_of: :georeference_authors has_many :taxon_determinations, through: :determiner_roles, source: :role_object, source_type: 'TaxonDetermination', inverse_of: :determiners # TODO: !? has_many :sources, through: :roles, source: :role_object, source_type: 'Source' # Editor or Author or Person has_many :collection_objects, through: :collecting_events scope :created_before, -> (time) { where('created_at < ?', time) } scope :with_role, -> (role) { includes(:roles).where(roles: {type: role}) } scope :ordered_by_last_name, -> { order(:last_name) } scope :used_in_project, -> (project_id) { joins(:roles).where( roles: { project_id: } ) } # Apply a "proper" case to all strings def namecase_names write_attribute(:last_name, NameCase(last_name)) if last_name && will_save_change_to_last_name? write_attribute(:first_name, NameCase(first_name)) if first_name && will_save_change_to_first_name? write_attribute(:prefix, NameCase(prefix)) if prefix && will_save_change_to_prefix? write_attribute(:suffix, NameCase(suffix)) if suffix && will_save_change_to_suffix? end # @return [Boolean] # !! overwrites IsData#is_in_use? def is_in_use? roles.reload.any? end # @return Boolean # whether or not this Person is linked to any data in the project def used_in_project?(project_id) Role.where(person_id: id, project_id:).any? || Source.joins(:project_sources, :roles).where(roles: {person_id: id}, project_sources: { project_id: }).any? end # @return [String] def name [first_name, prefix, last_name, suffix].compact.join(' ') end # @return [String] # The person's name in BibTeX format (von last, Jr, first) def bibtex_name out = '' out << prefix + ' ' if prefix.present? out << last_name if last_name.present? out << ', ' unless out.blank? || (first_name.blank? && suffix.blank?) out << suffix if suffix.present? out << ', ' unless out.end_with?(', ') || first_name.blank? || out.blank? out << first_name if first_name.present? out.strip end # @return [String] # The person's full last name including prefix & suffix (von last Jr) def full_last_name [prefix, last_name, suffix].compact.join(' ') end # Return [String, nil] # convenience, maybe a delegate: candidate def orcid identifiers.where(type: 'Identifier::Global::Orcid').first&.cached end # Return [String, nil] # convenience, maybe a delegate: candidate def wikidata_id identifiers.where(type: 'Identifier::Global::Wikidata').first&.cached end # @param [Integer] person_id # @return [Boolean] # true if all records updated, false if any one failed (all or none) # # No person is destroyed, see `hard_merge`. # # r_person is merged into l_person (self) # !! the intent is to keep self and remove target # def merge_with(person_id) return false if person_id == id if r_person = Person.find(person_id) # get the person to merge to into self begin ApplicationRecord.transaction do # !! Role.where(person_id: r_person.id).update(person_id: id) is BAAAD # !! It appends person_id: <old> to Role.where() in callbacks, breaking # !! Role#vet_person, etc. # update merge person's roles to old Role.where(person_id: r_person.id).each do |r| return false unless r.update(person_id: id) end roles.reload l_person_hash = annotations_hash if r_person.first_name.present? if first_name.blank? update(first_name: r_person.first_name) else if first_name != r_person.first_name # create a first_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.first_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'first_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.first_name, alternate_value_object_attribute: 'first_name', alternate_value_object_id: id) unless skip_av end end end if r_person.last_name.present? if last_name.blank? self.update(last_name: r_person.last_name) # NameCase() ? else if self.last_name != r_person.last_name # create a last_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.last_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'last_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.last_name, alternate_value_object_attribute: 'last_name', alternate_value_object_id: id) unless skip_av end end end r_person.annotations_hash.each do |r_kee, r_objects| r_objects.each do |r_o| skip = false l_test = l_person_hash[r_kee] if l_test.present? l_test.each do |l_o| # only look at same-type annotations # four types of annotations: # # data attributes, # # identifiers, # # notes, # # alternate values case r_kee when 'data attributes' if l_o.type == r_o.type && l_o.controlled_vocabulary_term_id == r_o.controlled_vocabulary_term_id && l_o.value == r_o.value && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'identifiers' if l_o.type == r_o.type && l_o.identifier == r_o.identifier && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'notes' if l_o.text == r_o.text && l_o.note_object_attribute == r_o.note.object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'alternate values' if l_o.value == r_o.value if l_o.type == r_o.type && l_o.alternate_value_object_attribute == r_o.alternate_value_object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end end end end skip end unless skip r_o.annotated_object = self r_o.save! end end end # TODO: handle prefix and suffix if false && prefix.blank? ## DD: do not change the name of verified person write_attribute(:prefix, r_person.prefix) else if r_person.prefix.present? # What to do when both have some content? end end if false && suffix.blank? ## DD: do not change the name of verified person self.suffix = r_person.suffix else if r_person.suffix.present? # What to do when both have some content? end end # TODO: handle years attributes if year_born.nil? self.year_born = r_person.year_born else unless r_person.year_born.nil? # What to do when both have some (different) numbers? end end if year_died.nil? self.year_died = r_person.year_died else unless r_person.year_died.nil? # What to do when both have some (different) numbers? end end if r_person.year_active_start # if not, r_person has nothing to contribute if self.year_active_start.nil? || (self.year_active_start > r_person.year_active_start) self.year_active_start = r_person.year_active_start end end if r_person.year_active_end # if not, r_person has nothing to contribute if self.year_active_end.nil? || (self.year_active_end < r_person.year_active_end) self.year_active_end = r_person.year_active_end end end # last thing to do in the transaction... # NO!!! - unless self.persisted? (all people are at this point persisted!) self.save! if self.changed? end rescue ActiveRecord::RecordInvalid return false end end true end def hard_merge(person_id_to_destroy) return false if id == person_id_to_destroy begin person_to_destroy = Person.find(person_id_to_destroy) Person.transaction do merge_with(person_to_destroy.id) person_to_destroy.destroy! end rescue ActiveRecord::RecordNotDestroyed return false rescue ActiveRecord::RecordInvalid return false rescue ActiveRecord::RecordNotFound return false end true end # @return [Boolean] def is_determiner? determiner_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_georeferencer? georeferencer_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_editor? editor_roles.any? end # @return [Boolean] def is_source? source_roles.any? end # @return [Boolean] def is_collector? collector_roles.any? end def role_counts(project_id) { in_project: self.roles.where(project_id:).group(:type).count, not_in_project: self.roles.where.not(project_id:).where.not(project_id: nil).group(:type).count, community: self.roles.where(project_id: nil).group(:type).count } end def dwc_occurrences ::Queries::DwcOccurrence::Filter.new(person_id: [id]).all end # @param [String] name_string # @return [Array] of Hashes # use citeproc to parse strings # see also https://github.com/SpeciesFileGroup/taxonworks/issues/1161 def self.parser(name_string) BibTeX::Entry.new(type: :book, author: name_string).parse_names.to_citeproc['author'] end # @param [String] name_string # @return [Array] of People # return people for name strings def self.parse_to_people(name_string) parser(name_string).collect { |n| Person::Unvetted.new( last_name: n['family'] ? NameCase(n['family']) : nil, first_name: n['given'] ? NameCase(n['given']) : nil, prefix: n['non-dropping-particle'] ? NameCase( n['non-dropping-particle']) : nil )} end # @param role_type [String] one of the Role types # @return [Scope] # the max 10 most recently used (1 week, could parameterize) people def self.used_recently(user_id, role_type = 'SourceAuthor') t = Role.arel_table p = Person.arel_table # i is a select manager i = t.project(t['person_id'], t['type'], t['updated_at']).from(t) .where(t['updated_at'].gt(1.week.ago)) .where(t['updated_by_id'].eq(user_id)) .where(t['type'].eq(role_type)) .order(t['updated_at'].desc) # z is a table alias z = i.as('recent_t') Person.joins( Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['person_id'].eq(p['id']))) ).pluck(:person_id).uniq end # @params Role [String] one the available roles # @return [Hash] geographic_areas optimized for user selection def self.select_optimized(user_id, project_id, role_type = 'SourceAuthor') r = used_recently(user_id, role_type) h = { quick: [], pinboard: Person.pinned_by(user_id).where(pinboard_items: {project_id:}).to_a, recent: [] } if r.empty? h[:quick] = Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a else h[:recent] = Person.where('"people"."id" IN (?)', r.first(10) ).to_a h[:quick] = ( Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a + Person.where('"people"."id" IN (?)', r.first(4) ).to_a ).uniq end h end protected # @return [Ignored] def died_after_born errors.add(:year_born, 'is older than died year') if year_born && year_died && year_born > year_died end # @return [Ignored] def activity_ended_after_started errors.add(:year_active_start, 'is older than died year') if year_active_start && year_active_end && year_active_start > year_active_end end # @return [Ignored] def not_active_after_death unless is_editor? || errors.add(:year_active_start, 'is older than year of death') if year_active_start && year_died && year_active_start > year_died errors.add(:year_active_end, 'is older than year of death') if year_active_end && year_died && year_active_end > year_died end true end # @return [Ignored] def not_active_before_birth errors.add(:year_active_start, 'is younger than than year of birth') if year_active_start && year_born && year_active_start < year_born errors.add(:year_active_end, 'is younger than year of birth') if year_active_end && year_born && year_active_end < year_born end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_gandalf errors.add(:base, 'fountain of eternal life does not exist yet') if year_born && year_died && year_died - year_born > 119 end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_balrog errors.add(:base, 'nobody is that active') if year_active_start && year_active_end && (year_active_end - year_active_start > 119) end # TODO: deprecate this, always set explicitly # @return [Ignored] def set_type_if_blank self.type = 'Person::Unvetted' if self.type.blank? end # @return [Ignored] def set_cached update_column(:cached, bibtex_name) set_role_cached end # @return [Ignored] def set_role_cached if change_to_cached_attribute? if roles.count > 25 delay(queue: :cache).update_role_cached else update_role_cached end end end # @return Integer # the total objects updated def update_role_cached # don't update the same object many times (e.g. CE of many COs?) updated = {} total = 0 roles.reload.find_each do |r| i = r.role_object.class.base_class.to_s + r.role_object.to_param next if updated[i] r.send(:set_cached) updated[i] = true total += 1 end total end # @return [Boolean] # Difficult to anticipate what # attributes will be cached in different models def change_to_cached_attribute? saved_change_to_last_name? || saved_change_to_prefix? || saved_change_to_suffix? end end |
#type ⇒ String
Person::Vetted or Person::Unvetted
51 52 53 54 55 56 57 58 59 60 61 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'app/models/person.rb', line 51 class Person < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Shared::AlternateValues include Shared::DataAttributes include Shared::Identifiers include Shared::Notes include Shared::Depictions include Shared::Tags include Shared::SharedAcrossProjects include Shared::HasPapertrail include Shared::DwcOccurrenceHooks include Shared::IsData include Shared::OriginRelationship ALTERNATE_VALUES_FOR = [:last_name, :first_name].freeze IGNORE_SIMILAR = [:type, :cached].freeze IGNORE_IDENTICAL = [:type, :first_name, :last_name, :prefix, :suffix].freeze # @return [true, nil] # set as true to prevent caching attr_accessor :no_cached # @return [true, nil] # set as true to prevent application of NameCase() attr_accessor :no_namecase validates_presence_of :last_name, :type validates :year_born, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_died, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_active_start, inclusion: {in: 0..Time.now.year, message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validates :year_active_end, inclusion: {in: 0..Time.now.year , message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validate :died_after_born validate :activity_ended_after_started validate :not_active_after_death validate :not_active_before_birth validate :not_gandalf validate :not_balrog before_validation :namecase_names, unless: Proc.new {|n| n.no_namecase } # TODO: remove this before_validation :set_type_if_blank after_save :set_cached, unless: Proc.new { |n| n.no_cached || errors.any? } validates :type, inclusion: { in: ['Person::Vetted', 'Person::Unvetted'], message: '%{value} is not a validly_published type'} has_one :user, dependent: :restrict_with_error, inverse_of: :person has_many :roles, dependent: :restrict_with_error, inverse_of: :person # before_remove: :set_cached_for_related has_many :author_roles, class_name: 'SourceAuthor', dependent: :restrict_with_error, inverse_of: :person #, before_remove: :set_cached_for_related has_many :collector_roles, class_name: 'Collector', dependent: :restrict_with_error, inverse_of: :person has_many :determiner_roles, class_name: 'Determiner', dependent: :restrict_with_error, inverse_of: :person has_many :editor_roles, class_name: 'SourceEditor', dependent: :restrict_with_error, inverse_of: :person has_many :georeferencer_roles, class_name: 'Georeferencer', dependent: :restrict_with_error, inverse_of: :person has_many :source_roles, class_name: 'SourceSource', dependent: :restrict_with_error, inverse_of: :person has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', dependent: :restrict_with_error, inverse_of: :person has_many :authored_sources, class_name: 'Source::Bibtex', through: :author_roles, source: :role_object, source_type: 'Source', inverse_of: :authors has_many :edited_sources, class_name: 'Source::Bibtex', through: :editor_roles, source: :role_object, source_type: 'Source', inverse_of: :editors has_many :human_sources, class_name: 'Source::Bibtex', through: :source_roles, source: :role_object, source_type: 'Source', inverse_of: :people has_many :authored_taxon_names, through: :taxon_name_author_roles, source: :role_object, source_type: 'TaxonName', class_name: 'Protonym', inverse_of: :taxon_name_authors has_many :collecting_events, through: :collector_roles, source: :role_object, source_type: 'CollectingEvent', inverse_of: :collectors has_many :georeferences, through: :georeferencer_roles, source: :role_object, source_type: 'Georeference', inverse_of: :georeference_authors has_many :taxon_determinations, through: :determiner_roles, source: :role_object, source_type: 'TaxonDetermination', inverse_of: :determiners # TODO: !? has_many :sources, through: :roles, source: :role_object, source_type: 'Source' # Editor or Author or Person has_many :collection_objects, through: :collecting_events scope :created_before, -> (time) { where('created_at < ?', time) } scope :with_role, -> (role) { includes(:roles).where(roles: {type: role}) } scope :ordered_by_last_name, -> { order(:last_name) } scope :used_in_project, -> (project_id) { joins(:roles).where( roles: { project_id: } ) } # Apply a "proper" case to all strings def namecase_names write_attribute(:last_name, NameCase(last_name)) if last_name && will_save_change_to_last_name? write_attribute(:first_name, NameCase(first_name)) if first_name && will_save_change_to_first_name? write_attribute(:prefix, NameCase(prefix)) if prefix && will_save_change_to_prefix? write_attribute(:suffix, NameCase(suffix)) if suffix && will_save_change_to_suffix? end # @return [Boolean] # !! overwrites IsData#is_in_use? def is_in_use? roles.reload.any? end # @return Boolean # whether or not this Person is linked to any data in the project def used_in_project?(project_id) Role.where(person_id: id, project_id:).any? || Source.joins(:project_sources, :roles).where(roles: {person_id: id}, project_sources: { project_id: }).any? end # @return [String] def name [first_name, prefix, last_name, suffix].compact.join(' ') end # @return [String] # The person's name in BibTeX format (von last, Jr, first) def bibtex_name out = '' out << prefix + ' ' if prefix.present? out << last_name if last_name.present? out << ', ' unless out.blank? || (first_name.blank? && suffix.blank?) out << suffix if suffix.present? out << ', ' unless out.end_with?(', ') || first_name.blank? || out.blank? out << first_name if first_name.present? out.strip end # @return [String] # The person's full last name including prefix & suffix (von last Jr) def full_last_name [prefix, last_name, suffix].compact.join(' ') end # Return [String, nil] # convenience, maybe a delegate: candidate def orcid identifiers.where(type: 'Identifier::Global::Orcid').first&.cached end # Return [String, nil] # convenience, maybe a delegate: candidate def wikidata_id identifiers.where(type: 'Identifier::Global::Wikidata').first&.cached end # @param [Integer] person_id # @return [Boolean] # true if all records updated, false if any one failed (all or none) # # No person is destroyed, see `hard_merge`. # # r_person is merged into l_person (self) # !! the intent is to keep self and remove target # def merge_with(person_id) return false if person_id == id if r_person = Person.find(person_id) # get the person to merge to into self begin ApplicationRecord.transaction do # !! Role.where(person_id: r_person.id).update(person_id: id) is BAAAD # !! It appends person_id: <old> to Role.where() in callbacks, breaking # !! Role#vet_person, etc. # update merge person's roles to old Role.where(person_id: r_person.id).each do |r| return false unless r.update(person_id: id) end roles.reload l_person_hash = annotations_hash if r_person.first_name.present? if first_name.blank? update(first_name: r_person.first_name) else if first_name != r_person.first_name # create a first_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.first_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'first_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.first_name, alternate_value_object_attribute: 'first_name', alternate_value_object_id: id) unless skip_av end end end if r_person.last_name.present? if last_name.blank? self.update(last_name: r_person.last_name) # NameCase() ? else if self.last_name != r_person.last_name # create a last_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.last_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'last_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.last_name, alternate_value_object_attribute: 'last_name', alternate_value_object_id: id) unless skip_av end end end r_person.annotations_hash.each do |r_kee, r_objects| r_objects.each do |r_o| skip = false l_test = l_person_hash[r_kee] if l_test.present? l_test.each do |l_o| # only look at same-type annotations # four types of annotations: # # data attributes, # # identifiers, # # notes, # # alternate values case r_kee when 'data attributes' if l_o.type == r_o.type && l_o.controlled_vocabulary_term_id == r_o.controlled_vocabulary_term_id && l_o.value == r_o.value && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'identifiers' if l_o.type == r_o.type && l_o.identifier == r_o.identifier && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'notes' if l_o.text == r_o.text && l_o.note_object_attribute == r_o.note.object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'alternate values' if l_o.value == r_o.value if l_o.type == r_o.type && l_o.alternate_value_object_attribute == r_o.alternate_value_object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end end end end skip end unless skip r_o.annotated_object = self r_o.save! end end end # TODO: handle prefix and suffix if false && prefix.blank? ## DD: do not change the name of verified person write_attribute(:prefix, r_person.prefix) else if r_person.prefix.present? # What to do when both have some content? end end if false && suffix.blank? ## DD: do not change the name of verified person self.suffix = r_person.suffix else if r_person.suffix.present? # What to do when both have some content? end end # TODO: handle years attributes if year_born.nil? self.year_born = r_person.year_born else unless r_person.year_born.nil? # What to do when both have some (different) numbers? end end if year_died.nil? self.year_died = r_person.year_died else unless r_person.year_died.nil? # What to do when both have some (different) numbers? end end if r_person.year_active_start # if not, r_person has nothing to contribute if self.year_active_start.nil? || (self.year_active_start > r_person.year_active_start) self.year_active_start = r_person.year_active_start end end if r_person.year_active_end # if not, r_person has nothing to contribute if self.year_active_end.nil? || (self.year_active_end < r_person.year_active_end) self.year_active_end = r_person.year_active_end end end # last thing to do in the transaction... # NO!!! - unless self.persisted? (all people are at this point persisted!) self.save! if self.changed? end rescue ActiveRecord::RecordInvalid return false end end true end def hard_merge(person_id_to_destroy) return false if id == person_id_to_destroy begin person_to_destroy = Person.find(person_id_to_destroy) Person.transaction do merge_with(person_to_destroy.id) person_to_destroy.destroy! end rescue ActiveRecord::RecordNotDestroyed return false rescue ActiveRecord::RecordInvalid return false rescue ActiveRecord::RecordNotFound return false end true end # @return [Boolean] def is_determiner? determiner_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_georeferencer? georeferencer_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_editor? editor_roles.any? end # @return [Boolean] def is_source? source_roles.any? end # @return [Boolean] def is_collector? collector_roles.any? end def role_counts(project_id) { in_project: self.roles.where(project_id:).group(:type).count, not_in_project: self.roles.where.not(project_id:).where.not(project_id: nil).group(:type).count, community: self.roles.where(project_id: nil).group(:type).count } end def dwc_occurrences ::Queries::DwcOccurrence::Filter.new(person_id: [id]).all end # @param [String] name_string # @return [Array] of Hashes # use citeproc to parse strings # see also https://github.com/SpeciesFileGroup/taxonworks/issues/1161 def self.parser(name_string) BibTeX::Entry.new(type: :book, author: name_string).parse_names.to_citeproc['author'] end # @param [String] name_string # @return [Array] of People # return people for name strings def self.parse_to_people(name_string) parser(name_string).collect { |n| Person::Unvetted.new( last_name: n['family'] ? NameCase(n['family']) : nil, first_name: n['given'] ? NameCase(n['given']) : nil, prefix: n['non-dropping-particle'] ? NameCase( n['non-dropping-particle']) : nil )} end # @param role_type [String] one of the Role types # @return [Scope] # the max 10 most recently used (1 week, could parameterize) people def self.used_recently(user_id, role_type = 'SourceAuthor') t = Role.arel_table p = Person.arel_table # i is a select manager i = t.project(t['person_id'], t['type'], t['updated_at']).from(t) .where(t['updated_at'].gt(1.week.ago)) .where(t['updated_by_id'].eq(user_id)) .where(t['type'].eq(role_type)) .order(t['updated_at'].desc) # z is a table alias z = i.as('recent_t') Person.joins( Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['person_id'].eq(p['id']))) ).pluck(:person_id).uniq end # @params Role [String] one the available roles # @return [Hash] geographic_areas optimized for user selection def self.select_optimized(user_id, project_id, role_type = 'SourceAuthor') r = used_recently(user_id, role_type) h = { quick: [], pinboard: Person.pinned_by(user_id).where(pinboard_items: {project_id:}).to_a, recent: [] } if r.empty? h[:quick] = Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a else h[:recent] = Person.where('"people"."id" IN (?)', r.first(10) ).to_a h[:quick] = ( Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a + Person.where('"people"."id" IN (?)', r.first(4) ).to_a ).uniq end h end protected # @return [Ignored] def died_after_born errors.add(:year_born, 'is older than died year') if year_born && year_died && year_born > year_died end # @return [Ignored] def activity_ended_after_started errors.add(:year_active_start, 'is older than died year') if year_active_start && year_active_end && year_active_start > year_active_end end # @return [Ignored] def not_active_after_death unless is_editor? || errors.add(:year_active_start, 'is older than year of death') if year_active_start && year_died && year_active_start > year_died errors.add(:year_active_end, 'is older than year of death') if year_active_end && year_died && year_active_end > year_died end true end # @return [Ignored] def not_active_before_birth errors.add(:year_active_start, 'is younger than than year of birth') if year_active_start && year_born && year_active_start < year_born errors.add(:year_active_end, 'is younger than year of birth') if year_active_end && year_born && year_active_end < year_born end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_gandalf errors.add(:base, 'fountain of eternal life does not exist yet') if year_born && year_died && year_died - year_born > 119 end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_balrog errors.add(:base, 'nobody is that active') if year_active_start && year_active_end && (year_active_end - year_active_start > 119) end # TODO: deprecate this, always set explicitly # @return [Ignored] def set_type_if_blank self.type = 'Person::Unvetted' if self.type.blank? end # @return [Ignored] def set_cached update_column(:cached, bibtex_name) set_role_cached end # @return [Ignored] def set_role_cached if change_to_cached_attribute? if roles.count > 25 delay(queue: :cache).update_role_cached else update_role_cached end end end # @return Integer # the total objects updated def update_role_cached # don't update the same object many times (e.g. CE of many COs?) updated = {} total = 0 roles.reload.find_each do |r| i = r.role_object.class.base_class.to_s + r.role_object.to_param next if updated[i] r.send(:set_cached) updated[i] = true total += 1 end total end # @return [Boolean] # Difficult to anticipate what # attributes will be cached in different models def change_to_cached_attribute? saved_change_to_last_name? || saved_change_to_prefix? || saved_change_to_suffix? end end |
#year_active_end ⇒ Integer
Returns (rough) ending point of when person made scientific assertions that were dissemenated (i.e. could be seen by others).
51 52 53 54 55 56 57 58 59 60 61 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'app/models/person.rb', line 51 class Person < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Shared::AlternateValues include Shared::DataAttributes include Shared::Identifiers include Shared::Notes include Shared::Depictions include Shared::Tags include Shared::SharedAcrossProjects include Shared::HasPapertrail include Shared::DwcOccurrenceHooks include Shared::IsData include Shared::OriginRelationship ALTERNATE_VALUES_FOR = [:last_name, :first_name].freeze IGNORE_SIMILAR = [:type, :cached].freeze IGNORE_IDENTICAL = [:type, :first_name, :last_name, :prefix, :suffix].freeze # @return [true, nil] # set as true to prevent caching attr_accessor :no_cached # @return [true, nil] # set as true to prevent application of NameCase() attr_accessor :no_namecase validates_presence_of :last_name, :type validates :year_born, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_died, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_active_start, inclusion: {in: 0..Time.now.year, message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validates :year_active_end, inclusion: {in: 0..Time.now.year , message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validate :died_after_born validate :activity_ended_after_started validate :not_active_after_death validate :not_active_before_birth validate :not_gandalf validate :not_balrog before_validation :namecase_names, unless: Proc.new {|n| n.no_namecase } # TODO: remove this before_validation :set_type_if_blank after_save :set_cached, unless: Proc.new { |n| n.no_cached || errors.any? } validates :type, inclusion: { in: ['Person::Vetted', 'Person::Unvetted'], message: '%{value} is not a validly_published type'} has_one :user, dependent: :restrict_with_error, inverse_of: :person has_many :roles, dependent: :restrict_with_error, inverse_of: :person # before_remove: :set_cached_for_related has_many :author_roles, class_name: 'SourceAuthor', dependent: :restrict_with_error, inverse_of: :person #, before_remove: :set_cached_for_related has_many :collector_roles, class_name: 'Collector', dependent: :restrict_with_error, inverse_of: :person has_many :determiner_roles, class_name: 'Determiner', dependent: :restrict_with_error, inverse_of: :person has_many :editor_roles, class_name: 'SourceEditor', dependent: :restrict_with_error, inverse_of: :person has_many :georeferencer_roles, class_name: 'Georeferencer', dependent: :restrict_with_error, inverse_of: :person has_many :source_roles, class_name: 'SourceSource', dependent: :restrict_with_error, inverse_of: :person has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', dependent: :restrict_with_error, inverse_of: :person has_many :authored_sources, class_name: 'Source::Bibtex', through: :author_roles, source: :role_object, source_type: 'Source', inverse_of: :authors has_many :edited_sources, class_name: 'Source::Bibtex', through: :editor_roles, source: :role_object, source_type: 'Source', inverse_of: :editors has_many :human_sources, class_name: 'Source::Bibtex', through: :source_roles, source: :role_object, source_type: 'Source', inverse_of: :people has_many :authored_taxon_names, through: :taxon_name_author_roles, source: :role_object, source_type: 'TaxonName', class_name: 'Protonym', inverse_of: :taxon_name_authors has_many :collecting_events, through: :collector_roles, source: :role_object, source_type: 'CollectingEvent', inverse_of: :collectors has_many :georeferences, through: :georeferencer_roles, source: :role_object, source_type: 'Georeference', inverse_of: :georeference_authors has_many :taxon_determinations, through: :determiner_roles, source: :role_object, source_type: 'TaxonDetermination', inverse_of: :determiners # TODO: !? has_many :sources, through: :roles, source: :role_object, source_type: 'Source' # Editor or Author or Person has_many :collection_objects, through: :collecting_events scope :created_before, -> (time) { where('created_at < ?', time) } scope :with_role, -> (role) { includes(:roles).where(roles: {type: role}) } scope :ordered_by_last_name, -> { order(:last_name) } scope :used_in_project, -> (project_id) { joins(:roles).where( roles: { project_id: } ) } # Apply a "proper" case to all strings def namecase_names write_attribute(:last_name, NameCase(last_name)) if last_name && will_save_change_to_last_name? write_attribute(:first_name, NameCase(first_name)) if first_name && will_save_change_to_first_name? write_attribute(:prefix, NameCase(prefix)) if prefix && will_save_change_to_prefix? write_attribute(:suffix, NameCase(suffix)) if suffix && will_save_change_to_suffix? end # @return [Boolean] # !! overwrites IsData#is_in_use? def is_in_use? roles.reload.any? end # @return Boolean # whether or not this Person is linked to any data in the project def used_in_project?(project_id) Role.where(person_id: id, project_id:).any? || Source.joins(:project_sources, :roles).where(roles: {person_id: id}, project_sources: { project_id: }).any? end # @return [String] def name [first_name, prefix, last_name, suffix].compact.join(' ') end # @return [String] # The person's name in BibTeX format (von last, Jr, first) def bibtex_name out = '' out << prefix + ' ' if prefix.present? out << last_name if last_name.present? out << ', ' unless out.blank? || (first_name.blank? && suffix.blank?) out << suffix if suffix.present? out << ', ' unless out.end_with?(', ') || first_name.blank? || out.blank? out << first_name if first_name.present? out.strip end # @return [String] # The person's full last name including prefix & suffix (von last Jr) def full_last_name [prefix, last_name, suffix].compact.join(' ') end # Return [String, nil] # convenience, maybe a delegate: candidate def orcid identifiers.where(type: 'Identifier::Global::Orcid').first&.cached end # Return [String, nil] # convenience, maybe a delegate: candidate def wikidata_id identifiers.where(type: 'Identifier::Global::Wikidata').first&.cached end # @param [Integer] person_id # @return [Boolean] # true if all records updated, false if any one failed (all or none) # # No person is destroyed, see `hard_merge`. # # r_person is merged into l_person (self) # !! the intent is to keep self and remove target # def merge_with(person_id) return false if person_id == id if r_person = Person.find(person_id) # get the person to merge to into self begin ApplicationRecord.transaction do # !! Role.where(person_id: r_person.id).update(person_id: id) is BAAAD # !! It appends person_id: <old> to Role.where() in callbacks, breaking # !! Role#vet_person, etc. # update merge person's roles to old Role.where(person_id: r_person.id).each do |r| return false unless r.update(person_id: id) end roles.reload l_person_hash = annotations_hash if r_person.first_name.present? if first_name.blank? update(first_name: r_person.first_name) else if first_name != r_person.first_name # create a first_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.first_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'first_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.first_name, alternate_value_object_attribute: 'first_name', alternate_value_object_id: id) unless skip_av end end end if r_person.last_name.present? if last_name.blank? self.update(last_name: r_person.last_name) # NameCase() ? else if self.last_name != r_person.last_name # create a last_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.last_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'last_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.last_name, alternate_value_object_attribute: 'last_name', alternate_value_object_id: id) unless skip_av end end end r_person.annotations_hash.each do |r_kee, r_objects| r_objects.each do |r_o| skip = false l_test = l_person_hash[r_kee] if l_test.present? l_test.each do |l_o| # only look at same-type annotations # four types of annotations: # # data attributes, # # identifiers, # # notes, # # alternate values case r_kee when 'data attributes' if l_o.type == r_o.type && l_o.controlled_vocabulary_term_id == r_o.controlled_vocabulary_term_id && l_o.value == r_o.value && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'identifiers' if l_o.type == r_o.type && l_o.identifier == r_o.identifier && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'notes' if l_o.text == r_o.text && l_o.note_object_attribute == r_o.note.object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'alternate values' if l_o.value == r_o.value if l_o.type == r_o.type && l_o.alternate_value_object_attribute == r_o.alternate_value_object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end end end end skip end unless skip r_o.annotated_object = self r_o.save! end end end # TODO: handle prefix and suffix if false && prefix.blank? ## DD: do not change the name of verified person write_attribute(:prefix, r_person.prefix) else if r_person.prefix.present? # What to do when both have some content? end end if false && suffix.blank? ## DD: do not change the name of verified person self.suffix = r_person.suffix else if r_person.suffix.present? # What to do when both have some content? end end # TODO: handle years attributes if year_born.nil? self.year_born = r_person.year_born else unless r_person.year_born.nil? # What to do when both have some (different) numbers? end end if year_died.nil? self.year_died = r_person.year_died else unless r_person.year_died.nil? # What to do when both have some (different) numbers? end end if r_person.year_active_start # if not, r_person has nothing to contribute if self.year_active_start.nil? || (self.year_active_start > r_person.year_active_start) self.year_active_start = r_person.year_active_start end end if r_person.year_active_end # if not, r_person has nothing to contribute if self.year_active_end.nil? || (self.year_active_end < r_person.year_active_end) self.year_active_end = r_person.year_active_end end end # last thing to do in the transaction... # NO!!! - unless self.persisted? (all people are at this point persisted!) self.save! if self.changed? end rescue ActiveRecord::RecordInvalid return false end end true end def hard_merge(person_id_to_destroy) return false if id == person_id_to_destroy begin person_to_destroy = Person.find(person_id_to_destroy) Person.transaction do merge_with(person_to_destroy.id) person_to_destroy.destroy! end rescue ActiveRecord::RecordNotDestroyed return false rescue ActiveRecord::RecordInvalid return false rescue ActiveRecord::RecordNotFound return false end true end # @return [Boolean] def is_determiner? determiner_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_georeferencer? georeferencer_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_editor? editor_roles.any? end # @return [Boolean] def is_source? source_roles.any? end # @return [Boolean] def is_collector? collector_roles.any? end def role_counts(project_id) { in_project: self.roles.where(project_id:).group(:type).count, not_in_project: self.roles.where.not(project_id:).where.not(project_id: nil).group(:type).count, community: self.roles.where(project_id: nil).group(:type).count } end def dwc_occurrences ::Queries::DwcOccurrence::Filter.new(person_id: [id]).all end # @param [String] name_string # @return [Array] of Hashes # use citeproc to parse strings # see also https://github.com/SpeciesFileGroup/taxonworks/issues/1161 def self.parser(name_string) BibTeX::Entry.new(type: :book, author: name_string).parse_names.to_citeproc['author'] end # @param [String] name_string # @return [Array] of People # return people for name strings def self.parse_to_people(name_string) parser(name_string).collect { |n| Person::Unvetted.new( last_name: n['family'] ? NameCase(n['family']) : nil, first_name: n['given'] ? NameCase(n['given']) : nil, prefix: n['non-dropping-particle'] ? NameCase( n['non-dropping-particle']) : nil )} end # @param role_type [String] one of the Role types # @return [Scope] # the max 10 most recently used (1 week, could parameterize) people def self.used_recently(user_id, role_type = 'SourceAuthor') t = Role.arel_table p = Person.arel_table # i is a select manager i = t.project(t['person_id'], t['type'], t['updated_at']).from(t) .where(t['updated_at'].gt(1.week.ago)) .where(t['updated_by_id'].eq(user_id)) .where(t['type'].eq(role_type)) .order(t['updated_at'].desc) # z is a table alias z = i.as('recent_t') Person.joins( Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['person_id'].eq(p['id']))) ).pluck(:person_id).uniq end # @params Role [String] one the available roles # @return [Hash] geographic_areas optimized for user selection def self.select_optimized(user_id, project_id, role_type = 'SourceAuthor') r = used_recently(user_id, role_type) h = { quick: [], pinboard: Person.pinned_by(user_id).where(pinboard_items: {project_id:}).to_a, recent: [] } if r.empty? h[:quick] = Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a else h[:recent] = Person.where('"people"."id" IN (?)', r.first(10) ).to_a h[:quick] = ( Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a + Person.where('"people"."id" IN (?)', r.first(4) ).to_a ).uniq end h end protected # @return [Ignored] def died_after_born errors.add(:year_born, 'is older than died year') if year_born && year_died && year_born > year_died end # @return [Ignored] def activity_ended_after_started errors.add(:year_active_start, 'is older than died year') if year_active_start && year_active_end && year_active_start > year_active_end end # @return [Ignored] def not_active_after_death unless is_editor? || errors.add(:year_active_start, 'is older than year of death') if year_active_start && year_died && year_active_start > year_died errors.add(:year_active_end, 'is older than year of death') if year_active_end && year_died && year_active_end > year_died end true end # @return [Ignored] def not_active_before_birth errors.add(:year_active_start, 'is younger than than year of birth') if year_active_start && year_born && year_active_start < year_born errors.add(:year_active_end, 'is younger than year of birth') if year_active_end && year_born && year_active_end < year_born end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_gandalf errors.add(:base, 'fountain of eternal life does not exist yet') if year_born && year_died && year_died - year_born > 119 end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_balrog errors.add(:base, 'nobody is that active') if year_active_start && year_active_end && (year_active_end - year_active_start > 119) end # TODO: deprecate this, always set explicitly # @return [Ignored] def set_type_if_blank self.type = 'Person::Unvetted' if self.type.blank? end # @return [Ignored] def set_cached update_column(:cached, bibtex_name) set_role_cached end # @return [Ignored] def set_role_cached if change_to_cached_attribute? if roles.count > 25 delay(queue: :cache).update_role_cached else update_role_cached end end end # @return Integer # the total objects updated def update_role_cached # don't update the same object many times (e.g. CE of many COs?) updated = {} total = 0 roles.reload.find_each do |r| i = r.role_object.class.base_class.to_s + r.role_object.to_param next if updated[i] r.send(:set_cached) updated[i] = true total += 1 end total end # @return [Boolean] # Difficult to anticipate what # attributes will be cached in different models def change_to_cached_attribute? saved_change_to_last_name? || saved_change_to_prefix? || saved_change_to_suffix? end end |
#year_active_start ⇒ Integer
Returns (rough) starting point of when person made scientific assertions that were dissemenated (i.e. could be seen by others).
51 52 53 54 55 56 57 58 59 60 61 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'app/models/person.rb', line 51 class Person < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Shared::AlternateValues include Shared::DataAttributes include Shared::Identifiers include Shared::Notes include Shared::Depictions include Shared::Tags include Shared::SharedAcrossProjects include Shared::HasPapertrail include Shared::DwcOccurrenceHooks include Shared::IsData include Shared::OriginRelationship ALTERNATE_VALUES_FOR = [:last_name, :first_name].freeze IGNORE_SIMILAR = [:type, :cached].freeze IGNORE_IDENTICAL = [:type, :first_name, :last_name, :prefix, :suffix].freeze # @return [true, nil] # set as true to prevent caching attr_accessor :no_cached # @return [true, nil] # set as true to prevent application of NameCase() attr_accessor :no_namecase validates_presence_of :last_name, :type validates :year_born, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_died, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_active_start, inclusion: {in: 0..Time.now.year, message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validates :year_active_end, inclusion: {in: 0..Time.now.year , message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validate :died_after_born validate :activity_ended_after_started validate :not_active_after_death validate :not_active_before_birth validate :not_gandalf validate :not_balrog before_validation :namecase_names, unless: Proc.new {|n| n.no_namecase } # TODO: remove this before_validation :set_type_if_blank after_save :set_cached, unless: Proc.new { |n| n.no_cached || errors.any? } validates :type, inclusion: { in: ['Person::Vetted', 'Person::Unvetted'], message: '%{value} is not a validly_published type'} has_one :user, dependent: :restrict_with_error, inverse_of: :person has_many :roles, dependent: :restrict_with_error, inverse_of: :person # before_remove: :set_cached_for_related has_many :author_roles, class_name: 'SourceAuthor', dependent: :restrict_with_error, inverse_of: :person #, before_remove: :set_cached_for_related has_many :collector_roles, class_name: 'Collector', dependent: :restrict_with_error, inverse_of: :person has_many :determiner_roles, class_name: 'Determiner', dependent: :restrict_with_error, inverse_of: :person has_many :editor_roles, class_name: 'SourceEditor', dependent: :restrict_with_error, inverse_of: :person has_many :georeferencer_roles, class_name: 'Georeferencer', dependent: :restrict_with_error, inverse_of: :person has_many :source_roles, class_name: 'SourceSource', dependent: :restrict_with_error, inverse_of: :person has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', dependent: :restrict_with_error, inverse_of: :person has_many :authored_sources, class_name: 'Source::Bibtex', through: :author_roles, source: :role_object, source_type: 'Source', inverse_of: :authors has_many :edited_sources, class_name: 'Source::Bibtex', through: :editor_roles, source: :role_object, source_type: 'Source', inverse_of: :editors has_many :human_sources, class_name: 'Source::Bibtex', through: :source_roles, source: :role_object, source_type: 'Source', inverse_of: :people has_many :authored_taxon_names, through: :taxon_name_author_roles, source: :role_object, source_type: 'TaxonName', class_name: 'Protonym', inverse_of: :taxon_name_authors has_many :collecting_events, through: :collector_roles, source: :role_object, source_type: 'CollectingEvent', inverse_of: :collectors has_many :georeferences, through: :georeferencer_roles, source: :role_object, source_type: 'Georeference', inverse_of: :georeference_authors has_many :taxon_determinations, through: :determiner_roles, source: :role_object, source_type: 'TaxonDetermination', inverse_of: :determiners # TODO: !? has_many :sources, through: :roles, source: :role_object, source_type: 'Source' # Editor or Author or Person has_many :collection_objects, through: :collecting_events scope :created_before, -> (time) { where('created_at < ?', time) } scope :with_role, -> (role) { includes(:roles).where(roles: {type: role}) } scope :ordered_by_last_name, -> { order(:last_name) } scope :used_in_project, -> (project_id) { joins(:roles).where( roles: { project_id: } ) } # Apply a "proper" case to all strings def namecase_names write_attribute(:last_name, NameCase(last_name)) if last_name && will_save_change_to_last_name? write_attribute(:first_name, NameCase(first_name)) if first_name && will_save_change_to_first_name? write_attribute(:prefix, NameCase(prefix)) if prefix && will_save_change_to_prefix? write_attribute(:suffix, NameCase(suffix)) if suffix && will_save_change_to_suffix? end # @return [Boolean] # !! overwrites IsData#is_in_use? def is_in_use? roles.reload.any? end # @return Boolean # whether or not this Person is linked to any data in the project def used_in_project?(project_id) Role.where(person_id: id, project_id:).any? || Source.joins(:project_sources, :roles).where(roles: {person_id: id}, project_sources: { project_id: }).any? end # @return [String] def name [first_name, prefix, last_name, suffix].compact.join(' ') end # @return [String] # The person's name in BibTeX format (von last, Jr, first) def bibtex_name out = '' out << prefix + ' ' if prefix.present? out << last_name if last_name.present? out << ', ' unless out.blank? || (first_name.blank? && suffix.blank?) out << suffix if suffix.present? out << ', ' unless out.end_with?(', ') || first_name.blank? || out.blank? out << first_name if first_name.present? out.strip end # @return [String] # The person's full last name including prefix & suffix (von last Jr) def full_last_name [prefix, last_name, suffix].compact.join(' ') end # Return [String, nil] # convenience, maybe a delegate: candidate def orcid identifiers.where(type: 'Identifier::Global::Orcid').first&.cached end # Return [String, nil] # convenience, maybe a delegate: candidate def wikidata_id identifiers.where(type: 'Identifier::Global::Wikidata').first&.cached end # @param [Integer] person_id # @return [Boolean] # true if all records updated, false if any one failed (all or none) # # No person is destroyed, see `hard_merge`. # # r_person is merged into l_person (self) # !! the intent is to keep self and remove target # def merge_with(person_id) return false if person_id == id if r_person = Person.find(person_id) # get the person to merge to into self begin ApplicationRecord.transaction do # !! Role.where(person_id: r_person.id).update(person_id: id) is BAAAD # !! It appends person_id: <old> to Role.where() in callbacks, breaking # !! Role#vet_person, etc. # update merge person's roles to old Role.where(person_id: r_person.id).each do |r| return false unless r.update(person_id: id) end roles.reload l_person_hash = annotations_hash if r_person.first_name.present? if first_name.blank? update(first_name: r_person.first_name) else if first_name != r_person.first_name # create a first_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.first_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'first_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.first_name, alternate_value_object_attribute: 'first_name', alternate_value_object_id: id) unless skip_av end end end if r_person.last_name.present? if last_name.blank? self.update(last_name: r_person.last_name) # NameCase() ? else if self.last_name != r_person.last_name # create a last_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.last_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'last_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.last_name, alternate_value_object_attribute: 'last_name', alternate_value_object_id: id) unless skip_av end end end r_person.annotations_hash.each do |r_kee, r_objects| r_objects.each do |r_o| skip = false l_test = l_person_hash[r_kee] if l_test.present? l_test.each do |l_o| # only look at same-type annotations # four types of annotations: # # data attributes, # # identifiers, # # notes, # # alternate values case r_kee when 'data attributes' if l_o.type == r_o.type && l_o.controlled_vocabulary_term_id == r_o.controlled_vocabulary_term_id && l_o.value == r_o.value && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'identifiers' if l_o.type == r_o.type && l_o.identifier == r_o.identifier && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'notes' if l_o.text == r_o.text && l_o.note_object_attribute == r_o.note.object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'alternate values' if l_o.value == r_o.value if l_o.type == r_o.type && l_o.alternate_value_object_attribute == r_o.alternate_value_object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end end end end skip end unless skip r_o.annotated_object = self r_o.save! end end end # TODO: handle prefix and suffix if false && prefix.blank? ## DD: do not change the name of verified person write_attribute(:prefix, r_person.prefix) else if r_person.prefix.present? # What to do when both have some content? end end if false && suffix.blank? ## DD: do not change the name of verified person self.suffix = r_person.suffix else if r_person.suffix.present? # What to do when both have some content? end end # TODO: handle years attributes if year_born.nil? self.year_born = r_person.year_born else unless r_person.year_born.nil? # What to do when both have some (different) numbers? end end if year_died.nil? self.year_died = r_person.year_died else unless r_person.year_died.nil? # What to do when both have some (different) numbers? end end if r_person.year_active_start # if not, r_person has nothing to contribute if self.year_active_start.nil? || (self.year_active_start > r_person.year_active_start) self.year_active_start = r_person.year_active_start end end if r_person.year_active_end # if not, r_person has nothing to contribute if self.year_active_end.nil? || (self.year_active_end < r_person.year_active_end) self.year_active_end = r_person.year_active_end end end # last thing to do in the transaction... # NO!!! - unless self.persisted? (all people are at this point persisted!) self.save! if self.changed? end rescue ActiveRecord::RecordInvalid return false end end true end def hard_merge(person_id_to_destroy) return false if id == person_id_to_destroy begin person_to_destroy = Person.find(person_id_to_destroy) Person.transaction do merge_with(person_to_destroy.id) person_to_destroy.destroy! end rescue ActiveRecord::RecordNotDestroyed return false rescue ActiveRecord::RecordInvalid return false rescue ActiveRecord::RecordNotFound return false end true end # @return [Boolean] def is_determiner? determiner_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_georeferencer? georeferencer_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_editor? editor_roles.any? end # @return [Boolean] def is_source? source_roles.any? end # @return [Boolean] def is_collector? collector_roles.any? end def role_counts(project_id) { in_project: self.roles.where(project_id:).group(:type).count, not_in_project: self.roles.where.not(project_id:).where.not(project_id: nil).group(:type).count, community: self.roles.where(project_id: nil).group(:type).count } end def dwc_occurrences ::Queries::DwcOccurrence::Filter.new(person_id: [id]).all end # @param [String] name_string # @return [Array] of Hashes # use citeproc to parse strings # see also https://github.com/SpeciesFileGroup/taxonworks/issues/1161 def self.parser(name_string) BibTeX::Entry.new(type: :book, author: name_string).parse_names.to_citeproc['author'] end # @param [String] name_string # @return [Array] of People # return people for name strings def self.parse_to_people(name_string) parser(name_string).collect { |n| Person::Unvetted.new( last_name: n['family'] ? NameCase(n['family']) : nil, first_name: n['given'] ? NameCase(n['given']) : nil, prefix: n['non-dropping-particle'] ? NameCase( n['non-dropping-particle']) : nil )} end # @param role_type [String] one of the Role types # @return [Scope] # the max 10 most recently used (1 week, could parameterize) people def self.used_recently(user_id, role_type = 'SourceAuthor') t = Role.arel_table p = Person.arel_table # i is a select manager i = t.project(t['person_id'], t['type'], t['updated_at']).from(t) .where(t['updated_at'].gt(1.week.ago)) .where(t['updated_by_id'].eq(user_id)) .where(t['type'].eq(role_type)) .order(t['updated_at'].desc) # z is a table alias z = i.as('recent_t') Person.joins( Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['person_id'].eq(p['id']))) ).pluck(:person_id).uniq end # @params Role [String] one the available roles # @return [Hash] geographic_areas optimized for user selection def self.select_optimized(user_id, project_id, role_type = 'SourceAuthor') r = used_recently(user_id, role_type) h = { quick: [], pinboard: Person.pinned_by(user_id).where(pinboard_items: {project_id:}).to_a, recent: [] } if r.empty? h[:quick] = Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a else h[:recent] = Person.where('"people"."id" IN (?)', r.first(10) ).to_a h[:quick] = ( Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a + Person.where('"people"."id" IN (?)', r.first(4) ).to_a ).uniq end h end protected # @return [Ignored] def died_after_born errors.add(:year_born, 'is older than died year') if year_born && year_died && year_born > year_died end # @return [Ignored] def activity_ended_after_started errors.add(:year_active_start, 'is older than died year') if year_active_start && year_active_end && year_active_start > year_active_end end # @return [Ignored] def not_active_after_death unless is_editor? || errors.add(:year_active_start, 'is older than year of death') if year_active_start && year_died && year_active_start > year_died errors.add(:year_active_end, 'is older than year of death') if year_active_end && year_died && year_active_end > year_died end true end # @return [Ignored] def not_active_before_birth errors.add(:year_active_start, 'is younger than than year of birth') if year_active_start && year_born && year_active_start < year_born errors.add(:year_active_end, 'is younger than year of birth') if year_active_end && year_born && year_active_end < year_born end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_gandalf errors.add(:base, 'fountain of eternal life does not exist yet') if year_born && year_died && year_died - year_born > 119 end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_balrog errors.add(:base, 'nobody is that active') if year_active_start && year_active_end && (year_active_end - year_active_start > 119) end # TODO: deprecate this, always set explicitly # @return [Ignored] def set_type_if_blank self.type = 'Person::Unvetted' if self.type.blank? end # @return [Ignored] def set_cached update_column(:cached, bibtex_name) set_role_cached end # @return [Ignored] def set_role_cached if change_to_cached_attribute? if roles.count > 25 delay(queue: :cache).update_role_cached else update_role_cached end end end # @return Integer # the total objects updated def update_role_cached # don't update the same object many times (e.g. CE of many COs?) updated = {} total = 0 roles.reload.find_each do |r| i = r.role_object.class.base_class.to_s + r.role_object.to_param next if updated[i] r.send(:set_cached) updated[i] = true total += 1 end total end # @return [Boolean] # Difficult to anticipate what # attributes will be cached in different models def change_to_cached_attribute? saved_change_to_last_name? || saved_change_to_prefix? || saved_change_to_suffix? end end |
#year_born ⇒ Integer
Returns born on.
51 52 53 54 55 56 57 58 59 60 61 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'app/models/person.rb', line 51 class Person < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Shared::AlternateValues include Shared::DataAttributes include Shared::Identifiers include Shared::Notes include Shared::Depictions include Shared::Tags include Shared::SharedAcrossProjects include Shared::HasPapertrail include Shared::DwcOccurrenceHooks include Shared::IsData include Shared::OriginRelationship ALTERNATE_VALUES_FOR = [:last_name, :first_name].freeze IGNORE_SIMILAR = [:type, :cached].freeze IGNORE_IDENTICAL = [:type, :first_name, :last_name, :prefix, :suffix].freeze # @return [true, nil] # set as true to prevent caching attr_accessor :no_cached # @return [true, nil] # set as true to prevent application of NameCase() attr_accessor :no_namecase validates_presence_of :last_name, :type validates :year_born, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_died, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_active_start, inclusion: {in: 0..Time.now.year, message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validates :year_active_end, inclusion: {in: 0..Time.now.year , message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validate :died_after_born validate :activity_ended_after_started validate :not_active_after_death validate :not_active_before_birth validate :not_gandalf validate :not_balrog before_validation :namecase_names, unless: Proc.new {|n| n.no_namecase } # TODO: remove this before_validation :set_type_if_blank after_save :set_cached, unless: Proc.new { |n| n.no_cached || errors.any? } validates :type, inclusion: { in: ['Person::Vetted', 'Person::Unvetted'], message: '%{value} is not a validly_published type'} has_one :user, dependent: :restrict_with_error, inverse_of: :person has_many :roles, dependent: :restrict_with_error, inverse_of: :person # before_remove: :set_cached_for_related has_many :author_roles, class_name: 'SourceAuthor', dependent: :restrict_with_error, inverse_of: :person #, before_remove: :set_cached_for_related has_many :collector_roles, class_name: 'Collector', dependent: :restrict_with_error, inverse_of: :person has_many :determiner_roles, class_name: 'Determiner', dependent: :restrict_with_error, inverse_of: :person has_many :editor_roles, class_name: 'SourceEditor', dependent: :restrict_with_error, inverse_of: :person has_many :georeferencer_roles, class_name: 'Georeferencer', dependent: :restrict_with_error, inverse_of: :person has_many :source_roles, class_name: 'SourceSource', dependent: :restrict_with_error, inverse_of: :person has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', dependent: :restrict_with_error, inverse_of: :person has_many :authored_sources, class_name: 'Source::Bibtex', through: :author_roles, source: :role_object, source_type: 'Source', inverse_of: :authors has_many :edited_sources, class_name: 'Source::Bibtex', through: :editor_roles, source: :role_object, source_type: 'Source', inverse_of: :editors has_many :human_sources, class_name: 'Source::Bibtex', through: :source_roles, source: :role_object, source_type: 'Source', inverse_of: :people has_many :authored_taxon_names, through: :taxon_name_author_roles, source: :role_object, source_type: 'TaxonName', class_name: 'Protonym', inverse_of: :taxon_name_authors has_many :collecting_events, through: :collector_roles, source: :role_object, source_type: 'CollectingEvent', inverse_of: :collectors has_many :georeferences, through: :georeferencer_roles, source: :role_object, source_type: 'Georeference', inverse_of: :georeference_authors has_many :taxon_determinations, through: :determiner_roles, source: :role_object, source_type: 'TaxonDetermination', inverse_of: :determiners # TODO: !? has_many :sources, through: :roles, source: :role_object, source_type: 'Source' # Editor or Author or Person has_many :collection_objects, through: :collecting_events scope :created_before, -> (time) { where('created_at < ?', time) } scope :with_role, -> (role) { includes(:roles).where(roles: {type: role}) } scope :ordered_by_last_name, -> { order(:last_name) } scope :used_in_project, -> (project_id) { joins(:roles).where( roles: { project_id: } ) } # Apply a "proper" case to all strings def namecase_names write_attribute(:last_name, NameCase(last_name)) if last_name && will_save_change_to_last_name? write_attribute(:first_name, NameCase(first_name)) if first_name && will_save_change_to_first_name? write_attribute(:prefix, NameCase(prefix)) if prefix && will_save_change_to_prefix? write_attribute(:suffix, NameCase(suffix)) if suffix && will_save_change_to_suffix? end # @return [Boolean] # !! overwrites IsData#is_in_use? def is_in_use? roles.reload.any? end # @return Boolean # whether or not this Person is linked to any data in the project def used_in_project?(project_id) Role.where(person_id: id, project_id:).any? || Source.joins(:project_sources, :roles).where(roles: {person_id: id}, project_sources: { project_id: }).any? end # @return [String] def name [first_name, prefix, last_name, suffix].compact.join(' ') end # @return [String] # The person's name in BibTeX format (von last, Jr, first) def bibtex_name out = '' out << prefix + ' ' if prefix.present? out << last_name if last_name.present? out << ', ' unless out.blank? || (first_name.blank? && suffix.blank?) out << suffix if suffix.present? out << ', ' unless out.end_with?(', ') || first_name.blank? || out.blank? out << first_name if first_name.present? out.strip end # @return [String] # The person's full last name including prefix & suffix (von last Jr) def full_last_name [prefix, last_name, suffix].compact.join(' ') end # Return [String, nil] # convenience, maybe a delegate: candidate def orcid identifiers.where(type: 'Identifier::Global::Orcid').first&.cached end # Return [String, nil] # convenience, maybe a delegate: candidate def wikidata_id identifiers.where(type: 'Identifier::Global::Wikidata').first&.cached end # @param [Integer] person_id # @return [Boolean] # true if all records updated, false if any one failed (all or none) # # No person is destroyed, see `hard_merge`. # # r_person is merged into l_person (self) # !! the intent is to keep self and remove target # def merge_with(person_id) return false if person_id == id if r_person = Person.find(person_id) # get the person to merge to into self begin ApplicationRecord.transaction do # !! Role.where(person_id: r_person.id).update(person_id: id) is BAAAD # !! It appends person_id: <old> to Role.where() in callbacks, breaking # !! Role#vet_person, etc. # update merge person's roles to old Role.where(person_id: r_person.id).each do |r| return false unless r.update(person_id: id) end roles.reload l_person_hash = annotations_hash if r_person.first_name.present? if first_name.blank? update(first_name: r_person.first_name) else if first_name != r_person.first_name # create a first_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.first_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'first_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.first_name, alternate_value_object_attribute: 'first_name', alternate_value_object_id: id) unless skip_av end end end if r_person.last_name.present? if last_name.blank? self.update(last_name: r_person.last_name) # NameCase() ? else if self.last_name != r_person.last_name # create a last_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.last_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'last_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.last_name, alternate_value_object_attribute: 'last_name', alternate_value_object_id: id) unless skip_av end end end r_person.annotations_hash.each do |r_kee, r_objects| r_objects.each do |r_o| skip = false l_test = l_person_hash[r_kee] if l_test.present? l_test.each do |l_o| # only look at same-type annotations # four types of annotations: # # data attributes, # # identifiers, # # notes, # # alternate values case r_kee when 'data attributes' if l_o.type == r_o.type && l_o.controlled_vocabulary_term_id == r_o.controlled_vocabulary_term_id && l_o.value == r_o.value && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'identifiers' if l_o.type == r_o.type && l_o.identifier == r_o.identifier && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'notes' if l_o.text == r_o.text && l_o.note_object_attribute == r_o.note.object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'alternate values' if l_o.value == r_o.value if l_o.type == r_o.type && l_o.alternate_value_object_attribute == r_o.alternate_value_object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end end end end skip end unless skip r_o.annotated_object = self r_o.save! end end end # TODO: handle prefix and suffix if false && prefix.blank? ## DD: do not change the name of verified person write_attribute(:prefix, r_person.prefix) else if r_person.prefix.present? # What to do when both have some content? end end if false && suffix.blank? ## DD: do not change the name of verified person self.suffix = r_person.suffix else if r_person.suffix.present? # What to do when both have some content? end end # TODO: handle years attributes if year_born.nil? self.year_born = r_person.year_born else unless r_person.year_born.nil? # What to do when both have some (different) numbers? end end if year_died.nil? self.year_died = r_person.year_died else unless r_person.year_died.nil? # What to do when both have some (different) numbers? end end if r_person.year_active_start # if not, r_person has nothing to contribute if self.year_active_start.nil? || (self.year_active_start > r_person.year_active_start) self.year_active_start = r_person.year_active_start end end if r_person.year_active_end # if not, r_person has nothing to contribute if self.year_active_end.nil? || (self.year_active_end < r_person.year_active_end) self.year_active_end = r_person.year_active_end end end # last thing to do in the transaction... # NO!!! - unless self.persisted? (all people are at this point persisted!) self.save! if self.changed? end rescue ActiveRecord::RecordInvalid return false end end true end def hard_merge(person_id_to_destroy) return false if id == person_id_to_destroy begin person_to_destroy = Person.find(person_id_to_destroy) Person.transaction do merge_with(person_to_destroy.id) person_to_destroy.destroy! end rescue ActiveRecord::RecordNotDestroyed return false rescue ActiveRecord::RecordInvalid return false rescue ActiveRecord::RecordNotFound return false end true end # @return [Boolean] def is_determiner? determiner_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_georeferencer? georeferencer_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_editor? editor_roles.any? end # @return [Boolean] def is_source? source_roles.any? end # @return [Boolean] def is_collector? collector_roles.any? end def role_counts(project_id) { in_project: self.roles.where(project_id:).group(:type).count, not_in_project: self.roles.where.not(project_id:).where.not(project_id: nil).group(:type).count, community: self.roles.where(project_id: nil).group(:type).count } end def dwc_occurrences ::Queries::DwcOccurrence::Filter.new(person_id: [id]).all end # @param [String] name_string # @return [Array] of Hashes # use citeproc to parse strings # see also https://github.com/SpeciesFileGroup/taxonworks/issues/1161 def self.parser(name_string) BibTeX::Entry.new(type: :book, author: name_string).parse_names.to_citeproc['author'] end # @param [String] name_string # @return [Array] of People # return people for name strings def self.parse_to_people(name_string) parser(name_string).collect { |n| Person::Unvetted.new( last_name: n['family'] ? NameCase(n['family']) : nil, first_name: n['given'] ? NameCase(n['given']) : nil, prefix: n['non-dropping-particle'] ? NameCase( n['non-dropping-particle']) : nil )} end # @param role_type [String] one of the Role types # @return [Scope] # the max 10 most recently used (1 week, could parameterize) people def self.used_recently(user_id, role_type = 'SourceAuthor') t = Role.arel_table p = Person.arel_table # i is a select manager i = t.project(t['person_id'], t['type'], t['updated_at']).from(t) .where(t['updated_at'].gt(1.week.ago)) .where(t['updated_by_id'].eq(user_id)) .where(t['type'].eq(role_type)) .order(t['updated_at'].desc) # z is a table alias z = i.as('recent_t') Person.joins( Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['person_id'].eq(p['id']))) ).pluck(:person_id).uniq end # @params Role [String] one the available roles # @return [Hash] geographic_areas optimized for user selection def self.select_optimized(user_id, project_id, role_type = 'SourceAuthor') r = used_recently(user_id, role_type) h = { quick: [], pinboard: Person.pinned_by(user_id).where(pinboard_items: {project_id:}).to_a, recent: [] } if r.empty? h[:quick] = Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a else h[:recent] = Person.where('"people"."id" IN (?)', r.first(10) ).to_a h[:quick] = ( Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a + Person.where('"people"."id" IN (?)', r.first(4) ).to_a ).uniq end h end protected # @return [Ignored] def died_after_born errors.add(:year_born, 'is older than died year') if year_born && year_died && year_born > year_died end # @return [Ignored] def activity_ended_after_started errors.add(:year_active_start, 'is older than died year') if year_active_start && year_active_end && year_active_start > year_active_end end # @return [Ignored] def not_active_after_death unless is_editor? || errors.add(:year_active_start, 'is older than year of death') if year_active_start && year_died && year_active_start > year_died errors.add(:year_active_end, 'is older than year of death') if year_active_end && year_died && year_active_end > year_died end true end # @return [Ignored] def not_active_before_birth errors.add(:year_active_start, 'is younger than than year of birth') if year_active_start && year_born && year_active_start < year_born errors.add(:year_active_end, 'is younger than year of birth') if year_active_end && year_born && year_active_end < year_born end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_gandalf errors.add(:base, 'fountain of eternal life does not exist yet') if year_born && year_died && year_died - year_born > 119 end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_balrog errors.add(:base, 'nobody is that active') if year_active_start && year_active_end && (year_active_end - year_active_start > 119) end # TODO: deprecate this, always set explicitly # @return [Ignored] def set_type_if_blank self.type = 'Person::Unvetted' if self.type.blank? end # @return [Ignored] def set_cached update_column(:cached, bibtex_name) set_role_cached end # @return [Ignored] def set_role_cached if change_to_cached_attribute? if roles.count > 25 delay(queue: :cache).update_role_cached else update_role_cached end end end # @return Integer # the total objects updated def update_role_cached # don't update the same object many times (e.g. CE of many COs?) updated = {} total = 0 roles.reload.find_each do |r| i = r.role_object.class.base_class.to_s + r.role_object.to_param next if updated[i] r.send(:set_cached) updated[i] = true total += 1 end total end # @return [Boolean] # Difficult to anticipate what # attributes will be cached in different models def change_to_cached_attribute? saved_change_to_last_name? || saved_change_to_prefix? || saved_change_to_suffix? end end |
#year_died ⇒ Integer
Returns year died.
51 52 53 54 55 56 57 58 59 60 61 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 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 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 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'app/models/person.rb', line 51 class Person < ApplicationRecord include Housekeeping::Users include Housekeeping::Timestamps include Shared::AlternateValues include Shared::DataAttributes include Shared::Identifiers include Shared::Notes include Shared::Depictions include Shared::Tags include Shared::SharedAcrossProjects include Shared::HasPapertrail include Shared::DwcOccurrenceHooks include Shared::IsData include Shared::OriginRelationship ALTERNATE_VALUES_FOR = [:last_name, :first_name].freeze IGNORE_SIMILAR = [:type, :cached].freeze IGNORE_IDENTICAL = [:type, :first_name, :last_name, :prefix, :suffix].freeze # @return [true, nil] # set as true to prevent caching attr_accessor :no_cached # @return [true, nil] # set as true to prevent application of NameCase() attr_accessor :no_namecase validates_presence_of :last_name, :type validates :year_born, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_died, inclusion: {in: 0..Time.now.year}, allow_nil: true validates :year_active_start, inclusion: {in: 0..Time.now.year, message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validates :year_active_end, inclusion: {in: 0..Time.now.year , message: "%{value} is not within the year 0-#{Time.now.year}"}, allow_nil: true validate :died_after_born validate :activity_ended_after_started validate :not_active_after_death validate :not_active_before_birth validate :not_gandalf validate :not_balrog before_validation :namecase_names, unless: Proc.new {|n| n.no_namecase } # TODO: remove this before_validation :set_type_if_blank after_save :set_cached, unless: Proc.new { |n| n.no_cached || errors.any? } validates :type, inclusion: { in: ['Person::Vetted', 'Person::Unvetted'], message: '%{value} is not a validly_published type'} has_one :user, dependent: :restrict_with_error, inverse_of: :person has_many :roles, dependent: :restrict_with_error, inverse_of: :person # before_remove: :set_cached_for_related has_many :author_roles, class_name: 'SourceAuthor', dependent: :restrict_with_error, inverse_of: :person #, before_remove: :set_cached_for_related has_many :collector_roles, class_name: 'Collector', dependent: :restrict_with_error, inverse_of: :person has_many :determiner_roles, class_name: 'Determiner', dependent: :restrict_with_error, inverse_of: :person has_many :editor_roles, class_name: 'SourceEditor', dependent: :restrict_with_error, inverse_of: :person has_many :georeferencer_roles, class_name: 'Georeferencer', dependent: :restrict_with_error, inverse_of: :person has_many :source_roles, class_name: 'SourceSource', dependent: :restrict_with_error, inverse_of: :person has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', dependent: :restrict_with_error, inverse_of: :person has_many :authored_sources, class_name: 'Source::Bibtex', through: :author_roles, source: :role_object, source_type: 'Source', inverse_of: :authors has_many :edited_sources, class_name: 'Source::Bibtex', through: :editor_roles, source: :role_object, source_type: 'Source', inverse_of: :editors has_many :human_sources, class_name: 'Source::Bibtex', through: :source_roles, source: :role_object, source_type: 'Source', inverse_of: :people has_many :authored_taxon_names, through: :taxon_name_author_roles, source: :role_object, source_type: 'TaxonName', class_name: 'Protonym', inverse_of: :taxon_name_authors has_many :collecting_events, through: :collector_roles, source: :role_object, source_type: 'CollectingEvent', inverse_of: :collectors has_many :georeferences, through: :georeferencer_roles, source: :role_object, source_type: 'Georeference', inverse_of: :georeference_authors has_many :taxon_determinations, through: :determiner_roles, source: :role_object, source_type: 'TaxonDetermination', inverse_of: :determiners # TODO: !? has_many :sources, through: :roles, source: :role_object, source_type: 'Source' # Editor or Author or Person has_many :collection_objects, through: :collecting_events scope :created_before, -> (time) { where('created_at < ?', time) } scope :with_role, -> (role) { includes(:roles).where(roles: {type: role}) } scope :ordered_by_last_name, -> { order(:last_name) } scope :used_in_project, -> (project_id) { joins(:roles).where( roles: { project_id: } ) } # Apply a "proper" case to all strings def namecase_names write_attribute(:last_name, NameCase(last_name)) if last_name && will_save_change_to_last_name? write_attribute(:first_name, NameCase(first_name)) if first_name && will_save_change_to_first_name? write_attribute(:prefix, NameCase(prefix)) if prefix && will_save_change_to_prefix? write_attribute(:suffix, NameCase(suffix)) if suffix && will_save_change_to_suffix? end # @return [Boolean] # !! overwrites IsData#is_in_use? def is_in_use? roles.reload.any? end # @return Boolean # whether or not this Person is linked to any data in the project def used_in_project?(project_id) Role.where(person_id: id, project_id:).any? || Source.joins(:project_sources, :roles).where(roles: {person_id: id}, project_sources: { project_id: }).any? end # @return [String] def name [first_name, prefix, last_name, suffix].compact.join(' ') end # @return [String] # The person's name in BibTeX format (von last, Jr, first) def bibtex_name out = '' out << prefix + ' ' if prefix.present? out << last_name if last_name.present? out << ', ' unless out.blank? || (first_name.blank? && suffix.blank?) out << suffix if suffix.present? out << ', ' unless out.end_with?(', ') || first_name.blank? || out.blank? out << first_name if first_name.present? out.strip end # @return [String] # The person's full last name including prefix & suffix (von last Jr) def full_last_name [prefix, last_name, suffix].compact.join(' ') end # Return [String, nil] # convenience, maybe a delegate: candidate def orcid identifiers.where(type: 'Identifier::Global::Orcid').first&.cached end # Return [String, nil] # convenience, maybe a delegate: candidate def wikidata_id identifiers.where(type: 'Identifier::Global::Wikidata').first&.cached end # @param [Integer] person_id # @return [Boolean] # true if all records updated, false if any one failed (all or none) # # No person is destroyed, see `hard_merge`. # # r_person is merged into l_person (self) # !! the intent is to keep self and remove target # def merge_with(person_id) return false if person_id == id if r_person = Person.find(person_id) # get the person to merge to into self begin ApplicationRecord.transaction do # !! Role.where(person_id: r_person.id).update(person_id: id) is BAAAD # !! It appends person_id: <old> to Role.where() in callbacks, breaking # !! Role#vet_person, etc. # update merge person's roles to old Role.where(person_id: r_person.id).each do |r| return false unless r.update(person_id: id) end roles.reload l_person_hash = annotations_hash if r_person.first_name.present? if first_name.blank? update(first_name: r_person.first_name) else if first_name != r_person.first_name # create a first_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.first_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'first_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.first_name, alternate_value_object_attribute: 'first_name', alternate_value_object_id: id) unless skip_av end end end if r_person.last_name.present? if last_name.blank? self.update(last_name: r_person.last_name) # NameCase() ? else if self.last_name != r_person.last_name # create a last_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.last_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'last_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.last_name, alternate_value_object_attribute: 'last_name', alternate_value_object_id: id) unless skip_av end end end r_person.annotations_hash.each do |r_kee, r_objects| r_objects.each do |r_o| skip = false l_test = l_person_hash[r_kee] if l_test.present? l_test.each do |l_o| # only look at same-type annotations # four types of annotations: # # data attributes, # # identifiers, # # notes, # # alternate values case r_kee when 'data attributes' if l_o.type == r_o.type && l_o.controlled_vocabulary_term_id == r_o.controlled_vocabulary_term_id && l_o.value == r_o.value && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'identifiers' if l_o.type == r_o.type && l_o.identifier == r_o.identifier && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'notes' if l_o.text == r_o.text && l_o.note_object_attribute == r_o.note.object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'alternate values' if l_o.value == r_o.value if l_o.type == r_o.type && l_o.alternate_value_object_attribute == r_o.alternate_value_object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end end end end skip end unless skip r_o.annotated_object = self r_o.save! end end end # TODO: handle prefix and suffix if false && prefix.blank? ## DD: do not change the name of verified person write_attribute(:prefix, r_person.prefix) else if r_person.prefix.present? # What to do when both have some content? end end if false && suffix.blank? ## DD: do not change the name of verified person self.suffix = r_person.suffix else if r_person.suffix.present? # What to do when both have some content? end end # TODO: handle years attributes if year_born.nil? self.year_born = r_person.year_born else unless r_person.year_born.nil? # What to do when both have some (different) numbers? end end if year_died.nil? self.year_died = r_person.year_died else unless r_person.year_died.nil? # What to do when both have some (different) numbers? end end if r_person.year_active_start # if not, r_person has nothing to contribute if self.year_active_start.nil? || (self.year_active_start > r_person.year_active_start) self.year_active_start = r_person.year_active_start end end if r_person.year_active_end # if not, r_person has nothing to contribute if self.year_active_end.nil? || (self.year_active_end < r_person.year_active_end) self.year_active_end = r_person.year_active_end end end # last thing to do in the transaction... # NO!!! - unless self.persisted? (all people are at this point persisted!) self.save! if self.changed? end rescue ActiveRecord::RecordInvalid return false end end true end def hard_merge(person_id_to_destroy) return false if id == person_id_to_destroy begin person_to_destroy = Person.find(person_id_to_destroy) Person.transaction do merge_with(person_to_destroy.id) person_to_destroy.destroy! end rescue ActiveRecord::RecordNotDestroyed return false rescue ActiveRecord::RecordInvalid return false rescue ActiveRecord::RecordNotFound return false end true end # @return [Boolean] def is_determiner? determiner_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_georeferencer? georeferencer_roles.any? end # @return [Boolean] def .any? end # @return [Boolean] def is_editor? editor_roles.any? end # @return [Boolean] def is_source? source_roles.any? end # @return [Boolean] def is_collector? collector_roles.any? end def role_counts(project_id) { in_project: self.roles.where(project_id:).group(:type).count, not_in_project: self.roles.where.not(project_id:).where.not(project_id: nil).group(:type).count, community: self.roles.where(project_id: nil).group(:type).count } end def dwc_occurrences ::Queries::DwcOccurrence::Filter.new(person_id: [id]).all end # @param [String] name_string # @return [Array] of Hashes # use citeproc to parse strings # see also https://github.com/SpeciesFileGroup/taxonworks/issues/1161 def self.parser(name_string) BibTeX::Entry.new(type: :book, author: name_string).parse_names.to_citeproc['author'] end # @param [String] name_string # @return [Array] of People # return people for name strings def self.parse_to_people(name_string) parser(name_string).collect { |n| Person::Unvetted.new( last_name: n['family'] ? NameCase(n['family']) : nil, first_name: n['given'] ? NameCase(n['given']) : nil, prefix: n['non-dropping-particle'] ? NameCase( n['non-dropping-particle']) : nil )} end # @param role_type [String] one of the Role types # @return [Scope] # the max 10 most recently used (1 week, could parameterize) people def self.used_recently(user_id, role_type = 'SourceAuthor') t = Role.arel_table p = Person.arel_table # i is a select manager i = t.project(t['person_id'], t['type'], t['updated_at']).from(t) .where(t['updated_at'].gt(1.week.ago)) .where(t['updated_by_id'].eq(user_id)) .where(t['type'].eq(role_type)) .order(t['updated_at'].desc) # z is a table alias z = i.as('recent_t') Person.joins( Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['person_id'].eq(p['id']))) ).pluck(:person_id).uniq end # @params Role [String] one the available roles # @return [Hash] geographic_areas optimized for user selection def self.select_optimized(user_id, project_id, role_type = 'SourceAuthor') r = used_recently(user_id, role_type) h = { quick: [], pinboard: Person.pinned_by(user_id).where(pinboard_items: {project_id:}).to_a, recent: [] } if r.empty? h[:quick] = Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a else h[:recent] = Person.where('"people"."id" IN (?)', r.first(10) ).to_a h[:quick] = ( Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a + Person.where('"people"."id" IN (?)', r.first(4) ).to_a ).uniq end h end protected # @return [Ignored] def died_after_born errors.add(:year_born, 'is older than died year') if year_born && year_died && year_born > year_died end # @return [Ignored] def activity_ended_after_started errors.add(:year_active_start, 'is older than died year') if year_active_start && year_active_end && year_active_start > year_active_end end # @return [Ignored] def not_active_after_death unless is_editor? || errors.add(:year_active_start, 'is older than year of death') if year_active_start && year_died && year_active_start > year_died errors.add(:year_active_end, 'is older than year of death') if year_active_end && year_died && year_active_end > year_died end true end # @return [Ignored] def not_active_before_birth errors.add(:year_active_start, 'is younger than than year of birth') if year_active_start && year_born && year_active_start < year_born errors.add(:year_active_end, 'is younger than year of birth') if year_active_end && year_born && year_active_end < year_born end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_gandalf errors.add(:base, 'fountain of eternal life does not exist yet') if year_born && year_died && year_died - year_born > 119 end # https://en.wikipedia.org/wiki/List_of_the_verified_oldest_people def not_balrog errors.add(:base, 'nobody is that active') if year_active_start && year_active_end && (year_active_end - year_active_start > 119) end # TODO: deprecate this, always set explicitly # @return [Ignored] def set_type_if_blank self.type = 'Person::Unvetted' if self.type.blank? end # @return [Ignored] def set_cached update_column(:cached, bibtex_name) set_role_cached end # @return [Ignored] def set_role_cached if change_to_cached_attribute? if roles.count > 25 delay(queue: :cache).update_role_cached else update_role_cached end end end # @return Integer # the total objects updated def update_role_cached # don't update the same object many times (e.g. CE of many COs?) updated = {} total = 0 roles.reload.find_each do |r| i = r.role_object.class.base_class.to_s + r.role_object.to_param next if updated[i] r.send(:set_cached) updated[i] = true total += 1 end total end # @return [Boolean] # Difficult to anticipate what # attributes will be cached in different models def change_to_cached_attribute? saved_change_to_last_name? || saved_change_to_prefix? || saved_change_to_suffix? end end |
Class Method Details
.parse_to_people(name_string) ⇒ Array
Of People return people for name strings
466 467 468 469 470 471 472 |
# File 'app/models/person.rb', line 466 def self.parse_to_people(name_string) parser(name_string).collect { |n| Person::Unvetted.new( last_name: n['family'] ? NameCase(n['family']) : nil, first_name: n['given'] ? NameCase(n['given']) : nil, prefix: n['non-dropping-particle'] ? NameCase( n['non-dropping-particle']) : nil )} end |
.parser(name_string) ⇒ Array
Returns of Hashes use citeproc to parse strings see also github.com/SpeciesFileGroup/taxonworks/issues/1161.
459 460 461 |
# File 'app/models/person.rb', line 459 def self.parser(name_string) BibTeX::Entry.new(type: :book, author: name_string).parse_names.to_citeproc['author'] end |
.select_optimized(user_id, project_id, role_type = 'SourceAuthor') ⇒ Hash
Returns geographic_areas optimized for user selection.
498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 |
# File 'app/models/person.rb', line 498 def self.select_optimized(user_id, project_id, role_type = 'SourceAuthor') r = used_recently(user_id, role_type) h = { quick: [], pinboard: Person.pinned_by(user_id).where(pinboard_items: {project_id:}).to_a, recent: [] } if r.empty? h[:quick] = Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a else h[:recent] = Person.where('"people"."id" IN (?)', r.first(10) ).to_a h[:quick] = ( Person.pinned_by(user_id).pinboard_inserted.where(pinboard_items: {project_id:}).to_a + Person.where('"people"."id" IN (?)', r.first(4) ).to_a ).uniq end h end |
.used_recently(user_id, role_type = 'SourceAuthor') ⇒ Scope
Returns the max 10 most recently used (1 week, could parameterize) people.
477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 |
# File 'app/models/person.rb', line 477 def self.used_recently(user_id, role_type = 'SourceAuthor') t = Role.arel_table p = Person.arel_table # i is a select manager i = t.project(t['person_id'], t['type'], t['updated_at']).from(t) .where(t['updated_at'].gt(1.week.ago)) .where(t['updated_by_id'].eq(user_id)) .where(t['type'].eq(role_type)) .order(t['updated_at'].desc) # z is a table alias z = i.as('recent_t') Person.joins( Arel::Nodes::InnerJoin.new(z, Arel::Nodes::On.new(z['person_id'].eq(p['id']))) ).pluck(:person_id).uniq end |
Instance Method Details
#activity_ended_after_started ⇒ Ignored (protected)
526 527 528 |
# File 'app/models/person.rb', line 526 def activity_ended_after_started errors.add(:year_active_start, 'is older than died year') if year_active_start && year_active_end && year_active_start > year_active_end end |
#bibtex_name ⇒ String
Returns The person’s name in BibTeX format (von last, Jr, first).
162 163 164 165 166 167 168 169 170 171 172 173 |
# File 'app/models/person.rb', line 162 def bibtex_name out = '' out << prefix + ' ' if prefix.present? out << last_name if last_name.present? out << ', ' unless out.blank? || (first_name.blank? && suffix.blank?) out << suffix if suffix.present? out << ', ' unless out.end_with?(', ') || first_name.blank? || out.blank? out << first_name if first_name.present? out.strip end |
#change_to_cached_attribute? ⇒ Boolean (protected)
Difficult to anticipate what attributes will be cached in different models
599 600 601 |
# File 'app/models/person.rb', line 599 def change_to_cached_attribute? saved_change_to_last_name? || saved_change_to_prefix? || saved_change_to_suffix? end |
#died_after_born ⇒ Ignored (protected)
521 522 523 |
# File 'app/models/person.rb', line 521 def died_after_born errors.add(:year_born, 'is older than died year') if year_born && year_died && year_born > year_died end |
#dwc_occurrences ⇒ Object
451 452 453 |
# File 'app/models/person.rb', line 451 def dwc_occurrences ::Queries::DwcOccurrence::Filter.new(person_id: [id]).all end |
#full_last_name ⇒ String
Returns The person’s full last name including prefix & suffix (von last Jr).
177 178 179 |
# File 'app/models/person.rb', line 177 def full_last_name [prefix, last_name, suffix].compact.join(' ') end |
#hard_merge(person_id_to_destroy) ⇒ Object
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 |
# File 'app/models/person.rb', line 388 def hard_merge(person_id_to_destroy) return false if id == person_id_to_destroy begin person_to_destroy = Person.find(person_id_to_destroy) Person.transaction do merge_with(person_to_destroy.id) person_to_destroy.destroy! end rescue ActiveRecord::RecordNotDestroyed return false rescue ActiveRecord::RecordInvalid return false rescue ActiveRecord::RecordNotFound return false end true end |
#is_author? ⇒ Boolean
424 425 426 |
# File 'app/models/person.rb', line 424 def .any? end |
#is_collector? ⇒ Boolean
439 440 441 |
# File 'app/models/person.rb', line 439 def is_collector? collector_roles.any? end |
#is_determiner? ⇒ Boolean
409 410 411 |
# File 'app/models/person.rb', line 409 def is_determiner? determiner_roles.any? end |
#is_editor? ⇒ Boolean
429 430 431 |
# File 'app/models/person.rb', line 429 def is_editor? editor_roles.any? end |
#is_georeferencer? ⇒ Boolean
419 420 421 |
# File 'app/models/person.rb', line 419 def is_georeferencer? georeferencer_roles.any? end |
#is_in_use? ⇒ Boolean
Returns !! overwrites IsData#is_in_use?.
144 145 146 |
# File 'app/models/person.rb', line 144 def is_in_use? roles.reload.any? end |
#is_source? ⇒ Boolean
434 435 436 |
# File 'app/models/person.rb', line 434 def is_source? source_roles.any? end |
#is_taxon_name_author? ⇒ Boolean
414 415 416 |
# File 'app/models/person.rb', line 414 def .any? end |
#merge_with(person_id) ⇒ Object
r_person is merged into l_person (self) !! the intent is to keep self and remove target
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 |
# File 'app/models/person.rb', line 203 def merge_with(person_id) return false if person_id == id if r_person = Person.find(person_id) # get the person to merge to into self begin ApplicationRecord.transaction do # !! Role.where(person_id: r_person.id).update(person_id: id) is BAAAD # !! It appends person_id: <old> to Role.where() in callbacks, breaking # !! Role#vet_person, etc. # update merge person's roles to old Role.where(person_id: r_person.id).each do |r| return false unless r.update(person_id: id) end roles.reload l_person_hash = annotations_hash if r_person.first_name.present? if first_name.blank? update(first_name: r_person.first_name) else if first_name != r_person.first_name # create a first_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.first_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'first_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.first_name, alternate_value_object_attribute: 'first_name', alternate_value_object_id: id) unless skip_av end end end if r_person.last_name.present? if last_name.blank? self.update(last_name: r_person.last_name) # NameCase() ? else if self.last_name != r_person.last_name # create a last_name alternate_value of the r_person first name skip_av = false av_list = l_person_hash['alternate values'] av_list ||= {} av_list.each do |av| if av.value == r_person.last_name if av.type == 'AlternateValue::AlternateSpelling' && av.alternate_value_object_attribute == 'last_name' # && skip_av = true break # stop looking in this bunch, if you found a match end end end AlternateValue::AlternateSpelling.create!( alternate_value_object_type: 'Person', value: r_person.last_name, alternate_value_object_attribute: 'last_name', alternate_value_object_id: id) unless skip_av end end end r_person.annotations_hash.each do |r_kee, r_objects| r_objects.each do |r_o| skip = false l_test = l_person_hash[r_kee] if l_test.present? l_test.each do |l_o| # only look at same-type annotations # four types of annotations: # # data attributes, # # identifiers, # # notes, # # alternate values case r_kee when 'data attributes' if l_o.type == r_o.type && l_o.controlled_vocabulary_term_id == r_o.controlled_vocabulary_term_id && l_o.value == r_o.value && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'identifiers' if l_o.type == r_o.type && l_o.identifier == r_o.identifier && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'notes' if l_o.text == r_o.text && l_o.note_object_attribute == r_o.note.object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end when 'alternate values' if l_o.value == r_o.value if l_o.type == r_o.type && l_o.alternate_value_object_attribute == r_o.alternate_value_object_attribute && l_o.project_id == r_o.project_id skip = true break # stop looking in this bunch, if you found a match end end end end skip end unless skip r_o.annotated_object = self r_o.save! end end end # TODO: handle prefix and suffix if false && prefix.blank? ## DD: do not change the name of verified person write_attribute(:prefix, r_person.prefix) else if r_person.prefix.present? # What to do when both have some content? end end if false && suffix.blank? ## DD: do not change the name of verified person self.suffix = r_person.suffix else if r_person.suffix.present? # What to do when both have some content? end end # TODO: handle years attributes if year_born.nil? self.year_born = r_person.year_born else unless r_person.year_born.nil? # What to do when both have some (different) numbers? end end if year_died.nil? self.year_died = r_person.year_died else unless r_person.year_died.nil? # What to do when both have some (different) numbers? end end if r_person.year_active_start # if not, r_person has nothing to contribute if self.year_active_start.nil? || (self.year_active_start > r_person.year_active_start) self.year_active_start = r_person.year_active_start end end if r_person.year_active_end # if not, r_person has nothing to contribute if self.year_active_end.nil? || (self.year_active_end < r_person.year_active_end) self.year_active_end = r_person.year_active_end end end # last thing to do in the transaction... # NO!!! - unless self.persisted? (all people are at this point persisted!) self.save! if self.changed? end rescue ActiveRecord::RecordInvalid return false end end true end |
#name ⇒ String
156 157 158 |
# File 'app/models/person.rb', line 156 def name [first_name, prefix, last_name, suffix].compact.join(' ') end |
#namecase_names ⇒ Object
Apply a “proper” case to all strings
135 136 137 138 139 140 |
# File 'app/models/person.rb', line 135 def namecase_names write_attribute(:last_name, NameCase(last_name)) if last_name && will_save_change_to_last_name? write_attribute(:first_name, NameCase(first_name)) if first_name && will_save_change_to_first_name? write_attribute(:prefix, NameCase(prefix)) if prefix && will_save_change_to_prefix? write_attribute(:suffix, NameCase(suffix)) if suffix && will_save_change_to_suffix? end |
#not_active_after_death ⇒ Ignored (protected)
531 532 533 534 535 536 537 |
# File 'app/models/person.rb', line 531 def not_active_after_death unless is_editor? || errors.add(:year_active_start, 'is older than year of death') if year_active_start && year_died && year_active_start > year_died errors.add(:year_active_end, 'is older than year of death') if year_active_end && year_died && year_active_end > year_died end true end |
#not_active_before_birth ⇒ Ignored (protected)
540 541 542 543 |
# File 'app/models/person.rb', line 540 def not_active_before_birth errors.add(:year_active_start, 'is younger than than year of birth') if year_active_start && year_born && year_active_start < year_born errors.add(:year_active_end, 'is younger than year of birth') if year_active_end && year_born && year_active_end < year_born end |
#not_balrog ⇒ Object (protected)
551 552 553 |
# File 'app/models/person.rb', line 551 def not_balrog errors.add(:base, 'nobody is that active') if year_active_start && year_active_end && (year_active_end - year_active_start > 119) end |
#not_gandalf ⇒ Object (protected)
546 547 548 |
# File 'app/models/person.rb', line 546 def not_gandalf errors.add(:base, 'fountain of eternal life does not exist yet') if year_born && year_died && year_died - year_born > 119 end |
#orcid ⇒ Object
Return [String, nil]
convenience, maybe a delegate: candidate
183 184 185 |
# File 'app/models/person.rb', line 183 def orcid identifiers.where(type: 'Identifier::Global::Orcid').first&.cached end |
#role_counts(project_id) ⇒ Object
443 444 445 446 447 448 449 |
# File 'app/models/person.rb', line 443 def role_counts(project_id) { in_project: self.roles.where(project_id:).group(:type).count, not_in_project: self.roles.where.not(project_id:).where.not(project_id: nil).group(:type).count, community: self.roles.where(project_id: nil).group(:type).count } end |
#set_cached ⇒ Ignored (protected)
562 563 564 565 |
# File 'app/models/person.rb', line 562 def set_cached update_column(:cached, bibtex_name) set_role_cached end |
#set_role_cached ⇒ Ignored (protected)
568 569 570 571 572 573 574 575 576 |
# File 'app/models/person.rb', line 568 def set_role_cached if change_to_cached_attribute? if roles.count > 25 delay(queue: :cache).update_role_cached else update_role_cached end end end |
#set_type_if_blank ⇒ Ignored (protected)
TODO: deprecate this, always set explicitly
557 558 559 |
# File 'app/models/person.rb', line 557 def set_type_if_blank self.type = 'Person::Unvetted' if self.type.blank? end |
#update_role_cached ⇒ Object (protected)
Returns Integer the total objects updated.
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 |
# File 'app/models/person.rb', line 580 def update_role_cached # don't update the same object many times (e.g. CE of many COs?) updated = {} total = 0 roles.reload.find_each do |r| i = r.role_object.class.base_class.to_s + r.role_object.to_param next if updated[i] r.send(:set_cached) updated[i] = true total += 1 end total end |
#used_in_project?(project_id) ⇒ Boolean
Returns Boolean whether or not this Person is linked to any data in the project.
150 151 152 153 |
# File 'app/models/person.rb', line 150 def used_in_project?(project_id) Role.where(person_id: id, project_id:).any? || Source.joins(:project_sources, :roles).where(roles: {person_id: id}, project_sources: { project_id: }).any? end |
#wikidata_id ⇒ Object
Return [String, nil]
convenience, maybe a delegate: candidate
189 190 191 |
# File 'app/models/person.rb', line 189 def wikidata_id identifiers.where(type: 'Identifier::Global::Wikidata').first&.cached end |