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 and Gazetteers.

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

#annotated_object_is_persisted?

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, #detect_version

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

#asserted_distribution_shape_idInteger

polymorphic spatial shape ID

Returns:

  • (Integer)


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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/models/asserted_distribution.rb', line 26

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 DwcExtensions

  include AssertedDistribution::DwcExtensions
  include Shared::IsData

  include Shared::Maps
  include Shared::QueryBatchUpdate
  include Shared::PolymorphicAnnotator
  polymorphic_annotates('asserted_distribution_shape')

  originates_from 'Specimen', 'Lot', 'FieldOccurrence'

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

  delegate :geo_object, to: :asserted_distribution_shape

  belongs_to :geographic_area, class_name: :GeographicArea, foreign_key: :asserted_distribution_shape_id
  belongs_to :gazetteer, class_name: :Gazetteer, foreign_key: :asserted_distribution_shape_id

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

  before_validation :unify_is_absent

  validates :otu, presence: true
  validates_uniqueness_of :otu, scope: [:project_id, :asserted_distribution_shape_id, :asserted_distribution_shape_type, :is_absent], message: 'this shape, OTU and present/absent combination already exists'
  validates_presence_of :asserted_distribution_shape
  validate :new_records_include_citation

  # TODO: deprecate scopes referencing single parameter where()
  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("asserted_distribution_shape_type = 'GeographicArea' AND asserted_distribution_shape_id IN (?)", geographic_area_array) }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }
  # Includes a `geographic_item_id` column.
  scope :associated_with_geographic_items, -> {
    a = AssertedDistribution
      .where(asserted_distribution_shape_type: 'GeographicArea')
      .joins('JOIN geographic_areas ON asserted_distribution_shape_id = geographic_areas.id')
      .joins('JOIN geographic_areas_geographic_items on geographic_areas.id = geographic_areas_geographic_items.geographic_area_id')
      .joins('JOIN geographic_items on geographic_items.id = geographic_areas_geographic_items.geographic_item_id')
      .select('asserted_distributions.*, geographic_items.id geographic_item_id')

    b = AssertedDistribution
      .where(asserted_distribution_shape_type: 'Gazetteer')
      .joins('JOIN gazetteers ON asserted_distribution_shape_id = gazetteers.id')
      .joins('JOIN geographic_items on gazetteers.geographic_item_id = geographic_items.id')
      .select('asserted_distributions.*, geographic_items.id geographic_item_id')

    ::Queries.union(AssertedDistribution, [a, b])
  }

  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?
    # TODO: Possibly provide a2/a3 info from gazetteers??
    @geographic_names ||=
      asserted_distribution_shape.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(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
    asserted_distribution_shape.default_geographic_item
  end

  def has_shape?
    asserted_distribution_shape.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(:asserted_distribution_shape_id, :asserted_distribution_shape_type)
      .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 shapes to one.' # TODO: revist constraint
    else
      cap = 2000
    end

    request.cap = cap

    query_batch_update(request)
  end


  def self.asserted_distributions_for_api_index(params, project_id)
    a = ::Queries::AssertedDistribution::Filter.new(params)
      .all
      .where(project_id: project_id)
      .includes(:citations, :otu, origin_citation: [:source])
      .includes(geographic_area: :parent)
      .includes(:gazetteer)
      .order('asserted_distributions.id')
      .page(params[:page])
      .per(params[:per])

    if a.all.count > 50
      params['extend']&.delete('geo_json')
    end

    a
  end

  protected

  # Never record "false" in the datase, only true
  def unify_is_absent
    self.is_absent = nil if self.is_absent != true
  end

  # @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
    # TODO: more expensive for gazetteers, which would require a spatial check.
    geographic_area = asserted_distribution_shape if asserted_distribution_shape_type == 'GeographicArea'
    return if 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.asserted_distribution_shape.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.asserted_distribution_shape.name}") unless presence.empty?
    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],
          asserted_distribution_shape: ga,
          origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end



end

#asserted_distribution_shape_typeString

polymorphic spatial shape type

Returns:

  • (String)


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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/models/asserted_distribution.rb', line 26

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 DwcExtensions

  include AssertedDistribution::DwcExtensions
  include Shared::IsData

  include Shared::Maps
  include Shared::QueryBatchUpdate
  include Shared::PolymorphicAnnotator
  polymorphic_annotates('asserted_distribution_shape')

  originates_from 'Specimen', 'Lot', 'FieldOccurrence'

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

  delegate :geo_object, to: :asserted_distribution_shape

  belongs_to :geographic_area, class_name: :GeographicArea, foreign_key: :asserted_distribution_shape_id
  belongs_to :gazetteer, class_name: :Gazetteer, foreign_key: :asserted_distribution_shape_id

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

  before_validation :unify_is_absent

  validates :otu, presence: true
  validates_uniqueness_of :otu, scope: [:project_id, :asserted_distribution_shape_id, :asserted_distribution_shape_type, :is_absent], message: 'this shape, OTU and present/absent combination already exists'
  validates_presence_of :asserted_distribution_shape
  validate :new_records_include_citation

  # TODO: deprecate scopes referencing single parameter where()
  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("asserted_distribution_shape_type = 'GeographicArea' AND asserted_distribution_shape_id IN (?)", geographic_area_array) }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }
  # Includes a `geographic_item_id` column.
  scope :associated_with_geographic_items, -> {
    a = AssertedDistribution
      .where(asserted_distribution_shape_type: 'GeographicArea')
      .joins('JOIN geographic_areas ON asserted_distribution_shape_id = geographic_areas.id')
      .joins('JOIN geographic_areas_geographic_items on geographic_areas.id = geographic_areas_geographic_items.geographic_area_id')
      .joins('JOIN geographic_items on geographic_items.id = geographic_areas_geographic_items.geographic_item_id')
      .select('asserted_distributions.*, geographic_items.id geographic_item_id')

    b = AssertedDistribution
      .where(asserted_distribution_shape_type: 'Gazetteer')
      .joins('JOIN gazetteers ON asserted_distribution_shape_id = gazetteers.id')
      .joins('JOIN geographic_items on gazetteers.geographic_item_id = geographic_items.id')
      .select('asserted_distributions.*, geographic_items.id geographic_item_id')

    ::Queries.union(AssertedDistribution, [a, b])
  }

  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?
    # TODO: Possibly provide a2/a3 info from gazetteers??
    @geographic_names ||=
      asserted_distribution_shape.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(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
    asserted_distribution_shape.default_geographic_item
  end

  def has_shape?
    asserted_distribution_shape.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(:asserted_distribution_shape_id, :asserted_distribution_shape_type)
      .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 shapes to one.' # TODO: revist constraint
    else
      cap = 2000
    end

    request.cap = cap

    query_batch_update(request)
  end


  def self.asserted_distributions_for_api_index(params, project_id)
    a = ::Queries::AssertedDistribution::Filter.new(params)
      .all
      .where(project_id: project_id)
      .includes(:citations, :otu, origin_citation: [:source])
      .includes(geographic_area: :parent)
      .includes(:gazetteer)
      .order('asserted_distributions.id')
      .page(params[:page])
      .per(params[:per])

    if a.all.count > 50
      params['extend']&.delete('geo_json')
    end

    a
  end

  protected

  # Never record "false" in the datase, only true
  def unify_is_absent
    self.is_absent = nil if self.is_absent != true
  end

  # @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
    # TODO: more expensive for gazetteers, which would require a spatial check.
    geographic_area = asserted_distribution_shape if asserted_distribution_shape_type == 'GeographicArea'
    return if 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.asserted_distribution_shape.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.asserted_distribution_shape.name}") unless presence.empty?
    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],
          asserted_distribution_shape: ga,
          origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end



end

#geographic_namesObject

getter for attr :geographic_names



52
53
54
# File 'app/models/asserted_distribution.rb', line 52

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



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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/models/asserted_distribution.rb', line 26

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 DwcExtensions

  include AssertedDistribution::DwcExtensions
  include Shared::IsData

  include Shared::Maps
  include Shared::QueryBatchUpdate
  include Shared::PolymorphicAnnotator
  polymorphic_annotates('asserted_distribution_shape')

  originates_from 'Specimen', 'Lot', 'FieldOccurrence'

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

  delegate :geo_object, to: :asserted_distribution_shape

  belongs_to :geographic_area, class_name: :GeographicArea, foreign_key: :asserted_distribution_shape_id
  belongs_to :gazetteer, class_name: :Gazetteer, foreign_key: :asserted_distribution_shape_id

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

  before_validation :unify_is_absent

  validates :otu, presence: true
  validates_uniqueness_of :otu, scope: [:project_id, :asserted_distribution_shape_id, :asserted_distribution_shape_type, :is_absent], message: 'this shape, OTU and present/absent combination already exists'
  validates_presence_of :asserted_distribution_shape
  validate :new_records_include_citation

  # TODO: deprecate scopes referencing single parameter where()
  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("asserted_distribution_shape_type = 'GeographicArea' AND asserted_distribution_shape_id IN (?)", geographic_area_array) }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }
  # Includes a `geographic_item_id` column.
  scope :associated_with_geographic_items, -> {
    a = AssertedDistribution
      .where(asserted_distribution_shape_type: 'GeographicArea')
      .joins('JOIN geographic_areas ON asserted_distribution_shape_id = geographic_areas.id')
      .joins('JOIN geographic_areas_geographic_items on geographic_areas.id = geographic_areas_geographic_items.geographic_area_id')
      .joins('JOIN geographic_items on geographic_items.id = geographic_areas_geographic_items.geographic_item_id')
      .select('asserted_distributions.*, geographic_items.id geographic_item_id')

    b = AssertedDistribution
      .where(asserted_distribution_shape_type: 'Gazetteer')
      .joins('JOIN gazetteers ON asserted_distribution_shape_id = gazetteers.id')
      .joins('JOIN geographic_items on gazetteers.geographic_item_id = geographic_items.id')
      .select('asserted_distributions.*, geographic_items.id geographic_item_id')

    ::Queries.union(AssertedDistribution, [a, b])
  }

  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?
    # TODO: Possibly provide a2/a3 info from gazetteers??
    @geographic_names ||=
      asserted_distribution_shape.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(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
    asserted_distribution_shape.default_geographic_item
  end

  def has_shape?
    asserted_distribution_shape.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(:asserted_distribution_shape_id, :asserted_distribution_shape_type)
      .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 shapes to one.' # TODO: revist constraint
    else
      cap = 2000
    end

    request.cap = cap

    query_batch_update(request)
  end


  def self.asserted_distributions_for_api_index(params, project_id)
    a = ::Queries::AssertedDistribution::Filter.new(params)
      .all
      .where(project_id: project_id)
      .includes(:citations, :otu, origin_citation: [:source])
      .includes(geographic_area: :parent)
      .includes(:gazetteer)
      .order('asserted_distributions.id')
      .page(params[:page])
      .per(params[:per])

    if a.all.count > 50
      params['extend']&.delete('geo_json')
    end

    a
  end

  protected

  # Never record "false" in the datase, only true
  def unify_is_absent
    self.is_absent = nil if self.is_absent != true
  end

  # @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
    # TODO: more expensive for gazetteers, which would require a spatial check.
    geographic_area = asserted_distribution_shape if asserted_distribution_shape_type == 'GeographicArea'
    return if 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.asserted_distribution_shape.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.asserted_distribution_shape.name}") unless presence.empty?
    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],
          asserted_distribution_shape: ga,
          origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end



end

#otu_idInteger

the OTU ID

Returns:

  • (Integer)


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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/models/asserted_distribution.rb', line 26

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 DwcExtensions

  include AssertedDistribution::DwcExtensions
  include Shared::IsData

  include Shared::Maps
  include Shared::QueryBatchUpdate
  include Shared::PolymorphicAnnotator
  polymorphic_annotates('asserted_distribution_shape')

  originates_from 'Specimen', 'Lot', 'FieldOccurrence'

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

  delegate :geo_object, to: :asserted_distribution_shape

  belongs_to :geographic_area, class_name: :GeographicArea, foreign_key: :asserted_distribution_shape_id
  belongs_to :gazetteer, class_name: :Gazetteer, foreign_key: :asserted_distribution_shape_id

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

  before_validation :unify_is_absent

  validates :otu, presence: true
  validates_uniqueness_of :otu, scope: [:project_id, :asserted_distribution_shape_id, :asserted_distribution_shape_type, :is_absent], message: 'this shape, OTU and present/absent combination already exists'
  validates_presence_of :asserted_distribution_shape
  validate :new_records_include_citation

  # TODO: deprecate scopes referencing single parameter where()
  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("asserted_distribution_shape_type = 'GeographicArea' AND asserted_distribution_shape_id IN (?)", geographic_area_array) }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }
  # Includes a `geographic_item_id` column.
  scope :associated_with_geographic_items, -> {
    a = AssertedDistribution
      .where(asserted_distribution_shape_type: 'GeographicArea')
      .joins('JOIN geographic_areas ON asserted_distribution_shape_id = geographic_areas.id')
      .joins('JOIN geographic_areas_geographic_items on geographic_areas.id = geographic_areas_geographic_items.geographic_area_id')
      .joins('JOIN geographic_items on geographic_items.id = geographic_areas_geographic_items.geographic_item_id')
      .select('asserted_distributions.*, geographic_items.id geographic_item_id')

    b = AssertedDistribution
      .where(asserted_distribution_shape_type: 'Gazetteer')
      .joins('JOIN gazetteers ON asserted_distribution_shape_id = gazetteers.id')
      .joins('JOIN geographic_items on gazetteers.geographic_item_id = geographic_items.id')
      .select('asserted_distributions.*, geographic_items.id geographic_item_id')

    ::Queries.union(AssertedDistribution, [a, b])
  }

  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?
    # TODO: Possibly provide a2/a3 info from gazetteers??
    @geographic_names ||=
      asserted_distribution_shape.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(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
    asserted_distribution_shape.default_geographic_item
  end

  def has_shape?
    asserted_distribution_shape.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(:asserted_distribution_shape_id, :asserted_distribution_shape_type)
      .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 shapes to one.' # TODO: revist constraint
    else
      cap = 2000
    end

    request.cap = cap

    query_batch_update(request)
  end


  def self.asserted_distributions_for_api_index(params, project_id)
    a = ::Queries::AssertedDistribution::Filter.new(params)
      .all
      .where(project_id: project_id)
      .includes(:citations, :otu, origin_citation: [:source])
      .includes(geographic_area: :parent)
      .includes(:gazetteer)
      .order('asserted_distributions.id')
      .page(params[:page])
      .per(params[:per])

    if a.all.count > 50
      params['extend']&.delete('geo_json')
    end

    a
  end

  protected

  # Never record "false" in the datase, only true
  def unify_is_absent
    self.is_absent = nil if self.is_absent != true
  end

  # @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
    # TODO: more expensive for gazetteers, which would require a spatial check.
    geographic_area = asserted_distribution_shape if asserted_distribution_shape_type == 'GeographicArea'
    return if 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.asserted_distribution_shape.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.asserted_distribution_shape.name}") unless presence.empty?
    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],
          asserted_distribution_shape: ga,
          origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end



end

#project_idInteger

the project ID

Returns:

  • (Integer)


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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/models/asserted_distribution.rb', line 26

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 DwcExtensions

  include AssertedDistribution::DwcExtensions
  include Shared::IsData

  include Shared::Maps
  include Shared::QueryBatchUpdate
  include Shared::PolymorphicAnnotator
  polymorphic_annotates('asserted_distribution_shape')

  originates_from 'Specimen', 'Lot', 'FieldOccurrence'

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

  delegate :geo_object, to: :asserted_distribution_shape

  belongs_to :geographic_area, class_name: :GeographicArea, foreign_key: :asserted_distribution_shape_id
  belongs_to :gazetteer, class_name: :Gazetteer, foreign_key: :asserted_distribution_shape_id

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

  before_validation :unify_is_absent

  validates :otu, presence: true
  validates_uniqueness_of :otu, scope: [:project_id, :asserted_distribution_shape_id, :asserted_distribution_shape_type, :is_absent], message: 'this shape, OTU and present/absent combination already exists'
  validates_presence_of :asserted_distribution_shape
  validate :new_records_include_citation

  # TODO: deprecate scopes referencing single parameter where()
  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("asserted_distribution_shape_type = 'GeographicArea' AND asserted_distribution_shape_id IN (?)", geographic_area_array) }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }
  # Includes a `geographic_item_id` column.
  scope :associated_with_geographic_items, -> {
    a = AssertedDistribution
      .where(asserted_distribution_shape_type: 'GeographicArea')
      .joins('JOIN geographic_areas ON asserted_distribution_shape_id = geographic_areas.id')
      .joins('JOIN geographic_areas_geographic_items on geographic_areas.id = geographic_areas_geographic_items.geographic_area_id')
      .joins('JOIN geographic_items on geographic_items.id = geographic_areas_geographic_items.geographic_item_id')
      .select('asserted_distributions.*, geographic_items.id geographic_item_id')

    b = AssertedDistribution
      .where(asserted_distribution_shape_type: 'Gazetteer')
      .joins('JOIN gazetteers ON asserted_distribution_shape_id = gazetteers.id')
      .joins('JOIN geographic_items on gazetteers.geographic_item_id = geographic_items.id')
      .select('asserted_distributions.*, geographic_items.id geographic_item_id')

    ::Queries.union(AssertedDistribution, [a, b])
  }

  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?
    # TODO: Possibly provide a2/a3 info from gazetteers??
    @geographic_names ||=
      asserted_distribution_shape.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(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
    asserted_distribution_shape.default_geographic_item
  end

  def has_shape?
    asserted_distribution_shape.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(:asserted_distribution_shape_id, :asserted_distribution_shape_type)
      .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 shapes to one.' # TODO: revist constraint
    else
      cap = 2000
    end

    request.cap = cap

    query_batch_update(request)
  end


  def self.asserted_distributions_for_api_index(params, project_id)
    a = ::Queries::AssertedDistribution::Filter.new(params)
      .all
      .where(project_id: project_id)
      .includes(:citations, :otu, origin_citation: [:source])
      .includes(geographic_area: :parent)
      .includes(:gazetteer)
      .order('asserted_distributions.id')
      .page(params[:page])
      .per(params[:per])

    if a.all.count > 50
      params['extend']&.delete('geo_json')
    end

    a
  end

  protected

  # Never record "false" in the datase, only true
  def unify_is_absent
    self.is_absent = nil if self.is_absent != true
  end

  # @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
    # TODO: more expensive for gazetteers, which would require a spatial check.
    geographic_area = asserted_distribution_shape if asserted_distribution_shape_type == 'GeographicArea'
    return if 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.asserted_distribution_shape.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.asserted_distribution_shape.name}") unless presence.empty?
    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],
          asserted_distribution_shape: ga,
          origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end



end

Class Method Details

.asserted_distributions_for_api_index(params, project_id) ⇒ Object



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

def self.asserted_distributions_for_api_index(params, project_id)
  a = ::Queries::AssertedDistribution::Filter.new(params)
    .all
    .where(project_id: project_id)
    .includes(:citations, :otu, origin_citation: [:source])
    .includes(geographic_area: :parent)
    .includes(:gazetteer)
    .order('asserted_distributions.id')
    .page(params[:page])
    .per(params[:per])

  if a.all.count > 50
    params['extend']&.delete('geo_json')
  end

  a
end

.batch_update(params) ⇒ Object



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

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(:asserted_distribution_shape_id, :asserted_distribution_shape_type)
    .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 shapes 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:



109
110
111
112
113
114
115
# File 'app/models/asserted_distribution.rb', line 109

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



235
236
237
238
239
240
241
242
243
244
245
246
247
# File 'app/models/asserted_distribution.rb', line 235

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

Instance Method Details

#geographic_itemObject



137
138
139
# File 'app/models/asserted_distribution.rb', line 137

def geographic_item
  asserted_distribution_shape.default_geographic_item
end

#has_shape?Boolean

Returns:

  • (Boolean)


141
142
143
# File 'app/models/asserted_distribution.rb', line 141

def has_shape?
  asserted_distribution_shape.geographic_items.any?
end

#new_records_include_citationBoolean (protected)

Returns:

  • (Boolean)


205
206
207
208
209
# File 'app/models/asserted_distribution.rb', line 205

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



133
134
135
# File 'app/models/asserted_distribution.rb', line 133

def requires_citation?
  true
end

#sv_conflicting_geographic_areaBoolean (protected)

Returns:

  • (Boolean)


212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'app/models/asserted_distribution.rb', line 212

def sv_conflicting_geographic_area
  # TODO: more expensive for gazetteers, which would require a spatial check.
  geographic_area = asserted_distribution_shape if asserted_distribution_shape_type == 'GeographicArea'
  return if 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.asserted_distribution_shape.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.asserted_distribution_shape.name}") unless presence.empty?
  end
end

#to_geo_json_featureHash

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

Returns:

  • (Hash)

    GeoJSON feature



120
121
122
123
124
125
126
127
# File 'app/models/asserted_distribution.rb', line 120

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

#unify_is_absentObject (protected)

Never record “false” in the datase, only true



200
201
202
# File 'app/models/asserted_distribution.rb', line 200

def unify_is_absent
  self.is_absent = nil if self.is_absent != true
end