Class: TaxonName

Overview

A taxon name (nomenclature only). See also NOMEN (github.com/SpeciesFileGroup/nomen).

Direct Known Subclasses

Combination, Hybrid, Protonym

Defined Under Namespace

Modules: MatrixHooks, OtuSyncronization

Constant Summary collapse

ALTERNATE_VALUES_FOR =

Allows users to provide arbitrary annotations that “over-ride” rank string

[:rank_class].freeze
COMBINATION_ELEMENTS =
[:genus, :subgenus, :species, :subspecies, :variety, :subvariety, :form, :subform].freeze
SPECIES_EPITHET_RANKS =
%w{species subspecies variety subvariety form subform}.freeze
NOT_LATIN =

Dash is allowed?

Regexp.new(/[^a-zA-Z|\-]/).freeze
NO_CACHED_MESSAGE =
'REBUILD PROJECT TAXON NAME CACHE'.freeze
NOMEN_VALID =
{
  icn: 'http://purl.obolibrary.org/obo/NOMEN_0000383',
  icnp: 'http://purl.obolibrary.org/obo/NOMEN_0000081',
  icvcn: 'http://purl.obolibrary.org/obo/NOMEN_0000125',
  iczn: 'http://purl.obolibrary.org/obo/NOMEN_0000224'
}

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from MatrixHooks

#coordinate_observation_matrix_row_items, #in_scope_observation_matrix_row_items, #out_of_scope_observation_matrix_row_items

Methods included from Shared::MatrixHooks::Dynamic

#dynamic_add_to_matrix_column_items, #dynamic_add_to_matrix_row_items, #dynamic_cleanup_in_scope_column_items, #dynamic_cleanup_in_scope_row_items, #dynamic_cleanup_out_of_scope_column_items, #dynamic_cleanup_out_of_scope_row_items, #dynamic_column_items_in, #dynamic_column_items_out, #dynamic_inspect_matrices, #dynamic_remove_from_matrix_column_items, #dynamic_remove_from_matrix_row_items, #dynamic_row_items_in, #dynamic_row_items_out, #dynamic_syncronize_matrices, #dynamic_update_matrix_column_items?, #dynamic_update_matrix_row_items?, #in_scope_observation_matrix_column_items, #in_scope_observation_matrix_row_items, #out_of_scope_observation_matrix_column_items, #out_of_scope_observation_matrix_row_items, #prepare_matrix_items

Methods included from Shared::MatrixHooks::Member

#member_add_matrix_columns, #member_add_matrix_rows, #member_add_to_matrix_items, #member_of_new_matrix_column_items, #member_of_new_matrix_row_items, #member_of_old_matrix_column_items, #member_of_old_matrix_row_items, #member_remove_from_matrix_items, #member_remove_matrix_columns, #member_remove_matrix_rows, #member_syncronize_matrices, #member_update_matrix_items?

Methods included from Shared::IsData

#errors_excepting, #full_error_messages_excepting, #identical, #is_community?, #is_destroyable?, #is_editable?, #is_in_use?, #is_in_users_projects?, #metamorphosize, #similar

Methods included from SoftValidation

#clear_soft_validations, #fix_for, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations, #soft_validators

Methods included from Shared::HasPapertrail

#attribute_updated, #attribute_updater

Methods included from Shared::AlternateValues

#all_values_for, #alternate_valued?

Methods included from Shared::Confidences

#reject_confidences

Methods included from Shared::Citations

#cited?, #mark_citations_for_destruction, #origin_citation_source_id, #reject_citations, #requires_citation?, #sources_by_topic_id

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

Methods included from Shared::Tags

#reject_tags, #tag_with, #tagged?, #tagged_with?

Methods included from Shared::DataAttributes

#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#also_create_otuBoolean

Returns When true, also creates an OTU that is tied to this taxon name.

Returns:

  • (Boolean)

    When true, also creates an OTU that is tied to this taxon name



181
182
183
# File 'app/models/taxon_name.rb', line 181

def also_create_otu
  @also_create_otu
end

#cachedString

Genus-species combination for genus and lower, monominal for higher. The string has NO html, and no author/year.

Returns:

  • (String)


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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
# File 'app/models/taxon_name.rb', line 129

class TaxonName < ApplicationRecord

  # @return class
  #   this method calls Module#module_parent
  # TODO: This method can be placed elsewhere inside this class (or even removed if not used)
  #       when https://github.com/ClosureTree/closure_tree/issues/346 is fixed.
  def self.parent
    self.module_parent
  end

  # Must be before various of these includes, in particular MatrixHooks
  has_closure_tree

  include Housekeeping
  include Shared::DataAttributes
  include Shared::Tags
  include Shared::Identifiers
  include Shared::Notes
  include Shared::Depictions
  include Shared::Citations
  include Shared::Confidences
  include Shared::AlternateValues
  include Shared::HasPapertrail
  include SoftValidation
  include Shared::IsData
  include TaxonName::OtuSyncronization

  include Shared::MatrixHooks::Member
  include Shared::MatrixHooks::Dynamic

  include TaxonName::MatrixHooks

  # Allows users to provide arbitrary annotations that "over-ride" rank string
  ALTERNATE_VALUES_FOR = [:rank_class].freeze # !! Don't even think about putting this on `name`

  COMBINATION_ELEMENTS = [:genus, :subgenus, :species, :subspecies, :variety, :subvariety, :form, :subform].freeze

  SPECIES_EPITHET_RANKS = %w{species subspecies variety subvariety form subform}.freeze

  NOT_LATIN = Regexp.new(/[^a-zA-Z|\-]/).freeze # Dash is allowed?

  NO_CACHED_MESSAGE = 'REBUILD PROJECT TAXON NAME CACHE'.freeze

  NOMEN_VALID = {
    icn: 'http://purl.obolibrary.org/obo/NOMEN_0000383',
    icnp: 'http://purl.obolibrary.org/obo/NOMEN_0000081',
    icvcn: 'http://purl.obolibrary.org/obo/NOMEN_0000125',
    iczn: 'http://purl.obolibrary.org/obo/NOMEN_0000224'
  }

  # @return [Boolean]
  #   When true, also creates an OTU that is tied to this taxon name
  attr_accessor :also_create_otu

  # @return [Boolean]
  #   When true cached values are not built
  attr_accessor :no_cached
  delegate :nomenclatural_code, to: :rank_class, allow_nil: true
  delegate :rank_name, to: :rank_class, allow_nil: true

  # TODO: this was not implemented and tested properly
  # I think the intent is *before* save, i.e. the name will change
  # to a new cached value, so let's record the old one
  #  after_save :create_new_combination_if_absent

  after_save :set_cached, unless: Proc.new {|n| n.no_cached || errors.any? }
  after_save :set_cached_warnings, if: Proc.new {|n| n.no_cached }
  after_create :create_otu, if: :also_create_otu
  before_destroy :check_for_children, prepend: true

  validate :validate_rank_class_class,
    # :check_format_of_name,
    :validate_parent_from_the_same_project,
    :validate_parent_is_set,
    :check_new_rank_class,
    :check_new_parent_class,
    :validate_source_type,
    :validate_one_root_per_project

  # TODO: remove, this is handled natively
  validates_presence_of :type, message: 'is not specified'

  validates :year_of_publication, date_year: {min_year: 1000, max_year: Time.now.year + 5}, allow_nil: true
  validates :name, format: { without: /\s/ }

  # TODO: move some of these down to Protonym when they don't apply to Combination

  # TODO: think of a different name, and test
  has_many :historical_taxon_names, class_name: 'TaxonName', foreign_key: :cached_valid_taxon_name_id

  has_many :observation_matrix_row_items, inverse_of: :taxon_name, class_name: 'ObservationMatrixRowItem::Dynamic::TaxonName', dependent: :delete_all
  has_many :observation_matrices, through: :observation_matrix_row_items

  # TODO: revisit?
  belongs_to :valid_taxon_name, class_name: 'TaxonName', foreign_key: :cached_valid_taxon_name_id

  has_one :source_classified_as_relationship, -> {
    where(taxon_name_relationships: {type: 'TaxonNameRelationship::SourceClassifiedAs'})
  }, class_name: 'TaxonNameRelationship::SourceClassifiedAs', foreign_key: :subject_taxon_name_id

  has_one :source_classified_as, through: :source_classified_as_relationship, source: :object_taxon_name

  has_many :otus, inverse_of: :taxon_name, dependent: :restrict_with_error
  has_many :taxon_determinations, through: :otus
  has_many :collection_objects, through: :taxon_determinations, source: :biological_collection_object
  has_many :related_taxon_name_relationships, class_name: 'TaxonNameRelationship', foreign_key: :object_taxon_name_id, dependent: :restrict_with_error, inverse_of: :object_taxon_name

  has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', as: :role_object, dependent: :destroy
  has_many :taxon_name_authors, -> { order('roles.position ASC') }, through: :taxon_name_author_roles, source: :person

  # TODO: Combinations shouldn't have classifications or relationships?  Move to Protonym?
  has_many :taxon_name_classifications, dependent: :destroy, foreign_key: :taxon_name_id, inverse_of: :taxon_name
  has_many :taxon_name_relationships, foreign_key: :subject_taxon_name_id, dependent: :restrict_with_error, inverse_of: :subject_taxon_name

  # NOTE: Protonym subclassed methods might not be nicely tracked here, we'll have to see.  Placement is after has_many relationships. (?)
  accepts_nested_attributes_for :related_taxon_name_relationships, allow_destroy: true, reject_if: proc { |attributes| attributes['type'].blank? || attributes['subject_taxon_name_id'].blank? }
  accepts_nested_attributes_for :taxon_name_authors, :taxon_name_author_roles, allow_destroy: true
  accepts_nested_attributes_for :taxon_name_classifications, allow_destroy: true, reject_if: proc { |attributes| attributes['type'].blank?  }

  has_many :classified_as_unavailable_or_invalid, -> { where type: TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID }, class_name: 'TaxonNameClassification'

  scope :with_same_cached_valid_id, -> { where(arel_table[:id].eq(arel_table[:cached_valid_taxon_name_id])) }
  scope :with_different_cached_valid_id, -> { where(arel_table[:id].not_eq(arel_table[:cached_valid_taxon_name_id])) } # This doesn't catch all invalid names.  Those with classifications only are missed !$#!@#

  scope :that_is_valid, -> {where(cached_is_valid: true) }
  scope :that_is_invalid, -> {where(cached_is_valid: false) }

  def self.calculated_invalid
    a = TaxonName.with_different_cached_valid_id # that_is_invalid
    b = TaxonName.joins(:taxon_name_classifications).where(taxon_name_classifications: {type: TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID })
    TaxonName.from("((#{a.to_sql}) UNION (#{b.to_sql})) as taxon_names")
  end

  def self.calculated_valid
    # Alt format: TaxonName.that_is_valid.left_joins(:classified_as_unavailable_or_invalid).merge(TaxonNameClassification.where(id: nil))
    TaxonName
      .with_same_cached_valid_id
      .where.not(
        id: TaxonNameClassification.select(:taxon_name_id).where(type: TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID)
      )
  end

  scope :with_type, -> (type) {where(type: type)}
  scope :descendants_of, -> (taxon_name) { with_ancestor(taxon_name )}

  scope :ancestors_of, -> (taxon_name) {
    joins(:descendant_hierarchies)
      .where(taxon_name_hierarchies: {descendant_id: taxon_name.id})
      .where('taxon_name_hierarchies.ancestor_id != ?', taxon_name.id)
      .order('taxon_name_hierarchies.generations DESC') # root is at index 0
  }

  # LEAVE UNORDERED, if you want order:
  #   .order('taxon_name_hierarchies.generations DESC')
  scope :self_and_ancestors_of, -> (taxon_name) {
    joins(:descendant_hierarchies)
      .where(taxon_name_hierarchies: {descendant_id: taxon_name.id})
  }

  # Includes taxon_name, doesn't order result
  scope :ancestors_and_descendants_of, -> (taxon_name) do
    scoping do
      a = TaxonName.self_and_ancestors_of(taxon_name)
      b = TaxonName.descendants_of(taxon_name)
      TaxonName.from("((#{a.to_sql}) UNION (#{b.to_sql})) as taxon_names")
    end
  end

  # TODO: remove
  scope :with_rank_class, -> (rank_class_name) { where(rank_class: rank_class_name) }

  # TODO: remove
  scope :with_parent_taxon_name, -> (parent) { where(parent_id: parent) }

  scope :with_base_of_rank_class, -> (rank_class) { where('rank_class LIKE ?', "#{rank_class}%") }
  scope :with_rank_class_including, -> (include_string) { where('rank_class LIKE ?', "%#{include_string}%") }
  scope :project_root, -> (root_id) {where("(taxon_names.rank_class = 'NomenclaturalRank' AND taxon_names.project_id = ?)", root_id)}

  # A specific relationship
  scope :as_subject_with_taxon_name_relationship, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where(taxon_name_relationships: {type: taxon_name_relationship}) }
  scope :as_subject_with_taxon_name_relationship_base, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "#{taxon_name_relationship}%").references(:taxon_name_relationships) }
  scope :as_subject_without_taxon_name_relationship_base, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where('(taxon_name_relationships.type NOT LIKE ?) OR (taxon_name_relationships.subject_taxon_name_id IS NULL)', "#{taxon_name_relationship}%").references(:taxon_name_relationships) }
  scope :as_subject_with_taxon_name_relationship_array, -> (taxon_name_relationship_name_array) { includes(:taxon_name_relationships).where('(taxon_name_relationships.type IN (?)) OR (taxon_name_relationships.subject_taxon_name_id IS NULL)', "#{taxon_name_relationship_name_array}%").references(:taxon_name_relationships) }
  scope :as_subject_without_taxon_name_relationship_array, -> (taxon_name_relationship_name_array) { includes(:taxon_name_relationships).where('(taxon_name_relationships.type NOT IN (?)) OR (taxon_name_relationships.subject_taxon_name_id IS NULL)', "#{taxon_name_relationship_name_array}%").references(:taxon_name_relationships) }
  scope :as_subject_with_taxon_name_relationship_containing, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "%#{taxon_name_relationship}%").references(:taxon_name_relationships) }
  scope :as_object_with_taxon_name_relationship, -> (taxon_name_relationship) { includes(:related_taxon_name_relationships).where(taxon_name_relationships: {type: taxon_name_relationship}) }
  scope :as_object_with_taxon_name_relationship_base, -> (taxon_name_relationship) { includes(:related_taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "#{taxon_name_relationship}%").references(:related_taxon_name_relationships) }
  scope :as_object_with_taxon_name_relationship_containing, -> (taxon_name_relationship) { includes(:related_taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "%#{taxon_name_relationship}%").references(:related_taxon_name_relationships) }

  # @param relationship [Array, String]
  def self.with_taxon_name_relationship(relationship)
    a = TaxonName.joins(:taxon_name_relationships).where(taxon_name_relationships: {type: relationship})
    b = TaxonName.joins(:related_taxon_name_relationships).where(taxon_name_relationships: {type: relationship})
    TaxonName.from("((#{a.to_sql}) UNION (#{b.to_sql})) as taxon_names")
  end

  scope :with_taxon_name_relationships, -> {
    joins('LEFT OUTER JOIN taxon_name_relationships tnr1 ON taxon_names.id = tnr1.subject_taxon_name_id').
    joins('LEFT OUTER JOIN taxon_name_relationships tnr2 ON taxon_names.id = tnr2.object_taxon_name_id').
    where('tnr1.subject_taxon_name_id IS NOT NULL OR tnr2.object_taxon_name_id IS NOT NULL')
  }
  # *Any* relationship where there IS a relationship for a subject/object/both
  scope :with_taxon_name_relationships_as_subject, -> { joins(:taxon_name_relationships) }
  scope :with_taxon_name_relationships_as_object, -> { joins(:related_taxon_name_relationships) }

  # *Any* relationship where there is NOT a relationship for a subject/object/both
  scope :without_subject_taxon_name_relationships, -> { includes(:taxon_name_relationships).where(taxon_name_relationships: {subject_taxon_name_id: nil}) }
  scope :without_object_taxon_name_relationships, -> { includes(:related_taxon_name_relationships).where(taxon_name_relationships: {object_taxon_name_id: nil}) }

  scope :without_taxon_name_relationships, -> {
    joins('LEFT OUTER JOIN taxon_name_relationships tnr1 ON taxon_names.id = tnr1.subject_taxon_name_id').
    joins('LEFT OUTER JOIN taxon_name_relationships tnr2 ON taxon_names.id = tnr2.object_taxon_name_id').
    where('tnr1.subject_taxon_name_id IS NULL AND tnr2.object_taxon_name_id IS NULL')
  }

  # TODO: deprecate all of these for where()
  scope :with_parent_id, -> (parent_id) {where(parent_id: parent_id)}
  scope :with_cached_valid_taxon_name_id, -> (cached_valid_taxon_name_id) {where(cached_valid_taxon_name_id: cached_valid_taxon_name_id)}
  scope :with_cached_original_combination, -> (original_combination) { where(cached_original_combination: original_combination) }

  scope :without_otus, -> { includes(:otus).where(otus: {id: nil}) }
  scope :with_otus, -> { includes(:otus).where.not(otus: {id: nil}) }

  # @return [Scope]
  #   Combinations that are composed of children of this taxon name
  #     when those children are not currently descendants of this taxon name
  #
  # !! When :cached_valid_taxon_name_id is properly set then this method is not required
  # rather you should use :historical_taxon_names.
  #
  def self.out_of_scope_combinations(taxon_name_id)
    t = ::TaxonName.arel_table
    h = ::TaxonNameHierarchy.arel_table
    r = ::TaxonNameRelationship.arel_table

    h1 = h.alias('osch_')
    h2 = h.alias('oschh_')

    b = h.project(
      h1[:descendant_id].as('b'),
      h2[:ancestor_id].as('c'),
    ).from([h])

    b = b.join(r, Arel::Nodes::InnerJoin).on(h[:descendant_id].eq(r[:subject_taxon_name_id]).and(h[:ancestor_id].eq(taxon_name_id)))
      .join(h1, Arel::Nodes::InnerJoin).on(r[:object_taxon_name_id].eq(h1[:descendant_id]).and(h1[:ancestor_id].not_eq(h1[:descendant_id])))
      .join(h2, Arel::Nodes::OuterJoin).on(
        h1[:ancestor_id].eq(h2[:ancestor_id]).
        and(h2[:descendant_id].eq(taxon_name_id))
      )

    # This was particularly useful in debugging the join chain:
    # ap TaxonNameHierarchy.connection.execute(b.to_sql).collect{|a| a}

    b = b.as('abc')

    ::Combination
      .joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['b'].eq(t[:id]))))
      .where(b['c'].eq(nil))
      .distinct
  end

  # @return Scope
  #   names that are not leaves
  # TODO: belongs in lib/queries/filter.rb likely
  def self.not_leaves
    t = self.arel_table
    h = ::TaxonNameHierarchy.arel_table

    a = t.alias('a_')
    b = t.project(a[Arel.star]).from(a)

    c = h.alias('h1')

    b = b.join(c, Arel::Nodes::OuterJoin)
      .on(
        a[:id].eq(c[:ancestor_id])
      )

    e = c[:generations].not_eq(0)
    f = c[:ancestor_id].not_eq(c[:descendant_id])

    b = b.where(e.and(f))
    b = b.group(a[:id])
    b = b.as('tnh_')

    ::TaxonName.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(t['id']))))
  end

  # @return [Scope] Protonym(s) the **broad sense** synonyms of this name
  def synonyms
    TaxonName.with_cached_valid_taxon_name_id(self.id)
  end

  soft_validate(:sv_missing_confidence_level,
                set: :missing_fields,
                name: 'Missing confidence level',
                description: 'To remaind that the taxon spelling have to be compared to the original source' )

  soft_validate(:sv_missing_original_publication,
                set: :missing_fields,
                name: 'Missing original source',
                description: 'Original source is not selected' )

=begin
  soft_validate(:sv_missing_author,
                set: :missing_fields,
                name: 'Missing author')

  soft_validate(:sv_missing_year,
                set: :missing_fields,
                name: 'Missing year')
=end

  soft_validate(
    :sv_parent_is_valid_name,
    set: :parent_is_valid_name,
    fix: :sv_fix_parent_is_valid_name,
    name: 'Parent should be a valid taxon',
    description: 'When the parent taxon (for example a genus) is treated as a synonym, all subordinate taxa from this taxon should be transferred to the valid taxon' )

  soft_validate(
    :sv_conflicting_subordinate_taxa,
    set: :parent_is_valid_name,
    name: 'Conflicting subordinate taxa',
    description: 'Unavailable or invalid taxon should not have subordinate taxa' )

  soft_validate(
    :sv_cached_names,
    set: :cached_names,
    fix: :sv_fix_cached_names,
    name: 'Cached names',
    description: 'Check if cached values need to be updated' )

  # TODO: move to protonym spec?
  soft_validate(
    :sv_not_synonym_of_self,
    set: :not_synonym_of_self,
    name: 'Not synonym of self',
    description: 'Taxon has two conflicting relationships (invalidating and validating).' )

  # TODO: move to protonym spec?
  soft_validate(
    :sv_two_unresolved_alternative_synonyms,
    set: :two_unresolved_alternative_synonyms,
    name: 'Two conflicting relationships',
    description: 'Taxon has two conflicting relationships.' )

  # TODO: move to combination spec?
  soft_validate(
    :sv_incomplete_combination,
    set: :incomplete_combination,
    name: 'Incomplete combination',
    description: 'Intermediate ranks are missing in combination (either original or subsequent)' )

  # @return [Array of TaxonName]
  #   ordered by rank, a scope-like hack
  def self.sort_by_rank(taxon_names)
    taxon_names.sort!{|a, b| RANKS.index(a.rank_string) <=> RANKS.index(b.rank_string)}
  end

  # TODO: what is this:!? :)
  def self.foo(rank_classes)
    from <<-SQL.strip_heredoc
      ( SELECT *, rank()
           OVER (
               PARTITION BY rank_class, parent_id
               ORDER BY generations asc, name
            ) AS rn
         FROM taxon_names
         INNER JOIN "taxon_name_hierarchies" ON "taxon_names"."id" = "taxon_name_hierarchies"."descendant_id"
         WHERE #{rank_classes.collect{|c| "rank_class = '#{c}'" }.join(' OR ')}
         ) as taxon_names
    SQL
  end

  # @return [String]
  #   rank as human readable short-form, like 'genus' or 'species'
  def rank
    ::RANKS.include?(rank_string) ? rank_name : nil
  end

  # @return [String]
  #   rank (Kindom, Phylum...) as a string, like {NomenclaturalRank::Iczn::SpeciesGroup::Species}
  def rank_string
    read_attribute(:rank_class)
  end

  def rank_class=(value)
    write_attribute(:rank_class, value.to_s)
  end

  # @return [NomenclaturalRank class]
  #   rank as a {NomenclaturalRank} class, like {NomenclaturalRank::Iczn::SpeciesGroup::Species}
  def rank_class
    r = read_attribute(:rank_class)
    Ranks.valid?(r) ? r.safe_constantize : r
  end

  # @see .out_of_scope_combinations
  def out_of_scope_combinations
    ::TaxonName
      .where(project_id: project_id)
      .out_of_scope_combinations(id)
  end

  # @return [TaxonName, nil] an ancestor at the specified rank
  # @param rank [symbol|string|
  #   like :species or 'genus'
  # @param include_self [Boolean]
  #   if true then self will also be returned
  def ancestor_at_rank(rank, include_self = false)
    if target_code = (is_combination? ? combination_taxon_names.first.nomenclatural_code : nomenclatural_code)
      r = Ranks.lookup(target_code, rank)
      return self if include_self && (rank_class.to_s == r)
      ancestors.with_rank_class( r ).first
    else
      # Root has no nomenclature code
      return nil
    end
  end

  # @return scope [TaxonName, nil] an ancestor at the specified rank
  # @params rank [symbol|string|
  #   like :species or 'genus'
  def descendants_at_rank(rank)
    return TaxonName.none if nomenclatural_code.blank? # Root names
    descendants.with_rank_class(
      Ranks.lookup(nomenclatural_code, rank)
    )
  end

  # @return [Scope] Protonym(s) the **broad sense** synonyms of this name
  def synonyms
    TaxonName.with_cached_valid_taxon_name_id(id)
  end

  # @return [Array]
  #   all TaxonNameRelationships where this taxon is an object or subject.
  def all_taxon_name_relationships
    # !! If self relationships are ever made possible this needs a DISTINCT clause
    TaxonNameRelationship.find_by_sql(
      "SELECT taxon_name_relationships.*
         FROM taxon_name_relationships
         WHERE taxon_name_relationships.subject_taxon_name_id = #{self.id}
       UNION
       SELECT taxon_name_relationships.*
         FROM taxon_name_relationships
         WHERE taxon_name_relationships.object_taxon_name_id = #{self.id}")
  end

  # @return [Array of TaxonName]
  #   all taxon_names which have relationships to this taxon as an object or subject.
  def related_taxon_names
    TaxonName.find_by_sql(
      "SELECT tn.* from taxon_names tn join taxon_name_relationships tnr1 on tn.id = tnr1.subject_taxon_name_id and tnr1.object_taxon_name_id = #{self.id}
      UNION
      SELECT tn.* from taxon_names tn join taxon_name_relationships tnr2 on tn.id = tnr2.object_taxon_name_id and tnr2.subject_taxon_name_id = #{self.id}"
    )
  end

  # @return [String, nil]
  #   the baseline means of displaying name authorship, i.e. the author for this taxon, last name only.
  # Important, string format priority is
  #   1) as provided verbatim
  #   2) as generated from People
  #   3) as taken from the Source
  #       Source People (Authors)
  #       Source Verbatim authors
  def author_string
    return verbatim_author if !verbatim_author.nil?
    if taxon_name_authors.any?
      # TODO: Technically not correct if prefix/suffix involved.
      return Utilities::Strings.authorship_sentence( taxon_name_authors.pluck(:last_name) )
    end

    return source.authority_name if !source.nil?
    nil
  end

  # @return [Integer]
  #   a 4 digit integer representing year of publication, like 1974
  def year_integer
    return year_of_publication if !year_of_publication.nil?
    try(:source).try(:year)
  end

  # @return String, nil
  #   virtual attribute, to ultimately be fixed in db
  def cached_author
    cached_author_year&.gsub(/,\s\d+/, '')
  end

  # !! Overrides Shared::Citations#nomenclature_date
  #
  # @return [Time]
  #   effective date of publication, used to determine nomenclatural priority
  def nomenclature_date
    return nil if !persisted?
    family_before_1961 = TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_string('TaxonNameRelationship::Iczn::PotentiallyValidating::FamilyBefore1961').first

    # family_before_1961 = taxon_name_relationships.with_type_string('TaxonNameRelationship::Iczn::PotentiallyValidating::FamilyBefore1961').first
    if family_before_1961.nil?
      year = year_of_publication ? Time.utc(year_of_publication, 12, 31) : nil
      self.source ? (self.source.cached_nomenclature_date ? self.source.nomenclature_date : year) : year
    else
      obj = family_before_1961.object_taxon_name
      year = obj.year_of_publication ? Time.utc(obj.year_of_publication, 12, 31) : nil
      obj.source ? (source.cached_nomenclature_date ? obj.source.nomenclature_date : year) : year
    end
  end

  # @return [array]
  # returns array of hashes for history of taxon. Could be used for catalogue construction
  def nomeclatural_history
    history = []
    TaxonName.where(cached_valid_taxon_name_id: self.id).order(:cached_nomenclature_date).each do |t|
      item = {}
      source_author_string = t.is_combination? ? [t.origin_citation&.source&.authority_name, t.origin_citation&.source&.year].join(', ') : nil
      source_author_string = ' in ' + source_author_string unless source_author_string.nil?
      item[:name] = t.is_combination? ? t.cached_html : t.cached_original_combination_html
      item[:author_year] = t.is_combination? ? t.cached_author_year + source_author_string : t.original_author_year
      item[:statuses] = t.combined_statuses
      history.append(item)
    end
    return history
  end

  # @return [Class, nil]
  #   gender of a genus as class
  def gender_class
    gender_instance.try(:type_class)
  end

  # @return [TaxonNameClassification instance, nil]
  #    the gender classification of this name, if provided
  def gender_instance
    taxon_name_classifications.with_type_base('TaxonNameClassification::Latinized::Gender').first
  end

  # @return [String, nil]
  #    gender as a string (only applicable to Genera)
  def gender_name
    gender_instance.try(:classification_label).try(:downcase)
  end

  # @return [Class]
  #   part of speech of a species as class.
  def part_of_speech_class
    part_of_speech_instance.try(:type_class)
  end

  def part_of_speech_instance
    taxon_name_classifications.with_type_base('TaxonNameClassification::Latinized::PartOfSpeech').first
  end

  # @return [String]
  #   part of speech of a species as string.
  def part_of_speech_name
    part_of_speech_instance.try(:classification_label).try(:downcase)
  end

  # @return [Array of String]
  #   the unique string labels (human readable) derived from TaxonNameClassifications
  def statuses_from_classifications
    list = taxon_name_classifications_for_statuses
    list.empty? ? [] : list.collect{|c| c.classification_label }.sort
  end

  # @return [Scope]
  def taxon_name_classifications_for_statuses
    taxon_name_classifications.with_type_array(ICZN_TAXON_NAME_CLASSIFICATION_NAMES + ICN_TAXON_NAME_CLASSIFICATION_NAMES + ICNP_TAXON_NAME_CLASSIFICATION_NAMES + ICVCN_TAXON_NAME_CLASSIFICATION_NAMES)
  end

  # @return [Array of String]
  #   the unique string labels derived from and TaxonNameRelationships
  def statuses_from_relationships
    list = taxon_name_relationships.with_type_array(STATUS_TAXON_NAME_RELATIONSHIP_NAMES)
    list.empty? ? [] : list.collect{|c| c.subject_status}.sort
  end

  # @return [Array of String]
  #   the unique string labels derived from both TaxonNameClassifications and TaxonNameRelationships
  def combined_statuses
    (statuses_from_classifications + statuses_from_relationships).uniq.sort
  end

  # @return [Array of Protonym]
  #   All of the names this name has been in combination with
  def combination_list_all
    taxon_name_relationships.with_type_base('TaxonNameRelationship::Combination').collect {|r| r.object_taxon_name}.uniq
  end

  # @return [Array of Protonym]
  #   for all names this name has been in combination with, select those names that are of the same rank (!! CONFIRM?)
  def combination_list_self
    combination_list_all.select{|c| c.protonyms_by_rank[c.protonyms_by_rank.keys.last] == self}
  end

  # @return [String]
  #   combination of cached_html and cached_author_year.
  def cached_html_name_and_author_year
    [cached_html, cached_author_year].compact.join(' ')
  end

  # @return [String]
  #   combination of cached_original_combination_html and original_author_year.
  def cached_html_original_name_and_author_year
    [cached_original_combination_html, original_author_year].compact.join(' ')
  end

  # @return [String] combination of cached and cached_author_year.
  def cached_name_and_author_year
    [cached, cached_author_year].compact.join(' ')
  end

  # @return [String, nil]
  #   derived from cached_author_year
  #   !! DO NOT USE IN building cached !!
  #   See also app/helpers/taxon_names_helper
  def original_author_year
    if nomenclatural_code == :iczn && !cached_misspelling && !name_is_misapplied?
      cached_author_year&.gsub(/^\(|\)/, '')
    elsif nomenclatural_code == :icn && cached_author_year
      if matchdata1 = cached_author_year.match(/(\(.*\))/)
        matchdata1[1].gsub(/^\(|\)/, '')
      else
        cached_author_year
      end
    else
      cached_author_year
    end
  end

  # @return [Array of TaxonName] ancestors of type 'Protonym'
  def ancestor_protonyms
    Protonym.ancestors_of(self)
  end

  # @return [Array of TaxonName] descendants of type 'Protonym'
  def descendant_protonyms
    Protonym.descendants_of(self)
  end

  # @return [Boolean]
  #   true if there is a relationship where then name is asserted to be invalid
  def relationship_invalid?
    !first_possible_valid_taxon_name_relationship.nil?
  end

  # @return [Boolean]
  #  true if this name has any classification asserting that it is valid
  def classification_valid?
    taxon_name_classifications.with_type_array(TAXON_NAME_CLASS_NAMES_VALID).any? # !TaxonNameClassification.where_taxon_name(self).with_type_array(TAXON_NAME_CLASS_NAMES_VALID).empty?
  end

  # @return [Boolean]
  #  whether this name has any classification asserting that this the name is NOT valid or that it is unavailable
  def classification_invalid_or_unavailable?
    taxon_name_classifications.with_type_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).any?
  end

  #  @return [Boolean]
  #     return true if name is unavailable OR invalid, else false, checks both classifications and relationships
  # !! Should only be referenced when building cached values, all other uses should rather be `!is_valid?`
  def unavailable_or_invalid?
    return false if classification_valid?
    classification_invalid_or_unavailable? || relationship_invalid?
  end

  # @return [Boolean]
  #   after all inference on the validity of a name, the result is stored
  #   in cached_valid_taxon_name_id, #is_valid checks that result
  def is_valid?
    cached_is_valid
  end

  # Has Classification, but no relationship describing why
  def is_ambiguously_invalid?
    !is_valid? && (id == cached_valid_taxon_name_id)
  end

  # @return [Boolean]
  #   whether this name needs italics applied
  def is_italicized?
    is_genus_or_species_rank? || kind_of?(Combination) || kind_of?(Hybrid)
  end

  def is_protonym?
    type == 'Protonym'
  end

  def is_combination?
    type == 'Combination'
  end

  # @return [True|False]
  #   true if this name has a TaxonNameClassification of Fossil
  def is_fossil?
    taxon_name_classifications.with_type_contains('::Fossil').any?
  end

  # @return [Boolean]
  #   true if this name has a TaxonNameClassification of hybrid
  def is_hybrid?
    taxon_name_classifications.where_taxon_name(self).with_type_contains('Hybrid').any?
  end

  # @return [True|False]
  #   true if this name has a TaxonNameClassification of candidatus
  def is_candidatus?
    return false unless rank_string =~ /Icnp/
    taxon_name_classifications.where_taxon_name(self).with_type_contains('Candidatus').any?
  end

  # @return [True|False]
  #   true if this name has a TaxonNameClassification of not_binominal
  def not_binominal?
    taxon_name_classifications.where_taxon_name(self).with_type_contains('NonBinominal').any?
  end

  # @return [Boolean]
  #  see subclasses
  def is_genus_or_species_rank?
    false
  end

  # @return [TaxonName]
  #  a valid taxon_name for an invalid name or self for valid name.
  #  a stub here - See Protonym and Combination
  def get_valid_taxon_name
    nil
  end

  # @return [TaxonNameRelationship]
  #  returns youngest taxon name relationship where self is the subject.
  def first_possible_valid_taxon_name_relationship
    taxon_name_relationships.reload.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_SYNONYM).youngest_by_citation
  end

  # @return [TaxonName]
  #    returns the youngest #object_taxon_name from the youngest taxon name relationship.
  def first_possible_valid_taxon_name
    return self if !unavailable_or_invalid?                      # catches all cases where no Classifications or Relationships are provided
    relationship = first_possible_valid_taxon_name_relationship
    relationship.nil? ? self : relationship.object_taxon_name    # ?! probably the if is caught by unavailable_or_invalid already
   end

  # @return [Array of TaxonName]
  #  returns list of invalid names for a given taxon.
  # TODO: Can't we just use #valid_id now?
  # this list does not return combinations
  def list_of_invalid_taxon_names
    first_pass = true
    list = {}
    while first_pass || !list.keys.select{|t| list[t] == false}.empty? do
      first_pass = false
      list_of_taxa_to_check = list.empty? ? [self] : list.keys.select{|t| list[t] == false}
      list_of_taxa_to_check.each do |t|
        potentialy_invalid_relationships = t.related_taxon_name_relationships.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_SYNONYM).order_by_oldest_source_first
        potentialy_invalid_relationships.each do |r|
          if !TaxonNameClassification.where_taxon_name(r.subject_taxon_name).with_type_array(TAXON_NAME_CLASS_NAMES_VALID).empty?
            # do nothing, taxon has a status of valid name
          elsif r == r.subject_taxon_name.first_possible_valid_taxon_name_relationship
            list[r.subject_taxon_name] = false if list[r.subject_taxon_name].nil?
          end

        end
        list[t] = true if list[t] == false
      end
    end
    return [] if list.empty?
    list.sort_by{|t, a| (t.nomenclature_date || Time.now)}.collect{|t, a| t}
  end

  def gbif_status_array
    return nil if self.class.nil?
    return ['combination'] if self.class == 'Combination'
    s1 = taxon_name_classifications.collect{|c| c.class.gbif_status}.compact
    return s1 unless s1.empty?
    s2 = taxon_name_relationships.collect{|r| r.class.gbif_status_of_subject}
    s3 = related_taxon_name_relationships.collect{|r| r.class.gbif_status_of_object}

    s = s2 + s3
    s.compact!
    return ['valid'] if s.empty?
    s
  end

  # @return [Array of Strings]
  #   names of all genera where the species was placed
  def name_in_gender(gender = nil)
    case gender
      when 'masculine'
        n = masculine_name
      when 'feminine'
        n = feminine_name
      when 'neuter'
        n = neuter_name
      else
        n = nil
    end
    n = n.blank? ? name : n
    return n
  end

  # def create_new_combination_if_absent
  # return true unless type == 'Protonym'
  # if !TaxonName.with_cached_html(cached_html).count == 0 (was intent to make this always fail?!)
  #
  #  if TaxonName.where(cached: cached, project_id: project_id).any?
  #    begin
  #      TaxonName.transaction do
  #        c = Combination.new
  #        safe_self_and_ancestors.each do |i|
  #          case i.rank
  #            when 'genus'
  #              c.genus = i
  #            when 'subgenus'
  #              c.subgenus = i
  #            when 'species'
  #              c.species = i
  #            when 'subspecies'
  #              c.subspecies = i
  #          end
  #        end
  #        c.save
  #      end
  #    rescue
  #    end
  #    false
  #  end
  # end

  def clear_cached(update: false)
    assign_attributes(
      cached_html: nil,
      cached_author_year: nil,
      cached_original_combination_html: nil,
      cached_secondary_homonym: nil,
      cached_primary_homonym: nil,
      cached_secondary_homonym_alternative_spelling: nil,
      cached_primary_homonym_alternative_spelling: nil,
      cached_misspelling: nil,
      cached_classified_as: nil,
      cached: nil,
      cached_valid_taxon_name_id: nil,
      cached_is_valid: nil,
      cached_original_combination: nil,
      cached_nomenclature_date: nil
    )
    save if update
  end

  def set_cached
    n = get_full_name
    update_column(:cached, n)

    # We can't use the in-memory cache approach for combination names, force reload each time
    n = nil if is_combination?

    update_columns(
      cached_html: get_full_name_html(n),
      cached_nomenclature_date: nomenclature_date)

    set_cached_valid_taxon_name_id
    set_cached_is_valid

    # These two can be isolated as they are not always pertinent to a generalized cascading cache setting
    # For example, when a TaxonName relationship forces a cached reload it may/not need to call these two things
    set_cached_classified_as
    set_cached_author_year
  end

  def set_cached_valid_taxon_name_id
    update_column(:cached_valid_taxon_name_id, get_valid_taxon_name.id)
  end

  def set_cached_is_valid
    v = is_combination? ? false : !unavailable_or_invalid?
    update_column(:cached_is_valid, v)
  end

  def set_cached_warnings
    update_columns(
      cached:  NO_CACHED_MESSAGE,
      cached_author_year:  NO_CACHED_MESSAGE,
      cached_nomenclature_date: NO_CACHED_MESSAGE,
      cached_classified_as: NO_CACHED_MESSAGE,
      cached_html:  NO_CACHED_MESSAGE
    )
  end

  def set_cached_author_year
    update_column(:cached_author_year, get_author_and_year)
  end

  def set_cached_classified_as
    update_column(:cached_classified_as, get_cached_classified_as)
  end

  def get_cached_misspelling
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_ONLY).first
    unless misspelling.nil?
      n1 = verbatim_name? ? verbatim_name : name
      n2 = misspelling.object_taxon_name.verbatim_name? ? misspelling.object_taxon_name.verbatim_name : misspelling.object_taxon_name.name
      return true if n1 != n2
    end
    nil
  end

  # Stub, see subclasses
  # TaxonNameRelationships call it for Combinations
  def get_original_combination
    nil
  end

  # Stub, see subclasses
  #   only Protonym, but TaxonNameRelationships call it for Combinations
  def get_original_combination_html
    nil
  end

  # Returns an Array of ancestors
  #   same as self.ancestors, but also works
  #   for new records when parents specified
  def ancestors_through_parents(result = [self], start = self)
    if start.parent.nil?
      return result.reverse
    else
      result << start.parent
      ancestors_through_parents(result, start.parent)
    end
  end

  # @return [Array of TaxonName]
  #   an list of ancestors, Root first
  # Uses parent recursion when record is new and awesome_nested_set_is_not_usable
  def safe_self_and_ancestors
    if new_record?
      ancestors_through_parents
    else
      self_and_ancestors.reload.to_a.reverse ## .self_and_ancestors returns empty array!!!!!!!
    end
  end

  # @return [ rank, prefix, name], ...] for genus and below
  # @taxon_name.full_name_array # =>
  #   [ ["genus", [nil, "Aus"]],
  #     ["subgenus", [nil, "Aus"]],
  #  "section"=>["sect.", "Aus"], "series"=>["ser.", "Aus"], "species"=>[nil, "aaa"], "subspecies"=>[nil, "bbb"], "variety"=>["var.", "ccc"]\}
  def full_name_array
    gender = nil
    data = []
    safe_self_and_ancestors.each do |i|
      rank = i.rank
      gender = i.gender_name if rank == 'genus'
      method = "#{rank.gsub(/\s/, '_')}_name_elements"
      data.push([rank] + send(method, i, gender)) if self.respond_to?(method)
    end
    data
  end

  # @return Hash
  #  { 'family' => 'Aidae', ...}
  def ancestor_hash
    h = {}
    safe_self_and_ancestors.each do |n|
      h[n.rank] = n.name
    end
    h
  end

  # !! TODO: when name is a subgenus will not grab genus
  # !! TODO: Higher classification does not follow the same pattern
  # ?? TODO: Replace with `taxonomy` object .to_h?
  #
  # @!return [ { rank => [prefix, name] }
  #   Returns a hash of rank => [prefix, name] for genus and below
  # @taxon_name.full_name_hash # =>
  #      { "family' => 'Gidae',
  #        "genus" => [nil, "Aus"],
  #        "subgenus" => [nil, "Aus"],
  #        "section" => ["sect.", "Aus"],
  #        "series" => ["ser.", "Aus"],
  #        "species" => [nil, "aaa"],
  #        "subspecies" => [nil, "bbb"],
  #        "variety" => ["var.", "ccc"]}
  def full_name_hash
    gender = nil
    data = {}
    safe_self_and_ancestors.each do |i| # !! You can not use self.self_and_ancestors because (this) record is not saved off.
      rank = i.rank
      gender = i.gender_name if rank == 'genus'

      if i.is_genus_or_species_rank?
        if ['genus', 'subgenus', 'species', 'subspecies'].include?(rank)
          data[rank] = [nil, i.name_with_misspelling(gender)]
        else
          data[rank] = [i.rank_class.abbreviation, i.name_with_misspelling(gender)]
        end
      else
        data[rank] = i.name
      end
    end

    # Only check for these ranks
    if COMBINATION_ELEMENTS.include?(rank.to_sym)
      if data['genus'].nil?
        if original_genus
          data['genus'] = [nil, "[#{original_genus&.name}]"]
        else
          data['genus'] = [nil, '[GENUS NOT SPECIFIED]']
        end
      end

      if data['species'].nil? && (!data['subspecies'].nil? || !data['variety'].nil? || !data['subvariety'].nil? || !data['form'].nil? || !data['subform'].nil?)
        data['species'] = [nil, '[SPECIES NOT SPECIFIED]']
      end

      if !data['subvariety'].nil? && data['variety'].nil?
        data['variety'] = [nil, '[VARIETY NOT SPECIFIED]']
      end

      if !data['subform'].nil? && data['form'].nil?
        data['form'] = [nil, '[FORM NOT SPECIFIED]']
      end
    end

    data
  end

  # @return [String, nil]
  #  A monominal if names is above genus, or a full epithet if below.
  #  Does not include author_year. Does not include HTML.
  def get_full_name
    return name_with_misspelling(nil) if type != 'Combination' && !GENUS_AND_SPECIES_RANK_NAMES.include?(rank_string)
    return name if rank_class.to_s =~ /Icvcn/
    return verbatim_name if verbatim_name.present? && is_combination?

    d = full_name_hash

    elements = []

    elements.push(d['genus']) unless (not_binominal? && d['genus'][1] == '[GENUS NOT SPECIFIED]')

    elements.push ['(', d['subgenus'], ')']
    elements.push ['(', d['infragenus'], ')'] if rank_name == 'infragenus'
    elements.push ['(', d['supergenus'], ')'] if rank_name == 'supergenus'
    elements.push ['(', d['supersubgenus'], ')'] if rank_name == 'supersubgenus'
    elements.push ['(', d['supersupersubgenus'], ')'] if rank_name == 'supersupersubgenus'
    elements.push [d['supersuperspecies']] if rank_name == 'supersuperspecies'
    elements.push [d['superspecies']] if rank_name == 'superspecies'
    elements.push [d['subsuperspecies']] if rank_name == 'subsuperspecies'

    elements.push(d['species'], d['subspecies'], d['variety'], d['subvariety'], d['form'], d['subform'])

    elements = elements.flatten.compact.join(' ').gsub(/\(\s*\)/, '').gsub(/\(\s/, '(').gsub(/\s\)/, ')').squish
    elements.blank? ? nil : elements
  end

  def get_full_name_html(name = nil)
    name = get_full_name if name.nil?
    return  "\"<i>Candidatus</i> #{name}\"" if is_candidatus?
    if !name.blank? && is_hybrid?
      w = name.split(' ')
      w[-1] = ('×' + w[-1]).gsub('×(', '')
      name = w.join(' ')
    end

    m = name
    m = Utilities::Italicize.taxon_name(name) if is_italicized?
    m = '' + m if is_fossil?
    m
  end

  # @return [String]
  #    TODO: does this form of the name contain parens for subgenus?
  #    TODO: provide a default to gender (but do NOT eliminate param)
  #    TODO: on third thought- eliminate this mess
  def name_with_misspelling(gender)
    if cached_misspelling
      if rank_string =~ /Icnp/
        name.to_s + ' (sic)'
      else
        name.to_s + ' [sic]'
      end
    elsif gender.nil? || rank_string =~ /Genus/
      name.to_s
    else
      name_in_gender(gender).to_s
    end
  end

  # @return [String, nil]
  def genderized_name(gender = nil)
    if gender.nil? || is_genus_rank?
      name
    else
      name_in_gender(gender)
    end
  end

  # return [String, nil, false] # TODO: fix
  def get_genus_species(genus_option, self_option)
  # see Protonym
    true
  end

  # return [Boolean]
  #   whether there is an ICZN missapplication relationship present on this name
  def name_is_misapplied?
    !TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_string('TaxonNameRelationship::Iczn::Invalidating::Misapplication').empty?
  end

  # return [String]
  #   the author and year of the name, adds parenthesis where asserted
  def get_author_and_year
    if self.type == 'Combination'
      c = protonyms_by_rank
      return nil if c.empty?
      taxon = c[c.keys.last]
    else
      taxon = self
    end

    case taxon.rank_class.try(:nomenclatural_code)
    when :iczn
      ay = iczn_author_and_year(taxon)
    when :icvcn
      ay = icn_author_and_year(taxon)
    when :icnp
      ay = icn_author_and_year(taxon)
    when :icn
      ay = icn_author_and_year(taxon)
    else
      ay = ([author_string] + [year_integer]).compact.join(' ')
    end
    ay.blank? ? nil : ay
  end

  def icn_author_and_year(taxon)
    ay = nil

    misapplication = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_string('TaxonNameRelationship::Icn::Unaccepting::Misapplication')
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_AUTHOR_STRING)
    m_obj = misapplication.empty? ? nil : misapplication.first.object_taxon_name
    mobj = misspelling.empty? ? taxon : misspelling.first.object_taxon_name
    ay = mobj.try(:author_string) # author string for basionym
    if self.type == 'Combination'
      cc = self
    else
      current_combination = TaxonNameRelationship.where_object_is_taxon_name(mobj).with_type_string('TaxonNameRelationship::CurrentCombination')
      cc = current_combination.empty? ? self : current_combination.first.subject_taxon_name
    end

    if !self.author_string.blank? && mobj.id != cc.id
      ay = '(' + ay.to_s + ') ' + cc.try(:author_string).to_s
    end

    if !misapplication.empty? && !m_obj.author_string.blank?
      ay += ' non ' + m_obj.author_string
    end

    ay.blank? ? nil : ay
  end

  # @return [String, nil]
  #   the authors, and year, with parentheses as inferred by the data
  def iczn_author_and_year(taxon)
    ay = nil
    p = nil

    misapplication = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_string('TaxonNameRelationship::Iczn::Invalidating::Misapplication')
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_AUTHOR_STRING)

    mobj = misspelling.empty? ? nil : misspelling.first.object_taxon_name
    unless mobj.blank?
      a = [mobj.try(:author_string)]
      y = [mobj.try(:year_integer)]
    else
      a = [taxon.try(:author_string)]
      y = [taxon.try(:year_integer)]
    end

    if a[0] =~ /^\(.+\)$/ # (Author)
      a[0] = a[0][1..-2] ## remove parentheses in the author string
      p = true
    else
      p = false
    end

    ay = (a + y).compact.join(', ')

    obj = misapplication.empty? ? nil : misapplication.first.object_taxon_name

    if SPECIES_RANK_NAMES_ICZN.include?(taxon.rank_string)
      if p
        ay = '(' + ay + ')' unless ay.empty?
      else
        og = taxon.original_genus
        if self.type == 'Combination'
          cg = genus
        else
          cg = ancestor_at_rank('genus')
        end
        unless og.nil? || cg.nil?
          ay = '(' + ay + ')' if !ay.empty? && og.normalized_genus.id != cg.normalized_genus.id
        end
      end
    end

    unless misapplication.empty? || obj.author_string.blank?
      ay += ' non ' + ([obj.author_string] + [obj.year_integer]).compact.join(', ')
    end

    ay.blank? ? nil : ay
  end

  # @return Protonym
  def normalized_genus
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING)
    tn = misspelling.empty? ? self : misspelling.first.object_taxon_name
    return tn.lowest_rank_coordinated_taxon
  end

  # @return [String, nil]
  def get_cached_classified_as
    return nil unless is_protonym? || is_combination?
    # source_classified_as is a method generated through relationships
    r = reload_source_classified_as
    return " (as #{r.name})" unless r.blank?
    nil
  end

  # @return [Boolean]
  def parent_is_set?
    !parent_id.nil? || (parent && parent.persisted?)
  end

  # TODO: this should be paginated, not all IDs!
  def next_sibling
    if siblings.where(project_id: project_id).load.any?
      sibs = self_and_siblings.order(:cached).pluck(:id)
      s = sibs.index(id)
      TaxonName.find(sibs[ s + 1] ) if s < sibs.length - 1
    else
      nil
    end
  end

  # TODO: this should be paginated, not all IDs!
  def previous_sibling
    if siblings.where(project_id: project_id).load.any?
      sibs = self_and_siblings.order(:cached).pluck(:id)
      s = sibs.index(id)
      TaxonName.find(sibs[s - 1]) if s != 0
    else
      nil
    end
  end

  def create_otu
    Otu.create(by: creator, project: project, taxon_name_id: id)
  end

  # @return [Scope]
  #   All taxon names attached to relationships recently created by user
  def self.used_recently_in_classifications(user_id, project_id)
    TaxonName.where(project_id: project_id, created_by_id: user_id)
      .joins(:taxon_name_classifications)
      .includes(:taxon_name_classifications)
      .where(taxon_name_classifications: { created_at: 1.weeks.ago..Time.now } )
      .order('taxon_name_classifications.updated_at DESC')
  end

  # @return [Scope]
  #   All taxon names attached to relationships recently created by user
  def self.used_recently_in_relationships(user_id, project_id)
    t = TaxonNameRelationship.arel_table
    t1 = t.alias('tnr1')
    t2 = t.alias('tnr2')

    sql = t1[:updated_by_id].eq(user_id).or(t1[:created_by_id].eq(user_id))
      .or(t2[:updated_by_id].eq(user_id).or(t2[:created_by_id].eq(user_id))
    ).to_sql

    sql2 = t1[:created_at].between( 1.weeks.ago..Time.now )
      .or( t2[:created_at].between( 1.weeks.ago..Time.now ) ).to_sql

    TaxonName.with_taxon_name_relationships
      .where(taxon_names: {project_id: project_id})
      .where(sql2)
      .where(sql)
      .order('taxon_names.updated_at DESC') ## needs optimisation. Does not sort by TNR date
  end

  # @return [Array]
  def self.used_recently(user_id, project_id, target: nil)
    klass, a,b,c = nil, nil, nil, nil
    if target == 'TypeMaterial'
      klass = Protonym
      a = klass.is_species_group.touched_by(user_id).where(project_id: project_id).order(updated_at: :desc).limit(6).to_a
      b = used_recently_in_classifications(user_id, project_id).is_species_group.where(type: klass.name).limit(6).to_a
      c = used_recently_in_relationships(user_id, project_id).is_species_group.where(type: klass.name).limit(6).to_a
    else
      klass = TaxonName
      a = klass.touched_by(user_id).where(project_id: project_id).order(updated_at: :desc).limit(6).to_a
      b = used_recently_in_classifications(user_id, project_id).where(type: klass.name).limit(6).to_a
      c = used_recently_in_relationships(user_id, project_id).where(type: klass.name).limit(6).to_a
    end

    d = [ a,b,c ].flatten.compact.uniq.sort{|e,f| e.cached <=> f.cached}
  end

  # @return [Hash]
  def self.select_optimized(user_id, project_id, target: nil)
    klass = (target == 'TypeMaterial' ? Protonym : TaxonName)
    h = {
      recent: klass.used_recently(user_id, project_id, target: klass.name),
      pinboard: klass.pinned_by(user_id).pinned_in_project(project_id).to_a
    }

    h[:quick] = (klass.pinned_by(user_id).pinboard_inserted.pinned_in_project(project_id).to_a + h[:recent][0..3]).uniq
    h
  end

  # See Shared::MatrixHooks
  # @return [{"matrix_row_item": matrix_column_item, "object": object}, false]
  # the hash corresponding to the keyword used in this tag if it exists
  # !! Assumes it can only be in one matrix, this is wrong !!
  def matrix_row_item
    mri = ObservationMatrixRowItem::TaxonNameRowItem.where(taxon_name_id: id, project_id: project_id).limit(1)

    if mri.any?
      return { matrix_row_item: mri.first, object: taxon_name }
    else
      return false
    end
  end

  # @return [String]
  #  a reified ID is used when the original combination, which does not yet have it's own ID, is not the same as the current classification
  # Some observations:
  #  - reified ids are only for original combinations (for which we have no ID)
  #  - reified ids never reference gender changes because they are always in context of original combination, i.e. there is never a gender change
  # Mental note- consider combination - is_current_placement? (presently excluded in CoL code, which is the correct place to decide that.)
  # Duplicated in COLDP export code
  def reified_id
    return id.to_s if is_combination?
    return id.to_s unless has_alternate_original?
    id.to_s + '-' + Digest::MD5.hexdigest(cached_original_combination)
  end

  protected

  def check_for_children
    if leaf?
      true
    else
      errors.add(:base, 'This taxon has children names attached, delete those first.')
      throw :abort
    end
  end

  def validate_parent_is_set
    if !(rank_class == NomenclaturalRank) && !(type == 'Combination')
      errors.add(:parent_id, 'is not selected') if !parent_is_set?
    end
  end

  def validate_parent_from_the_same_project
    if parent && !project_id.blank?
      errors.add(:project_id, "The parent taxon is not from the same project") if project_id != parent.project_id
    end
  end

  def validate_one_root_per_project
    if new_record? || parent_id_changed? # project_id !?@
      if !parent_is_set? && TaxonName.where(parent_id: nil, project_id: project_id).count > 0
        errors.add(:parent_id, 'should not be empty/only one root is allowed per project')
      end
    end
  end

  # TODO: move to Protonym when we eliminate TaxonName.new()
  def check_new_parent_class
    if is_protonym? && parent_id != parent_id_was && !parent_id_was.nil? && nomenclatural_code == :iczn
      if old_parent = TaxonName.find_by(id: parent_id_was)
        if (rank_name == 'subgenus' || rank_name == 'subspecies') && old_parent.name == name
          errors.add(:parent_id, "The nominotypical #{rank_name} #{name} can not be moved out of the nominal #{old_parent.rank_name}")
        end
      end
    end
  end

  # See subclasses
  def validate_rank_class_class
    true
  end

  # Note- prior version prevented groups from moving when set in error, and was far too strict
  def check_new_rank_class
    if (rank_class != rank_class_was) && !rank_class_was.nil?

      if rank_class_was == 'NomenclaturalRank' && rank_class_changed?
        errors.add(:rank_class, 'Root can not have a new rank')
        return
      end
    end
  end

  def validate_source_type
    a = source && source.type != 'Source::Bibtex'
    b = origin_citation && origin_citation.try(:source).try(:type) != 'Source::Bibtex'
    if a || b
      errors.add(:base, 'Source must be a Bibtex')
    end
  end

  # TODO: this needs to go.
  def sv_missing_confidence_level # should be removed once the alternative solution is implemented. It is heavily used now.
    confidence_level_array = [93]
    confidence_level_array = confidence_level_array & ConfidenceLevel.where(project_id: self.project_id).pluck(:id)
    soft_validations.add(:base, 'Confidence level is missing') if !confidence_level_array.empty? && (self.confidences.pluck(:confidence_level_id) & confidence_level_array).empty?
  end

  def sv_missing_original_publication
    if rank_class.nil? || is_family_or_genus_or_species_rank?
      if self.source.nil?
        soft_validations.add(:base, 'Original publication is not selected')
      elsif self.origin_citation.try(:pages).blank?
        soft_validations.add(:base, 'Original citation pages are not recorded')
      elsif !self.source.pages.blank?
        matchdata1 = self.origin_citation.pages.match(/(\d+) ?[-–] ?(\d+)|(\d+)/)
        if matchdata1
          citMinP = matchdata1[1] ? matchdata1[1].to_i : matchdata1[3].to_i
          citMaxP = matchdata1[2] ? matchdata1[2].to_i : matchdata1[3].to_i
          matchdata = self.source.pages.match(/(\d+) ?[-–] ?(\d+)|(\d+)/)
          if citMinP && citMaxP && matchdata
          minP = matchdata[1] ? matchdata[1].to_i : matchdata[3].to_i
          maxP = matchdata[2] ? matchdata[2].to_i : matchdata[3].to_i
            minP = 1 if minP == maxP && %w{book booklet manual mastersthesis phdthesis techreport}.include?(self.source.bibtex_type)
            unless (maxP && minP && minP <= citMinP && maxP >= citMaxP)
              soft_validations.add(:base, 'Original citation could be out of the source page range')
            end
          end
        end
      end
    end
  end

  # def sv_missing_author
  #   true # see Protonym
  # end
  #
  # def sv_missing_year
  #   true # see Protonym
  # end

  def sv_missing_etymology
    true # see Protonym
  end

  def sv_parent_is_valid_name
    if !parent.nil? && parent.unavailable_or_invalid?
      soft_validations.add(:parent_id, 'Parent should be a valid taxon', success_message: 'Parent was updated', failure_message:  'Failed to update parent')
    end
  end

  def sv_fix_parent_is_valid_name
    res = false
    if self.parent.unavailable_or_invalid?
      new_parent = self.parent.get_valid_taxon_name
      if self.parent != new_parent
        self.parent = new_parent
        if self.parent.rank_class.parent.to_s == 'NomenclaturalRank::Iczn::GenusGroup' && self.rank_class.to_s == 'NomenclaturalRank::Iczn::SpeciesGroup::Subspecies'
          self.rank_class = 'NomenclaturalRank::Iczn::SpeciesGroup::Species'
        elsif self.parent.rank_class.parent.to_s == 'NomenclaturalRank::Iczn::FamilyGroup' && self.rank_class.to_s == 'NomenclaturalRank::Iczn::GenusGroup::Subgenus'
          self.rank_class = 'NomenclaturalRank::Iczn::GenusGroup::Genus'
        end
        begin
          TaxonName.transaction do
            self.save
            res = true
          end
        rescue
        end
      end
    end
    res
  end

  def sv_conflicting_subordinate_taxa
    classifications = self.taxon_name_classifications.reload
    classification_names = classifications.map { |i| i.type_name }
    compare = TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID & classification_names
    unless compare.empty?
      unless Protonym.with_parent_taxon_name(self).without_taxon_name_classification_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).empty?
        compare.each do |i|
          # taxon is unavailable or invalid, but has valid children
          soft_validations.add(:base, "Taxon has a status ('#{i.safe_constantize.label}') conflicting with presence of subordinate taxa")
        end
      end
    end
  end

  def sv_fix_cached_names
    begin
      TaxonName.transaction do
        self.set_cached
      end
      true
    rescue
      false
    end
  end

  # TODO: does this make sense now, with #valid_taxon_name_id in place?
  def sv_not_synonym_of_self
    if list_of_invalid_taxon_names.include?(self)
      soft_validations.add(:base, "Taxon has two conflicting relationships (invalidating and validating). To resolve a conflict, add a status 'Valid' to a valid taxon.")
    end
  end

  def sv_two_unresolved_alternative_synonyms
    r = taxon_name_relationships.includes(:source).order_by_oldest_source_first.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_SYNONYM)
    if r.to_a.size > 1
      if r.first.nomenclature_date.to_date == r.second.nomenclature_date.to_date
        soft_validations.add(:base, 'Taxon has two alternative invalidating relationships with identical dates. To resolve ambiguity, add original sources to the relationships with different priority dates.')
      end
    end
  end

  def sv_incomplete_combination
    soft_validations.add(:base, 'The genus in the combination is not specified') if !cached.nil? && cached.include?('GENUS NOT SPECIFIED')
    soft_validations.add(:base, 'The species in the combination is not specified') if !cached.nil? && cached.include?('SPECIES NOT SPECIFIED')
    soft_validations.add(:base, 'The variety in the combination is not specified') if !cached.nil? && cached.include?('VARIETY NOT SPECIFIED')
    soft_validations.add(:base, 'The form in the combination is not specified') if !cached.nil? && cached.include?('FORM NOT SPECIFIED')
    soft_validations.add(:base, 'The genus in the original combination is not specified') if !cached_original_combination.nil? && cached_original_combination.include?('GENUS NOT SPECIFIED')
    soft_validations.add(:base, 'The species in the original combination is not specified') if !cached_original_combination.nil? && cached_original_combination.include?('SPECIES NOT SPECIFIED')
  end

  def sv_cached_names
    true # see validation in subclasses
  end

  def sv_validate_parent_rank
    true # see validation in Protonym.rb
  end

  def sv_missing_relationships
    true # see validation in Protonym.rb
  end

  def sv_validate_name
    true # see validation in Protonym.rb
  end

  def sv_missing_classifications
    true # see validation in Protonym.rb
  end

  def sv_species_gender_agreement
    true # see validation in Protonym.rb
  end

  def sv_primary_types
    true # see validation in Protonym.rb
  end

  def sv_validate_coordinated_names
    true # see validation in Protonym.rb
  end

  def sv_type_placement
    true # see validation in Protonym.rb
  end

  def sv_single_sub_taxon
    true # see validation in Protonym.rb
  end

  def sv_parent_priority
    true # see validation in Protonym.rb
  end

  def sv_homotypic_synonyms
    true # see validation in Protonym.rb
  end

  def sv_potential_homonyms
    true # see validation in Protonym.rb
  end

  def sv_combination_duplicates
    true # see validation in Combination.rb
  end

  def sv_hybrid_name_relationships
    true # see validation in Hybrid.rb
  end

end

#cached_author_yearString?

Returns author and year string with parentheses where necessary, i.e. with context of present placement for ICZN.

Returns:

  • (String, nil)

    author and year string with parentheses where necessary, i.e. with context of present placement for ICZN



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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
# File 'app/models/taxon_name.rb', line 129

class TaxonName < ApplicationRecord

  # @return class
  #   this method calls Module#module_parent
  # TODO: This method can be placed elsewhere inside this class (or even removed if not used)
  #       when https://github.com/ClosureTree/closure_tree/issues/346 is fixed.
  def self.parent
    self.module_parent
  end

  # Must be before various of these includes, in particular MatrixHooks
  has_closure_tree

  include Housekeeping
  include Shared::DataAttributes
  include Shared::Tags
  include Shared::Identifiers
  include Shared::Notes
  include Shared::Depictions
  include Shared::Citations
  include Shared::Confidences
  include Shared::AlternateValues
  include Shared::HasPapertrail
  include SoftValidation
  include Shared::IsData
  include TaxonName::OtuSyncronization

  include Shared::MatrixHooks::Member
  include Shared::MatrixHooks::Dynamic

  include TaxonName::MatrixHooks

  # Allows users to provide arbitrary annotations that "over-ride" rank string
  ALTERNATE_VALUES_FOR = [:rank_class].freeze # !! Don't even think about putting this on `name`

  COMBINATION_ELEMENTS = [:genus, :subgenus, :species, :subspecies, :variety, :subvariety, :form, :subform].freeze

  SPECIES_EPITHET_RANKS = %w{species subspecies variety subvariety form subform}.freeze

  NOT_LATIN = Regexp.new(/[^a-zA-Z|\-]/).freeze # Dash is allowed?

  NO_CACHED_MESSAGE = 'REBUILD PROJECT TAXON NAME CACHE'.freeze

  NOMEN_VALID = {
    icn: 'http://purl.obolibrary.org/obo/NOMEN_0000383',
    icnp: 'http://purl.obolibrary.org/obo/NOMEN_0000081',
    icvcn: 'http://purl.obolibrary.org/obo/NOMEN_0000125',
    iczn: 'http://purl.obolibrary.org/obo/NOMEN_0000224'
  }

  # @return [Boolean]
  #   When true, also creates an OTU that is tied to this taxon name
  attr_accessor :also_create_otu

  # @return [Boolean]
  #   When true cached values are not built
  attr_accessor :no_cached
  delegate :nomenclatural_code, to: :rank_class, allow_nil: true
  delegate :rank_name, to: :rank_class, allow_nil: true

  # TODO: this was not implemented and tested properly
  # I think the intent is *before* save, i.e. the name will change
  # to a new cached value, so let's record the old one
  #  after_save :create_new_combination_if_absent

  after_save :set_cached, unless: Proc.new {|n| n.no_cached || errors.any? }
  after_save :set_cached_warnings, if: Proc.new {|n| n.no_cached }
  after_create :create_otu, if: :also_create_otu
  before_destroy :check_for_children, prepend: true

  validate :validate_rank_class_class,
    # :check_format_of_name,
    :validate_parent_from_the_same_project,
    :validate_parent_is_set,
    :check_new_rank_class,
    :check_new_parent_class,
    :validate_source_type,
    :validate_one_root_per_project

  # TODO: remove, this is handled natively
  validates_presence_of :type, message: 'is not specified'

  validates :year_of_publication, date_year: {min_year: 1000, max_year: Time.now.year + 5}, allow_nil: true
  validates :name, format: { without: /\s/ }

  # TODO: move some of these down to Protonym when they don't apply to Combination

  # TODO: think of a different name, and test
  has_many :historical_taxon_names, class_name: 'TaxonName', foreign_key: :cached_valid_taxon_name_id

  has_many :observation_matrix_row_items, inverse_of: :taxon_name, class_name: 'ObservationMatrixRowItem::Dynamic::TaxonName', dependent: :delete_all
  has_many :observation_matrices, through: :observation_matrix_row_items

  # TODO: revisit?
  belongs_to :valid_taxon_name, class_name: 'TaxonName', foreign_key: :cached_valid_taxon_name_id

  has_one :source_classified_as_relationship, -> {
    where(taxon_name_relationships: {type: 'TaxonNameRelationship::SourceClassifiedAs'})
  }, class_name: 'TaxonNameRelationship::SourceClassifiedAs', foreign_key: :subject_taxon_name_id

  has_one :source_classified_as, through: :source_classified_as_relationship, source: :object_taxon_name

  has_many :otus, inverse_of: :taxon_name, dependent: :restrict_with_error
  has_many :taxon_determinations, through: :otus
  has_many :collection_objects, through: :taxon_determinations, source: :biological_collection_object
  has_many :related_taxon_name_relationships, class_name: 'TaxonNameRelationship', foreign_key: :object_taxon_name_id, dependent: :restrict_with_error, inverse_of: :object_taxon_name

  has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', as: :role_object, dependent: :destroy
  has_many :taxon_name_authors, -> { order('roles.position ASC') }, through: :taxon_name_author_roles, source: :person

  # TODO: Combinations shouldn't have classifications or relationships?  Move to Protonym?
  has_many :taxon_name_classifications, dependent: :destroy, foreign_key: :taxon_name_id, inverse_of: :taxon_name
  has_many :taxon_name_relationships, foreign_key: :subject_taxon_name_id, dependent: :restrict_with_error, inverse_of: :subject_taxon_name

  # NOTE: Protonym subclassed methods might not be nicely tracked here, we'll have to see.  Placement is after has_many relationships. (?)
  accepts_nested_attributes_for :related_taxon_name_relationships, allow_destroy: true, reject_if: proc { |attributes| attributes['type'].blank? || attributes['subject_taxon_name_id'].blank? }
  accepts_nested_attributes_for :taxon_name_authors, :taxon_name_author_roles, allow_destroy: true
  accepts_nested_attributes_for :taxon_name_classifications, allow_destroy: true, reject_if: proc { |attributes| attributes['type'].blank?  }

  has_many :classified_as_unavailable_or_invalid, -> { where type: TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID }, class_name: 'TaxonNameClassification'

  scope :with_same_cached_valid_id, -> { where(arel_table[:id].eq(arel_table[:cached_valid_taxon_name_id])) }
  scope :with_different_cached_valid_id, -> { where(arel_table[:id].not_eq(arel_table[:cached_valid_taxon_name_id])) } # This doesn't catch all invalid names.  Those with classifications only are missed !$#!@#

  scope :that_is_valid, -> {where(cached_is_valid: true) }
  scope :that_is_invalid, -> {where(cached_is_valid: false) }

  def self.calculated_invalid
    a = TaxonName.with_different_cached_valid_id # that_is_invalid
    b = TaxonName.joins(:taxon_name_classifications).where(taxon_name_classifications: {type: TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID })
    TaxonName.from("((#{a.to_sql}) UNION (#{b.to_sql})) as taxon_names")
  end

  def self.calculated_valid
    # Alt format: TaxonName.that_is_valid.left_joins(:classified_as_unavailable_or_invalid).merge(TaxonNameClassification.where(id: nil))
    TaxonName
      .with_same_cached_valid_id
      .where.not(
        id: TaxonNameClassification.select(:taxon_name_id).where(type: TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID)
      )
  end

  scope :with_type, -> (type) {where(type: type)}
  scope :descendants_of, -> (taxon_name) { with_ancestor(taxon_name )}

  scope :ancestors_of, -> (taxon_name) {
    joins(:descendant_hierarchies)
      .where(taxon_name_hierarchies: {descendant_id: taxon_name.id})
      .where('taxon_name_hierarchies.ancestor_id != ?', taxon_name.id)
      .order('taxon_name_hierarchies.generations DESC') # root is at index 0
  }

  # LEAVE UNORDERED, if you want order:
  #   .order('taxon_name_hierarchies.generations DESC')
  scope :self_and_ancestors_of, -> (taxon_name) {
    joins(:descendant_hierarchies)
      .where(taxon_name_hierarchies: {descendant_id: taxon_name.id})
  }

  # Includes taxon_name, doesn't order result
  scope :ancestors_and_descendants_of, -> (taxon_name) do
    scoping do
      a = TaxonName.self_and_ancestors_of(taxon_name)
      b = TaxonName.descendants_of(taxon_name)
      TaxonName.from("((#{a.to_sql}) UNION (#{b.to_sql})) as taxon_names")
    end
  end

  # TODO: remove
  scope :with_rank_class, -> (rank_class_name) { where(rank_class: rank_class_name) }

  # TODO: remove
  scope :with_parent_taxon_name, -> (parent) { where(parent_id: parent) }

  scope :with_base_of_rank_class, -> (rank_class) { where('rank_class LIKE ?', "#{rank_class}%") }
  scope :with_rank_class_including, -> (include_string) { where('rank_class LIKE ?', "%#{include_string}%") }
  scope :project_root, -> (root_id) {where("(taxon_names.rank_class = 'NomenclaturalRank' AND taxon_names.project_id = ?)", root_id)}

  # A specific relationship
  scope :as_subject_with_taxon_name_relationship, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where(taxon_name_relationships: {type: taxon_name_relationship}) }
  scope :as_subject_with_taxon_name_relationship_base, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "#{taxon_name_relationship}%").references(:taxon_name_relationships) }
  scope :as_subject_without_taxon_name_relationship_base, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where('(taxon_name_relationships.type NOT LIKE ?) OR (taxon_name_relationships.subject_taxon_name_id IS NULL)', "#{taxon_name_relationship}%").references(:taxon_name_relationships) }
  scope :as_subject_with_taxon_name_relationship_array, -> (taxon_name_relationship_name_array) { includes(:taxon_name_relationships).where('(taxon_name_relationships.type IN (?)) OR (taxon_name_relationships.subject_taxon_name_id IS NULL)', "#{taxon_name_relationship_name_array}%").references(:taxon_name_relationships) }
  scope :as_subject_without_taxon_name_relationship_array, -> (taxon_name_relationship_name_array) { includes(:taxon_name_relationships).where('(taxon_name_relationships.type NOT IN (?)) OR (taxon_name_relationships.subject_taxon_name_id IS NULL)', "#{taxon_name_relationship_name_array}%").references(:taxon_name_relationships) }
  scope :as_subject_with_taxon_name_relationship_containing, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "%#{taxon_name_relationship}%").references(:taxon_name_relationships) }
  scope :as_object_with_taxon_name_relationship, -> (taxon_name_relationship) { includes(:related_taxon_name_relationships).where(taxon_name_relationships: {type: taxon_name_relationship}) }
  scope :as_object_with_taxon_name_relationship_base, -> (taxon_name_relationship) { includes(:related_taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "#{taxon_name_relationship}%").references(:related_taxon_name_relationships) }
  scope :as_object_with_taxon_name_relationship_containing, -> (taxon_name_relationship) { includes(:related_taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "%#{taxon_name_relationship}%").references(:related_taxon_name_relationships) }

  # @param relationship [Array, String]
  def self.with_taxon_name_relationship(relationship)
    a = TaxonName.joins(:taxon_name_relationships).where(taxon_name_relationships: {type: relationship})
    b = TaxonName.joins(:related_taxon_name_relationships).where(taxon_name_relationships: {type: relationship})
    TaxonName.from("((#{a.to_sql}) UNION (#{b.to_sql})) as taxon_names")
  end

  scope :with_taxon_name_relationships, -> {
    joins('LEFT OUTER JOIN taxon_name_relationships tnr1 ON taxon_names.id = tnr1.subject_taxon_name_id').
    joins('LEFT OUTER JOIN taxon_name_relationships tnr2 ON taxon_names.id = tnr2.object_taxon_name_id').
    where('tnr1.subject_taxon_name_id IS NOT NULL OR tnr2.object_taxon_name_id IS NOT NULL')
  }
  # *Any* relationship where there IS a relationship for a subject/object/both
  scope :with_taxon_name_relationships_as_subject, -> { joins(:taxon_name_relationships) }
  scope :with_taxon_name_relationships_as_object, -> { joins(:related_taxon_name_relationships) }

  # *Any* relationship where there is NOT a relationship for a subject/object/both
  scope :without_subject_taxon_name_relationships, -> { includes(:taxon_name_relationships).where(taxon_name_relationships: {subject_taxon_name_id: nil}) }
  scope :without_object_taxon_name_relationships, -> { includes(:related_taxon_name_relationships).where(taxon_name_relationships: {object_taxon_name_id: nil}) }

  scope :without_taxon_name_relationships, -> {
    joins('LEFT OUTER JOIN taxon_name_relationships tnr1 ON taxon_names.id = tnr1.subject_taxon_name_id').
    joins('LEFT OUTER JOIN taxon_name_relationships tnr2 ON taxon_names.id = tnr2.object_taxon_name_id').
    where('tnr1.subject_taxon_name_id IS NULL AND tnr2.object_taxon_name_id IS NULL')
  }

  # TODO: deprecate all of these for where()
  scope :with_parent_id, -> (parent_id) {where(parent_id: parent_id)}
  scope :with_cached_valid_taxon_name_id, -> (cached_valid_taxon_name_id) {where(cached_valid_taxon_name_id: cached_valid_taxon_name_id)}
  scope :with_cached_original_combination, -> (original_combination) { where(cached_original_combination: original_combination) }

  scope :without_otus, -> { includes(:otus).where(otus: {id: nil}) }
  scope :with_otus, -> { includes(:otus).where.not(otus: {id: nil}) }

  # @return [Scope]
  #   Combinations that are composed of children of this taxon name
  #     when those children are not currently descendants of this taxon name
  #
  # !! When :cached_valid_taxon_name_id is properly set then this method is not required
  # rather you should use :historical_taxon_names.
  #
  def self.out_of_scope_combinations(taxon_name_id)
    t = ::TaxonName.arel_table
    h = ::TaxonNameHierarchy.arel_table
    r = ::TaxonNameRelationship.arel_table

    h1 = h.alias('osch_')
    h2 = h.alias('oschh_')

    b = h.project(
      h1[:descendant_id].as('b'),
      h2[:ancestor_id].as('c'),
    ).from([h])

    b = b.join(r, Arel::Nodes::InnerJoin).on(h[:descendant_id].eq(r[:subject_taxon_name_id]).and(h[:ancestor_id].eq(taxon_name_id)))
      .join(h1, Arel::Nodes::InnerJoin).on(r[:object_taxon_name_id].eq(h1[:descendant_id]).and(h1[:ancestor_id].not_eq(h1[:descendant_id])))
      .join(h2, Arel::Nodes::OuterJoin).on(
        h1[:ancestor_id].eq(h2[:ancestor_id]).
        and(h2[:descendant_id].eq(taxon_name_id))
      )

    # This was particularly useful in debugging the join chain:
    # ap TaxonNameHierarchy.connection.execute(b.to_sql).collect{|a| a}

    b = b.as('abc')

    ::Combination
      .joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['b'].eq(t[:id]))))
      .where(b['c'].eq(nil))
      .distinct
  end

  # @return Scope
  #   names that are not leaves
  # TODO: belongs in lib/queries/filter.rb likely
  def self.not_leaves
    t = self.arel_table
    h = ::TaxonNameHierarchy.arel_table

    a = t.alias('a_')
    b = t.project(a[Arel.star]).from(a)

    c = h.alias('h1')

    b = b.join(c, Arel::Nodes::OuterJoin)
      .on(
        a[:id].eq(c[:ancestor_id])
      )

    e = c[:generations].not_eq(0)
    f = c[:ancestor_id].not_eq(c[:descendant_id])

    b = b.where(e.and(f))
    b = b.group(a[:id])
    b = b.as('tnh_')

    ::TaxonName.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(t['id']))))
  end

  # @return [Scope] Protonym(s) the **broad sense** synonyms of this name
  def synonyms
    TaxonName.with_cached_valid_taxon_name_id(self.id)
  end

  soft_validate(:sv_missing_confidence_level,
                set: :missing_fields,
                name: 'Missing confidence level',
                description: 'To remaind that the taxon spelling have to be compared to the original source' )

  soft_validate(:sv_missing_original_publication,
                set: :missing_fields,
                name: 'Missing original source',
                description: 'Original source is not selected' )

=begin
  soft_validate(:sv_missing_author,
                set: :missing_fields,
                name: 'Missing author')

  soft_validate(:sv_missing_year,
                set: :missing_fields,
                name: 'Missing year')
=end

  soft_validate(
    :sv_parent_is_valid_name,
    set: :parent_is_valid_name,
    fix: :sv_fix_parent_is_valid_name,
    name: 'Parent should be a valid taxon',
    description: 'When the parent taxon (for example a genus) is treated as a synonym, all subordinate taxa from this taxon should be transferred to the valid taxon' )

  soft_validate(
    :sv_conflicting_subordinate_taxa,
    set: :parent_is_valid_name,
    name: 'Conflicting subordinate taxa',
    description: 'Unavailable or invalid taxon should not have subordinate taxa' )

  soft_validate(
    :sv_cached_names,
    set: :cached_names,
    fix: :sv_fix_cached_names,
    name: 'Cached names',
    description: 'Check if cached values need to be updated' )

  # TODO: move to protonym spec?
  soft_validate(
    :sv_not_synonym_of_self,
    set: :not_synonym_of_self,
    name: 'Not synonym of self',
    description: 'Taxon has two conflicting relationships (invalidating and validating).' )

  # TODO: move to protonym spec?
  soft_validate(
    :sv_two_unresolved_alternative_synonyms,
    set: :two_unresolved_alternative_synonyms,
    name: 'Two conflicting relationships',
    description: 'Taxon has two conflicting relationships.' )

  # TODO: move to combination spec?
  soft_validate(
    :sv_incomplete_combination,
    set: :incomplete_combination,
    name: 'Incomplete combination',
    description: 'Intermediate ranks are missing in combination (either original or subsequent)' )

  # @return [Array of TaxonName]
  #   ordered by rank, a scope-like hack
  def self.sort_by_rank(taxon_names)
    taxon_names.sort!{|a, b| RANKS.index(a.rank_string) <=> RANKS.index(b.rank_string)}
  end

  # TODO: what is this:!? :)
  def self.foo(rank_classes)
    from <<-SQL.strip_heredoc
      ( SELECT *, rank()
           OVER (
               PARTITION BY rank_class, parent_id
               ORDER BY generations asc, name
            ) AS rn
         FROM taxon_names
         INNER JOIN "taxon_name_hierarchies" ON "taxon_names"."id" = "taxon_name_hierarchies"."descendant_id"
         WHERE #{rank_classes.collect{|c| "rank_class = '#{c}'" }.join(' OR ')}
         ) as taxon_names
    SQL
  end

  # @return [String]
  #   rank as human readable short-form, like 'genus' or 'species'
  def rank
    ::RANKS.include?(rank_string) ? rank_name : nil
  end

  # @return [String]
  #   rank (Kindom, Phylum...) as a string, like {NomenclaturalRank::Iczn::SpeciesGroup::Species}
  def rank_string
    read_attribute(:rank_class)
  end

  def rank_class=(value)
    write_attribute(:rank_class, value.to_s)
  end

  # @return [NomenclaturalRank class]
  #   rank as a {NomenclaturalRank} class, like {NomenclaturalRank::Iczn::SpeciesGroup::Species}
  def rank_class
    r = read_attribute(:rank_class)
    Ranks.valid?(r) ? r.safe_constantize : r
  end

  # @see .out_of_scope_combinations
  def out_of_scope_combinations
    ::TaxonName
      .where(project_id: project_id)
      .out_of_scope_combinations(id)
  end

  # @return [TaxonName, nil] an ancestor at the specified rank
  # @param rank [symbol|string|
  #   like :species or 'genus'
  # @param include_self [Boolean]
  #   if true then self will also be returned
  def ancestor_at_rank(rank, include_self = false)
    if target_code = (is_combination? ? combination_taxon_names.first.nomenclatural_code : nomenclatural_code)
      r = Ranks.lookup(target_code, rank)
      return self if include_self && (rank_class.to_s == r)
      ancestors.with_rank_class( r ).first
    else
      # Root has no nomenclature code
      return nil
    end
  end

  # @return scope [TaxonName, nil] an ancestor at the specified rank
  # @params rank [symbol|string|
  #   like :species or 'genus'
  def descendants_at_rank(rank)
    return TaxonName.none if nomenclatural_code.blank? # Root names
    descendants.with_rank_class(
      Ranks.lookup(nomenclatural_code, rank)
    )
  end

  # @return [Scope] Protonym(s) the **broad sense** synonyms of this name
  def synonyms
    TaxonName.with_cached_valid_taxon_name_id(id)
  end

  # @return [Array]
  #   all TaxonNameRelationships where this taxon is an object or subject.
  def all_taxon_name_relationships
    # !! If self relationships are ever made possible this needs a DISTINCT clause
    TaxonNameRelationship.find_by_sql(
      "SELECT taxon_name_relationships.*
         FROM taxon_name_relationships
         WHERE taxon_name_relationships.subject_taxon_name_id = #{self.id}
       UNION
       SELECT taxon_name_relationships.*
         FROM taxon_name_relationships
         WHERE taxon_name_relationships.object_taxon_name_id = #{self.id}")
  end

  # @return [Array of TaxonName]
  #   all taxon_names which have relationships to this taxon as an object or subject.
  def related_taxon_names
    TaxonName.find_by_sql(
      "SELECT tn.* from taxon_names tn join taxon_name_relationships tnr1 on tn.id = tnr1.subject_taxon_name_id and tnr1.object_taxon_name_id = #{self.id}
      UNION
      SELECT tn.* from taxon_names tn join taxon_name_relationships tnr2 on tn.id = tnr2.object_taxon_name_id and tnr2.subject_taxon_name_id = #{self.id}"
    )
  end

  # @return [String, nil]
  #   the baseline means of displaying name authorship, i.e. the author for this taxon, last name only.
  # Important, string format priority is
  #   1) as provided verbatim
  #   2) as generated from People
  #   3) as taken from the Source
  #       Source People (Authors)
  #       Source Verbatim authors
  def author_string
    return verbatim_author if !verbatim_author.nil?
    if taxon_name_authors.any?
      # TODO: Technically not correct if prefix/suffix involved.
      return Utilities::Strings.authorship_sentence( taxon_name_authors.pluck(:last_name) )
    end

    return source.authority_name if !source.nil?
    nil
  end

  # @return [Integer]
  #   a 4 digit integer representing year of publication, like 1974
  def year_integer
    return year_of_publication if !year_of_publication.nil?
    try(:source).try(:year)
  end

  # @return String, nil
  #   virtual attribute, to ultimately be fixed in db
  def cached_author
    cached_author_year&.gsub(/,\s\d+/, '')
  end

  # !! Overrides Shared::Citations#nomenclature_date
  #
  # @return [Time]
  #   effective date of publication, used to determine nomenclatural priority
  def nomenclature_date
    return nil if !persisted?
    family_before_1961 = TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_string('TaxonNameRelationship::Iczn::PotentiallyValidating::FamilyBefore1961').first

    # family_before_1961 = taxon_name_relationships.with_type_string('TaxonNameRelationship::Iczn::PotentiallyValidating::FamilyBefore1961').first
    if family_before_1961.nil?
      year = year_of_publication ? Time.utc(year_of_publication, 12, 31) : nil
      self.source ? (self.source.cached_nomenclature_date ? self.source.nomenclature_date : year) : year
    else
      obj = family_before_1961.object_taxon_name
      year = obj.year_of_publication ? Time.utc(obj.year_of_publication, 12, 31) : nil
      obj.source ? (source.cached_nomenclature_date ? obj.source.nomenclature_date : year) : year
    end
  end

  # @return [array]
  # returns array of hashes for history of taxon. Could be used for catalogue construction
  def nomeclatural_history
    history = []
    TaxonName.where(cached_valid_taxon_name_id: self.id).order(:cached_nomenclature_date).each do |t|
      item = {}
      source_author_string = t.is_combination? ? [t.origin_citation&.source&.authority_name, t.origin_citation&.source&.year].join(', ') : nil
      source_author_string = ' in ' + source_author_string unless source_author_string.nil?
      item[:name] = t.is_combination? ? t.cached_html : t.cached_original_combination_html
      item[:author_year] = t.is_combination? ? t.cached_author_year + source_author_string : t.original_author_year
      item[:statuses] = t.combined_statuses
      history.append(item)
    end
    return history
  end

  # @return [Class, nil]
  #   gender of a genus as class
  def gender_class
    gender_instance.try(:type_class)
  end

  # @return [TaxonNameClassification instance, nil]
  #    the gender classification of this name, if provided
  def gender_instance
    taxon_name_classifications.with_type_base('TaxonNameClassification::Latinized::Gender').first
  end

  # @return [String, nil]
  #    gender as a string (only applicable to Genera)
  def gender_name
    gender_instance.try(:classification_label).try(:downcase)
  end

  # @return [Class]
  #   part of speech of a species as class.
  def part_of_speech_class
    part_of_speech_instance.try(:type_class)
  end

  def part_of_speech_instance
    taxon_name_classifications.with_type_base('TaxonNameClassification::Latinized::PartOfSpeech').first
  end

  # @return [String]
  #   part of speech of a species as string.
  def part_of_speech_name
    part_of_speech_instance.try(:classification_label).try(:downcase)
  end

  # @return [Array of String]
  #   the unique string labels (human readable) derived from TaxonNameClassifications
  def statuses_from_classifications
    list = taxon_name_classifications_for_statuses
    list.empty? ? [] : list.collect{|c| c.classification_label }.sort
  end

  # @return [Scope]
  def taxon_name_classifications_for_statuses
    taxon_name_classifications.with_type_array(ICZN_TAXON_NAME_CLASSIFICATION_NAMES + ICN_TAXON_NAME_CLASSIFICATION_NAMES + ICNP_TAXON_NAME_CLASSIFICATION_NAMES + ICVCN_TAXON_NAME_CLASSIFICATION_NAMES)
  end

  # @return [Array of String]
  #   the unique string labels derived from and TaxonNameRelationships
  def statuses_from_relationships
    list = taxon_name_relationships.with_type_array(STATUS_TAXON_NAME_RELATIONSHIP_NAMES)
    list.empty? ? [] : list.collect{|c| c.subject_status}.sort
  end

  # @return [Array of String]
  #   the unique string labels derived from both TaxonNameClassifications and TaxonNameRelationships
  def combined_statuses
    (statuses_from_classifications + statuses_from_relationships).uniq.sort
  end

  # @return [Array of Protonym]
  #   All of the names this name has been in combination with
  def combination_list_all
    taxon_name_relationships.with_type_base('TaxonNameRelationship::Combination').collect {|r| r.object_taxon_name}.uniq
  end

  # @return [Array of Protonym]
  #   for all names this name has been in combination with, select those names that are of the same rank (!! CONFIRM?)
  def combination_list_self
    combination_list_all.select{|c| c.protonyms_by_rank[c.protonyms_by_rank.keys.last] == self}
  end

  # @return [String]
  #   combination of cached_html and cached_author_year.
  def cached_html_name_and_author_year
    [cached_html, cached_author_year].compact.join(' ')
  end

  # @return [String]
  #   combination of cached_original_combination_html and original_author_year.
  def cached_html_original_name_and_author_year
    [cached_original_combination_html, original_author_year].compact.join(' ')
  end

  # @return [String] combination of cached and cached_author_year.
  def cached_name_and_author_year
    [cached, cached_author_year].compact.join(' ')
  end

  # @return [String, nil]
  #   derived from cached_author_year
  #   !! DO NOT USE IN building cached !!
  #   See also app/helpers/taxon_names_helper
  def original_author_year
    if nomenclatural_code == :iczn && !cached_misspelling && !name_is_misapplied?
      cached_author_year&.gsub(/^\(|\)/, '')
    elsif nomenclatural_code == :icn && cached_author_year
      if matchdata1 = cached_author_year.match(/(\(.*\))/)
        matchdata1[1].gsub(/^\(|\)/, '')
      else
        cached_author_year
      end
    else
      cached_author_year
    end
  end

  # @return [Array of TaxonName] ancestors of type 'Protonym'
  def ancestor_protonyms
    Protonym.ancestors_of(self)
  end

  # @return [Array of TaxonName] descendants of type 'Protonym'
  def descendant_protonyms
    Protonym.descendants_of(self)
  end

  # @return [Boolean]
  #   true if there is a relationship where then name is asserted to be invalid
  def relationship_invalid?
    !first_possible_valid_taxon_name_relationship.nil?
  end

  # @return [Boolean]
  #  true if this name has any classification asserting that it is valid
  def classification_valid?
    taxon_name_classifications.with_type_array(TAXON_NAME_CLASS_NAMES_VALID).any? # !TaxonNameClassification.where_taxon_name(self).with_type_array(TAXON_NAME_CLASS_NAMES_VALID).empty?
  end

  # @return [Boolean]
  #  whether this name has any classification asserting that this the name is NOT valid or that it is unavailable
  def classification_invalid_or_unavailable?
    taxon_name_classifications.with_type_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).any?
  end

  #  @return [Boolean]
  #     return true if name is unavailable OR invalid, else false, checks both classifications and relationships
  # !! Should only be referenced when building cached values, all other uses should rather be `!is_valid?`
  def unavailable_or_invalid?
    return false if classification_valid?
    classification_invalid_or_unavailable? || relationship_invalid?
  end

  # @return [Boolean]
  #   after all inference on the validity of a name, the result is stored
  #   in cached_valid_taxon_name_id, #is_valid checks that result
  def is_valid?
    cached_is_valid
  end

  # Has Classification, but no relationship describing why
  def is_ambiguously_invalid?
    !is_valid? && (id == cached_valid_taxon_name_id)
  end

  # @return [Boolean]
  #   whether this name needs italics applied
  def is_italicized?
    is_genus_or_species_rank? || kind_of?(Combination) || kind_of?(Hybrid)
  end

  def is_protonym?
    type == 'Protonym'
  end

  def is_combination?
    type == 'Combination'
  end

  # @return [True|False]
  #   true if this name has a TaxonNameClassification of Fossil
  def is_fossil?
    taxon_name_classifications.with_type_contains('::Fossil').any?
  end

  # @return [Boolean]
  #   true if this name has a TaxonNameClassification of hybrid
  def is_hybrid?
    taxon_name_classifications.where_taxon_name(self).with_type_contains('Hybrid').any?
  end

  # @return [True|False]
  #   true if this name has a TaxonNameClassification of candidatus
  def is_candidatus?
    return false unless rank_string =~ /Icnp/
    taxon_name_classifications.where_taxon_name(self).with_type_contains('Candidatus').any?
  end

  # @return [True|False]
  #   true if this name has a TaxonNameClassification of not_binominal
  def not_binominal?
    taxon_name_classifications.where_taxon_name(self).with_type_contains('NonBinominal').any?
  end

  # @return [Boolean]
  #  see subclasses
  def is_genus_or_species_rank?
    false
  end

  # @return [TaxonName]
  #  a valid taxon_name for an invalid name or self for valid name.
  #  a stub here - See Protonym and Combination
  def get_valid_taxon_name
    nil
  end

  # @return [TaxonNameRelationship]
  #  returns youngest taxon name relationship where self is the subject.
  def first_possible_valid_taxon_name_relationship
    taxon_name_relationships.reload.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_SYNONYM).youngest_by_citation
  end

  # @return [TaxonName]
  #    returns the youngest #object_taxon_name from the youngest taxon name relationship.
  def first_possible_valid_taxon_name
    return self if !unavailable_or_invalid?                      # catches all cases where no Classifications or Relationships are provided
    relationship = first_possible_valid_taxon_name_relationship
    relationship.nil? ? self : relationship.object_taxon_name    # ?! probably the if is caught by unavailable_or_invalid already
   end

  # @return [Array of TaxonName]
  #  returns list of invalid names for a given taxon.
  # TODO: Can't we just use #valid_id now?
  # this list does not return combinations
  def list_of_invalid_taxon_names
    first_pass = true
    list = {}
    while first_pass || !list.keys.select{|t| list[t] == false}.empty? do
      first_pass = false
      list_of_taxa_to_check = list.empty? ? [self] : list.keys.select{|t| list[t] == false}
      list_of_taxa_to_check.each do |t|
        potentialy_invalid_relationships = t.related_taxon_name_relationships.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_SYNONYM).order_by_oldest_source_first
        potentialy_invalid_relationships.each do |r|
          if !TaxonNameClassification.where_taxon_name(r.subject_taxon_name).with_type_array(TAXON_NAME_CLASS_NAMES_VALID).empty?
            # do nothing, taxon has a status of valid name
          elsif r == r.subject_taxon_name.first_possible_valid_taxon_name_relationship
            list[r.subject_taxon_name] = false if list[r.subject_taxon_name].nil?
          end

        end
        list[t] = true if list[t] == false
      end
    end
    return [] if list.empty?
    list.sort_by{|t, a| (t.nomenclature_date || Time.now)}.collect{|t, a| t}
  end

  def gbif_status_array
    return nil if self.class.nil?
    return ['combination'] if self.class == 'Combination'
    s1 = taxon_name_classifications.collect{|c| c.class.gbif_status}.compact
    return s1 unless s1.empty?
    s2 = taxon_name_relationships.collect{|r| r.class.gbif_status_of_subject}
    s3 = related_taxon_name_relationships.collect{|r| r.class.gbif_status_of_object}

    s = s2 + s3
    s.compact!
    return ['valid'] if s.empty?
    s
  end

  # @return [Array of Strings]
  #   names of all genera where the species was placed
  def name_in_gender(gender = nil)
    case gender
      when 'masculine'
        n = masculine_name
      when 'feminine'
        n = feminine_name
      when 'neuter'
        n = neuter_name
      else
        n = nil
    end
    n = n.blank? ? name : n
    return n
  end

  # def create_new_combination_if_absent
  # return true unless type == 'Protonym'
  # if !TaxonName.with_cached_html(cached_html).count == 0 (was intent to make this always fail?!)
  #
  #  if TaxonName.where(cached: cached, project_id: project_id).any?
  #    begin
  #      TaxonName.transaction do
  #        c = Combination.new
  #        safe_self_and_ancestors.each do |i|
  #          case i.rank
  #            when 'genus'
  #              c.genus = i
  #            when 'subgenus'
  #              c.subgenus = i
  #            when 'species'
  #              c.species = i
  #            when 'subspecies'
  #              c.subspecies = i
  #          end
  #        end
  #        c.save
  #      end
  #    rescue
  #    end
  #    false
  #  end
  # end

  def clear_cached(update: false)
    assign_attributes(
      cached_html: nil,
      cached_author_year: nil,
      cached_original_combination_html: nil,
      cached_secondary_homonym: nil,
      cached_primary_homonym: nil,
      cached_secondary_homonym_alternative_spelling: nil,
      cached_primary_homonym_alternative_spelling: nil,
      cached_misspelling: nil,
      cached_classified_as: nil,
      cached: nil,
      cached_valid_taxon_name_id: nil,
      cached_is_valid: nil,
      cached_original_combination: nil,
      cached_nomenclature_date: nil
    )
    save if update
  end

  def set_cached
    n = get_full_name
    update_column(:cached, n)

    # We can't use the in-memory cache approach for combination names, force reload each time
    n = nil if is_combination?

    update_columns(
      cached_html: get_full_name_html(n),
      cached_nomenclature_date: nomenclature_date)

    set_cached_valid_taxon_name_id
    set_cached_is_valid

    # These two can be isolated as they are not always pertinent to a generalized cascading cache setting
    # For example, when a TaxonName relationship forces a cached reload it may/not need to call these two things
    set_cached_classified_as
    set_cached_author_year
  end

  def set_cached_valid_taxon_name_id
    update_column(:cached_valid_taxon_name_id, get_valid_taxon_name.id)
  end

  def set_cached_is_valid
    v = is_combination? ? false : !unavailable_or_invalid?
    update_column(:cached_is_valid, v)
  end

  def set_cached_warnings
    update_columns(
      cached:  NO_CACHED_MESSAGE,
      cached_author_year:  NO_CACHED_MESSAGE,
      cached_nomenclature_date: NO_CACHED_MESSAGE,
      cached_classified_as: NO_CACHED_MESSAGE,
      cached_html:  NO_CACHED_MESSAGE
    )
  end

  def set_cached_author_year
    update_column(:cached_author_year, get_author_and_year)
  end

  def set_cached_classified_as
    update_column(:cached_classified_as, get_cached_classified_as)
  end

  def get_cached_misspelling
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_ONLY).first
    unless misspelling.nil?
      n1 = verbatim_name? ? verbatim_name : name
      n2 = misspelling.object_taxon_name.verbatim_name? ? misspelling.object_taxon_name.verbatim_name : misspelling.object_taxon_name.name
      return true if n1 != n2
    end
    nil
  end

  # Stub, see subclasses
  # TaxonNameRelationships call it for Combinations
  def get_original_combination
    nil
  end

  # Stub, see subclasses
  #   only Protonym, but TaxonNameRelationships call it for Combinations
  def get_original_combination_html
    nil
  end

  # Returns an Array of ancestors
  #   same as self.ancestors, but also works
  #   for new records when parents specified
  def ancestors_through_parents(result = [self], start = self)
    if start.parent.nil?
      return result.reverse
    else
      result << start.parent
      ancestors_through_parents(result, start.parent)
    end
  end

  # @return [Array of TaxonName]
  #   an list of ancestors, Root first
  # Uses parent recursion when record is new and awesome_nested_set_is_not_usable
  def safe_self_and_ancestors
    if new_record?
      ancestors_through_parents
    else
      self_and_ancestors.reload.to_a.reverse ## .self_and_ancestors returns empty array!!!!!!!
    end
  end

  # @return [ rank, prefix, name], ...] for genus and below
  # @taxon_name.full_name_array # =>
  #   [ ["genus", [nil, "Aus"]],
  #     ["subgenus", [nil, "Aus"]],
  #  "section"=>["sect.", "Aus"], "series"=>["ser.", "Aus"], "species"=>[nil, "aaa"], "subspecies"=>[nil, "bbb"], "variety"=>["var.", "ccc"]\}
  def full_name_array
    gender = nil
    data = []
    safe_self_and_ancestors.each do |i|
      rank = i.rank
      gender = i.gender_name if rank == 'genus'
      method = "#{rank.gsub(/\s/, '_')}_name_elements"
      data.push([rank] + send(method, i, gender)) if self.respond_to?(method)
    end
    data
  end

  # @return Hash
  #  { 'family' => 'Aidae', ...}
  def ancestor_hash
    h = {}
    safe_self_and_ancestors.each do |n|
      h[n.rank] = n.name
    end
    h
  end

  # !! TODO: when name is a subgenus will not grab genus
  # !! TODO: Higher classification does not follow the same pattern
  # ?? TODO: Replace with `taxonomy` object .to_h?
  #
  # @!return [ { rank => [prefix, name] }
  #   Returns a hash of rank => [prefix, name] for genus and below
  # @taxon_name.full_name_hash # =>
  #      { "family' => 'Gidae',
  #        "genus" => [nil, "Aus"],
  #        "subgenus" => [nil, "Aus"],
  #        "section" => ["sect.", "Aus"],
  #        "series" => ["ser.", "Aus"],
  #        "species" => [nil, "aaa"],
  #        "subspecies" => [nil, "bbb"],
  #        "variety" => ["var.", "ccc"]}
  def full_name_hash
    gender = nil
    data = {}
    safe_self_and_ancestors.each do |i| # !! You can not use self.self_and_ancestors because (this) record is not saved off.
      rank = i.rank
      gender = i.gender_name if rank == 'genus'

      if i.is_genus_or_species_rank?
        if ['genus', 'subgenus', 'species', 'subspecies'].include?(rank)
          data[rank] = [nil, i.name_with_misspelling(gender)]
        else
          data[rank] = [i.rank_class.abbreviation, i.name_with_misspelling(gender)]
        end
      else
        data[rank] = i.name
      end
    end

    # Only check for these ranks
    if COMBINATION_ELEMENTS.include?(rank.to_sym)
      if data['genus'].nil?
        if original_genus
          data['genus'] = [nil, "[#{original_genus&.name}]"]
        else
          data['genus'] = [nil, '[GENUS NOT SPECIFIED]']
        end
      end

      if data['species'].nil? && (!data['subspecies'].nil? || !data['variety'].nil? || !data['subvariety'].nil? || !data['form'].nil? || !data['subform'].nil?)
        data['species'] = [nil, '[SPECIES NOT SPECIFIED]']
      end

      if !data['subvariety'].nil? && data['variety'].nil?
        data['variety'] = [nil, '[VARIETY NOT SPECIFIED]']
      end

      if !data['subform'].nil? && data['form'].nil?
        data['form'] = [nil, '[FORM NOT SPECIFIED]']
      end
    end

    data
  end

  # @return [String, nil]
  #  A monominal if names is above genus, or a full epithet if below.
  #  Does not include author_year. Does not include HTML.
  def get_full_name
    return name_with_misspelling(nil) if type != 'Combination' && !GENUS_AND_SPECIES_RANK_NAMES.include?(rank_string)
    return name if rank_class.to_s =~ /Icvcn/
    return verbatim_name if verbatim_name.present? && is_combination?

    d = full_name_hash

    elements = []

    elements.push(d['genus']) unless (not_binominal? && d['genus'][1] == '[GENUS NOT SPECIFIED]')

    elements.push ['(', d['subgenus'], ')']
    elements.push ['(', d['infragenus'], ')'] if rank_name == 'infragenus'
    elements.push ['(', d['supergenus'], ')'] if rank_name == 'supergenus'
    elements.push ['(', d['supersubgenus'], ')'] if rank_name == 'supersubgenus'
    elements.push ['(', d['supersupersubgenus'], ')'] if rank_name == 'supersupersubgenus'
    elements.push [d['supersuperspecies']] if rank_name == 'supersuperspecies'
    elements.push [d['superspecies']] if rank_name == 'superspecies'
    elements.push [d['subsuperspecies']] if rank_name == 'subsuperspecies'

    elements.push(d['species'], d['subspecies'], d['variety'], d['subvariety'], d['form'], d['subform'])

    elements = elements.flatten.compact.join(' ').gsub(/\(\s*\)/, '').gsub(/\(\s/, '(').gsub(/\s\)/, ')').squish
    elements.blank? ? nil : elements
  end

  def get_full_name_html(name = nil)
    name = get_full_name if name.nil?
    return  "\"<i>Candidatus</i> #{name}\"" if is_candidatus?
    if !name.blank? && is_hybrid?
      w = name.split(' ')
      w[-1] = ('×' + w[-1]).gsub('×(', '')
      name = w.join(' ')
    end

    m = name
    m = Utilities::Italicize.taxon_name(name) if is_italicized?
    m = '' + m if is_fossil?
    m
  end

  # @return [String]
  #    TODO: does this form of the name contain parens for subgenus?
  #    TODO: provide a default to gender (but do NOT eliminate param)
  #    TODO: on third thought- eliminate this mess
  def name_with_misspelling(gender)
    if cached_misspelling
      if rank_string =~ /Icnp/
        name.to_s + ' (sic)'
      else
        name.to_s + ' [sic]'
      end
    elsif gender.nil? || rank_string =~ /Genus/
      name.to_s
    else
      name_in_gender(gender).to_s
    end
  end

  # @return [String, nil]
  def genderized_name(gender = nil)
    if gender.nil? || is_genus_rank?
      name
    else
      name_in_gender(gender)
    end
  end

  # return [String, nil, false] # TODO: fix
  def get_genus_species(genus_option, self_option)
  # see Protonym
    true
  end

  # return [Boolean]
  #   whether there is an ICZN missapplication relationship present on this name
  def name_is_misapplied?
    !TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_string('TaxonNameRelationship::Iczn::Invalidating::Misapplication').empty?
  end

  # return [String]
  #   the author and year of the name, adds parenthesis where asserted
  def get_author_and_year
    if self.type == 'Combination'
      c = protonyms_by_rank
      return nil if c.empty?
      taxon = c[c.keys.last]
    else
      taxon = self
    end

    case taxon.rank_class.try(:nomenclatural_code)
    when :iczn
      ay = iczn_author_and_year(taxon)
    when :icvcn
      ay = icn_author_and_year(taxon)
    when :icnp
      ay = icn_author_and_year(taxon)
    when :icn
      ay = icn_author_and_year(taxon)
    else
      ay = ([author_string] + [year_integer]).compact.join(' ')
    end
    ay.blank? ? nil : ay
  end

  def icn_author_and_year(taxon)
    ay = nil

    misapplication = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_string('TaxonNameRelationship::Icn::Unaccepting::Misapplication')
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_AUTHOR_STRING)
    m_obj = misapplication.empty? ? nil : misapplication.first.object_taxon_name
    mobj = misspelling.empty? ? taxon : misspelling.first.object_taxon_name
    ay = mobj.try(:author_string) # author string for basionym
    if self.type == 'Combination'
      cc = self
    else
      current_combination = TaxonNameRelationship.where_object_is_taxon_name(mobj).with_type_string('TaxonNameRelationship::CurrentCombination')
      cc = current_combination.empty? ? self : current_combination.first.subject_taxon_name
    end

    if !self.author_string.blank? && mobj.id != cc.id
      ay = '(' + ay.to_s + ') ' + cc.try(:author_string).to_s
    end

    if !misapplication.empty? && !m_obj.author_string.blank?
      ay += ' non ' + m_obj.author_string
    end

    ay.blank? ? nil : ay
  end

  # @return [String, nil]
  #   the authors, and year, with parentheses as inferred by the data
  def iczn_author_and_year(taxon)
    ay = nil
    p = nil

    misapplication = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_string('TaxonNameRelationship::Iczn::Invalidating::Misapplication')
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_AUTHOR_STRING)

    mobj = misspelling.empty? ? nil : misspelling.first.object_taxon_name
    unless mobj.blank?
      a = [mobj.try(:author_string)]
      y = [mobj.try(:year_integer)]
    else
      a = [taxon.try(:author_string)]
      y = [taxon.try(:year_integer)]
    end

    if a[0] =~ /^\(.+\)$/ # (Author)
      a[0] = a[0][1..-2] ## remove parentheses in the author string
      p = true
    else
      p = false
    end

    ay = (a + y).compact.join(', ')

    obj = misapplication.empty? ? nil : misapplication.first.object_taxon_name

    if SPECIES_RANK_NAMES_ICZN.include?(taxon.rank_string)
      if p
        ay = '(' + ay + ')' unless ay.empty?
      else
        og = taxon.original_genus
        if self.type == 'Combination'
          cg = genus
        else
          cg = ancestor_at_rank('genus')
        end
        unless og.nil? || cg.nil?
          ay = '(' + ay + ')' if !ay.empty? && og.normalized_genus.id != cg.normalized_genus.id
        end
      end
    end

    unless misapplication.empty? || obj.author_string.blank?
      ay += ' non ' + ([obj.author_string] + [obj.year_integer]).compact.join(', ')
    end

    ay.blank? ? nil : ay
  end

  # @return Protonym
  def normalized_genus
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING)
    tn = misspelling.empty? ? self : misspelling.first.object_taxon_name
    return tn.lowest_rank_coordinated_taxon
  end

  # @return [String, nil]
  def get_cached_classified_as
    return nil unless is_protonym? || is_combination?
    # source_classified_as is a method generated through relationships
    r = reload_source_classified_as
    return " (as #{r.name})" unless r.blank?
    nil
  end

  # @return [Boolean]
  def parent_is_set?
    !parent_id.nil? || (parent && parent.persisted?)
  end

  # TODO: this should be paginated, not all IDs!
  def next_sibling
    if siblings.where(project_id: project_id).load.any?
      sibs = self_and_siblings.order(:cached).pluck(:id)
      s = sibs.index(id)
      TaxonName.find(sibs[ s + 1] ) if s < sibs.length - 1
    else
      nil
    end
  end

  # TODO: this should be paginated, not all IDs!
  def previous_sibling
    if siblings.where(project_id: project_id).load.any?
      sibs = self_and_siblings.order(:cached).pluck(:id)
      s = sibs.index(id)
      TaxonName.find(sibs[s - 1]) if s != 0
    else
      nil
    end
  end

  def create_otu
    Otu.create(by: creator, project: project, taxon_name_id: id)
  end

  # @return [Scope]
  #   All taxon names attached to relationships recently created by user
  def self.used_recently_in_classifications(user_id, project_id)
    TaxonName.where(project_id: project_id, created_by_id: user_id)
      .joins(:taxon_name_classifications)
      .includes(:taxon_name_classifications)
      .where(taxon_name_classifications: { created_at: 1.weeks.ago..Time.now } )
      .order('taxon_name_classifications.updated_at DESC')
  end

  # @return [Scope]
  #   All taxon names attached to relationships recently created by user
  def self.used_recently_in_relationships(user_id, project_id)
    t = TaxonNameRelationship.arel_table
    t1 = t.alias('tnr1')
    t2 = t.alias('tnr2')

    sql = t1[:updated_by_id].eq(user_id).or(t1[:created_by_id].eq(user_id))
      .or(t2[:updated_by_id].eq(user_id).or(t2[:created_by_id].eq(user_id))
    ).to_sql

    sql2 = t1[:created_at].between( 1.weeks.ago..Time.now )
      .or( t2[:created_at].between( 1.weeks.ago..Time.now ) ).to_sql

    TaxonName.with_taxon_name_relationships
      .where(taxon_names: {project_id: project_id})
      .where(sql2)
      .where(sql)
      .order('taxon_names.updated_at DESC') ## needs optimisation. Does not sort by TNR date
  end

  # @return [Array]
  def self.used_recently(user_id, project_id, target: nil)
    klass, a,b,c = nil, nil, nil, nil
    if target == 'TypeMaterial'
      klass = Protonym
      a = klass.is_species_group.touched_by(user_id).where(project_id: project_id).order(updated_at: :desc).limit(6).to_a
      b = used_recently_in_classifications(user_id, project_id).is_species_group.where(type: klass.name).limit(6).to_a
      c = used_recently_in_relationships(user_id, project_id).is_species_group.where(type: klass.name).limit(6).to_a
    else
      klass = TaxonName
      a = klass.touched_by(user_id).where(project_id: project_id).order(updated_at: :desc).limit(6).to_a
      b = used_recently_in_classifications(user_id, project_id).where(type: klass.name).limit(6).to_a
      c = used_recently_in_relationships(user_id, project_id).where(type: klass.name).limit(6).to_a
    end

    d = [ a,b,c ].flatten.compact.uniq.sort{|e,f| e.cached <=> f.cached}
  end

  # @return [Hash]
  def self.select_optimized(user_id, project_id, target: nil)
    klass = (target == 'TypeMaterial' ? Protonym : TaxonName)
    h = {
      recent: klass.used_recently(user_id, project_id, target: klass.name),
      pinboard: klass.pinned_by(user_id).pinned_in_project(project_id).to_a
    }

    h[:quick] = (klass.pinned_by(user_id).pinboard_inserted.pinned_in_project(project_id).to_a + h[:recent][0..3]).uniq
    h
  end

  # See Shared::MatrixHooks
  # @return [{"matrix_row_item": matrix_column_item, "object": object}, false]
  # the hash corresponding to the keyword used in this tag if it exists
  # !! Assumes it can only be in one matrix, this is wrong !!
  def matrix_row_item
    mri = ObservationMatrixRowItem::TaxonNameRowItem.where(taxon_name_id: id, project_id: project_id).limit(1)

    if mri.any?
      return { matrix_row_item: mri.first, object: taxon_name }
    else
      return false
    end
  end

  # @return [String]
  #  a reified ID is used when the original combination, which does not yet have it's own ID, is not the same as the current classification
  # Some observations:
  #  - reified ids are only for original combinations (for which we have no ID)
  #  - reified ids never reference gender changes because they are always in context of original combination, i.e. there is never a gender change
  # Mental note- consider combination - is_current_placement? (presently excluded in CoL code, which is the correct place to decide that.)
  # Duplicated in COLDP export code
  def reified_id
    return id.to_s if is_combination?
    return id.to_s unless has_alternate_original?
    id.to_s + '-' + Digest::MD5.hexdigest(cached_original_combination)
  end

  protected

  def check_for_children
    if leaf?
      true
    else
      errors.add(:base, 'This taxon has children names attached, delete those first.')
      throw :abort
    end
  end

  def validate_parent_is_set
    if !(rank_class == NomenclaturalRank) && !(type == 'Combination')
      errors.add(:parent_id, 'is not selected') if !parent_is_set?
    end
  end

  def validate_parent_from_the_same_project
    if parent && !project_id.blank?
      errors.add(:project_id, "The parent taxon is not from the same project") if project_id != parent.project_id
    end
  end

  def validate_one_root_per_project
    if new_record? || parent_id_changed? # project_id !?@
      if !parent_is_set? && TaxonName.where(parent_id: nil, project_id: project_id).count > 0
        errors.add(:parent_id, 'should not be empty/only one root is allowed per project')
      end
    end
  end

  # TODO: move to Protonym when we eliminate TaxonName.new()
  def check_new_parent_class
    if is_protonym? && parent_id != parent_id_was && !parent_id_was.nil? && nomenclatural_code == :iczn
      if old_parent = TaxonName.find_by(id: parent_id_was)
        if (rank_name == 'subgenus' || rank_name == 'subspecies') && old_parent.name == name
          errors.add(:parent_id, "The nominotypical #{rank_name} #{name} can not be moved out of the nominal #{old_parent.rank_name}")
        end
      end
    end
  end

  # See subclasses
  def validate_rank_class_class
    true
  end

  # Note- prior version prevented groups from moving when set in error, and was far too strict
  def check_new_rank_class
    if (rank_class != rank_class_was) && !rank_class_was.nil?

      if rank_class_was == 'NomenclaturalRank' && rank_class_changed?
        errors.add(:rank_class, 'Root can not have a new rank')
        return
      end
    end
  end

  def validate_source_type
    a = source && source.type != 'Source::Bibtex'
    b = origin_citation && origin_citation.try(:source).try(:type) != 'Source::Bibtex'
    if a || b
      errors.add(:base, 'Source must be a Bibtex')
    end
  end

  # TODO: this needs to go.
  def sv_missing_confidence_level # should be removed once the alternative solution is implemented. It is heavily used now.
    confidence_level_array = [93]
    confidence_level_array = confidence_level_array & ConfidenceLevel.where(project_id: self.project_id).pluck(:id)
    soft_validations.add(:base, 'Confidence level is missing') if !confidence_level_array.empty? && (self.confidences.pluck(:confidence_level_id) & confidence_level_array).empty?
  end

  def sv_missing_original_publication
    if rank_class.nil? || is_family_or_genus_or_species_rank?
      if self.source.nil?
        soft_validations.add(:base, 'Original publication is not selected')
      elsif self.origin_citation.try(:pages).blank?
        soft_validations.add(:base, 'Original citation pages are not recorded')
      elsif !self.source.pages.blank?
        matchdata1 = self.origin_citation.pages.match(/(\d+) ?[-–] ?(\d+)|(\d+)/)
        if matchdata1
          citMinP = matchdata1[1] ? matchdata1[1].to_i : matchdata1[3].to_i
          citMaxP = matchdata1[2] ? matchdata1[2].to_i : matchdata1[3].to_i
          matchdata = self.source.pages.match(/(\d+) ?[-–] ?(\d+)|(\d+)/)
          if citMinP && citMaxP && matchdata
          minP = matchdata[1] ? matchdata[1].to_i : matchdata[3].to_i
          maxP = matchdata[2] ? matchdata[2].to_i : matchdata[3].to_i
            minP = 1 if minP == maxP && %w{book booklet manual mastersthesis phdthesis techreport}.include?(self.source.bibtex_type)
            unless (maxP && minP && minP <= citMinP && maxP >= citMaxP)
              soft_validations.add(:base, 'Original citation could be out of the source page range')
            end
          end
        end
      end
    end
  end

  # def sv_missing_author
  #   true # see Protonym
  # end
  #
  # def sv_missing_year
  #   true # see Protonym
  # end

  def sv_missing_etymology
    true # see Protonym
  end

  def sv_parent_is_valid_name
    if !parent.nil? && parent.unavailable_or_invalid?
      soft_validations.add(:parent_id, 'Parent should be a valid taxon', success_message: 'Parent was updated', failure_message:  'Failed to update parent')
    end
  end

  def sv_fix_parent_is_valid_name
    res = false
    if self.parent.unavailable_or_invalid?
      new_parent = self.parent.get_valid_taxon_name
      if self.parent != new_parent
        self.parent = new_parent
        if self.parent.rank_class.parent.to_s == 'NomenclaturalRank::Iczn::GenusGroup' && self.rank_class.to_s == 'NomenclaturalRank::Iczn::SpeciesGroup::Subspecies'
          self.rank_class = 'NomenclaturalRank::Iczn::SpeciesGroup::Species'
        elsif self.parent.rank_class.parent.to_s == 'NomenclaturalRank::Iczn::FamilyGroup' && self.rank_class.to_s == 'NomenclaturalRank::Iczn::GenusGroup::Subgenus'
          self.rank_class = 'NomenclaturalRank::Iczn::GenusGroup::Genus'
        end
        begin
          TaxonName.transaction do
            self.save
            res = true
          end
        rescue
        end
      end
    end
    res
  end

  def sv_conflicting_subordinate_taxa
    classifications = self.taxon_name_classifications.reload
    classification_names = classifications.map { |i| i.type_name }
    compare = TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID & classification_names
    unless compare.empty?
      unless Protonym.with_parent_taxon_name(self).without_taxon_name_classification_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).empty?
        compare.each do |i|
          # taxon is unavailable or invalid, but has valid children
          soft_validations.add(:base, "Taxon has a status ('#{i.safe_constantize.label}') conflicting with presence of subordinate taxa")
        end
      end
    end
  end

  def sv_fix_cached_names
    begin
      TaxonName.transaction do
        self.set_cached
      end
      true
    rescue
      false
    end
  end

  # TODO: does this make sense now, with #valid_taxon_name_id in place?
  def sv_not_synonym_of_self
    if list_of_invalid_taxon_names.include?(self)
      soft_validations.add(:base, "Taxon has two conflicting relationships (invalidating and validating). To resolve a conflict, add a status 'Valid' to a valid taxon.")
    end
  end

  def sv_two_unresolved_alternative_synonyms
    r = taxon_name_relationships.includes(:source).order_by_oldest_source_first.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_SYNONYM)
    if r.to_a.size > 1
      if r.first.nomenclature_date.to_date == r.second.nomenclature_date.to_date
        soft_validations.add(:base, 'Taxon has two alternative invalidating relationships with identical dates. To resolve ambiguity, add original sources to the relationships with different priority dates.')
      end
    end
  end

  def sv_incomplete_combination
    soft_validations.add(:base, 'The genus in the combination is not specified') if !cached.nil? && cached.include?('GENUS NOT SPECIFIED')
    soft_validations.add(:base, 'The species in the combination is not specified') if !cached.nil? && cached.include?('SPECIES NOT SPECIFIED')
    soft_validations.add(:base, 'The variety in the combination is not specified') if !cached.nil? && cached.include?('VARIETY NOT SPECIFIED')
    soft_validations.add(:base, 'The form in the combination is not specified') if !cached.nil? && cached.include?('FORM NOT SPECIFIED')
    soft_validations.add(:base, 'The genus in the original combination is not specified') if !cached_original_combination.nil? && cached_original_combination.include?('GENUS NOT SPECIFIED')
    soft_validations.add(:base, 'The species in the original combination is not specified') if !cached_original_combination.nil? && cached_original_combination.include?('SPECIES NOT SPECIFIED')
  end

  def sv_cached_names
    true # see validation in subclasses
  end

  def sv_validate_parent_rank
    true # see validation in Protonym.rb
  end

  def sv_missing_relationships
    true # see validation in Protonym.rb
  end

  def sv_validate_name
    true # see validation in Protonym.rb
  end

  def sv_missing_classifications
    true # see validation in Protonym.rb
  end

  def sv_species_gender_agreement
    true # see validation in Protonym.rb
  end

  def sv_primary_types
    true # see validation in Protonym.rb
  end

  def sv_validate_coordinated_names
    true # see validation in Protonym.rb
  end

  def sv_type_placement
    true # see validation in Protonym.rb
  end

  def sv_single_sub_taxon
    true # see validation in Protonym.rb
  end

  def sv_parent_priority
    true # see validation in Protonym.rb
  end

  def sv_homotypic_synonyms
    true # see validation in Protonym.rb
  end

  def sv_potential_homonyms
    true # see validation in Protonym.rb
  end

  def sv_combination_duplicates
    true # see validation in Combination.rb
  end

  def sv_hybrid_name_relationships
    true # see validation in Hybrid.rb
  end

end

#cached_classified_asString

if the name was classified in different group (e.g. a genus placed in wrong family).

Returns:

  • (String)


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
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
# File 'app/models/taxon_name.rb', line 129

class TaxonName < ApplicationRecord

  # @return class
  #   this method calls Module#module_parent
  # TODO: This method can be placed elsewhere inside this class (or even removed if not used)
  #       when https://github.com/ClosureTree/closure_tree/issues/346 is fixed.
  def self.parent
    self.module_parent
  end

  # Must be before various of these includes, in particular MatrixHooks
  has_closure_tree

  include Housekeeping
  include Shared::DataAttributes
  include Shared::Tags
  include Shared::Identifiers
  include Shared::Notes
  include Shared::Depictions
  include Shared::Citations
  include Shared::Confidences
  include Shared::AlternateValues
  include Shared::HasPapertrail
  include SoftValidation
  include Shared::IsData
  include TaxonName::OtuSyncronization

  include Shared::MatrixHooks::Member
  include Shared::MatrixHooks::Dynamic

  include TaxonName::MatrixHooks

  # Allows users to provide arbitrary annotations that "over-ride" rank string
  ALTERNATE_VALUES_FOR = [:rank_class].freeze # !! Don't even think about putting this on `name`

  COMBINATION_ELEMENTS = [:genus, :subgenus, :species, :subspecies, :variety, :subvariety, :form, :subform].freeze

  SPECIES_EPITHET_RANKS = %w{species subspecies variety subvariety form subform}.freeze

  NOT_LATIN = Regexp.new(/[^a-zA-Z|\-]/).freeze # Dash is allowed?

  NO_CACHED_MESSAGE = 'REBUILD PROJECT TAXON NAME CACHE'.freeze

  NOMEN_VALID = {
    icn: 'http://purl.obolibrary.org/obo/NOMEN_0000383',
    icnp: 'http://purl.obolibrary.org/obo/NOMEN_0000081',
    icvcn: 'http://purl.obolibrary.org/obo/NOMEN_0000125',
    iczn: 'http://purl.obolibrary.org/obo/NOMEN_0000224'
  }

  # @return [Boolean]
  #   When true, also creates an OTU that is tied to this taxon name
  attr_accessor :also_create_otu

  # @return [Boolean]
  #   When true cached values are not built
  attr_accessor :no_cached
  delegate :nomenclatural_code, to: :rank_class, allow_nil: true
  delegate :rank_name, to: :rank_class, allow_nil: true

  # TODO: this was not implemented and tested properly
  # I think the intent is *before* save, i.e. the name will change
  # to a new cached value, so let's record the old one
  #  after_save :create_new_combination_if_absent

  after_save :set_cached, unless: Proc.new {|n| n.no_cached || errors.any? }
  after_save :set_cached_warnings, if: Proc.new {|n| n.no_cached }
  after_create :create_otu, if: :also_create_otu
  before_destroy :check_for_children, prepend: true

  validate :validate_rank_class_class,
    # :check_format_of_name,
    :validate_parent_from_the_same_project,
    :validate_parent_is_set,
    :check_new_rank_class,
    :check_new_parent_class,
    :validate_source_type,
    :validate_one_root_per_project

  # TODO: remove, this is handled natively
  validates_presence_of :type, message: 'is not specified'

  validates :year_of_publication, date_year: {min_year: 1000, max_year: Time.now.year + 5}, allow_nil: true
  validates :name, format: { without: /\s/ }

  # TODO: move some of these down to Protonym when they don't apply to Combination

  # TODO: think of a different name, and test
  has_many :historical_taxon_names, class_name: 'TaxonName', foreign_key: :cached_valid_taxon_name_id

  has_many :observation_matrix_row_items, inverse_of: :taxon_name, class_name: 'ObservationMatrixRowItem::Dynamic::TaxonName', dependent: :delete_all
  has_many :observation_matrices, through: :observation_matrix_row_items

  # TODO: revisit?
  belongs_to :valid_taxon_name, class_name: 'TaxonName', foreign_key: :cached_valid_taxon_name_id

  has_one :source_classified_as_relationship, -> {
    where(taxon_name_relationships: {type: 'TaxonNameRelationship::SourceClassifiedAs'})
  }, class_name: 'TaxonNameRelationship::SourceClassifiedAs', foreign_key: :subject_taxon_name_id

  has_one :source_classified_as, through: :source_classified_as_relationship, source: :object_taxon_name

  has_many :otus, inverse_of: :taxon_name, dependent: :restrict_with_error
  has_many :taxon_determinations, through: :otus
  has_many :collection_objects, through: :taxon_determinations, source: :biological_collection_object
  has_many :related_taxon_name_relationships, class_name: 'TaxonNameRelationship', foreign_key: :object_taxon_name_id, dependent: :restrict_with_error, inverse_of: :object_taxon_name

  has_many :taxon_name_author_roles, class_name: 'TaxonNameAuthor', as: :role_object, dependent: :destroy
  has_many :taxon_name_authors, -> { order('roles.position ASC') }, through: :taxon_name_author_roles, source: :person

  # TODO: Combinations shouldn't have classifications or relationships?  Move to Protonym?
  has_many :taxon_name_classifications, dependent: :destroy, foreign_key: :taxon_name_id, inverse_of: :taxon_name
  has_many :taxon_name_relationships, foreign_key: :subject_taxon_name_id, dependent: :restrict_with_error, inverse_of: :subject_taxon_name

  # NOTE: Protonym subclassed methods might not be nicely tracked here, we'll have to see.  Placement is after has_many relationships. (?)
  accepts_nested_attributes_for :related_taxon_name_relationships, allow_destroy: true, reject_if: proc { |attributes| attributes['type'].blank? || attributes['subject_taxon_name_id'].blank? }
  accepts_nested_attributes_for :taxon_name_authors, :taxon_name_author_roles, allow_destroy: true
  accepts_nested_attributes_for :taxon_name_classifications, allow_destroy: true, reject_if: proc { |attributes| attributes['type'].blank?  }

  has_many :classified_as_unavailable_or_invalid, -> { where type: TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID }, class_name: 'TaxonNameClassification'

  scope :with_same_cached_valid_id, -> { where(arel_table[:id].eq(arel_table[:cached_valid_taxon_name_id])) }
  scope :with_different_cached_valid_id, -> { where(arel_table[:id].not_eq(arel_table[:cached_valid_taxon_name_id])) } # This doesn't catch all invalid names.  Those with classifications only are missed !$#!@#

  scope :that_is_valid, -> {where(cached_is_valid: true) }
  scope :that_is_invalid, -> {where(cached_is_valid: false) }

  def self.calculated_invalid
    a = TaxonName.with_different_cached_valid_id # that_is_invalid
    b = TaxonName.joins(:taxon_name_classifications).where(taxon_name_classifications: {type: TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID })
    TaxonName.from("((#{a.to_sql}) UNION (#{b.to_sql})) as taxon_names")
  end

  def self.calculated_valid
    # Alt format: TaxonName.that_is_valid.left_joins(:classified_as_unavailable_or_invalid).merge(TaxonNameClassification.where(id: nil))
    TaxonName
      .with_same_cached_valid_id
      .where.not(
        id: TaxonNameClassification.select(:taxon_name_id).where(type: TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID)
      )
  end

  scope :with_type, -> (type) {where(type: type)}
  scope :descendants_of, -> (taxon_name) { with_ancestor(taxon_name )}

  scope :ancestors_of, -> (taxon_name) {
    joins(:descendant_hierarchies)
      .where(taxon_name_hierarchies: {descendant_id: taxon_name.id})
      .where('taxon_name_hierarchies.ancestor_id != ?', taxon_name.id)
      .order('taxon_name_hierarchies.generations DESC') # root is at index 0
  }

  # LEAVE UNORDERED, if you want order:
  #   .order('taxon_name_hierarchies.generations DESC')
  scope :self_and_ancestors_of, -> (taxon_name) {
    joins(:descendant_hierarchies)
      .where(taxon_name_hierarchies: {descendant_id: taxon_name.id})
  }

  # Includes taxon_name, doesn't order result
  scope :ancestors_and_descendants_of, -> (taxon_name) do
    scoping do
      a = TaxonName.self_and_ancestors_of(taxon_name)
      b = TaxonName.descendants_of(taxon_name)
      TaxonName.from("((#{a.to_sql}) UNION (#{b.to_sql})) as taxon_names")
    end
  end

  # TODO: remove
  scope :with_rank_class, -> (rank_class_name) { where(rank_class: rank_class_name) }

  # TODO: remove
  scope :with_parent_taxon_name, -> (parent) { where(parent_id: parent) }

  scope :with_base_of_rank_class, -> (rank_class) { where('rank_class LIKE ?', "#{rank_class}%") }
  scope :with_rank_class_including, -> (include_string) { where('rank_class LIKE ?', "%#{include_string}%") }
  scope :project_root, -> (root_id) {where("(taxon_names.rank_class = 'NomenclaturalRank' AND taxon_names.project_id = ?)", root_id)}

  # A specific relationship
  scope :as_subject_with_taxon_name_relationship, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where(taxon_name_relationships: {type: taxon_name_relationship}) }
  scope :as_subject_with_taxon_name_relationship_base, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "#{taxon_name_relationship}%").references(:taxon_name_relationships) }
  scope :as_subject_without_taxon_name_relationship_base, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where('(taxon_name_relationships.type NOT LIKE ?) OR (taxon_name_relationships.subject_taxon_name_id IS NULL)', "#{taxon_name_relationship}%").references(:taxon_name_relationships) }
  scope :as_subject_with_taxon_name_relationship_array, -> (taxon_name_relationship_name_array) { includes(:taxon_name_relationships).where('(taxon_name_relationships.type IN (?)) OR (taxon_name_relationships.subject_taxon_name_id IS NULL)', "#{taxon_name_relationship_name_array}%").references(:taxon_name_relationships) }
  scope :as_subject_without_taxon_name_relationship_array, -> (taxon_name_relationship_name_array) { includes(:taxon_name_relationships).where('(taxon_name_relationships.type NOT IN (?)) OR (taxon_name_relationships.subject_taxon_name_id IS NULL)', "#{taxon_name_relationship_name_array}%").references(:taxon_name_relationships) }
  scope :as_subject_with_taxon_name_relationship_containing, -> (taxon_name_relationship) { includes(:taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "%#{taxon_name_relationship}%").references(:taxon_name_relationships) }
  scope :as_object_with_taxon_name_relationship, -> (taxon_name_relationship) { includes(:related_taxon_name_relationships).where(taxon_name_relationships: {type: taxon_name_relationship}) }
  scope :as_object_with_taxon_name_relationship_base, -> (taxon_name_relationship) { includes(:related_taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "#{taxon_name_relationship}%").references(:related_taxon_name_relationships) }
  scope :as_object_with_taxon_name_relationship_containing, -> (taxon_name_relationship) { includes(:related_taxon_name_relationships).where('taxon_name_relationships.type LIKE ?', "%#{taxon_name_relationship}%").references(:related_taxon_name_relationships) }

  # @param relationship [Array, String]
  def self.with_taxon_name_relationship(relationship)
    a = TaxonName.joins(:taxon_name_relationships).where(taxon_name_relationships: {type: relationship})
    b = TaxonName.joins(:related_taxon_name_relationships).where(taxon_name_relationships: {type: relationship})
    TaxonName.from("((#{a.to_sql}) UNION (#{b.to_sql})) as taxon_names")
  end

  scope :with_taxon_name_relationships, -> {
    joins('LEFT OUTER JOIN taxon_name_relationships tnr1 ON taxon_names.id = tnr1.subject_taxon_name_id').
    joins('LEFT OUTER JOIN taxon_name_relationships tnr2 ON taxon_names.id = tnr2.object_taxon_name_id').
    where('tnr1.subject_taxon_name_id IS NOT NULL OR tnr2.object_taxon_name_id IS NOT NULL')
  }
  # *Any* relationship where there IS a relationship for a subject/object/both
  scope :with_taxon_name_relationships_as_subject, -> { joins(:taxon_name_relationships) }
  scope :with_taxon_name_relationships_as_object, -> { joins(:related_taxon_name_relationships) }

  # *Any* relationship where there is NOT a relationship for a subject/object/both
  scope :without_subject_taxon_name_relationships, -> { includes(:taxon_name_relationships).where(taxon_name_relationships: {subject_taxon_name_id: nil}) }
  scope :without_object_taxon_name_relationships, -> { includes(:related_taxon_name_relationships).where(taxon_name_relationships: {object_taxon_name_id: nil}) }

  scope :without_taxon_name_relationships, -> {
    joins('LEFT OUTER JOIN taxon_name_relationships tnr1 ON taxon_names.id = tnr1.subject_taxon_name_id').
    joins('LEFT OUTER JOIN taxon_name_relationships tnr2 ON taxon_names.id = tnr2.object_taxon_name_id').
    where('tnr1.subject_taxon_name_id IS NULL AND tnr2.object_taxon_name_id IS NULL')
  }

  # TODO: deprecate all of these for where()
  scope :with_parent_id, -> (parent_id) {where(parent_id: parent_id)}
  scope :with_cached_valid_taxon_name_id, -> (cached_valid_taxon_name_id) {where(cached_valid_taxon_name_id: cached_valid_taxon_name_id)}
  scope :with_cached_original_combination, -> (original_combination) { where(cached_original_combination: original_combination) }

  scope :without_otus, -> { includes(:otus).where(otus: {id: nil}) }
  scope :with_otus, -> { includes(:otus).where.not(otus: {id: nil}) }

  # @return [Scope]
  #   Combinations that are composed of children of this taxon name
  #     when those children are not currently descendants of this taxon name
  #
  # !! When :cached_valid_taxon_name_id is properly set then this method is not required
  # rather you should use :historical_taxon_names.
  #
  def self.out_of_scope_combinations(taxon_name_id)
    t = ::TaxonName.arel_table
    h = ::TaxonNameHierarchy.arel_table
    r = ::TaxonNameRelationship.arel_table

    h1 = h.alias('osch_')
    h2 = h.alias('oschh_')

    b = h.project(
      h1[:descendant_id].as('b'),
      h2[:ancestor_id].as('c'),
    ).from([h])

    b = b.join(r, Arel::Nodes::InnerJoin).on(h[:descendant_id].eq(r[:subject_taxon_name_id]).and(h[:ancestor_id].eq(taxon_name_id)))
      .join(h1, Arel::Nodes::InnerJoin).on(r[:object_taxon_name_id].eq(h1[:descendant_id]).and(h1[:ancestor_id].not_eq(h1[:descendant_id])))
      .join(h2, Arel::Nodes::OuterJoin).on(
        h1[:ancestor_id].eq(h2[:ancestor_id]).
        and(h2[:descendant_id].eq(taxon_name_id))
      )

    # This was particularly useful in debugging the join chain:
    # ap TaxonNameHierarchy.connection.execute(b.to_sql).collect{|a| a}

    b = b.as('abc')

    ::Combination
      .joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['b'].eq(t[:id]))))
      .where(b['c'].eq(nil))
      .distinct
  end

  # @return Scope
  #   names that are not leaves
  # TODO: belongs in lib/queries/filter.rb likely
  def self.not_leaves
    t = self.arel_table
    h = ::TaxonNameHierarchy.arel_table

    a = t.alias('a_')
    b = t.project(a[Arel.star]).from(a)

    c = h.alias('h1')

    b = b.join(c, Arel::Nodes::OuterJoin)
      .on(
        a[:id].eq(c[:ancestor_id])
      )

    e = c[:generations].not_eq(0)
    f = c[:ancestor_id].not_eq(c[:descendant_id])

    b = b.where(e.and(f))
    b = b.group(a[:id])
    b = b.as('tnh_')

    ::TaxonName.joins(Arel::Nodes::InnerJoin.new(b, Arel::Nodes::On.new(b['id'].eq(t['id']))))
  end

  # @return [Scope] Protonym(s) the **broad sense** synonyms of this name
  def synonyms
    TaxonName.with_cached_valid_taxon_name_id(self.id)
  end

  soft_validate(:sv_missing_confidence_level,
                set: :missing_fields,
                name: 'Missing confidence level',
                description: 'To remaind that the taxon spelling have to be compared to the original source' )

  soft_validate(:sv_missing_original_publication,
                set: :missing_fields,
                name: 'Missing original source',
                description: 'Original source is not selected' )

=begin
  soft_validate(:sv_missing_author,
                set: :missing_fields,
                name: 'Missing author')

  soft_validate(:sv_missing_year,
                set: :missing_fields,
                name: 'Missing year')
=end

  soft_validate(
    :sv_parent_is_valid_name,
    set: :parent_is_valid_name,
    fix: :sv_fix_parent_is_valid_name,
    name: 'Parent should be a valid taxon',
    description: 'When the parent taxon (for example a genus) is treated as a synonym, all subordinate taxa from this taxon should be transferred to the valid taxon' )

  soft_validate(
    :sv_conflicting_subordinate_taxa,
    set: :parent_is_valid_name,
    name: 'Conflicting subordinate taxa',
    description: 'Unavailable or invalid taxon should not have subordinate taxa' )

  soft_validate(
    :sv_cached_names,
    set: :cached_names,
    fix: :sv_fix_cached_names,
    name: 'Cached names',
    description: 'Check if cached values need to be updated' )

  # TODO: move to protonym spec?
  soft_validate(
    :sv_not_synonym_of_self,
    set: :not_synonym_of_self,
    name: 'Not synonym of self',
    description: 'Taxon has two conflicting relationships (invalidating and validating).' )

  # TODO: move to protonym spec?
  soft_validate(
    :sv_two_unresolved_alternative_synonyms,
    set: :two_unresolved_alternative_synonyms,
    name: 'Two conflicting relationships',
    description: 'Taxon has two conflicting relationships.' )

  # TODO: move to combination spec?
  soft_validate(
    :sv_incomplete_combination,
    set: :incomplete_combination,
    name: 'Incomplete combination',
    description: 'Intermediate ranks are missing in combination (either original or subsequent)' )

  # @return [Array of TaxonName]
  #   ordered by rank, a scope-like hack
  def self.sort_by_rank(taxon_names)
    taxon_names.sort!{|a, b| RANKS.index(a.rank_string) <=> RANKS.index(b.rank_string)}
  end

  # TODO: what is this:!? :)
  def self.foo(rank_classes)
    from <<-SQL.strip_heredoc
      ( SELECT *, rank()
           OVER (
               PARTITION BY rank_class, parent_id
               ORDER BY generations asc, name
            ) AS rn
         FROM taxon_names
         INNER JOIN "taxon_name_hierarchies" ON "taxon_names"."id" = "taxon_name_hierarchies"."descendant_id"
         WHERE #{rank_classes.collect{|c| "rank_class = '#{c}'" }.join(' OR ')}
         ) as taxon_names
    SQL
  end

  # @return [String]
  #   rank as human readable short-form, like 'genus' or 'species'
  def rank
    ::RANKS.include?(rank_string) ? rank_name : nil
  end

  # @return [String]
  #   rank (Kindom, Phylum...) as a string, like {NomenclaturalRank::Iczn::SpeciesGroup::Species}
  def rank_string
    read_attribute(:rank_class)
  end

  def rank_class=(value)
    write_attribute(:rank_class, value.to_s)
  end

  # @return [NomenclaturalRank class]
  #   rank as a {NomenclaturalRank} class, like {NomenclaturalRank::Iczn::SpeciesGroup::Species}
  def rank_class
    r = read_attribute(:rank_class)
    Ranks.valid?(r) ? r.safe_constantize : r
  end

  # @see .out_of_scope_combinations
  def out_of_scope_combinations
    ::TaxonName
      .where(project_id: project_id)
      .out_of_scope_combinations(id)
  end

  # @return [TaxonName, nil] an ancestor at the specified rank
  # @param rank [symbol|string|
  #   like :species or 'genus'
  # @param include_self [Boolean]
  #   if true then self will also be returned
  def ancestor_at_rank(rank, include_self = false)
    if target_code = (is_combination? ? combination_taxon_names.first.nomenclatural_code : nomenclatural_code)
      r = Ranks.lookup(target_code, rank)
      return self if include_self && (rank_class.to_s == r)
      ancestors.with_rank_class( r ).first
    else
      # Root has no nomenclature code
      return nil
    end
  end

  # @return scope [TaxonName, nil] an ancestor at the specified rank
  # @params rank [symbol|string|
  #   like :species or 'genus'
  def descendants_at_rank(rank)
    return TaxonName.none if nomenclatural_code.blank? # Root names
    descendants.with_rank_class(
      Ranks.lookup(nomenclatural_code, rank)
    )
  end

  # @return [Scope] Protonym(s) the **broad sense** synonyms of this name
  def synonyms
    TaxonName.with_cached_valid_taxon_name_id(id)
  end

  # @return [Array]
  #   all TaxonNameRelationships where this taxon is an object or subject.
  def all_taxon_name_relationships
    # !! If self relationships are ever made possible this needs a DISTINCT clause
    TaxonNameRelationship.find_by_sql(
      "SELECT taxon_name_relationships.*
         FROM taxon_name_relationships
         WHERE taxon_name_relationships.subject_taxon_name_id = #{self.id}
       UNION
       SELECT taxon_name_relationships.*
         FROM taxon_name_relationships
         WHERE taxon_name_relationships.object_taxon_name_id = #{self.id}")
  end

  # @return [Array of TaxonName]
  #   all taxon_names which have relationships to this taxon as an object or subject.
  def related_taxon_names
    TaxonName.find_by_sql(
      "SELECT tn.* from taxon_names tn join taxon_name_relationships tnr1 on tn.id = tnr1.subject_taxon_name_id and tnr1.object_taxon_name_id = #{self.id}
      UNION
      SELECT tn.* from taxon_names tn join taxon_name_relationships tnr2 on tn.id = tnr2.object_taxon_name_id and tnr2.subject_taxon_name_id = #{self.id}"
    )
  end

  # @return [String, nil]
  #   the baseline means of displaying name authorship, i.e. the author for this taxon, last name only.
  # Important, string format priority is
  #   1) as provided verbatim
  #   2) as generated from People
  #   3) as taken from the Source
  #       Source People (Authors)
  #       Source Verbatim authors
  def author_string
    return verbatim_author if !verbatim_author.nil?
    if taxon_name_authors.any?
      # TODO: Technically not correct if prefix/suffix involved.
      return Utilities::Strings.authorship_sentence( taxon_name_authors.pluck(:last_name) )
    end

    return source.authority_name if !source.nil?
    nil
  end

  # @return [Integer]
  #   a 4 digit integer representing year of publication, like 1974
  def year_integer
    return year_of_publication if !year_of_publication.nil?
    try(:source).try(:year)
  end

  # @return String, nil
  #   virtual attribute, to ultimately be fixed in db
  def cached_author
    cached_author_year&.gsub(/,\s\d+/, '')
  end

  # !! Overrides Shared::Citations#nomenclature_date
  #
  # @return [Time]
  #   effective date of publication, used to determine nomenclatural priority
  def nomenclature_date
    return nil if !persisted?
    family_before_1961 = TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_string('TaxonNameRelationship::Iczn::PotentiallyValidating::FamilyBefore1961').first

    # family_before_1961 = taxon_name_relationships.with_type_string('TaxonNameRelationship::Iczn::PotentiallyValidating::FamilyBefore1961').first
    if family_before_1961.nil?
      year = year_of_publication ? Time.utc(year_of_publication, 12, 31) : nil
      self.source ? (self.source.cached_nomenclature_date ? self.source.nomenclature_date : year) : year
    else
      obj = family_before_1961.object_taxon_name
      year = obj.year_of_publication ? Time.utc(obj.year_of_publication, 12, 31) : nil
      obj.source ? (source.cached_nomenclature_date ? obj.source.nomenclature_date : year) : year
    end
  end

  # @return [array]
  # returns array of hashes for history of taxon. Could be used for catalogue construction
  def nomeclatural_history
    history = []
    TaxonName.where(cached_valid_taxon_name_id: self.id).order(:cached_nomenclature_date).each do |t|
      item = {}
      source_author_string = t.is_combination? ? [t.origin_citation&.source&.authority_name, t.origin_citation&.source&.year].join(', ') : nil
      source_author_string = ' in ' + source_author_string unless source_author_string.nil?
      item[:name] = t.is_combination? ? t.cached_html : t.cached_original_combination_html
      item[:author_year] = t.is_combination? ? t.cached_author_year + source_author_string : t.original_author_year
      item[:statuses] = t.combined_statuses
      history.append(item)
    end
    return history
  end

  # @return [Class, nil]
  #   gender of a genus as class
  def gender_class
    gender_instance.try(:type_class)
  end

  # @return [TaxonNameClassification instance, nil]
  #    the gender classification of this name, if provided
  def gender_instance
    taxon_name_classifications.with_type_base('TaxonNameClassification::Latinized::Gender').first
  end

  # @return [String, nil]
  #    gender as a string (only applicable to Genera)
  def gender_name
    gender_instance.try(:classification_label).try(:downcase)
  end

  # @return [Class]
  #   part of speech of a species as class.
  def part_of_speech_class
    part_of_speech_instance.try(:type_class)
  end

  def part_of_speech_instance
    taxon_name_classifications.with_type_base('TaxonNameClassification::Latinized::PartOfSpeech').first
  end

  # @return [String]
  #   part of speech of a species as string.
  def part_of_speech_name
    part_of_speech_instance.try(:classification_label).try(:downcase)
  end

  # @return [Array of String]
  #   the unique string labels (human readable) derived from TaxonNameClassifications
  def statuses_from_classifications
    list = taxon_name_classifications_for_statuses
    list.empty? ? [] : list.collect{|c| c.classification_label }.sort
  end

  # @return [Scope]
  def taxon_name_classifications_for_statuses
    taxon_name_classifications.with_type_array(ICZN_TAXON_NAME_CLASSIFICATION_NAMES + ICN_TAXON_NAME_CLASSIFICATION_NAMES + ICNP_TAXON_NAME_CLASSIFICATION_NAMES + ICVCN_TAXON_NAME_CLASSIFICATION_NAMES)
  end

  # @return [Array of String]
  #   the unique string labels derived from and TaxonNameRelationships
  def statuses_from_relationships
    list = taxon_name_relationships.with_type_array(STATUS_TAXON_NAME_RELATIONSHIP_NAMES)
    list.empty? ? [] : list.collect{|c| c.subject_status}.sort
  end

  # @return [Array of String]
  #   the unique string labels derived from both TaxonNameClassifications and TaxonNameRelationships
  def combined_statuses
    (statuses_from_classifications + statuses_from_relationships).uniq.sort
  end

  # @return [Array of Protonym]
  #   All of the names this name has been in combination with
  def combination_list_all
    taxon_name_relationships.with_type_base('TaxonNameRelationship::Combination').collect {|r| r.object_taxon_name}.uniq
  end

  # @return [Array of Protonym]
  #   for all names this name has been in combination with, select those names that are of the same rank (!! CONFIRM?)
  def combination_list_self
    combination_list_all.select{|c| c.protonyms_by_rank[c.protonyms_by_rank.keys.last] == self}
  end

  # @return [String]
  #   combination of cached_html and cached_author_year.
  def cached_html_name_and_author_year
    [cached_html, cached_author_year].compact.join(' ')
  end

  # @return [String]
  #   combination of cached_original_combination_html and original_author_year.
  def cached_html_original_name_and_author_year
    [cached_original_combination_html, original_author_year].compact.join(' ')
  end

  # @return [String] combination of cached and cached_author_year.
  def cached_name_and_author_year
    [cached, cached_author_year].compact.join(' ')
  end

  # @return [String, nil]
  #   derived from cached_author_year
  #   !! DO NOT USE IN building cached !!
  #   See also app/helpers/taxon_names_helper
  def original_author_year
    if nomenclatural_code == :iczn && !cached_misspelling && !name_is_misapplied?
      cached_author_year&.gsub(/^\(|\)/, '')
    elsif nomenclatural_code == :icn && cached_author_year
      if matchdata1 = cached_author_year.match(/(\(.*\))/)
        matchdata1[1].gsub(/^\(|\)/, '')
      else
        cached_author_year
      end
    else
      cached_author_year
    end
  end

  # @return [Array of TaxonName] ancestors of type 'Protonym'
  def ancestor_protonyms
    Protonym.ancestors_of(self)
  end

  # @return [Array of TaxonName] descendants of type 'Protonym'
  def descendant_protonyms
    Protonym.descendants_of(self)
  end

  # @return [Boolean]
  #   true if there is a relationship where then name is asserted to be invalid
  def relationship_invalid?
    !first_possible_valid_taxon_name_relationship.nil?
  end

  # @return [Boolean]
  #  true if this name has any classification asserting that it is valid
  def classification_valid?
    taxon_name_classifications.with_type_array(TAXON_NAME_CLASS_NAMES_VALID).any? # !TaxonNameClassification.where_taxon_name(self).with_type_array(TAXON_NAME_CLASS_NAMES_VALID).empty?
  end

  # @return [Boolean]
  #  whether this name has any classification asserting that this the name is NOT valid or that it is unavailable
  def classification_invalid_or_unavailable?
    taxon_name_classifications.with_type_array(TAXON_NAME_CLASS_NAMES_UNAVAILABLE_AND_INVALID).any?
  end

  #  @return [Boolean]
  #     return true if name is unavailable OR invalid, else false, checks both classifications and relationships
  # !! Should only be referenced when building cached values, all other uses should rather be `!is_valid?`
  def unavailable_or_invalid?
    return false if classification_valid?
    classification_invalid_or_unavailable? || relationship_invalid?
  end

  # @return [Boolean]
  #   after all inference on the validity of a name, the result is stored
  #   in cached_valid_taxon_name_id, #is_valid checks that result
  def is_valid?
    cached_is_valid
  end

  # Has Classification, but no relationship describing why
  def is_ambiguously_invalid?
    !is_valid? && (id == cached_valid_taxon_name_id)
  end

  # @return [Boolean]
  #   whether this name needs italics applied
  def is_italicized?
    is_genus_or_species_rank? || kind_of?(Combination) || kind_of?(Hybrid)
  end

  def is_protonym?
    type == 'Protonym'
  end

  def is_combination?
    type == 'Combination'
  end

  # @return [True|False]
  #   true if this name has a TaxonNameClassification of Fossil
  def is_fossil?
    taxon_name_classifications.with_type_contains('::Fossil').any?
  end

  # @return [Boolean]
  #   true if this name has a TaxonNameClassification of hybrid
  def is_hybrid?
    taxon_name_classifications.where_taxon_name(self).with_type_contains('Hybrid').any?
  end

  # @return [True|False]
  #   true if this name has a TaxonNameClassification of candidatus
  def is_candidatus?
    return false unless rank_string =~ /Icnp/
    taxon_name_classifications.where_taxon_name(self).with_type_contains('Candidatus').any?
  end

  # @return [True|False]
  #   true if this name has a TaxonNameClassification of not_binominal
  def not_binominal?
    taxon_name_classifications.where_taxon_name(self).with_type_contains('NonBinominal').any?
  end

  # @return [Boolean]
  #  see subclasses
  def is_genus_or_species_rank?
    false
  end

  # @return [TaxonName]
  #  a valid taxon_name for an invalid name or self for valid name.
  #  a stub here - See Protonym and Combination
  def get_valid_taxon_name
    nil
  end

  # @return [TaxonNameRelationship]
  #  returns youngest taxon name relationship where self is the subject.
  def first_possible_valid_taxon_name_relationship
    taxon_name_relationships.reload.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_SYNONYM).youngest_by_citation
  end

  # @return [TaxonName]
  #    returns the youngest #object_taxon_name from the youngest taxon name relationship.
  def first_possible_valid_taxon_name
    return self if !unavailable_or_invalid?                      # catches all cases where no Classifications or Relationships are provided
    relationship = first_possible_valid_taxon_name_relationship
    relationship.nil? ? self : relationship.object_taxon_name    # ?! probably the if is caught by unavailable_or_invalid already
   end

  # @return [Array of TaxonName]
  #  returns list of invalid names for a given taxon.
  # TODO: Can't we just use #valid_id now?
  # this list does not return combinations
  def list_of_invalid_taxon_names
    first_pass = true
    list = {}
    while first_pass || !list.keys.select{|t| list[t] == false}.empty? do
      first_pass = false
      list_of_taxa_to_check = list.empty? ? [self] : list.keys.select{|t| list[t] == false}
      list_of_taxa_to_check.each do |t|
        potentialy_invalid_relationships = t.related_taxon_name_relationships.with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_SYNONYM).order_by_oldest_source_first
        potentialy_invalid_relationships.each do |r|
          if !TaxonNameClassification.where_taxon_name(r.subject_taxon_name).with_type_array(TAXON_NAME_CLASS_NAMES_VALID).empty?
            # do nothing, taxon has a status of valid name
          elsif r == r.subject_taxon_name.first_possible_valid_taxon_name_relationship
            list[r.subject_taxon_name] = false if list[r.subject_taxon_name].nil?
          end

        end
        list[t] = true if list[t] == false
      end
    end
    return [] if list.empty?
    list.sort_by{|t, a| (t.nomenclature_date || Time.now)}.collect{|t, a| t}
  end

  def gbif_status_array
    return nil if self.class.nil?
    return ['combination'] if self.class == 'Combination'
    s1 = taxon_name_classifications.collect{|c| c.class.gbif_status}.compact
    return s1 unless s1.empty?
    s2 = taxon_name_relationships.collect{|r| r.class.gbif_status_of_subject}
    s3 = related_taxon_name_relationships.collect{|r| r.class.gbif_status_of_object}

    s = s2 + s3
    s.compact!
    return ['valid'] if s.empty?
    s
  end

  # @return [Array of Strings]
  #   names of all genera where the species was placed
  def name_in_gender(gender = nil)
    case gender
      when 'masculine'
        n = masculine_name
      when 'feminine'
        n = feminine_name
      when 'neuter'
        n = neuter_name
      else
        n = nil
    end
    n = n.blank? ? name : n
    return n
  end

  # def create_new_combination_if_absent
  # return true unless type == 'Protonym'
  # if !TaxonName.with_cached_html(cached_html).count == 0 (was intent to make this always fail?!)
  #
  #  if TaxonName.where(cached: cached, project_id: project_id).any?
  #    begin
  #      TaxonName.transaction do
  #        c = Combination.new
  #        safe_self_and_ancestors.each do |i|
  #          case i.rank
  #            when 'genus'
  #              c.genus = i
  #            when 'subgenus'
  #              c.subgenus = i
  #            when 'species'
  #              c.species = i
  #            when 'subspecies'
  #              c.subspecies = i
  #          end
  #        end
  #        c.save
  #      end
  #    rescue
  #    end
  #    false
  #  end
  # end

  def clear_cached(update: false)
    assign_attributes(
      cached_html: nil,
      cached_author_year: nil,
      cached_original_combination_html: nil,
      cached_secondary_homonym: nil,
      cached_primary_homonym: nil,
      cached_secondary_homonym_alternative_spelling: nil,
      cached_primary_homonym_alternative_spelling: nil,
      cached_misspelling: nil,
      cached_classified_as: nil,
      cached: nil,
      cached_valid_taxon_name_id: nil,
      cached_is_valid: nil,
      cached_original_combination: nil,
      cached_nomenclature_date: nil
    )
    save if update
  end

  def set_cached
    n = get_full_name
    update_column(:cached, n)

    # We can't use the in-memory cache approach for combination names, force reload each time
    n = nil if is_combination?

    update_columns(
      cached_html: get_full_name_html(n),
      cached_nomenclature_date: nomenclature_date)

    set_cached_valid_taxon_name_id
    set_cached_is_valid

    # These two can be isolated as they are not always pertinent to a generalized cascading cache setting
    # For example, when a TaxonName relationship forces a cached reload it may/not need to call these two things
    set_cached_classified_as
    set_cached_author_year
  end

  def set_cached_valid_taxon_name_id
    update_column(:cached_valid_taxon_name_id, get_valid_taxon_name.id)
  end

  def set_cached_is_valid
    v = is_combination? ? false : !unavailable_or_invalid?
    update_column(:cached_is_valid, v)
  end

  def set_cached_warnings
    update_columns(
      cached:  NO_CACHED_MESSAGE,
      cached_author_year:  NO_CACHED_MESSAGE,
      cached_nomenclature_date: NO_CACHED_MESSAGE,
      cached_classified_as: NO_CACHED_MESSAGE,
      cached_html:  NO_CACHED_MESSAGE
    )
  end

  def set_cached_author_year
    update_column(:cached_author_year, get_author_and_year)
  end

  def set_cached_classified_as
    update_column(:cached_classified_as, get_cached_classified_as)
  end

  def get_cached_misspelling
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_ONLY).first
    unless misspelling.nil?
      n1 = verbatim_name? ? verbatim_name : name
      n2 = misspelling.object_taxon_name.verbatim_name? ? misspelling.object_taxon_name.verbatim_name : misspelling.object_taxon_name.name
      return true if n1 != n2
    end
    nil
  end

  # Stub, see subclasses
  # TaxonNameRelationships call it for Combinations
  def get_original_combination
    nil
  end

  # Stub, see subclasses
  #   only Protonym, but TaxonNameRelationships call it for Combinations
  def get_original_combination_html
    nil
  end

  # Returns an Array of ancestors
  #   same as self.ancestors, but also works
  #   for new records when parents specified
  def ancestors_through_parents(result = [self], start = self)
    if start.parent.nil?
      return result.reverse
    else
      result << start.parent
      ancestors_through_parents(result, start.parent)
    end
  end

  # @return [Array of TaxonName]
  #   an list of ancestors, Root first
  # Uses parent recursion when record is new and awesome_nested_set_is_not_usable
  def safe_self_and_ancestors
    if new_record?
      ancestors_through_parents
    else
      self_and_ancestors.reload.to_a.reverse ## .self_and_ancestors returns empty array!!!!!!!
    end
  end

  # @return [ rank, prefix, name], ...] for genus and below
  # @taxon_name.full_name_array # =>
  #   [ ["genus", [nil, "Aus"]],
  #     ["subgenus", [nil, "Aus"]],
  #  "section"=>["sect.", "Aus"], "series"=>["ser.", "Aus"], "species"=>[nil, "aaa"], "subspecies"=>[nil, "bbb"], "variety"=>["var.", "ccc"]\}
  def full_name_array
    gender = nil
    data = []
    safe_self_and_ancestors.each do |i|
      rank = i.rank
      gender = i.gender_name if rank == 'genus'
      method = "#{rank.gsub(/\s/, '_')}_name_elements"
      data.push([rank] + send(method, i, gender)) if self.respond_to?(method)
    end
    data
  end

  # @return Hash
  #  { 'family' => 'Aidae', ...}
  def ancestor_hash
    h = {}
    safe_self_and_ancestors.each do |n|
      h[n.rank] = n.name
    end
    h
  end

  # !! TODO: when name is a subgenus will not grab genus
  # !! TODO: Higher classification does not follow the same pattern
  # ?? TODO: Replace with `taxonomy` object .to_h?
  #
  # @!return [ { rank => [prefix, name] }
  #   Returns a hash of rank => [prefix, name] for genus and below
  # @taxon_name.full_name_hash # =>
  #      { "family' => 'Gidae',
  #        "genus" => [nil, "Aus"],
  #        "subgenus" => [nil, "Aus"],
  #        "section" => ["sect.", "Aus"],
  #        "series" => ["ser.", "Aus"],
  #        "species" => [nil, "aaa"],
  #        "subspecies" => [nil, "bbb"],
  #        "variety" => ["var.", "ccc"]}
  def full_name_hash
    gender = nil
    data = {}
    safe_self_and_ancestors.each do |i| # !! You can not use self.self_and_ancestors because (this) record is not saved off.
      rank = i.rank
      gender = i.gender_name if rank == 'genus'

      if i.is_genus_or_species_rank?
        if ['genus', 'subgenus', 'species', 'subspecies'].include?(rank)
          data[rank] = [nil, i.name_with_misspelling(gender)]
        else
          data[rank] = [i.rank_class.abbreviation, i.name_with_misspelling(gender)]
        end
      else
        data[rank] = i.name
      end
    end

    # Only check for these ranks
    if COMBINATION_ELEMENTS.include?(rank.to_sym)
      if data['genus'].nil?
        if original_genus
          data['genus'] = [nil, "[#{original_genus&.name}]"]
        else
          data['genus'] = [nil, '[GENUS NOT SPECIFIED]']
        end
      end

      if data['species'].nil? && (!data['subspecies'].nil? || !data['variety'].nil? || !data['subvariety'].nil? || !data['form'].nil? || !data['subform'].nil?)
        data['species'] = [nil, '[SPECIES NOT SPECIFIED]']
      end

      if !data['subvariety'].nil? && data['variety'].nil?
        data['variety'] = [nil, '[VARIETY NOT SPECIFIED]']
      end

      if !data['subform'].nil? && data['form'].nil?
        data['form'] = [nil, '[FORM NOT SPECIFIED]']
      end
    end

    data
  end

  # @return [String, nil]
  #  A monominal if names is above genus, or a full epithet if below.
  #  Does not include author_year. Does not include HTML.
  def get_full_name
    return name_with_misspelling(nil) if type != 'Combination' && !GENUS_AND_SPECIES_RANK_NAMES.include?(rank_string)
    return name if rank_class.to_s =~ /Icvcn/
    return verbatim_name if verbatim_name.present? && is_combination?

    d = full_name_hash

    elements = []

    elements.push(d['genus']) unless (not_binominal? && d['genus'][1] == '[GENUS NOT SPECIFIED]')

    elements.push ['(', d['subgenus'], ')']
    elements.push ['(', d['infragenus'], ')'] if rank_name == 'infragenus'
    elements.push ['(', d['supergenus'], ')'] if rank_name == 'supergenus'
    elements.push ['(', d['supersubgenus'], ')'] if rank_name == 'supersubgenus'
    elements.push ['(', d['supersupersubgenus'], ')'] if rank_name == 'supersupersubgenus'
    elements.push [d['supersuperspecies']] if rank_name == 'supersuperspecies'
    elements.push [d['superspecies']] if rank_name == 'superspecies'
    elements.push [d['subsuperspecies']] if rank_name == 'subsuperspecies'

    elements.push(d['species'], d['subspecies'], d['variety'], d['subvariety'], d['form'], d['subform'])

    elements = elements.flatten.compact.join(' ').gsub(/\(\s*\)/, '').gsub(/\(\s/, '(').gsub(/\s\)/, ')').squish
    elements.blank? ? nil : elements
  end

  def get_full_name_html(name = nil)
    name = get_full_name if name.nil?
    return  "\"<i>Candidatus</i> #{name}\"" if is_candidatus?
    if !name.blank? && is_hybrid?
      w = name.split(' ')
      w[-1] = ('×' + w[-1]).gsub('×(', '')
      name = w.join(' ')
    end

    m = name
    m = Utilities::Italicize.taxon_name(name) if is_italicized?
    m = '' + m if is_fossil?
    m
  end

  # @return [String]
  #    TODO: does this form of the name contain parens for subgenus?
  #    TODO: provide a default to gender (but do NOT eliminate param)
  #    TODO: on third thought- eliminate this mess
  def name_with_misspelling(gender)
    if cached_misspelling
      if rank_string =~ /Icnp/
        name.to_s + ' (sic)'
      else
        name.to_s + ' [sic]'
      end
    elsif gender.nil? || rank_string =~ /Genus/
      name.to_s
    else
      name_in_gender(gender).to_s
    end
  end

  # @return [String, nil]
  def genderized_name(gender = nil)
    if gender.nil? || is_genus_rank?
      name
    else
      name_in_gender(gender)
    end
  end

  # return [String, nil, false] # TODO: fix
  def get_genus_species(genus_option, self_option)
  # see Protonym
    true
  end

  # return [Boolean]
  #   whether there is an ICZN missapplication relationship present on this name
  def name_is_misapplied?
    !TaxonNameRelationship.where_subject_is_taxon_name(self).with_type_string('TaxonNameRelationship::Iczn::Invalidating::Misapplication').empty?
  end

  # return [String]
  #   the author and year of the name, adds parenthesis where asserted
  def get_author_and_year
    if self.type == 'Combination'
      c = protonyms_by_rank
      return nil if c.empty?
      taxon = c[c.keys.last]
    else
      taxon = self
    end

    case taxon.rank_class.try(:nomenclatural_code)
    when :iczn
      ay = iczn_author_and_year(taxon)
    when :icvcn
      ay = icn_author_and_year(taxon)
    when :icnp
      ay = icn_author_and_year(taxon)
    when :icn
      ay = icn_author_and_year(taxon)
    else
      ay = ([author_string] + [year_integer]).compact.join(' ')
    end
    ay.blank? ? nil : ay
  end

  def icn_author_and_year(taxon)
    ay = nil

    misapplication = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_string('TaxonNameRelationship::Icn::Unaccepting::Misapplication')
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_AUTHOR_STRING)
    m_obj = misapplication.empty? ? nil : misapplication.first.object_taxon_name
    mobj = misspelling.empty? ? taxon : misspelling.first.object_taxon_name
    ay = mobj.try(:author_string) # author string for basionym
    if self.type == 'Combination'
      cc = self
    else
      current_combination = TaxonNameRelationship.where_object_is_taxon_name(mobj).with_type_string('TaxonNameRelationship::CurrentCombination')
      cc = current_combination.empty? ? self : current_combination.first.subject_taxon_name
    end

    if !self.author_string.blank? && mobj.id != cc.id
      ay = '(' + ay.to_s + ') ' + cc.try(:author_string).to_s
    end

    if !misapplication.empty? && !m_obj.author_string.blank?
      ay += ' non ' + m_obj.author_string
    end

    ay.blank? ? nil : ay
  end

  # @return [String, nil]
  #   the authors, and year, with parentheses as inferred by the data
  def iczn_author_and_year(taxon)
    ay = nil
    p = nil

    misapplication = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_string('TaxonNameRelationship::Iczn::Invalidating::Misapplication')
    misspelling = TaxonNameRelationship.where_subject_is_taxon_name(taxon).with_type_array(TAXON_NAME_RELATIONSHIP_NAMES_MISSPELLING_AUTHOR_STRING)

    mobj = misspelling.empty? ? nil : misspelling.first.object_taxon_name
    unless mobj.blank?
      a = [mobj.try(:author_string)]
      y = [mobj.