Class: TypeMaterial

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::Citations, Shared::DataAttributes, Shared::HasRoles, Shared::Identifiers, Shared::IsData, Shared::Notes, Shared::Tags, SoftValidation
Defined in:
app/models/type_material.rb

Overview

TypeMaterial links CollectionObjects to Protonyms. It is the single direct relationship between nomenclature and collection objects in TaxonWorks (all other name/collection object relationships coming through OTUs). TypeMaterial is based on specific rules of nomenclature, it only includes those types (e.g. “holotype”) that are specifically goverened (e.g. “topotype” is not allowed).

Constant Summary

ICZN_TYPES =

Keys are valid values for type_type, values are required Class for material

{
  'holotype' =>  Specimen,
  'paratype' => Specimen,
  'paralectotype' => Specimen,
  'neotype' => Specimen,
  'lectotype' => Specimen,
  'syntype' => Specimen,
  'paratypes' => Lot,
  'syntypes' => Lot,
  'paralectotypes' => Lot
}.freeze
ICN_TYPES =
{
    'holotype' => Specimen,
    'paratype' => Specimen,
    'lectotype' => Specimen,
    'neotype' => Specimen,
    'epitype' => Specimen,
    'isotype' => Specimen,
    'syntype' => Specimen,
    'isosyntype' => Specimen,
    'syntypes' => Lot,
    'isotypes' => Lot,
    'isosyntypes' => Lot
}.freeze

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary (collapse)

Instance Method Summary (collapse)

Methods included from SoftValidation

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

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods included from ActiverecordUtilities

#trim_attributes

Instance Attribute Details

- (Integer) biological_object_id

Returns the CollectionObject

Returns:

  • (Integer)

    the CollectionObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/type_material.rb', line 24

class TypeMaterial < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::HasRoles
  include Shared::Identifiers
  include Shared::IsData
  include Shared::Notes
  include Shared::Tags
  include SoftValidation

  # Keys are valid values for type_type, values are
  # required Class for material
  ICZN_TYPES = {
    'holotype' =>  Specimen,
    'paratype' => Specimen,
    'paralectotype' => Specimen,
    'neotype' => Specimen,
    'lectotype' => Specimen,
    'syntype' => Specimen,
    'paratypes' => Lot,
    'syntypes' => Lot,
    'paralectotypes' => Lot
  }.freeze

  ICN_TYPES = {
      'holotype' => Specimen,
      'paratype' => Specimen,
      'lectotype' => Specimen,
      'neotype' => Specimen,
      'epitype' => Specimen,
      'isotype' => Specimen,
      'syntype' => Specimen,
      'isosyntype' => Specimen,
      'syntypes' => Lot,
      'isotypes' => Lot,
      'isosyntypes' => Lot
  }.freeze

  belongs_to :material, foreign_key: :biological_object_id, class_name: 'CollectionObject', inverse_of: :type_designations
  belongs_to :protonym
  has_many :type_designator_roles, class_name: 'TypeDesignator', as: :role_object
  has_many :type_designators, through: :type_designator_roles, source: :person

  accepts_nested_attributes_for :type_designators, :type_designator_roles, allow_destroy: true
  accepts_nested_attributes_for :material, allow_destroy: true

  scope :where_protonym, -> (taxon_name) { where(protonym_id: taxon_name) }
  scope :with_type_string, -> (base_string) { where('type_type LIKE ?', "#{base_string}" ) }
  scope :with_type_array, -> (base_array) { where('type_type IN (?)', base_array ) }

  scope :primary, -> {where(type_type: %w{neotype lectotype holotype}).order('biological_object_id')}
  scope :syntypes, -> {where(type_type: %w{syntype syntypes}).order('biological_object_id')}

  #  scope :primary_with_protonym_array, -> (base_array) {select('type_type, source_id, biological_object_id').group('type_type, source_id, biological_object_id').where("type_materials.type_type IN ('neotype', 'lectotype', 'holotype', 'syntype', 'syntypes') AND type_materials.protonym_id IN (?)", base_array ) }

  scope :primary_with_protonym_array, -> (base_array) {select('type_type, biological_object_id').group('type_type, biological_object_id').where("type_materials.type_type IN ('neotype', 'lectotype', 'holotype', 'syntype', 'syntypes') AND type_materials.protonym_id IN (?)", base_array ) }

  soft_validate(:sv_single_primary_type, set: :single_primary_type)
  soft_validate(:sv_type_source, set: :type_source)

  validates :protonym, presence: true
  validates :material, presence: true
  validates_presence_of :type_type

  validate :check_type_type
  validate :check_protonym_rank

  # TODO: really should be validating uniqueness at this point, it's type material, not garbage records

  def type_source
    [source, protonym.try(:source), nil].compact.first
  end

  def legal_type_type(code, type_type)
    case code
    when :iczn
      ICZN_TYPES.keys.include?(type_type)
    when :icn
      ICZN_TYPES.keys.include?(type_type)
    else
      false
    end
  end

  protected

  def check_type_type
    if protonym
      code = protonym.rank_class.nomenclatural_code
      errors.add(:type_type, 'Not a legal type for the nomenclatural code provided') if !legal_type_type(code, type_type)
    end
  end

  def check_protonym_rank
    errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name') if protonym && !protonym.is_species_rank?
  end

  def sv_single_primary_type

    primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(protonym).not_self(self)
    syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(protonym)

    if type_type =~ /syntype/
      soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
    end

    if ['holotype', 'neotype', 'lectotype'].include?(type_type)
      soft_validations.add(:type_type, 'More than one primary type associated with the taxon') if !primary_types.empty? || !syntypes.empty?
    end
  end

  def sv_type_source
    soft_validations.add(:base, 'Source is not selected neither for type nor for taxon') unless type_source
    if %w(paralectotype neotype lectotype paralectotypes).include?(type_type)
      if source.nil?
        soft_validations.add(:base, "Source for #{type_type} designation is not selected ") if source.nil?
      elsif !protonym.try(:source).nil? && source.nomenclature_date && protonym.nomenclature_date
        soft_validations.add(:base, "#{type_type.capitalize} could not be designated in the original publication") if source == protonym.source
        soft_validations.add(:base, "#{type_type.capitalize} could not be designated before taxon description") if source.nomenclature_date < protonym.nomenclature_date
      end
    end
  end

end

- (Integer) position

Returns sort column

Returns:

  • (Integer)

    sort column



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/type_material.rb', line 24

class TypeMaterial < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::HasRoles
  include Shared::Identifiers
  include Shared::IsData
  include Shared::Notes
  include Shared::Tags
  include SoftValidation

  # Keys are valid values for type_type, values are
  # required Class for material
  ICZN_TYPES = {
    'holotype' =>  Specimen,
    'paratype' => Specimen,
    'paralectotype' => Specimen,
    'neotype' => Specimen,
    'lectotype' => Specimen,
    'syntype' => Specimen,
    'paratypes' => Lot,
    'syntypes' => Lot,
    'paralectotypes' => Lot
  }.freeze

  ICN_TYPES = {
      'holotype' => Specimen,
      'paratype' => Specimen,
      'lectotype' => Specimen,
      'neotype' => Specimen,
      'epitype' => Specimen,
      'isotype' => Specimen,
      'syntype' => Specimen,
      'isosyntype' => Specimen,
      'syntypes' => Lot,
      'isotypes' => Lot,
      'isosyntypes' => Lot
  }.freeze

  belongs_to :material, foreign_key: :biological_object_id, class_name: 'CollectionObject', inverse_of: :type_designations
  belongs_to :protonym
  has_many :type_designator_roles, class_name: 'TypeDesignator', as: :role_object
  has_many :type_designators, through: :type_designator_roles, source: :person

  accepts_nested_attributes_for :type_designators, :type_designator_roles, allow_destroy: true
  accepts_nested_attributes_for :material, allow_destroy: true

  scope :where_protonym, -> (taxon_name) { where(protonym_id: taxon_name) }
  scope :with_type_string, -> (base_string) { where('type_type LIKE ?', "#{base_string}" ) }
  scope :with_type_array, -> (base_array) { where('type_type IN (?)', base_array ) }

  scope :primary, -> {where(type_type: %w{neotype lectotype holotype}).order('biological_object_id')}
  scope :syntypes, -> {where(type_type: %w{syntype syntypes}).order('biological_object_id')}

  #  scope :primary_with_protonym_array, -> (base_array) {select('type_type, source_id, biological_object_id').group('type_type, source_id, biological_object_id').where("type_materials.type_type IN ('neotype', 'lectotype', 'holotype', 'syntype', 'syntypes') AND type_materials.protonym_id IN (?)", base_array ) }

  scope :primary_with_protonym_array, -> (base_array) {select('type_type, biological_object_id').group('type_type, biological_object_id').where("type_materials.type_type IN ('neotype', 'lectotype', 'holotype', 'syntype', 'syntypes') AND type_materials.protonym_id IN (?)", base_array ) }

  soft_validate(:sv_single_primary_type, set: :single_primary_type)
  soft_validate(:sv_type_source, set: :type_source)

  validates :protonym, presence: true
  validates :material, presence: true
  validates_presence_of :type_type

  validate :check_type_type
  validate :check_protonym_rank

  # TODO: really should be validating uniqueness at this point, it's type material, not garbage records

  def type_source
    [source, protonym.try(:source), nil].compact.first
  end

  def legal_type_type(code, type_type)
    case code
    when :iczn
      ICZN_TYPES.keys.include?(type_type)
    when :icn
      ICZN_TYPES.keys.include?(type_type)
    else
      false
    end
  end

  protected

  def check_type_type
    if protonym
      code = protonym.rank_class.nomenclatural_code
      errors.add(:type_type, 'Not a legal type for the nomenclatural code provided') if !legal_type_type(code, type_type)
    end
  end

  def check_protonym_rank
    errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name') if protonym && !protonym.is_species_rank?
  end

  def sv_single_primary_type

    primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(protonym).not_self(self)
    syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(protonym)

    if type_type =~ /syntype/
      soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
    end

    if ['holotype', 'neotype', 'lectotype'].include?(type_type)
      soft_validations.add(:type_type, 'More than one primary type associated with the taxon') if !primary_types.empty? || !syntypes.empty?
    end
  end

  def sv_type_source
    soft_validations.add(:base, 'Source is not selected neither for type nor for taxon') unless type_source
    if %w(paralectotype neotype lectotype paralectotypes).include?(type_type)
      if source.nil?
        soft_validations.add(:base, "Source for #{type_type} designation is not selected ") if source.nil?
      elsif !protonym.try(:source).nil? && source.nomenclature_date && protonym.nomenclature_date
        soft_validations.add(:base, "#{type_type.capitalize} could not be designated in the original publication") if source == protonym.source
        soft_validations.add(:base, "#{type_type.capitalize} could not be designated before taxon description") if source.nomenclature_date < protonym.nomenclature_date
      end
    end
  end

end

- (Integer) project_id

the project ID

Returns:

  • (Integer)

    Integer



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/type_material.rb', line 24

class TypeMaterial < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::HasRoles
  include Shared::Identifiers
  include Shared::IsData
  include Shared::Notes
  include Shared::Tags
  include SoftValidation

  # Keys are valid values for type_type, values are
  # required Class for material
  ICZN_TYPES = {
    'holotype' =>  Specimen,
    'paratype' => Specimen,
    'paralectotype' => Specimen,
    'neotype' => Specimen,
    'lectotype' => Specimen,
    'syntype' => Specimen,
    'paratypes' => Lot,
    'syntypes' => Lot,
    'paralectotypes' => Lot
  }.freeze

  ICN_TYPES = {
      'holotype' => Specimen,
      'paratype' => Specimen,
      'lectotype' => Specimen,
      'neotype' => Specimen,
      'epitype' => Specimen,
      'isotype' => Specimen,
      'syntype' => Specimen,
      'isosyntype' => Specimen,
      'syntypes' => Lot,
      'isotypes' => Lot,
      'isosyntypes' => Lot
  }.freeze

  belongs_to :material, foreign_key: :biological_object_id, class_name: 'CollectionObject', inverse_of: :type_designations
  belongs_to :protonym
  has_many :type_designator_roles, class_name: 'TypeDesignator', as: :role_object
  has_many :type_designators, through: :type_designator_roles, source: :person

  accepts_nested_attributes_for :type_designators, :type_designator_roles, allow_destroy: true
  accepts_nested_attributes_for :material, allow_destroy: true

  scope :where_protonym, -> (taxon_name) { where(protonym_id: taxon_name) }
  scope :with_type_string, -> (base_string) { where('type_type LIKE ?', "#{base_string}" ) }
  scope :with_type_array, -> (base_array) { where('type_type IN (?)', base_array ) }

  scope :primary, -> {where(type_type: %w{neotype lectotype holotype}).order('biological_object_id')}
  scope :syntypes, -> {where(type_type: %w{syntype syntypes}).order('biological_object_id')}

  #  scope :primary_with_protonym_array, -> (base_array) {select('type_type, source_id, biological_object_id').group('type_type, source_id, biological_object_id').where("type_materials.type_type IN ('neotype', 'lectotype', 'holotype', 'syntype', 'syntypes') AND type_materials.protonym_id IN (?)", base_array ) }

  scope :primary_with_protonym_array, -> (base_array) {select('type_type, biological_object_id').group('type_type, biological_object_id').where("type_materials.type_type IN ('neotype', 'lectotype', 'holotype', 'syntype', 'syntypes') AND type_materials.protonym_id IN (?)", base_array ) }

  soft_validate(:sv_single_primary_type, set: :single_primary_type)
  soft_validate(:sv_type_source, set: :type_source)

  validates :protonym, presence: true
  validates :material, presence: true
  validates_presence_of :type_type

  validate :check_type_type
  validate :check_protonym_rank

  # TODO: really should be validating uniqueness at this point, it's type material, not garbage records

  def type_source
    [source, protonym.try(:source), nil].compact.first
  end

  def legal_type_type(code, type_type)
    case code
    when :iczn
      ICZN_TYPES.keys.include?(type_type)
    when :icn
      ICZN_TYPES.keys.include?(type_type)
    else
      false
    end
  end

  protected

  def check_type_type
    if protonym
      code = protonym.rank_class.nomenclatural_code
      errors.add(:type_type, 'Not a legal type for the nomenclatural code provided') if !legal_type_type(code, type_type)
    end
  end

  def check_protonym_rank
    errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name') if protonym && !protonym.is_species_rank?
  end

  def sv_single_primary_type

    primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(protonym).not_self(self)
    syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(protonym)

    if type_type =~ /syntype/
      soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
    end

    if ['holotype', 'neotype', 'lectotype'].include?(type_type)
      soft_validations.add(:type_type, 'More than one primary type associated with the taxon') if !primary_types.empty? || !syntypes.empty?
    end
  end

  def sv_type_source
    soft_validations.add(:base, 'Source is not selected neither for type nor for taxon') unless type_source
    if %w(paralectotype neotype lectotype paralectotypes).include?(type_type)
      if source.nil?
        soft_validations.add(:base, "Source for #{type_type} designation is not selected ") if source.nil?
      elsif !protonym.try(:source).nil? && source.nomenclature_date && protonym.nomenclature_date
        soft_validations.add(:base, "#{type_type.capitalize} could not be designated in the original publication") if source == protonym.source
        soft_validations.add(:base, "#{type_type.capitalize} could not be designated before taxon description") if source.nomenclature_date < protonym.nomenclature_date
      end
    end
  end

end

- (Integer) protonym_id

Returns the protonym in question

Returns:

  • (Integer)

    the protonym in question



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/type_material.rb', line 24

class TypeMaterial < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::HasRoles
  include Shared::Identifiers
  include Shared::IsData
  include Shared::Notes
  include Shared::Tags
  include SoftValidation

  # Keys are valid values for type_type, values are
  # required Class for material
  ICZN_TYPES = {
    'holotype' =>  Specimen,
    'paratype' => Specimen,
    'paralectotype' => Specimen,
    'neotype' => Specimen,
    'lectotype' => Specimen,
    'syntype' => Specimen,
    'paratypes' => Lot,
    'syntypes' => Lot,
    'paralectotypes' => Lot
  }.freeze

  ICN_TYPES = {
      'holotype' => Specimen,
      'paratype' => Specimen,
      'lectotype' => Specimen,
      'neotype' => Specimen,
      'epitype' => Specimen,
      'isotype' => Specimen,
      'syntype' => Specimen,
      'isosyntype' => Specimen,
      'syntypes' => Lot,
      'isotypes' => Lot,
      'isosyntypes' => Lot
  }.freeze

  belongs_to :material, foreign_key: :biological_object_id, class_name: 'CollectionObject', inverse_of: :type_designations
  belongs_to :protonym
  has_many :type_designator_roles, class_name: 'TypeDesignator', as: :role_object
  has_many :type_designators, through: :type_designator_roles, source: :person

  accepts_nested_attributes_for :type_designators, :type_designator_roles, allow_destroy: true
  accepts_nested_attributes_for :material, allow_destroy: true

  scope :where_protonym, -> (taxon_name) { where(protonym_id: taxon_name) }
  scope :with_type_string, -> (base_string) { where('type_type LIKE ?', "#{base_string}" ) }
  scope :with_type_array, -> (base_array) { where('type_type IN (?)', base_array ) }

  scope :primary, -> {where(type_type: %w{neotype lectotype holotype}).order('biological_object_id')}
  scope :syntypes, -> {where(type_type: %w{syntype syntypes}).order('biological_object_id')}

  #  scope :primary_with_protonym_array, -> (base_array) {select('type_type, source_id, biological_object_id').group('type_type, source_id, biological_object_id').where("type_materials.type_type IN ('neotype', 'lectotype', 'holotype', 'syntype', 'syntypes') AND type_materials.protonym_id IN (?)", base_array ) }

  scope :primary_with_protonym_array, -> (base_array) {select('type_type, biological_object_id').group('type_type, biological_object_id').where("type_materials.type_type IN ('neotype', 'lectotype', 'holotype', 'syntype', 'syntypes') AND type_materials.protonym_id IN (?)", base_array ) }

  soft_validate(:sv_single_primary_type, set: :single_primary_type)
  soft_validate(:sv_type_source, set: :type_source)

  validates :protonym, presence: true
  validates :material, presence: true
  validates_presence_of :type_type

  validate :check_type_type
  validate :check_protonym_rank

  # TODO: really should be validating uniqueness at this point, it's type material, not garbage records

  def type_source
    [source, protonym.try(:source), nil].compact.first
  end

  def legal_type_type(code, type_type)
    case code
    when :iczn
      ICZN_TYPES.keys.include?(type_type)
    when :icn
      ICZN_TYPES.keys.include?(type_type)
    else
      false
    end
  end

  protected

  def check_type_type
    if protonym
      code = protonym.rank_class.nomenclatural_code
      errors.add(:type_type, 'Not a legal type for the nomenclatural code provided') if !legal_type_type(code, type_type)
    end
  end

  def check_protonym_rank
    errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name') if protonym && !protonym.is_species_rank?
  end

  def sv_single_primary_type

    primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(protonym).not_self(self)
    syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(protonym)

    if type_type =~ /syntype/
      soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
    end

    if ['holotype', 'neotype', 'lectotype'].include?(type_type)
      soft_validations.add(:type_type, 'More than one primary type associated with the taxon') if !primary_types.empty? || !syntypes.empty?
    end
  end

  def sv_type_source
    soft_validations.add(:base, 'Source is not selected neither for type nor for taxon') unless type_source
    if %w(paralectotype neotype lectotype paralectotypes).include?(type_type)
      if source.nil?
        soft_validations.add(:base, "Source for #{type_type} designation is not selected ") if source.nil?
      elsif !protonym.try(:source).nil? && source.nomenclature_date && protonym.nomenclature_date
        soft_validations.add(:base, "#{type_type.capitalize} could not be designated in the original publication") if source == protonym.source
        soft_validations.add(:base, "#{type_type.capitalize} could not be designated before taxon description") if source.nomenclature_date < protonym.nomenclature_date
      end
    end
  end

end

- (String) type_type

Returns the type of Type relationship (e.g. holotype)

Returns:

  • (String)

    the type of Type relationship (e.g. holotype)



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'app/models/type_material.rb', line 24

class TypeMaterial < ApplicationRecord
  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::HasRoles
  include Shared::Identifiers
  include Shared::IsData
  include Shared::Notes
  include Shared::Tags
  include SoftValidation

  # Keys are valid values for type_type, values are
  # required Class for material
  ICZN_TYPES = {
    'holotype' =>  Specimen,
    'paratype' => Specimen,
    'paralectotype' => Specimen,
    'neotype' => Specimen,
    'lectotype' => Specimen,
    'syntype' => Specimen,
    'paratypes' => Lot,
    'syntypes' => Lot,
    'paralectotypes' => Lot
  }.freeze

  ICN_TYPES = {
      'holotype' => Specimen,
      'paratype' => Specimen,
      'lectotype' => Specimen,
      'neotype' => Specimen,
      'epitype' => Specimen,
      'isotype' => Specimen,
      'syntype' => Specimen,
      'isosyntype' => Specimen,
      'syntypes' => Lot,
      'isotypes' => Lot,
      'isosyntypes' => Lot
  }.freeze

  belongs_to :material, foreign_key: :biological_object_id, class_name: 'CollectionObject', inverse_of: :type_designations
  belongs_to :protonym
  has_many :type_designator_roles, class_name: 'TypeDesignator', as: :role_object
  has_many :type_designators, through: :type_designator_roles, source: :person

  accepts_nested_attributes_for :type_designators, :type_designator_roles, allow_destroy: true
  accepts_nested_attributes_for :material, allow_destroy: true

  scope :where_protonym, -> (taxon_name) { where(protonym_id: taxon_name) }
  scope :with_type_string, -> (base_string) { where('type_type LIKE ?', "#{base_string}" ) }
  scope :with_type_array, -> (base_array) { where('type_type IN (?)', base_array ) }

  scope :primary, -> {where(type_type: %w{neotype lectotype holotype}).order('biological_object_id')}
  scope :syntypes, -> {where(type_type: %w{syntype syntypes}).order('biological_object_id')}

  #  scope :primary_with_protonym_array, -> (base_array) {select('type_type, source_id, biological_object_id').group('type_type, source_id, biological_object_id').where("type_materials.type_type IN ('neotype', 'lectotype', 'holotype', 'syntype', 'syntypes') AND type_materials.protonym_id IN (?)", base_array ) }

  scope :primary_with_protonym_array, -> (base_array) {select('type_type, biological_object_id').group('type_type, biological_object_id').where("type_materials.type_type IN ('neotype', 'lectotype', 'holotype', 'syntype', 'syntypes') AND type_materials.protonym_id IN (?)", base_array ) }

  soft_validate(:sv_single_primary_type, set: :single_primary_type)
  soft_validate(:sv_type_source, set: :type_source)

  validates :protonym, presence: true
  validates :material, presence: true
  validates_presence_of :type_type

  validate :check_type_type
  validate :check_protonym_rank

  # TODO: really should be validating uniqueness at this point, it's type material, not garbage records

  def type_source
    [source, protonym.try(:source), nil].compact.first
  end

  def legal_type_type(code, type_type)
    case code
    when :iczn
      ICZN_TYPES.keys.include?(type_type)
    when :icn
      ICZN_TYPES.keys.include?(type_type)
    else
      false
    end
  end

  protected

  def check_type_type
    if protonym
      code = protonym.rank_class.nomenclatural_code
      errors.add(:type_type, 'Not a legal type for the nomenclatural code provided') if !legal_type_type(code, type_type)
    end
  end

  def check_protonym_rank
    errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name') if protonym && !protonym.is_species_rank?
  end

  def sv_single_primary_type

    primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(protonym).not_self(self)
    syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(protonym)

    if type_type =~ /syntype/
      soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
    end

    if ['holotype', 'neotype', 'lectotype'].include?(type_type)
      soft_validations.add(:type_type, 'More than one primary type associated with the taxon') if !primary_types.empty? || !syntypes.empty?
    end
  end

  def sv_type_source
    soft_validations.add(:base, 'Source is not selected neither for type nor for taxon') unless type_source
    if %w(paralectotype neotype lectotype paralectotypes).include?(type_type)
      if source.nil?
        soft_validations.add(:base, "Source for #{type_type} designation is not selected ") if source.nil?
      elsif !protonym.try(:source).nil? && source.nomenclature_date && protonym.nomenclature_date
        soft_validations.add(:base, "#{type_type.capitalize} could not be designated in the original publication") if source == protonym.source
        soft_validations.add(:base, "#{type_type.capitalize} could not be designated before taxon description") if source.nomenclature_date < protonym.nomenclature_date
      end
    end
  end

end

Instance Method Details

- (Object) check_protonym_rank (protected)



118
119
120
# File 'app/models/type_material.rb', line 118

def check_protonym_rank
  errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name') if protonym && !protonym.is_species_rank?
end

- (Object) check_type_type (protected)



111
112
113
114
115
116
# File 'app/models/type_material.rb', line 111

def check_type_type
  if protonym
    code = protonym.rank_class.nomenclatural_code
    errors.add(:type_type, 'Not a legal type for the nomenclatural code provided') if !legal_type_type(code, type_type)
  end
end


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

def legal_type_type(code, type_type)
  case code
  when :iczn
    ICZN_TYPES.keys.include?(type_type)
  when :icn
    ICZN_TYPES.keys.include?(type_type)
  else
    false
  end
end

- (Object) sv_single_primary_type (protected)



122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'app/models/type_material.rb', line 122

def sv_single_primary_type

  primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(protonym).not_self(self)
  syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(protonym)

  if type_type =~ /syntype/
    soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
  end

  if ['holotype', 'neotype', 'lectotype'].include?(type_type)
    soft_validations.add(:type_type, 'More than one primary type associated with the taxon') if !primary_types.empty? || !syntypes.empty?
  end
end

- (Object) sv_type_source (protected)



136
137
138
139
140
141
142
143
144
145
146
# File 'app/models/type_material.rb', line 136

def sv_type_source
  soft_validations.add(:base, 'Source is not selected neither for type nor for taxon') unless type_source
  if %w(paralectotype neotype lectotype paralectotypes).include?(type_type)
    if source.nil?
      soft_validations.add(:base, "Source for #{type_type} designation is not selected ") if source.nil?
    elsif !protonym.try(:source).nil? && source.nomenclature_date && protonym.nomenclature_date
      soft_validations.add(:base, "#{type_type.capitalize} could not be designated in the original publication") if source == protonym.source
      soft_validations.add(:base, "#{type_type.capitalize} could not be designated before taxon description") if source.nomenclature_date < protonym.nomenclature_date
    end
  end
end

- (Object) type_source

TODO: really should be validating uniqueness at this point, it's type material, not garbage records



94
95
96
# File 'app/models/type_material.rb', line 94

def type_source
  [source, protonym.try(:source), nil].compact.first
end