Class: TaxonDetermination

Overview

A Taxon determination is an assertion that a collection object belongs to a taxonomic concept.

If you wish to capture verbatim determinations then they should be added to CollectionObject#buffered_determinations, i.e. TaxonDeterminations are fully “normalized”.

Constant Summary

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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::ProtocolRelationships

#protocolled?, #reject_protocols

Methods included from Shared::Depictions

#has_depictions?, #image_array=, #reject_depictions, #reject_images

Methods included from Shared::Labels

#labeled?

Methods included from Shared::Confidences

#reject_confidences

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

Methods included from Shared::DataAttributes

#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes

Methods included from Shared::Citations

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

Methods included from Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#day_madeInteger

the day of the month the determination was made

Returns:

  • (Integer)


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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/taxon_determination.rb', line 40

class TaxonDetermination < ApplicationRecord
  acts_as_list scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id], add_new_at: :top # top is 1

  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Labels
  include Shared::Depictions
  include Shared::ProtocolRelationships
  include Shared::DwcOccurrenceHooks
  include SoftValidation
  include Shared::IsData

  ignore_whitespace_on(:print_label)

  belongs_to :otu, inverse_of: :taxon_determinations
  belongs_to :taxon_determination_object, polymorphic: true, inverse_of: :taxon_determinations

  has_many :determiner_roles, class_name: 'Determiner', as: :role_object, inverse_of: :role_object
  has_many :determiners, through: :determiner_roles, source: :person, inverse_of: :taxon_determinations

  has_many :determiners_organization, through: :determiner_roles, source: :organization, inverse_of: :taxon_determinations

  validates :taxon_determination_object, presence: true
  validates :otu, presence: true
  validates :year_made, date_year: { min_year: 1757, max_year: -> {Time.now.year} }
  validates :month_made, date_month: true
  validates :day_made, date_day: {year_sym: :year_made, month_sym: :month_made}, unless: -> {year_made.nil? || month_made.nil?}

  # Careful, position must be reset with :update_column!
  validates_uniqueness_of :position, scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id]

  # TODO: Add uniquiness constraint that also checks roles

  accepts_nested_attributes_for :determiners
  accepts_nested_attributes_for :determiner_roles, allow_destroy: true
  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: :reject_otu

  scope :current, -> { where(position: 1)}
  scope :historical, -> { where.not(position: 1)}

  before_destroy :prevent_if_required

  # TODO: refactor for lib/queries, hack to get
  # Unify base-line functionality
  def self.find_for_autocomplete(params)
    joins(otu: [:taxon_name])
      .where('taxon_names.cached ILIKE ? OR otus.name ILIKE ?',
             "%#{params[:term]}%", "%#{params[:term]}%")
  end

  # @params params [Hash]
  # @params collection_objectt_id [Array, Integer]
  #   an Array or single id
  # @return Hash
  def self.batch_create(collection_object_id, params)
    collection_object_ids = [collection_object_id].flatten.compact.uniq
    result = {
      failed: [],
      total_created: 0
    }

    collection_object_ids.each do |id|
      begin
        TaxonDetermination.create!(
          params.merge(
            taxon_determination_object_id: id,
            taxon_determination_object_type: 'CollectionObject'
          )
        )

        result[:total_created] += 1

      rescue ActiveRecord::RecordInvalid
        result[:failed].push id
        next
      end
    end
    result
  end

  # @return [String]
  def date
    [year_made, month_made, day_made].compact.join('-')
  end

  # @return [Time]
  def sort_date
    Utilities::Dates.nomenclature_date(day_made, month_made, year_made)
  end

  def dwc_occurrences

    return DwcOccurrence.none unless taxon_determination_object.present? # if object is not yet saved don't bother doing this, in theory it will be redundant

    # CollectionObjects

    DwcOccurrence
      .joins("JOIN collection_objects co on dwc_occurrence_object_id = co.id AND dwc_occurrence_object_type = 'CollectionObject'")
      .joins("JOIN taxon_determinations td on co.id = td.taxon_determination_object_id AND td.taxon_determination_object_type = 'CollectionObject'")
      .where(td: {id:} )
      .distinct

    # TODO: FieldOccurrences

  end

  protected

  # @param [Hash] attributed
  # @return [Boolean]
  def reject_otu(attributed)
    attributed['name'].blank? && attributed['taxon_name_id'].blank?
  end

  def prevent_if_required
    unless taxon_determination_object && taxon_determination_object.respond_to?(:ignore_taxon_determination_restriction) && taxon_determination_object.ignore_taxon_determination_restriction
      if !marked_for_destruction? && !new_record? && taxon_determination_object.requires_taxon_determination? && taxon_determination_object.taxon_determinations.count == 1
        errors.add(:base, 'at least one taxon determination is required')
        throw :abort
      end
    end
  end

end

#month_madeInteger

Returns the month the determination was made.

Returns:

  • (Integer)

    the month the determination was made



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/taxon_determination.rb', line 40

class TaxonDetermination < ApplicationRecord
  acts_as_list scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id], add_new_at: :top # top is 1

  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Labels
  include Shared::Depictions
  include Shared::ProtocolRelationships
  include Shared::DwcOccurrenceHooks
  include SoftValidation
  include Shared::IsData

  ignore_whitespace_on(:print_label)

  belongs_to :otu, inverse_of: :taxon_determinations
  belongs_to :taxon_determination_object, polymorphic: true, inverse_of: :taxon_determinations

  has_many :determiner_roles, class_name: 'Determiner', as: :role_object, inverse_of: :role_object
  has_many :determiners, through: :determiner_roles, source: :person, inverse_of: :taxon_determinations

  has_many :determiners_organization, through: :determiner_roles, source: :organization, inverse_of: :taxon_determinations

  validates :taxon_determination_object, presence: true
  validates :otu, presence: true
  validates :year_made, date_year: { min_year: 1757, max_year: -> {Time.now.year} }
  validates :month_made, date_month: true
  validates :day_made, date_day: {year_sym: :year_made, month_sym: :month_made}, unless: -> {year_made.nil? || month_made.nil?}

  # Careful, position must be reset with :update_column!
  validates_uniqueness_of :position, scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id]

  # TODO: Add uniquiness constraint that also checks roles

  accepts_nested_attributes_for :determiners
  accepts_nested_attributes_for :determiner_roles, allow_destroy: true
  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: :reject_otu

  scope :current, -> { where(position: 1)}
  scope :historical, -> { where.not(position: 1)}

  before_destroy :prevent_if_required

  # TODO: refactor for lib/queries, hack to get
  # Unify base-line functionality
  def self.find_for_autocomplete(params)
    joins(otu: [:taxon_name])
      .where('taxon_names.cached ILIKE ? OR otus.name ILIKE ?',
             "%#{params[:term]}%", "%#{params[:term]}%")
  end

  # @params params [Hash]
  # @params collection_objectt_id [Array, Integer]
  #   an Array or single id
  # @return Hash
  def self.batch_create(collection_object_id, params)
    collection_object_ids = [collection_object_id].flatten.compact.uniq
    result = {
      failed: [],
      total_created: 0
    }

    collection_object_ids.each do |id|
      begin
        TaxonDetermination.create!(
          params.merge(
            taxon_determination_object_id: id,
            taxon_determination_object_type: 'CollectionObject'
          )
        )

        result[:total_created] += 1

      rescue ActiveRecord::RecordInvalid
        result[:failed].push id
        next
      end
    end
    result
  end

  # @return [String]
  def date
    [year_made, month_made, day_made].compact.join('-')
  end

  # @return [Time]
  def sort_date
    Utilities::Dates.nomenclature_date(day_made, month_made, year_made)
  end

  def dwc_occurrences

    return DwcOccurrence.none unless taxon_determination_object.present? # if object is not yet saved don't bother doing this, in theory it will be redundant

    # CollectionObjects

    DwcOccurrence
      .joins("JOIN collection_objects co on dwc_occurrence_object_id = co.id AND dwc_occurrence_object_type = 'CollectionObject'")
      .joins("JOIN taxon_determinations td on co.id = td.taxon_determination_object_id AND td.taxon_determination_object_type = 'CollectionObject'")
      .where(td: {id:} )
      .distinct

    # TODO: FieldOccurrences

  end

  protected

  # @param [Hash] attributed
  # @return [Boolean]
  def reject_otu(attributed)
    attributed['name'].blank? && attributed['taxon_name_id'].blank?
  end

  def prevent_if_required
    unless taxon_determination_object && taxon_determination_object.respond_to?(:ignore_taxon_determination_restriction) && taxon_determination_object.ignore_taxon_determination_restriction
      if !marked_for_destruction? && !new_record? && taxon_determination_object.requires_taxon_determination? && taxon_determination_object.taxon_determinations.count == 1
        errors.add(:base, 'at least one taxon determination is required')
        throw :abort
      end
    end
  end

end

#otu_idInteger

the OTU (concept) of the determination

Returns:

  • (Integer)


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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/taxon_determination.rb', line 40

class TaxonDetermination < ApplicationRecord
  acts_as_list scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id], add_new_at: :top # top is 1

  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Labels
  include Shared::Depictions
  include Shared::ProtocolRelationships
  include Shared::DwcOccurrenceHooks
  include SoftValidation
  include Shared::IsData

  ignore_whitespace_on(:print_label)

  belongs_to :otu, inverse_of: :taxon_determinations
  belongs_to :taxon_determination_object, polymorphic: true, inverse_of: :taxon_determinations

  has_many :determiner_roles, class_name: 'Determiner', as: :role_object, inverse_of: :role_object
  has_many :determiners, through: :determiner_roles, source: :person, inverse_of: :taxon_determinations

  has_many :determiners_organization, through: :determiner_roles, source: :organization, inverse_of: :taxon_determinations

  validates :taxon_determination_object, presence: true
  validates :otu, presence: true
  validates :year_made, date_year: { min_year: 1757, max_year: -> {Time.now.year} }
  validates :month_made, date_month: true
  validates :day_made, date_day: {year_sym: :year_made, month_sym: :month_made}, unless: -> {year_made.nil? || month_made.nil?}

  # Careful, position must be reset with :update_column!
  validates_uniqueness_of :position, scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id]

  # TODO: Add uniquiness constraint that also checks roles

  accepts_nested_attributes_for :determiners
  accepts_nested_attributes_for :determiner_roles, allow_destroy: true
  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: :reject_otu

  scope :current, -> { where(position: 1)}
  scope :historical, -> { where.not(position: 1)}

  before_destroy :prevent_if_required

  # TODO: refactor for lib/queries, hack to get
  # Unify base-line functionality
  def self.find_for_autocomplete(params)
    joins(otu: [:taxon_name])
      .where('taxon_names.cached ILIKE ? OR otus.name ILIKE ?',
             "%#{params[:term]}%", "%#{params[:term]}%")
  end

  # @params params [Hash]
  # @params collection_objectt_id [Array, Integer]
  #   an Array or single id
  # @return Hash
  def self.batch_create(collection_object_id, params)
    collection_object_ids = [collection_object_id].flatten.compact.uniq
    result = {
      failed: [],
      total_created: 0
    }

    collection_object_ids.each do |id|
      begin
        TaxonDetermination.create!(
          params.merge(
            taxon_determination_object_id: id,
            taxon_determination_object_type: 'CollectionObject'
          )
        )

        result[:total_created] += 1

      rescue ActiveRecord::RecordInvalid
        result[:failed].push id
        next
      end
    end
    result
  end

  # @return [String]
  def date
    [year_made, month_made, day_made].compact.join('-')
  end

  # @return [Time]
  def sort_date
    Utilities::Dates.nomenclature_date(day_made, month_made, year_made)
  end

  def dwc_occurrences

    return DwcOccurrence.none unless taxon_determination_object.present? # if object is not yet saved don't bother doing this, in theory it will be redundant

    # CollectionObjects

    DwcOccurrence
      .joins("JOIN collection_objects co on dwc_occurrence_object_id = co.id AND dwc_occurrence_object_type = 'CollectionObject'")
      .joins("JOIN taxon_determinations td on co.id = td.taxon_determination_object_id AND td.taxon_determination_object_type = 'CollectionObject'")
      .where(td: {id:} )
      .distinct

    # TODO: FieldOccurrences

  end

  protected

  # @param [Hash] attributed
  # @return [Boolean]
  def reject_otu(attributed)
    attributed['name'].blank? && attributed['taxon_name_id'].blank?
  end

  def prevent_if_required
    unless taxon_determination_object && taxon_determination_object.respond_to?(:ignore_taxon_determination_restriction) && taxon_determination_object.ignore_taxon_determination_restriction
      if !marked_for_destruction? && !new_record? && taxon_determination_object.requires_taxon_determination? && taxon_determination_object.taxon_determinations.count == 1
        errors.add(:base, 'at least one taxon determination is required')
        throw :abort
      end
    end
  end

end

#positionInteger

Returns a cached, field managed by acts_as_list the deterimination of a specimen with position ‘1’ is the accepted determination, it NOT necessarily the most recent determination made.

Returns:

  • (Integer)

    a cached, field managed by acts_as_list the deterimination of a specimen with position ‘1’ is the accepted determination, it NOT necessarily the most recent determination made



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/taxon_determination.rb', line 40

class TaxonDetermination < ApplicationRecord
  acts_as_list scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id], add_new_at: :top # top is 1

  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Labels
  include Shared::Depictions
  include Shared::ProtocolRelationships
  include Shared::DwcOccurrenceHooks
  include SoftValidation
  include Shared::IsData

  ignore_whitespace_on(:print_label)

  belongs_to :otu, inverse_of: :taxon_determinations
  belongs_to :taxon_determination_object, polymorphic: true, inverse_of: :taxon_determinations

  has_many :determiner_roles, class_name: 'Determiner', as: :role_object, inverse_of: :role_object
  has_many :determiners, through: :determiner_roles, source: :person, inverse_of: :taxon_determinations

  has_many :determiners_organization, through: :determiner_roles, source: :organization, inverse_of: :taxon_determinations

  validates :taxon_determination_object, presence: true
  validates :otu, presence: true
  validates :year_made, date_year: { min_year: 1757, max_year: -> {Time.now.year} }
  validates :month_made, date_month: true
  validates :day_made, date_day: {year_sym: :year_made, month_sym: :month_made}, unless: -> {year_made.nil? || month_made.nil?}

  # Careful, position must be reset with :update_column!
  validates_uniqueness_of :position, scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id]

  # TODO: Add uniquiness constraint that also checks roles

  accepts_nested_attributes_for :determiners
  accepts_nested_attributes_for :determiner_roles, allow_destroy: true
  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: :reject_otu

  scope :current, -> { where(position: 1)}
  scope :historical, -> { where.not(position: 1)}

  before_destroy :prevent_if_required

  # TODO: refactor for lib/queries, hack to get
  # Unify base-line functionality
  def self.find_for_autocomplete(params)
    joins(otu: [:taxon_name])
      .where('taxon_names.cached ILIKE ? OR otus.name ILIKE ?',
             "%#{params[:term]}%", "%#{params[:term]}%")
  end

  # @params params [Hash]
  # @params collection_objectt_id [Array, Integer]
  #   an Array or single id
  # @return Hash
  def self.batch_create(collection_object_id, params)
    collection_object_ids = [collection_object_id].flatten.compact.uniq
    result = {
      failed: [],
      total_created: 0
    }

    collection_object_ids.each do |id|
      begin
        TaxonDetermination.create!(
          params.merge(
            taxon_determination_object_id: id,
            taxon_determination_object_type: 'CollectionObject'
          )
        )

        result[:total_created] += 1

      rescue ActiveRecord::RecordInvalid
        result[:failed].push id
        next
      end
    end
    result
  end

  # @return [String]
  def date
    [year_made, month_made, day_made].compact.join('-')
  end

  # @return [Time]
  def sort_date
    Utilities::Dates.nomenclature_date(day_made, month_made, year_made)
  end

  def dwc_occurrences

    return DwcOccurrence.none unless taxon_determination_object.present? # if object is not yet saved don't bother doing this, in theory it will be redundant

    # CollectionObjects

    DwcOccurrence
      .joins("JOIN collection_objects co on dwc_occurrence_object_id = co.id AND dwc_occurrence_object_type = 'CollectionObject'")
      .joins("JOIN taxon_determinations td on co.id = td.taxon_determination_object_id AND td.taxon_determination_object_type = 'CollectionObject'")
      .where(td: {id:} )
      .distinct

    # TODO: FieldOccurrences

  end

  protected

  # @param [Hash] attributed
  # @return [Boolean]
  def reject_otu(attributed)
    attributed['name'].blank? && attributed['taxon_name_id'].blank?
  end

  def prevent_if_required
    unless taxon_determination_object && taxon_determination_object.respond_to?(:ignore_taxon_determination_restriction) && taxon_determination_object.ignore_taxon_determination_restriction
      if !marked_for_destruction? && !new_record? && taxon_determination_object.requires_taxon_determination? && taxon_determination_object.taxon_determinations.count == 1
        errors.add(:base, 'at least one taxon determination is required')
        throw :abort
      end
    end
  end

end

#project_idInteger

the project ID

Returns:

  • (Integer)


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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/taxon_determination.rb', line 40

class TaxonDetermination < ApplicationRecord
  acts_as_list scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id], add_new_at: :top # top is 1

  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Labels
  include Shared::Depictions
  include Shared::ProtocolRelationships
  include Shared::DwcOccurrenceHooks
  include SoftValidation
  include Shared::IsData

  ignore_whitespace_on(:print_label)

  belongs_to :otu, inverse_of: :taxon_determinations
  belongs_to :taxon_determination_object, polymorphic: true, inverse_of: :taxon_determinations

  has_many :determiner_roles, class_name: 'Determiner', as: :role_object, inverse_of: :role_object
  has_many :determiners, through: :determiner_roles, source: :person, inverse_of: :taxon_determinations

  has_many :determiners_organization, through: :determiner_roles, source: :organization, inverse_of: :taxon_determinations

  validates :taxon_determination_object, presence: true
  validates :otu, presence: true
  validates :year_made, date_year: { min_year: 1757, max_year: -> {Time.now.year} }
  validates :month_made, date_month: true
  validates :day_made, date_day: {year_sym: :year_made, month_sym: :month_made}, unless: -> {year_made.nil? || month_made.nil?}

  # Careful, position must be reset with :update_column!
  validates_uniqueness_of :position, scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id]

  # TODO: Add uniquiness constraint that also checks roles

  accepts_nested_attributes_for :determiners
  accepts_nested_attributes_for :determiner_roles, allow_destroy: true
  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: :reject_otu

  scope :current, -> { where(position: 1)}
  scope :historical, -> { where.not(position: 1)}

  before_destroy :prevent_if_required

  # TODO: refactor for lib/queries, hack to get
  # Unify base-line functionality
  def self.find_for_autocomplete(params)
    joins(otu: [:taxon_name])
      .where('taxon_names.cached ILIKE ? OR otus.name ILIKE ?',
             "%#{params[:term]}%", "%#{params[:term]}%")
  end

  # @params params [Hash]
  # @params collection_objectt_id [Array, Integer]
  #   an Array or single id
  # @return Hash
  def self.batch_create(collection_object_id, params)
    collection_object_ids = [collection_object_id].flatten.compact.uniq
    result = {
      failed: [],
      total_created: 0
    }

    collection_object_ids.each do |id|
      begin
        TaxonDetermination.create!(
          params.merge(
            taxon_determination_object_id: id,
            taxon_determination_object_type: 'CollectionObject'
          )
        )

        result[:total_created] += 1

      rescue ActiveRecord::RecordInvalid
        result[:failed].push id
        next
      end
    end
    result
  end

  # @return [String]
  def date
    [year_made, month_made, day_made].compact.join('-')
  end

  # @return [Time]
  def sort_date
    Utilities::Dates.nomenclature_date(day_made, month_made, year_made)
  end

  def dwc_occurrences

    return DwcOccurrence.none unless taxon_determination_object.present? # if object is not yet saved don't bother doing this, in theory it will be redundant

    # CollectionObjects

    DwcOccurrence
      .joins("JOIN collection_objects co on dwc_occurrence_object_id = co.id AND dwc_occurrence_object_type = 'CollectionObject'")
      .joins("JOIN taxon_determinations td on co.id = td.taxon_determination_object_id AND td.taxon_determination_object_type = 'CollectionObject'")
      .where(td: {id:} )
      .distinct

    # TODO: FieldOccurrences

  end

  protected

  # @param [Hash] attributed
  # @return [Boolean]
  def reject_otu(attributed)
    attributed['name'].blank? && attributed['taxon_name_id'].blank?
  end

  def prevent_if_required
    unless taxon_determination_object && taxon_determination_object.respond_to?(:ignore_taxon_determination_restriction) && taxon_determination_object.ignore_taxon_determination_restriction
      if !marked_for_destruction? && !new_record? && taxon_determination_object.requires_taxon_determination? && taxon_determination_object.taxon_determinations.count == 1
        errors.add(:base, 'at least one taxon determination is required')
        throw :abort
      end
    end
  end

end

#taxon_determination_object_idInteger

Returns id of the object being determined.

Returns:

  • (Integer)

    id of the object being determined



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/taxon_determination.rb', line 40

class TaxonDetermination < ApplicationRecord
  acts_as_list scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id], add_new_at: :top # top is 1

  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Labels
  include Shared::Depictions
  include Shared::ProtocolRelationships
  include Shared::DwcOccurrenceHooks
  include SoftValidation
  include Shared::IsData

  ignore_whitespace_on(:print_label)

  belongs_to :otu, inverse_of: :taxon_determinations
  belongs_to :taxon_determination_object, polymorphic: true, inverse_of: :taxon_determinations

  has_many :determiner_roles, class_name: 'Determiner', as: :role_object, inverse_of: :role_object
  has_many :determiners, through: :determiner_roles, source: :person, inverse_of: :taxon_determinations

  has_many :determiners_organization, through: :determiner_roles, source: :organization, inverse_of: :taxon_determinations

  validates :taxon_determination_object, presence: true
  validates :otu, presence: true
  validates :year_made, date_year: { min_year: 1757, max_year: -> {Time.now.year} }
  validates :month_made, date_month: true
  validates :day_made, date_day: {year_sym: :year_made, month_sym: :month_made}, unless: -> {year_made.nil? || month_made.nil?}

  # Careful, position must be reset with :update_column!
  validates_uniqueness_of :position, scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id]

  # TODO: Add uniquiness constraint that also checks roles

  accepts_nested_attributes_for :determiners
  accepts_nested_attributes_for :determiner_roles, allow_destroy: true
  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: :reject_otu

  scope :current, -> { where(position: 1)}
  scope :historical, -> { where.not(position: 1)}

  before_destroy :prevent_if_required

  # TODO: refactor for lib/queries, hack to get
  # Unify base-line functionality
  def self.find_for_autocomplete(params)
    joins(otu: [:taxon_name])
      .where('taxon_names.cached ILIKE ? OR otus.name ILIKE ?',
             "%#{params[:term]}%", "%#{params[:term]}%")
  end

  # @params params [Hash]
  # @params collection_objectt_id [Array, Integer]
  #   an Array or single id
  # @return Hash
  def self.batch_create(collection_object_id, params)
    collection_object_ids = [collection_object_id].flatten.compact.uniq
    result = {
      failed: [],
      total_created: 0
    }

    collection_object_ids.each do |id|
      begin
        TaxonDetermination.create!(
          params.merge(
            taxon_determination_object_id: id,
            taxon_determination_object_type: 'CollectionObject'
          )
        )

        result[:total_created] += 1

      rescue ActiveRecord::RecordInvalid
        result[:failed].push id
        next
      end
    end
    result
  end

  # @return [String]
  def date
    [year_made, month_made, day_made].compact.join('-')
  end

  # @return [Time]
  def sort_date
    Utilities::Dates.nomenclature_date(day_made, month_made, year_made)
  end

  def dwc_occurrences

    return DwcOccurrence.none unless taxon_determination_object.present? # if object is not yet saved don't bother doing this, in theory it will be redundant

    # CollectionObjects

    DwcOccurrence
      .joins("JOIN collection_objects co on dwc_occurrence_object_id = co.id AND dwc_occurrence_object_type = 'CollectionObject'")
      .joins("JOIN taxon_determinations td on co.id = td.taxon_determination_object_id AND td.taxon_determination_object_type = 'CollectionObject'")
      .where(td: {id:} )
      .distinct

    # TODO: FieldOccurrences

  end

  protected

  # @param [Hash] attributed
  # @return [Boolean]
  def reject_otu(attributed)
    attributed['name'].blank? && attributed['taxon_name_id'].blank?
  end

  def prevent_if_required
    unless taxon_determination_object && taxon_determination_object.respond_to?(:ignore_taxon_determination_restriction) && taxon_determination_object.ignore_taxon_determination_restriction
      if !marked_for_destruction? && !new_record? && taxon_determination_object.requires_taxon_determination? && taxon_determination_object.taxon_determinations.count == 1
        errors.add(:base, 'at least one taxon determination is required')
        throw :abort
      end
    end
  end

end

#taxon_determination_object_typeInteger

Returns type of the object being determined.

Returns:

  • (Integer)

    type of the object being determined



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/taxon_determination.rb', line 40

class TaxonDetermination < ApplicationRecord
  acts_as_list scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id], add_new_at: :top # top is 1

  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Labels
  include Shared::Depictions
  include Shared::ProtocolRelationships
  include Shared::DwcOccurrenceHooks
  include SoftValidation
  include Shared::IsData

  ignore_whitespace_on(:print_label)

  belongs_to :otu, inverse_of: :taxon_determinations
  belongs_to :taxon_determination_object, polymorphic: true, inverse_of: :taxon_determinations

  has_many :determiner_roles, class_name: 'Determiner', as: :role_object, inverse_of: :role_object
  has_many :determiners, through: :determiner_roles, source: :person, inverse_of: :taxon_determinations

  has_many :determiners_organization, through: :determiner_roles, source: :organization, inverse_of: :taxon_determinations

  validates :taxon_determination_object, presence: true
  validates :otu, presence: true
  validates :year_made, date_year: { min_year: 1757, max_year: -> {Time.now.year} }
  validates :month_made, date_month: true
  validates :day_made, date_day: {year_sym: :year_made, month_sym: :month_made}, unless: -> {year_made.nil? || month_made.nil?}

  # Careful, position must be reset with :update_column!
  validates_uniqueness_of :position, scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id]

  # TODO: Add uniquiness constraint that also checks roles

  accepts_nested_attributes_for :determiners
  accepts_nested_attributes_for :determiner_roles, allow_destroy: true
  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: :reject_otu

  scope :current, -> { where(position: 1)}
  scope :historical, -> { where.not(position: 1)}

  before_destroy :prevent_if_required

  # TODO: refactor for lib/queries, hack to get
  # Unify base-line functionality
  def self.find_for_autocomplete(params)
    joins(otu: [:taxon_name])
      .where('taxon_names.cached ILIKE ? OR otus.name ILIKE ?',
             "%#{params[:term]}%", "%#{params[:term]}%")
  end

  # @params params [Hash]
  # @params collection_objectt_id [Array, Integer]
  #   an Array or single id
  # @return Hash
  def self.batch_create(collection_object_id, params)
    collection_object_ids = [collection_object_id].flatten.compact.uniq
    result = {
      failed: [],
      total_created: 0
    }

    collection_object_ids.each do |id|
      begin
        TaxonDetermination.create!(
          params.merge(
            taxon_determination_object_id: id,
            taxon_determination_object_type: 'CollectionObject'
          )
        )

        result[:total_created] += 1

      rescue ActiveRecord::RecordInvalid
        result[:failed].push id
        next
      end
    end
    result
  end

  # @return [String]
  def date
    [year_made, month_made, day_made].compact.join('-')
  end

  # @return [Time]
  def sort_date
    Utilities::Dates.nomenclature_date(day_made, month_made, year_made)
  end

  def dwc_occurrences

    return DwcOccurrence.none unless taxon_determination_object.present? # if object is not yet saved don't bother doing this, in theory it will be redundant

    # CollectionObjects

    DwcOccurrence
      .joins("JOIN collection_objects co on dwc_occurrence_object_id = co.id AND dwc_occurrence_object_type = 'CollectionObject'")
      .joins("JOIN taxon_determinations td on co.id = td.taxon_determination_object_id AND td.taxon_determination_object_type = 'CollectionObject'")
      .where(td: {id:} )
      .distinct

    # TODO: FieldOccurrences

  end

  protected

  # @param [Hash] attributed
  # @return [Boolean]
  def reject_otu(attributed)
    attributed['name'].blank? && attributed['taxon_name_id'].blank?
  end

  def prevent_if_required
    unless taxon_determination_object && taxon_determination_object.respond_to?(:ignore_taxon_determination_restriction) && taxon_determination_object.ignore_taxon_determination_restriction
      if !marked_for_destruction? && !new_record? && taxon_determination_object.requires_taxon_determination? && taxon_determination_object.taxon_determinations.count == 1
        errors.add(:base, 'at least one taxon determination is required')
        throw :abort
      end
    end
  end

end

#year_madeInteger

Returns the 4 digit year the determination was made.

Returns:

  • (Integer)

    the 4 digit year the determination was made



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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'app/models/taxon_determination.rb', line 40

class TaxonDetermination < ApplicationRecord
  acts_as_list scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id], add_new_at: :top # top is 1

  include Housekeeping
  include Shared::Citations
  include Shared::DataAttributes
  include Shared::Notes
  include Shared::Confidences
  include Shared::Labels
  include Shared::Depictions
  include Shared::ProtocolRelationships
  include Shared::DwcOccurrenceHooks
  include SoftValidation
  include Shared::IsData

  ignore_whitespace_on(:print_label)

  belongs_to :otu, inverse_of: :taxon_determinations
  belongs_to :taxon_determination_object, polymorphic: true, inverse_of: :taxon_determinations

  has_many :determiner_roles, class_name: 'Determiner', as: :role_object, inverse_of: :role_object
  has_many :determiners, through: :determiner_roles, source: :person, inverse_of: :taxon_determinations

  has_many :determiners_organization, through: :determiner_roles, source: :organization, inverse_of: :taxon_determinations

  validates :taxon_determination_object, presence: true
  validates :otu, presence: true
  validates :year_made, date_year: { min_year: 1757, max_year: -> {Time.now.year} }
  validates :month_made, date_month: true
  validates :day_made, date_day: {year_sym: :year_made, month_sym: :month_made}, unless: -> {year_made.nil? || month_made.nil?}

  # Careful, position must be reset with :update_column!
  validates_uniqueness_of :position, scope: [:taxon_determination_object_id, :taxon_determination_object_type, :project_id]

  # TODO: Add uniquiness constraint that also checks roles

  accepts_nested_attributes_for :determiners
  accepts_nested_attributes_for :determiner_roles, allow_destroy: true
  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: :reject_otu

  scope :current, -> { where(position: 1)}
  scope :historical, -> { where.not(position: 1)}

  before_destroy :prevent_if_required

  # TODO: refactor for lib/queries, hack to get
  # Unify base-line functionality
  def self.find_for_autocomplete(params)
    joins(otu: [:taxon_name])
      .where('taxon_names.cached ILIKE ? OR otus.name ILIKE ?',
             "%#{params[:term]}%", "%#{params[:term]}%")
  end

  # @params params [Hash]
  # @params collection_objectt_id [Array, Integer]
  #   an Array or single id
  # @return Hash
  def self.batch_create(collection_object_id, params)
    collection_object_ids = [collection_object_id].flatten.compact.uniq
    result = {
      failed: [],
      total_created: 0
    }

    collection_object_ids.each do |id|
      begin
        TaxonDetermination.create!(
          params.merge(
            taxon_determination_object_id: id,
            taxon_determination_object_type: 'CollectionObject'
          )
        )

        result[:total_created] += 1

      rescue ActiveRecord::RecordInvalid
        result[:failed].push id
        next
      end
    end
    result
  end

  # @return [String]
  def date
    [year_made, month_made, day_made].compact.join('-')
  end

  # @return [Time]
  def sort_date
    Utilities::Dates.nomenclature_date(day_made, month_made, year_made)
  end

  def dwc_occurrences

    return DwcOccurrence.none unless taxon_determination_object.present? # if object is not yet saved don't bother doing this, in theory it will be redundant

    # CollectionObjects

    DwcOccurrence
      .joins("JOIN collection_objects co on dwc_occurrence_object_id = co.id AND dwc_occurrence_object_type = 'CollectionObject'")
      .joins("JOIN taxon_determinations td on co.id = td.taxon_determination_object_id AND td.taxon_determination_object_type = 'CollectionObject'")
      .where(td: {id:} )
      .distinct

    # TODO: FieldOccurrences

  end

  protected

  # @param [Hash] attributed
  # @return [Boolean]
  def reject_otu(attributed)
    attributed['name'].blank? && attributed['taxon_name_id'].blank?
  end

  def prevent_if_required
    unless taxon_determination_object && taxon_determination_object.respond_to?(:ignore_taxon_determination_restriction) && taxon_determination_object.ignore_taxon_determination_restriction
      if !marked_for_destruction? && !new_record? && taxon_determination_object.requires_taxon_determination? && taxon_determination_object.taxon_determinations.count == 1
        errors.add(:base, 'at least one taxon determination is required')
        throw :abort
      end
    end
  end

end

Class Method Details

.batch_create(collection_object_id, params) ⇒ Object

Returns Hash.

Returns:

  • Hash



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
# File 'app/models/taxon_determination.rb', line 97

def self.batch_create(collection_object_id, params)
  collection_object_ids = [collection_object_id].flatten.compact.uniq
  result = {
    failed: [],
    total_created: 0
  }

  collection_object_ids.each do |id|
    begin
      TaxonDetermination.create!(
        params.merge(
          taxon_determination_object_id: id,
          taxon_determination_object_type: 'CollectionObject'
        )
      )

      result[:total_created] += 1

    rescue ActiveRecord::RecordInvalid
      result[:failed].push id
      next
    end
  end
  result
end

.find_for_autocomplete(params) ⇒ Object

TODO: refactor for lib/queries, hack to get Unify base-line functionality



87
88
89
90
91
# File 'app/models/taxon_determination.rb', line 87

def self.find_for_autocomplete(params)
  joins(otu: [:taxon_name])
    .where('taxon_names.cached ILIKE ? OR otus.name ILIKE ?',
           "%#{params[:term]}%", "%#{params[:term]}%")
end

Instance Method Details

#dateString

Returns:

  • (String)


124
125
126
# File 'app/models/taxon_determination.rb', line 124

def date
  [year_made, month_made, day_made].compact.join('-')
end

#dwc_occurrencesObject



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
# File 'app/models/taxon_determination.rb', line 133

def dwc_occurrences

  return DwcOccurrence.none unless taxon_determination_object.present? # if object is not yet saved don't bother doing this, in theory it will be redundant

  # CollectionObjects

  DwcOccurrence
    .joins("JOIN collection_objects co on dwc_occurrence_object_id = co.id AND dwc_occurrence_object_type = 'CollectionObject'")
    .joins("JOIN taxon_determinations td on co.id = td.taxon_determination_object_id AND td.taxon_determination_object_type = 'CollectionObject'")
    .where(td: {id:} )
    .distinct

  # TODO: FieldOccurrences

end

#prevent_if_requiredObject (protected)



157
158
159
160
161
162
163
164
# File 'app/models/taxon_determination.rb', line 157

def prevent_if_required
  unless taxon_determination_object && taxon_determination_object.respond_to?(:ignore_taxon_determination_restriction) && taxon_determination_object.ignore_taxon_determination_restriction
    if !marked_for_destruction? && !new_record? && taxon_determination_object.requires_taxon_determination? && taxon_determination_object.taxon_determinations.count == 1
      errors.add(:base, 'at least one taxon determination is required')
      throw :abort
    end
  end
end

#reject_otu(attributed) ⇒ Boolean (protected)

Parameters:

  • attributed (Hash)

Returns:

  • (Boolean)


153
154
155
# File 'app/models/taxon_determination.rb', line 153

def reject_otu(attributed)
  attributed['name'].blank? && attributed['taxon_name_id'].blank?
end

#sort_dateTime

Returns:

  • (Time)


129
130
131
# File 'app/models/taxon_determination.rb', line 129

def sort_date
  Utilities::Dates.nomenclature_date(day_made, month_made, year_made)
end