Class: AssertedDistribution

Overview

An AssertedDistribution is the Source-backed assertion that a taxon (OTU) is present in some *spatial area*. It requires a Citation indicating where/who made the assertion. In TaxonWorks the areas are drawn from GeographicAreas.

AssertedDistributions can be asserts that the source indicates that a taxon is NOT present in an area. This is a “positive negative” in , i.e. the Source can be thought of recording evidence that a taxon is not present. TaxonWorks does not differentiate between types of negative evidence.

Defined Under Namespace

Modules: DwcExtensions

Constant Summary

Constants included from DwcExtensions

DwcExtensions::DWC_OCCURRENCE_MAP

Constants included from Shared::IsDwcOccurrence

Shared::IsDwcOccurrence::DWC_DELIMITER, Shared::IsDwcOccurrence::VIEW_EXCLUSIONS

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

#query_update

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 DwcExtensions

#dwc_associated_references, #dwc_country, #dwc_county, #dwc_family, #dwc_genus, #dwc_infraspecific_epithet, #dwc_kingdom, #dwc_occurrence_status, #dwc_scientific_name, #dwc_specific_epithet, #dwc_state_province, #dwc_taxon_name_authorship, #dwc_taxon_rank

Methods included from Shared::IsDwcOccurrence

#dwc_occurrence_attribute_values, #dwc_occurrence_attributes, #dwc_occurrence_id, #get_dwc_occurrence, #set_dwc_occurrence

Methods included from Shared::HasPapertrail

#attribute_updated, #attribute_updater

Methods included from Shared::Identifiers

#dwc_occurrence_id, #identified?, #next_by_identifier, #previous_by_identifier, #reject_identifiers, #uri, #uuid

Methods included from Shared::OriginRelationship

#new_objects, #old_objects, #reject_origin_relationships, #set_origin

Methods included from Shared::Confidences

#reject_confidences

Methods included from Shared::Citations

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

Methods included from Shared::DataAttributes

#import_attributes, #internal_attributes, #keyword_value_hash, #reject_data_attributes

Methods included from Shared::Tags

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

Methods included from Shared::Notes

#concatenated_notes_string, #reject_notes

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 Housekeeping

#has_polymorphic_relationship?

Methods inherited from ApplicationRecord

transaction_with_retry

Instance Attribute Details

#geographic_area_idInteger

the geographic area ID

Returns:

  • (Integer)


22
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/models/asserted_distribution.rb', line 22

class AssertedDistribution < ApplicationRecord
  include Housekeeping
  include SoftValidation
  include Shared::Notes
  include Shared::Tags
  include Shared::DataAttributes # why?
  include Shared::CitationRequired # !! must preceed Shared::Citations
  include Shared::Citations
  include Shared::Confidences
  include Shared::OriginRelationship
  include Shared::Identifiers
  include Shared::HasPapertrail
  include Shared::Taxonomy # at present must preceed IsDwcOccurence
  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions
  include Shared::IsData

  include Shared::Maps
  include Shared::QueryBatchUpdate

  originates_from 'Specimen', 'Lot'

  # @return [Hash]
  #   of known country/state/county values
  attr_accessor :geographic_names

  belongs_to :otu, inverse_of: :asserted_distributions
  has_one :taxon_name, through: :otu
  belongs_to :geographic_area, inverse_of: :asserted_distributions

  has_one :geographic_item, through: :geographic_area, source: :default_geographic_item
  has_many :geographic_items, through: :geographic_area

  validates_presence_of :geographic_area_id, message: 'geographic area is not selected'
  validates :geographic_area, presence: true
  validates :otu, presence: true
  validates_uniqueness_of :geographic_area_id, scope: [:project_id, :otu_id, :is_absent], message: 'this geographic_area, OTU and present/absent combination already exists'
  validate :new_records_include_citation

  # TODO: deprecate scopes referencing single wheres
  scope :with_otu_id, -> (otu_id) { where(otu_id:) }
  scope :with_is_absent, -> { where('is_absent = true') }
  scope :with_geographic_area_array, -> (geographic_area_array) { where('geographic_area_id IN (?)', geographic_area_array) }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }

  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: proc { |attributes| attributes['name'].blank? && attributes['taxon_name_id'].blank? }

  soft_validate(:sv_conflicting_geographic_area, set: :conflicting_geographic_area, name: 'conflicting geographic area', description: 'conflicting geographic area')

  # getter for attr :geographic_names
  def geographic_names
    return @geographic_names if !@geographic_names.nil?
    @geographic_names ||= geographic_area.geographic_name_classification.delete_if{|k,v| v.nil?}
    @geographic_names ||= {}
  end

  # @param [Hash] defaults
  # @return [AssertedDistribution]
  #   used to also stub an #origin_citation, as required
  def self.stub(defaults: {})
    a = AssertedDistribution.new(
      otu_id: defaults[:otu_id],
      origin_citation_attributes: {source_id: defaults[:source_id]})
    a.origin_citation = Citation.new if defaults[:source_id].blank?
    a
  end

  # rubocop:disable Style/StringHashKeys
  # TODO: DRY with helper methods
  # @return [Hash] GeoJSON feature
  def to_geo_json_feature
    retval = {
      'type' => 'Feature',
      'geometry' => RGeo::GeoJSON.encode(self.geographic_area.geographic_items.first.geo_object),
      'properties' => {'asserted_distribution' => {'id' => self.id}}
    }
    retval
  end

  # rubocop:enable Style/StringHashKeys

  # @return [True]
  #   see citable.rb
  def requires_citation?
    true
  end

  def geographic_item
    geographic_area.default_geographic_item
  end

  def has_shape?
    geographic_area.geographic_items.any?
  end

  def self.batch_update(params)
    request = QueryBatchRequest.new(
      async_cutoff: params[:async_cutoff] || 26,
      klass: 'AssertedDistribution',
      object_filter_params: params[:asserted_distribution_query],
      object_params: params[:asserted_distribution],
      preview: params[:preview],
    )

    a = request.filter

    v1 = a.all.distinct.limit(2).pluck(:geographic_area_id).uniq.count
    v2 = a.all.distinct.limit(2).pluck(:otu_id).uniq.count

    cap = 0

    if v1 > 1 && v2 > 1 # many otus, many geographic areas
      cap = 0
      request.cap_reason = 'Records include multiple OTUs *and* multiple geographic areas.'
    elsif v1 > 1
      cap = 0
      request.cap_reason = 'May not update multiple geographic areas to one.' # TODO: revist constraint
    else
      cap = 2000
    end

    request.cap = cap

    query_batch_update(request)
  end

  protected

  # @return [Boolean]
  def new_records_include_citation
    if new_record? && source.blank? && origin_citation.blank? && !citations.any?
      errors.add(:base, 'required citation is not provided')
    end
  end

  # @return [Boolean]
  def sv_conflicting_geographic_area
    unless geographic_area.nil?
      areas = [geographic_area.level0_id, geographic_area.level1_id, geographic_area.level2_id].compact
      if is_absent # this returns an array, not a single GA so test below is not right
        presence = AssertedDistribution
          .without_is_absent
          .with_geographic_area_array(areas)
          .where(otu_id:)
        soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
      else
        presence = AssertedDistribution
          .with_is_absent
          .where(otu_id:)
          .with_geographic_area_array(areas)
        soft_validations.add(:geographic_area_id, "Taxon is reported as missing in #{presence.first.geographic_area.name}") unless presence.empty?
      end
    end
  end

  # @param [Hash] options of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return [Array] an array of AssertedDistributions
  def self.stub_new(options = {})
    options.symbolize_keys!
    result = []
    options[:geographic_areas].each do |ga|
      result.push(
        AssertedDistribution.new(
          otu_id: options[:otu_id],
          geographic_area: ga,
          origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end
end

#geographic_namesObject

getter for attr :geographic_names



46
47
48
# File 'app/models/asserted_distribution.rb', line 46

def geographic_names
  @geographic_names
end

#is_absentBoolean

Returns a positive negative, when true then there exists an assertion that the taxon is not present in the spatial area.

Returns:

  • (Boolean)

    a positive negative, when true then there exists an assertion that the taxon is not present in the spatial area



22
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/models/asserted_distribution.rb', line 22

class AssertedDistribution < ApplicationRecord
  include Housekeeping
  include SoftValidation
  include Shared::Notes
  include Shared::Tags
  include Shared::DataAttributes # why?
  include Shared::CitationRequired # !! must preceed Shared::Citations
  include Shared::Citations
  include Shared::Confidences
  include Shared::OriginRelationship
  include Shared::Identifiers
  include Shared::HasPapertrail
  include Shared::Taxonomy # at present must preceed IsDwcOccurence
  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions
  include Shared::IsData

  include Shared::Maps
  include Shared::QueryBatchUpdate

  originates_from 'Specimen', 'Lot'

  # @return [Hash]
  #   of known country/state/county values
  attr_accessor :geographic_names

  belongs_to :otu, inverse_of: :asserted_distributions
  has_one :taxon_name, through: :otu
  belongs_to :geographic_area, inverse_of: :asserted_distributions

  has_one :geographic_item, through: :geographic_area, source: :default_geographic_item
  has_many :geographic_items, through: :geographic_area

  validates_presence_of :geographic_area_id, message: 'geographic area is not selected'
  validates :geographic_area, presence: true
  validates :otu, presence: true
  validates_uniqueness_of :geographic_area_id, scope: [:project_id, :otu_id, :is_absent], message: 'this geographic_area, OTU and present/absent combination already exists'
  validate :new_records_include_citation

  # TODO: deprecate scopes referencing single wheres
  scope :with_otu_id, -> (otu_id) { where(otu_id:) }
  scope :with_is_absent, -> { where('is_absent = true') }
  scope :with_geographic_area_array, -> (geographic_area_array) { where('geographic_area_id IN (?)', geographic_area_array) }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }

  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: proc { |attributes| attributes['name'].blank? && attributes['taxon_name_id'].blank? }

  soft_validate(:sv_conflicting_geographic_area, set: :conflicting_geographic_area, name: 'conflicting geographic area', description: 'conflicting geographic area')

  # getter for attr :geographic_names
  def geographic_names
    return @geographic_names if !@geographic_names.nil?
    @geographic_names ||= geographic_area.geographic_name_classification.delete_if{|k,v| v.nil?}
    @geographic_names ||= {}
  end

  # @param [Hash] defaults
  # @return [AssertedDistribution]
  #   used to also stub an #origin_citation, as required
  def self.stub(defaults: {})
    a = AssertedDistribution.new(
      otu_id: defaults[:otu_id],
      origin_citation_attributes: {source_id: defaults[:source_id]})
    a.origin_citation = Citation.new if defaults[:source_id].blank?
    a
  end

  # rubocop:disable Style/StringHashKeys
  # TODO: DRY with helper methods
  # @return [Hash] GeoJSON feature
  def to_geo_json_feature
    retval = {
      'type' => 'Feature',
      'geometry' => RGeo::GeoJSON.encode(self.geographic_area.geographic_items.first.geo_object),
      'properties' => {'asserted_distribution' => {'id' => self.id}}
    }
    retval
  end

  # rubocop:enable Style/StringHashKeys

  # @return [True]
  #   see citable.rb
  def requires_citation?
    true
  end

  def geographic_item
    geographic_area.default_geographic_item
  end

  def has_shape?
    geographic_area.geographic_items.any?
  end

  def self.batch_update(params)
    request = QueryBatchRequest.new(
      async_cutoff: params[:async_cutoff] || 26,
      klass: 'AssertedDistribution',
      object_filter_params: params[:asserted_distribution_query],
      object_params: params[:asserted_distribution],
      preview: params[:preview],
    )

    a = request.filter

    v1 = a.all.distinct.limit(2).pluck(:geographic_area_id).uniq.count
    v2 = a.all.distinct.limit(2).pluck(:otu_id).uniq.count

    cap = 0

    if v1 > 1 && v2 > 1 # many otus, many geographic areas
      cap = 0
      request.cap_reason = 'Records include multiple OTUs *and* multiple geographic areas.'
    elsif v1 > 1
      cap = 0
      request.cap_reason = 'May not update multiple geographic areas to one.' # TODO: revist constraint
    else
      cap = 2000
    end

    request.cap = cap

    query_batch_update(request)
  end

  protected

  # @return [Boolean]
  def new_records_include_citation
    if new_record? && source.blank? && origin_citation.blank? && !citations.any?
      errors.add(:base, 'required citation is not provided')
    end
  end

  # @return [Boolean]
  def sv_conflicting_geographic_area
    unless geographic_area.nil?
      areas = [geographic_area.level0_id, geographic_area.level1_id, geographic_area.level2_id].compact
      if is_absent # this returns an array, not a single GA so test below is not right
        presence = AssertedDistribution
          .without_is_absent
          .with_geographic_area_array(areas)
          .where(otu_id:)
        soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
      else
        presence = AssertedDistribution
          .with_is_absent
          .where(otu_id:)
          .with_geographic_area_array(areas)
        soft_validations.add(:geographic_area_id, "Taxon is reported as missing in #{presence.first.geographic_area.name}") unless presence.empty?
      end
    end
  end

  # @param [Hash] options of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return [Array] an array of AssertedDistributions
  def self.stub_new(options = {})
    options.symbolize_keys!
    result = []
    options[:geographic_areas].each do |ga|
      result.push(
        AssertedDistribution.new(
          otu_id: options[:otu_id],
          geographic_area: ga,
          origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end
end

#otu_idInteger

the OTU ID

Returns:

  • (Integer)


22
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/models/asserted_distribution.rb', line 22

class AssertedDistribution < ApplicationRecord
  include Housekeeping
  include SoftValidation
  include Shared::Notes
  include Shared::Tags
  include Shared::DataAttributes # why?
  include Shared::CitationRequired # !! must preceed Shared::Citations
  include Shared::Citations
  include Shared::Confidences
  include Shared::OriginRelationship
  include Shared::Identifiers
  include Shared::HasPapertrail
  include Shared::Taxonomy # at present must preceed IsDwcOccurence
  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions
  include Shared::IsData

  include Shared::Maps
  include Shared::QueryBatchUpdate

  originates_from 'Specimen', 'Lot'

  # @return [Hash]
  #   of known country/state/county values
  attr_accessor :geographic_names

  belongs_to :otu, inverse_of: :asserted_distributions
  has_one :taxon_name, through: :otu
  belongs_to :geographic_area, inverse_of: :asserted_distributions

  has_one :geographic_item, through: :geographic_area, source: :default_geographic_item
  has_many :geographic_items, through: :geographic_area

  validates_presence_of :geographic_area_id, message: 'geographic area is not selected'
  validates :geographic_area, presence: true
  validates :otu, presence: true
  validates_uniqueness_of :geographic_area_id, scope: [:project_id, :otu_id, :is_absent], message: 'this geographic_area, OTU and present/absent combination already exists'
  validate :new_records_include_citation

  # TODO: deprecate scopes referencing single wheres
  scope :with_otu_id, -> (otu_id) { where(otu_id:) }
  scope :with_is_absent, -> { where('is_absent = true') }
  scope :with_geographic_area_array, -> (geographic_area_array) { where('geographic_area_id IN (?)', geographic_area_array) }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }

  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: proc { |attributes| attributes['name'].blank? && attributes['taxon_name_id'].blank? }

  soft_validate(:sv_conflicting_geographic_area, set: :conflicting_geographic_area, name: 'conflicting geographic area', description: 'conflicting geographic area')

  # getter for attr :geographic_names
  def geographic_names
    return @geographic_names if !@geographic_names.nil?
    @geographic_names ||= geographic_area.geographic_name_classification.delete_if{|k,v| v.nil?}
    @geographic_names ||= {}
  end

  # @param [Hash] defaults
  # @return [AssertedDistribution]
  #   used to also stub an #origin_citation, as required
  def self.stub(defaults: {})
    a = AssertedDistribution.new(
      otu_id: defaults[:otu_id],
      origin_citation_attributes: {source_id: defaults[:source_id]})
    a.origin_citation = Citation.new if defaults[:source_id].blank?
    a
  end

  # rubocop:disable Style/StringHashKeys
  # TODO: DRY with helper methods
  # @return [Hash] GeoJSON feature
  def to_geo_json_feature
    retval = {
      'type' => 'Feature',
      'geometry' => RGeo::GeoJSON.encode(self.geographic_area.geographic_items.first.geo_object),
      'properties' => {'asserted_distribution' => {'id' => self.id}}
    }
    retval
  end

  # rubocop:enable Style/StringHashKeys

  # @return [True]
  #   see citable.rb
  def requires_citation?
    true
  end

  def geographic_item
    geographic_area.default_geographic_item
  end

  def has_shape?
    geographic_area.geographic_items.any?
  end

  def self.batch_update(params)
    request = QueryBatchRequest.new(
      async_cutoff: params[:async_cutoff] || 26,
      klass: 'AssertedDistribution',
      object_filter_params: params[:asserted_distribution_query],
      object_params: params[:asserted_distribution],
      preview: params[:preview],
    )

    a = request.filter

    v1 = a.all.distinct.limit(2).pluck(:geographic_area_id).uniq.count
    v2 = a.all.distinct.limit(2).pluck(:otu_id).uniq.count

    cap = 0

    if v1 > 1 && v2 > 1 # many otus, many geographic areas
      cap = 0
      request.cap_reason = 'Records include multiple OTUs *and* multiple geographic areas.'
    elsif v1 > 1
      cap = 0
      request.cap_reason = 'May not update multiple geographic areas to one.' # TODO: revist constraint
    else
      cap = 2000
    end

    request.cap = cap

    query_batch_update(request)
  end

  protected

  # @return [Boolean]
  def new_records_include_citation
    if new_record? && source.blank? && origin_citation.blank? && !citations.any?
      errors.add(:base, 'required citation is not provided')
    end
  end

  # @return [Boolean]
  def sv_conflicting_geographic_area
    unless geographic_area.nil?
      areas = [geographic_area.level0_id, geographic_area.level1_id, geographic_area.level2_id].compact
      if is_absent # this returns an array, not a single GA so test below is not right
        presence = AssertedDistribution
          .without_is_absent
          .with_geographic_area_array(areas)
          .where(otu_id:)
        soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
      else
        presence = AssertedDistribution
          .with_is_absent
          .where(otu_id:)
          .with_geographic_area_array(areas)
        soft_validations.add(:geographic_area_id, "Taxon is reported as missing in #{presence.first.geographic_area.name}") unless presence.empty?
      end
    end
  end

  # @param [Hash] options of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return [Array] an array of AssertedDistributions
  def self.stub_new(options = {})
    options.symbolize_keys!
    result = []
    options[:geographic_areas].each do |ga|
      result.push(
        AssertedDistribution.new(
          otu_id: options[:otu_id],
          geographic_area: ga,
          origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end
end

#project_idInteger

the project ID

Returns:

  • (Integer)


22
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'app/models/asserted_distribution.rb', line 22

class AssertedDistribution < ApplicationRecord
  include Housekeeping
  include SoftValidation
  include Shared::Notes
  include Shared::Tags
  include Shared::DataAttributes # why?
  include Shared::CitationRequired # !! must preceed Shared::Citations
  include Shared::Citations
  include Shared::Confidences
  include Shared::OriginRelationship
  include Shared::Identifiers
  include Shared::HasPapertrail
  include Shared::Taxonomy # at present must preceed IsDwcOccurence
  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions
  include Shared::IsData

  include Shared::Maps
  include Shared::QueryBatchUpdate

  originates_from 'Specimen', 'Lot'

  # @return [Hash]
  #   of known country/state/county values
  attr_accessor :geographic_names

  belongs_to :otu, inverse_of: :asserted_distributions
  has_one :taxon_name, through: :otu
  belongs_to :geographic_area, inverse_of: :asserted_distributions

  has_one :geographic_item, through: :geographic_area, source: :default_geographic_item
  has_many :geographic_items, through: :geographic_area

  validates_presence_of :geographic_area_id, message: 'geographic area is not selected'
  validates :geographic_area, presence: true
  validates :otu, presence: true
  validates_uniqueness_of :geographic_area_id, scope: [:project_id, :otu_id, :is_absent], message: 'this geographic_area, OTU and present/absent combination already exists'
  validate :new_records_include_citation

  # TODO: deprecate scopes referencing single wheres
  scope :with_otu_id, -> (otu_id) { where(otu_id:) }
  scope :with_is_absent, -> { where('is_absent = true') }
  scope :with_geographic_area_array, -> (geographic_area_array) { where('geographic_area_id IN (?)', geographic_area_array) }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }

  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: proc { |attributes| attributes['name'].blank? && attributes['taxon_name_id'].blank? }

  soft_validate(:sv_conflicting_geographic_area, set: :conflicting_geographic_area, name: 'conflicting geographic area', description: 'conflicting geographic area')

  # getter for attr :geographic_names
  def geographic_names
    return @geographic_names if !@geographic_names.nil?
    @geographic_names ||= geographic_area.geographic_name_classification.delete_if{|k,v| v.nil?}
    @geographic_names ||= {}
  end

  # @param [Hash] defaults
  # @return [AssertedDistribution]
  #   used to also stub an #origin_citation, as required
  def self.stub(defaults: {})
    a = AssertedDistribution.new(
      otu_id: defaults[:otu_id],
      origin_citation_attributes: {source_id: defaults[:source_id]})
    a.origin_citation = Citation.new if defaults[:source_id].blank?
    a
  end

  # rubocop:disable Style/StringHashKeys
  # TODO: DRY with helper methods
  # @return [Hash] GeoJSON feature
  def to_geo_json_feature
    retval = {
      'type' => 'Feature',
      'geometry' => RGeo::GeoJSON.encode(self.geographic_area.geographic_items.first.geo_object),
      'properties' => {'asserted_distribution' => {'id' => self.id}}
    }
    retval
  end

  # rubocop:enable Style/StringHashKeys

  # @return [True]
  #   see citable.rb
  def requires_citation?
    true
  end

  def geographic_item
    geographic_area.default_geographic_item
  end

  def has_shape?
    geographic_area.geographic_items.any?
  end

  def self.batch_update(params)
    request = QueryBatchRequest.new(
      async_cutoff: params[:async_cutoff] || 26,
      klass: 'AssertedDistribution',
      object_filter_params: params[:asserted_distribution_query],
      object_params: params[:asserted_distribution],
      preview: params[:preview],
    )

    a = request.filter

    v1 = a.all.distinct.limit(2).pluck(:geographic_area_id).uniq.count
    v2 = a.all.distinct.limit(2).pluck(:otu_id).uniq.count

    cap = 0

    if v1 > 1 && v2 > 1 # many otus, many geographic areas
      cap = 0
      request.cap_reason = 'Records include multiple OTUs *and* multiple geographic areas.'
    elsif v1 > 1
      cap = 0
      request.cap_reason = 'May not update multiple geographic areas to one.' # TODO: revist constraint
    else
      cap = 2000
    end

    request.cap = cap

    query_batch_update(request)
  end

  protected

  # @return [Boolean]
  def new_records_include_citation
    if new_record? && source.blank? && origin_citation.blank? && !citations.any?
      errors.add(:base, 'required citation is not provided')
    end
  end

  # @return [Boolean]
  def sv_conflicting_geographic_area
    unless geographic_area.nil?
      areas = [geographic_area.level0_id, geographic_area.level1_id, geographic_area.level2_id].compact
      if is_absent # this returns an array, not a single GA so test below is not right
        presence = AssertedDistribution
          .without_is_absent
          .with_geographic_area_array(areas)
          .where(otu_id:)
        soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
      else
        presence = AssertedDistribution
          .with_is_absent
          .where(otu_id:)
          .with_geographic_area_array(areas)
        soft_validations.add(:geographic_area_id, "Taxon is reported as missing in #{presence.first.geographic_area.name}") unless presence.empty?
      end
    end
  end

  # @param [Hash] options of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return [Array] an array of AssertedDistributions
  def self.stub_new(options = {})
    options.symbolize_keys!
    result = []
    options[:geographic_areas].each do |ga|
      result.push(
        AssertedDistribution.new(
          otu_id: options[:otu_id],
          geographic_area: ga,
          origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end
end

Class Method Details

.batch_update(params) ⇒ Object



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

def self.batch_update(params)
  request = QueryBatchRequest.new(
    async_cutoff: params[:async_cutoff] || 26,
    klass: 'AssertedDistribution',
    object_filter_params: params[:asserted_distribution_query],
    object_params: params[:asserted_distribution],
    preview: params[:preview],
  )

  a = request.filter

  v1 = a.all.distinct.limit(2).pluck(:geographic_area_id).uniq.count
  v2 = a.all.distinct.limit(2).pluck(:otu_id).uniq.count

  cap = 0

  if v1 > 1 && v2 > 1 # many otus, many geographic areas
    cap = 0
    request.cap_reason = 'Records include multiple OTUs *and* multiple geographic areas.'
  elsif v1 > 1
    cap = 0
    request.cap_reason = 'May not update multiple geographic areas to one.' # TODO: revist constraint
  else
    cap = 2000
  end

  request.cap = cap

  query_batch_update(request)
end

.stub(defaults: {}) ⇒ AssertedDistribution

Returns used to also stub an #origin_citation, as required.

Parameters:

  • defaults (Hash) (defaults to: {})

Returns:



81
82
83
84
85
86
87
# File 'app/models/asserted_distribution.rb', line 81

def self.stub(defaults: {})
  a = AssertedDistribution.new(
    otu_id: defaults[:otu_id],
    origin_citation_attributes: {source_id: defaults[:source_id]})
  a.origin_citation = Citation.new if defaults[:source_id].blank?
  a
end

.stub_new(options = {}) ⇒ Array (protected)

Returns an array of AssertedDistributions.

Parameters:

  • options (Hash) (defaults to: {})

    of e.g., 5, source_id: 5, geographic_areas: Array of {GeographicArea}

Returns:

  • (Array)

    an array of AssertedDistributions



179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'app/models/asserted_distribution.rb', line 179

def self.stub_new(options = {})
  options.symbolize_keys!
  result = []
  options[:geographic_areas].each do |ga|
    result.push(
      AssertedDistribution.new(
        otu_id: options[:otu_id],
        geographic_area: ga,
        origin_citation_attributes: {source_id: options[:source_id]})
    )
  end
  result
end

Instance Method Details

#geographic_itemObject



109
110
111
# File 'app/models/asserted_distribution.rb', line 109

def geographic_item
  geographic_area.default_geographic_item
end

#has_shape?Boolean

Returns:

  • (Boolean)


113
114
115
# File 'app/models/asserted_distribution.rb', line 113

def has_shape?
  geographic_area.geographic_items.any?
end

#new_records_include_citationBoolean (protected)

Returns:

  • (Boolean)


151
152
153
154
155
# File 'app/models/asserted_distribution.rb', line 151

def new_records_include_citation
  if new_record? && source.blank? && origin_citation.blank? && !citations.any?
    errors.add(:base, 'required citation is not provided')
  end
end

#requires_citation?True

Returns see citable.rb.

Returns:

  • (True)

    see citable.rb



105
106
107
# File 'app/models/asserted_distribution.rb', line 105

def requires_citation?
  true
end

#sv_conflicting_geographic_areaBoolean (protected)

Returns:

  • (Boolean)


158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'app/models/asserted_distribution.rb', line 158

def sv_conflicting_geographic_area
  unless geographic_area.nil?
    areas = [geographic_area.level0_id, geographic_area.level1_id, geographic_area.level2_id].compact
    if is_absent # this returns an array, not a single GA so test below is not right
      presence = AssertedDistribution
        .without_is_absent
        .with_geographic_area_array(areas)
        .where(otu_id:)
      soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
    else
      presence = AssertedDistribution
        .with_is_absent
        .where(otu_id:)
        .with_geographic_area_array(areas)
      soft_validations.add(:geographic_area_id, "Taxon is reported as missing in #{presence.first.geographic_area.name}") unless presence.empty?
    end
  end
end

#to_geo_json_featureHash

rubocop:disable Style/StringHashKeys TODO: DRY with helper methods

Returns:

  • (Hash)

    GeoJSON feature



92
93
94
95
96
97
98
99
# File 'app/models/asserted_distribution.rb', line 92

def to_geo_json_feature
  retval = {
    'type' => 'Feature',
    'geometry' => RGeo::GeoJSON.encode(self.geographic_area.geographic_items.first.geo_object),
    'properties' => {'asserted_distribution' => {'id' => self.id}}
  }
  retval
end