Class: TypeMaterial

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::Citable, Shared::DataAttributes, Shared::HasRoles, Shared::Identifiable, Shared::IsData, Shared::Notable, Shared::Taggable, SoftValidation
Defined in:
app/models/type_material.rb

Overview

TypeMaterial is a definition of goverened type

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
}
ICN_TYPES =
{
    'holotype' => Specimen,
    'paratype' => Specimen,
    'lectotype' => Specimen,
    'neotype' => Specimen,
    'epitype' => Specimen,
    'isotype' => Specimen,
    'syntype' => Specimen,
    'isosyntype' => Specimen,
    'syntypes' => Lot,
    'isotypes' => Lot,
    'isosyntypes' => Lot
}

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary (collapse)

Class Method 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 specimen record

Returns:

  • (Integer)

    the specimen record



23
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
149
150
151
# File 'app/models/type_material.rb', line 23

class TypeMaterial < ApplicationRecord
  include Housekeeping
  include Shared::Citable
  include Shared::DataAttributes
  include Shared::HasRoles
  include Shared::Identifiable
  include Shared::IsData
  include Shared::Notable
  include Shared::Taggable
  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
  }

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

  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

  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

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

  def type_source
    if !!self.source
      self.source
    elsif !!self.protonym
      if !!self.protonym.source
        self.protonym.source
      else
        nil
      end
    else
      nil
    end
  end

  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  protected

  #region Validation

  def check_type_type
    if self.protonym
      code = self.protonym.rank_class.nomenclatural_code
      if (code == :iczn && !ICZN_TYPES.keys.include?(self.type_type)) || (code == :icn && !ICN_TYPES.keys.include?(self.type_type))
        errors.add(:type_type, 'Not a legal type for the nomenclatural code provided')
      end
      unless self.protonym.rank_class.parent.to_s =~ /Species/
        errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name')
      end
    end
  end

  #endregion

  #region Soft Validation

  def sv_single_primary_type
    primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(self.protonym).not_self(self)
    syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(self.protonym)
    if self.type_type == 'syntype' || self.type_type == 'syntypes'
      soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
    end
    if self.type_type == 'holotype' || self.type_type == 'neotype' || self.type_type == 'lectotype'
      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
  end

  #endregion
end

- (Integer) position

Returns sort column

Returns:

  • (Integer)

    sort column



23
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
149
150
151
# File 'app/models/type_material.rb', line 23

class TypeMaterial < ApplicationRecord
  include Housekeeping
  include Shared::Citable
  include Shared::DataAttributes
  include Shared::HasRoles
  include Shared::Identifiable
  include Shared::IsData
  include Shared::Notable
  include Shared::Taggable
  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
  }

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

  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

  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

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

  def type_source
    if !!self.source
      self.source
    elsif !!self.protonym
      if !!self.protonym.source
        self.protonym.source
      else
        nil
      end
    else
      nil
    end
  end

  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  protected

  #region Validation

  def check_type_type
    if self.protonym
      code = self.protonym.rank_class.nomenclatural_code
      if (code == :iczn && !ICZN_TYPES.keys.include?(self.type_type)) || (code == :icn && !ICN_TYPES.keys.include?(self.type_type))
        errors.add(:type_type, 'Not a legal type for the nomenclatural code provided')
      end
      unless self.protonym.rank_class.parent.to_s =~ /Species/
        errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name')
      end
    end
  end

  #endregion

  #region Soft Validation

  def sv_single_primary_type
    primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(self.protonym).not_self(self)
    syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(self.protonym)
    if self.type_type == 'syntype' || self.type_type == 'syntypes'
      soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
    end
    if self.type_type == 'holotype' || self.type_type == 'neotype' || self.type_type == 'lectotype'
      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
  end

  #endregion
end

- (Integer) project_id

the project ID

Returns:

  • (Integer)

    Integer



23
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
149
150
151
# File 'app/models/type_material.rb', line 23

class TypeMaterial < ApplicationRecord
  include Housekeeping
  include Shared::Citable
  include Shared::DataAttributes
  include Shared::HasRoles
  include Shared::Identifiable
  include Shared::IsData
  include Shared::Notable
  include Shared::Taggable
  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
  }

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

  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

  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

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

  def type_source
    if !!self.source
      self.source
    elsif !!self.protonym
      if !!self.protonym.source
        self.protonym.source
      else
        nil
      end
    else
      nil
    end
  end

  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  protected

  #region Validation

  def check_type_type
    if self.protonym
      code = self.protonym.rank_class.nomenclatural_code
      if (code == :iczn && !ICZN_TYPES.keys.include?(self.type_type)) || (code == :icn && !ICN_TYPES.keys.include?(self.type_type))
        errors.add(:type_type, 'Not a legal type for the nomenclatural code provided')
      end
      unless self.protonym.rank_class.parent.to_s =~ /Species/
        errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name')
      end
    end
  end

  #endregion

  #region Soft Validation

  def sv_single_primary_type
    primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(self.protonym).not_self(self)
    syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(self.protonym)
    if self.type_type == 'syntype' || self.type_type == 'syntypes'
      soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
    end
    if self.type_type == 'holotype' || self.type_type == 'neotype' || self.type_type == 'lectotype'
      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
  end

  #endregion
end

- (Integer) protonym_id

Returns the protonym in question

Returns:

  • (Integer)

    the protonym in question



23
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
149
150
151
# File 'app/models/type_material.rb', line 23

class TypeMaterial < ApplicationRecord
  include Housekeeping
  include Shared::Citable
  include Shared::DataAttributes
  include Shared::HasRoles
  include Shared::Identifiable
  include Shared::IsData
  include Shared::Notable
  include Shared::Taggable
  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
  }

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

  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

  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

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

  def type_source
    if !!self.source
      self.source
    elsif !!self.protonym
      if !!self.protonym.source
        self.protonym.source
      else
        nil
      end
    else
      nil
    end
  end

  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  protected

  #region Validation

  def check_type_type
    if self.protonym
      code = self.protonym.rank_class.nomenclatural_code
      if (code == :iczn && !ICZN_TYPES.keys.include?(self.type_type)) || (code == :icn && !ICN_TYPES.keys.include?(self.type_type))
        errors.add(:type_type, 'Not a legal type for the nomenclatural code provided')
      end
      unless self.protonym.rank_class.parent.to_s =~ /Species/
        errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name')
      end
    end
  end

  #endregion

  #region Soft Validation

  def sv_single_primary_type
    primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(self.protonym).not_self(self)
    syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(self.protonym)
    if self.type_type == 'syntype' || self.type_type == 'syntypes'
      soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
    end
    if self.type_type == 'holotype' || self.type_type == 'neotype' || self.type_type == 'lectotype'
      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
  end

  #endregion
end

- (String) type_type

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

Returns:

  • (String)

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



23
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
149
150
151
# File 'app/models/type_material.rb', line 23

class TypeMaterial < ApplicationRecord
  include Housekeeping
  include Shared::Citable
  include Shared::DataAttributes
  include Shared::HasRoles
  include Shared::Identifiable
  include Shared::IsData
  include Shared::Notable
  include Shared::Taggable
  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
  }

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

  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

  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

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

  def type_source
    if !!self.source
      self.source
    elsif !!self.protonym
      if !!self.protonym.source
        self.protonym.source
      else
        nil
      end
    else
      nil
    end
  end

  def self.generate_download(scope)
    CSV.generate do |csv|
      csv << column_names
      scope.order(id: :asc).each do |o|
        csv << o.attributes.values_at(*column_names).collect { |i|
          i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
        }
      end
    end
  end

  protected

  #region Validation

  def check_type_type
    if self.protonym
      code = self.protonym.rank_class.nomenclatural_code
      if (code == :iczn && !ICZN_TYPES.keys.include?(self.type_type)) || (code == :icn && !ICN_TYPES.keys.include?(self.type_type))
        errors.add(:type_type, 'Not a legal type for the nomenclatural code provided')
      end
      unless self.protonym.rank_class.parent.to_s =~ /Species/
        errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name')
      end
    end
  end

  #endregion

  #region Soft Validation

  def sv_single_primary_type
    primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(self.protonym).not_self(self)
    syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(self.protonym)
    if self.type_type == 'syntype' || self.type_type == 'syntypes'
      soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
    end
    if self.type_type == 'holotype' || self.type_type == 'neotype' || self.type_type == 'lectotype'
      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
  end

  #endregion
end

Class Method Details

+ (Object) generate_download(scope)



104
105
106
107
108
109
110
111
112
113
# File 'app/models/type_material.rb', line 104

def self.generate_download(scope)
  CSV.generate do |csv|
    csv << column_names
    scope.order(id: :asc).each do |o|
      csv << o.attributes.values_at(*column_names).collect { |i|
        i.to_s.gsub(/\n/, '\n').gsub(/\t/, '\t')
      }
    end
  end
end

Instance Method Details

- (Object) check_type_type (protected)

region Validation



119
120
121
122
123
124
125
126
127
128
129
# File 'app/models/type_material.rb', line 119

def check_type_type
  if self.protonym
    code = self.protonym.rank_class.nomenclatural_code
    if (code == :iczn && !ICZN_TYPES.keys.include?(self.type_type)) || (code == :icn && !ICN_TYPES.keys.include?(self.type_type))
      errors.add(:type_type, 'Not a legal type for the nomenclatural code provided')
    end
    unless self.protonym.rank_class.parent.to_s =~ /Species/
      errors.add(:protonym_id, 'Type cannot be designated, name is not a species group name')
    end
  end
end

- (Object) sv_single_primary_type (protected)

region Soft Validation



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

def sv_single_primary_type
  primary_types = TypeMaterial.with_type_array(['holotype', 'neotype', 'lectotype']).where_protonym(self.protonym).not_self(self)
  syntypes = TypeMaterial.with_type_array(['syntype', 'syntypes']).where_protonym(self.protonym)
  if self.type_type == 'syntype' || self.type_type == 'syntypes'
    soft_validations.add(:type_type, 'Other primary types selected for the taxon are conflicting with the syntypes') unless primary_types.empty?
  end
  if self.type_type == 'holotype' || self.type_type == 'neotype' || self.type_type == 'lectotype'
    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)



146
147
148
# File 'app/models/type_material.rb', line 146

def sv_type_source
  soft_validations.add(:base, 'Source is not selected neither for type nor for taxon') unless type_source
end

- (Object) type_source

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



90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'app/models/type_material.rb', line 90

def type_source
  if !!self.source
    self.source
  elsif !!self.protonym
    if !!self.protonym.source
      self.protonym.source
    else
      nil
    end
  else
    nil
  end
end