Class: AssertedDistribution
- Inherits:
-
ApplicationRecord
- Object
- ActiveRecord::Base
- ApplicationRecord
- AssertedDistribution
- Includes:
- DwcExtensions, Housekeeping, Shared::CitationRequired, Shared::Citations, Shared::Confidences, Shared::DataAttributes, Shared::HasPapertrail, Shared::Identifiers, Shared::IsData, Shared::IsDwcOccurrence, Shared::Maps, Shared::Notes, Shared::OriginRelationship, Shared::QueryBatchUpdate, Shared::Tags, Shared::Taxonomy, SoftValidation
- Defined in:
- app/models/asserted_distribution.rb
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
-
#geographic_area_id ⇒ Integer
the geographic area ID.
-
#geographic_names ⇒ Object
getter for attr :geographic_names.
-
#is_absent ⇒ Boolean
A positive negative, when true then there exists an assertion that the taxon is not present in the spatial area.
-
#otu_id ⇒ Integer
the OTU ID.
-
#project_id ⇒ Integer
the project ID.
Class Method Summary collapse
- .batch_update(params) ⇒ Object
-
.stub(defaults: {}) ⇒ AssertedDistribution
Used to also stub an #origin_citation, as required.
-
.stub_new(options = {}) ⇒ Array
protected
An array of AssertedDistributions.
Instance Method Summary collapse
- #geographic_item ⇒ Object
- #has_shape? ⇒ Boolean
- #new_records_include_citation ⇒ Boolean protected
-
#requires_citation? ⇒ True
See citable.rb.
- #sv_conflicting_geographic_area ⇒ Boolean protected
-
#to_geo_json_feature ⇒ Hash
rubocop:disable Style/StringHashKeys TODO: DRY with helper methods.
Methods included from Shared::QueryBatchUpdate
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
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
Instance Attribute Details
#geographic_area_id ⇒ Integer
the geographic area ID
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 193 194 195 196 197 |
# 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', 'FieldOccurrence' # @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 :otu, scope: [:project_id, :geographic_area, :is_absent], message: 'this geographic_area, OTU and present/absent combination already exists' # !! If we want to unify GeographicAreas then we need to raise a redundat error :geographic_area here, we should write a custom method # that checks for an error on otu then adds a record, i.e. no database calls would be made # 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( = {}) .symbolize_keys! result = [] [:geographic_areas].each do |ga| result.push( AssertedDistribution.new( otu_id: [:otu_id], geographic_area: ga, origin_citation_attributes: {source_id: [:source_id]}) ) end result end end |
#geographic_names ⇒ Object
getter for attr :geographic_names
46 47 48 |
# File 'app/models/asserted_distribution.rb', line 46 def geographic_names @geographic_names end |
#is_absent ⇒ Boolean
Returns 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 193 194 195 196 197 |
# 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', 'FieldOccurrence' # @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 :otu, scope: [:project_id, :geographic_area, :is_absent], message: 'this geographic_area, OTU and present/absent combination already exists' # !! If we want to unify GeographicAreas then we need to raise a redundat error :geographic_area here, we should write a custom method # that checks for an error on otu then adds a record, i.e. no database calls would be made # 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( = {}) .symbolize_keys! result = [] [:geographic_areas].each do |ga| result.push( AssertedDistribution.new( otu_id: [:otu_id], geographic_area: ga, origin_citation_attributes: {source_id: [:source_id]}) ) end result end end |
#otu_id ⇒ Integer
the OTU ID
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 193 194 195 196 197 |
# 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', 'FieldOccurrence' # @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 :otu, scope: [:project_id, :geographic_area, :is_absent], message: 'this geographic_area, OTU and present/absent combination already exists' # !! If we want to unify GeographicAreas then we need to raise a redundat error :geographic_area here, we should write a custom method # that checks for an error on otu then adds a record, i.e. no database calls would be made # 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( = {}) .symbolize_keys! result = [] [:geographic_areas].each do |ga| result.push( AssertedDistribution.new( otu_id: [:otu_id], geographic_area: ga, origin_citation_attributes: {source_id: [:source_id]}) ) end result end end |
#project_id ⇒ Integer
the project ID
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 193 194 195 196 197 |
# 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', 'FieldOccurrence' # @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 :otu, scope: [:project_id, :geographic_area, :is_absent], message: 'this geographic_area, OTU and present/absent combination already exists' # !! If we want to unify GeographicAreas then we need to raise a redundat error :geographic_area here, we should write a custom method # that checks for an error on otu then adds a record, i.e. no database calls would be made # 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( = {}) .symbolize_keys! result = [] [:geographic_areas].each do |ga| result.push( AssertedDistribution.new( otu_id: [:otu_id], geographic_area: ga, origin_citation_attributes: {source_id: [:source_id]}) ) end result end end |
Class Method Details
.batch_update(params) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 |
# File 'app/models/asserted_distribution.rb', line 122 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.
86 87 88 89 90 91 92 |
# File 'app/models/asserted_distribution.rb', line 86 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.
184 185 186 187 188 189 190 191 192 193 194 195 196 |
# File 'app/models/asserted_distribution.rb', line 184 def self.stub_new( = {}) .symbolize_keys! result = [] [:geographic_areas].each do |ga| result.push( AssertedDistribution.new( otu_id: [:otu_id], geographic_area: ga, origin_citation_attributes: {source_id: [:source_id]}) ) end result end |
Instance Method Details
#geographic_item ⇒ Object
114 115 116 |
# File 'app/models/asserted_distribution.rb', line 114 def geographic_item geographic_area.default_geographic_item end |
#has_shape? ⇒ Boolean
118 119 120 |
# File 'app/models/asserted_distribution.rb', line 118 def has_shape? geographic_area.geographic_items.any? end |
#new_records_include_citation ⇒ Boolean (protected)
156 157 158 159 160 |
# File 'app/models/asserted_distribution.rb', line 156 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.
110 111 112 |
# File 'app/models/asserted_distribution.rb', line 110 def requires_citation? true end |
#sv_conflicting_geographic_area ⇒ Boolean (protected)
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'app/models/asserted_distribution.rb', line 163 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_feature ⇒ Hash
rubocop:disable Style/StringHashKeys TODO: DRY with helper methods
97 98 99 100 101 102 103 104 |
# File 'app/models/asserted_distribution.rb', line 97 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 |