Class: AssertedDistribution

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
DwcExtensions, Housekeeping, Shared::Citable, Shared::DataAttributes, Shared::IsData, Shared::IsDwcOccurrence, Shared::Notable, Shared::Taggable, 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, which essentially represent a gazeteer of 3 levels of subdivision (e.g. country, state, county).

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

Constant Summary

Constants included from DwcExtensions

DwcExtensions::DWC_OCCURRENCE_MAP

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from SoftValidation

#clear_soft_validations, #fix_soft_validations, #soft_fixed?, #soft_valid?, #soft_validate, #soft_validated?, #soft_validations

Methods included from Housekeeping

#has_polymorphic_relationship?

Instance Attribute Details

- (Integer) geographic_area_id

the geographic area ID

Returns:

  • (Integer)


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

class AssertedDistribution < ActiveRecord::Base
  include Housekeeping
  include Shared::Notable
  include SoftValidation
  include Shared::IsData
  include Shared::Taggable
  include Shared::DataAttributes
  include Shared::Citable

  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions 

  belongs_to :otu, inverse_of: :asserted_distributions
  belongs_to :geographic_area, inverse_of: :asserted_distributions

  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: proc { |attributes| attributes['name'].blank? && attributes['taxon_name_id'].blank?  }
  
  # validates_presence_of :otu_id, message: 'Taxon is not specified', if:  proc { |attributes| attributes['otu_id'].nil?  ( attributes['otu_attributes'] && (!attributes['otu_attributes']['name'] || !attributes['otu_attributes']['taxon_name_id']))}
  validates_presence_of :geographic_area_id, message: 'geographic area is not selected'

  # Might not be able to do these for nested attributes
  validates :geographic_area, presence: true
  validates :otu, presence: true

  validates_uniqueness_of :geographic_area_id, scope: [:project_id, :otu_id], message: 'record for this source/otu combination already exists'

  validate :new_records_include_citation
  
  scope :with_otu_id, -> (otu_id) { where(otu_id: otu_id) }
  scope :with_geographic_area_id, -> (geographic_area_id) { where(geographic_area_id: geographic_area_id) }
  scope :with_geographic_area_array, -> (geographic_area_array) { where('geographic_area_id IN (?)', geographic_area_array) }
  scope :with_is_absent, -> { where('is_absent = true') }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }

  soft_validate(:sv_conflicting_geographic_area, set: :conflicting_geographic_area)

  def self.find_for_autocomplete(params)
    term = params[:term]
    include(:geographic_area, :otu).
        where(geographic_areas: {name: term}, otus: {name: term}).with_project_id(params[:project_id])
  end

  # @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

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

  # @return [Hash]
  #   this asserted distribution as a GeoJSON feature
  def to_geo_json_feature
    {
      'type'       => 'Feature',
      'geometry'   => RGeo::GeoJSON.encode(self.geographic_area.geographic_items.first.geo_object),
      'properties' => {
        'asserted_distribution' => {
          'id' => self.id
        }
      }
    }
  end

  protected

  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

  def new_records_include_otu

  end
  

  def sv_conflicting_geographic_area
    ga = self.geographic_area
    unless ga.nil?
      if self.is_absent == true
        presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id) # this returns an array, not a single GA so test below is not right
        soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
      else
        areas    = [ga.level0_id, ga.level1_id, ga.level2_id].compact
        presence = AssertedDistribution.with_is_absent.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 options [Hash] of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return an array of AssertedDistributions.new()
  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

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

- (Boolean) is_absent

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



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

class AssertedDistribution < ActiveRecord::Base
  include Housekeeping
  include Shared::Notable
  include SoftValidation
  include Shared::IsData
  include Shared::Taggable
  include Shared::DataAttributes
  include Shared::Citable

  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions 

  belongs_to :otu, inverse_of: :asserted_distributions
  belongs_to :geographic_area, inverse_of: :asserted_distributions

  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: proc { |attributes| attributes['name'].blank? && attributes['taxon_name_id'].blank?  }
  
  # validates_presence_of :otu_id, message: 'Taxon is not specified', if:  proc { |attributes| attributes['otu_id'].nil?  ( attributes['otu_attributes'] && (!attributes['otu_attributes']['name'] || !attributes['otu_attributes']['taxon_name_id']))}
  validates_presence_of :geographic_area_id, message: 'geographic area is not selected'

  # Might not be able to do these for nested attributes
  validates :geographic_area, presence: true
  validates :otu, presence: true

  validates_uniqueness_of :geographic_area_id, scope: [:project_id, :otu_id], message: 'record for this source/otu combination already exists'

  validate :new_records_include_citation
  
  scope :with_otu_id, -> (otu_id) { where(otu_id: otu_id) }
  scope :with_geographic_area_id, -> (geographic_area_id) { where(geographic_area_id: geographic_area_id) }
  scope :with_geographic_area_array, -> (geographic_area_array) { where('geographic_area_id IN (?)', geographic_area_array) }
  scope :with_is_absent, -> { where('is_absent = true') }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }

  soft_validate(:sv_conflicting_geographic_area, set: :conflicting_geographic_area)

  def self.find_for_autocomplete(params)
    term = params[:term]
    include(:geographic_area, :otu).
        where(geographic_areas: {name: term}, otus: {name: term}).with_project_id(params[:project_id])
  end

  # @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

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

  # @return [Hash]
  #   this asserted distribution as a GeoJSON feature
  def to_geo_json_feature
    {
      'type'       => 'Feature',
      'geometry'   => RGeo::GeoJSON.encode(self.geographic_area.geographic_items.first.geo_object),
      'properties' => {
        'asserted_distribution' => {
          'id' => self.id
        }
      }
    }
  end

  protected

  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

  def new_records_include_otu

  end
  

  def sv_conflicting_geographic_area
    ga = self.geographic_area
    unless ga.nil?
      if self.is_absent == true
        presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id) # this returns an array, not a single GA so test below is not right
        soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
      else
        areas    = [ga.level0_id, ga.level1_id, ga.level2_id].compact
        presence = AssertedDistribution.with_is_absent.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 options [Hash] of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return an array of AssertedDistributions.new()
  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

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

- (Integer) otu_id

the OTU ID

Returns:

  • (Integer)


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

class AssertedDistribution < ActiveRecord::Base
  include Housekeeping
  include Shared::Notable
  include SoftValidation
  include Shared::IsData
  include Shared::Taggable
  include Shared::DataAttributes
  include Shared::Citable

  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions 

  belongs_to :otu, inverse_of: :asserted_distributions
  belongs_to :geographic_area, inverse_of: :asserted_distributions

  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: proc { |attributes| attributes['name'].blank? && attributes['taxon_name_id'].blank?  }
  
  # validates_presence_of :otu_id, message: 'Taxon is not specified', if:  proc { |attributes| attributes['otu_id'].nil?  ( attributes['otu_attributes'] && (!attributes['otu_attributes']['name'] || !attributes['otu_attributes']['taxon_name_id']))}
  validates_presence_of :geographic_area_id, message: 'geographic area is not selected'

  # Might not be able to do these for nested attributes
  validates :geographic_area, presence: true
  validates :otu, presence: true

  validates_uniqueness_of :geographic_area_id, scope: [:project_id, :otu_id], message: 'record for this source/otu combination already exists'

  validate :new_records_include_citation
  
  scope :with_otu_id, -> (otu_id) { where(otu_id: otu_id) }
  scope :with_geographic_area_id, -> (geographic_area_id) { where(geographic_area_id: geographic_area_id) }
  scope :with_geographic_area_array, -> (geographic_area_array) { where('geographic_area_id IN (?)', geographic_area_array) }
  scope :with_is_absent, -> { where('is_absent = true') }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }

  soft_validate(:sv_conflicting_geographic_area, set: :conflicting_geographic_area)

  def self.find_for_autocomplete(params)
    term = params[:term]
    include(:geographic_area, :otu).
        where(geographic_areas: {name: term}, otus: {name: term}).with_project_id(params[:project_id])
  end

  # @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

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

  # @return [Hash]
  #   this asserted distribution as a GeoJSON feature
  def to_geo_json_feature
    {
      'type'       => 'Feature',
      'geometry'   => RGeo::GeoJSON.encode(self.geographic_area.geographic_items.first.geo_object),
      'properties' => {
        'asserted_distribution' => {
          'id' => self.id
        }
      }
    }
  end

  protected

  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

  def new_records_include_otu

  end
  

  def sv_conflicting_geographic_area
    ga = self.geographic_area
    unless ga.nil?
      if self.is_absent == true
        presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id) # this returns an array, not a single GA so test below is not right
        soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
      else
        areas    = [ga.level0_id, ga.level1_id, ga.level2_id].compact
        presence = AssertedDistribution.with_is_absent.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 options [Hash] of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return an array of AssertedDistributions.new()
  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

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

- (Integer) project_id

the project ID

Returns:

  • (Integer)


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

class AssertedDistribution < ActiveRecord::Base
  include Housekeeping
  include Shared::Notable
  include SoftValidation
  include Shared::IsData
  include Shared::Taggable
  include Shared::DataAttributes
  include Shared::Citable

  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions 

  belongs_to :otu, inverse_of: :asserted_distributions
  belongs_to :geographic_area, inverse_of: :asserted_distributions

  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: proc { |attributes| attributes['name'].blank? && attributes['taxon_name_id'].blank?  }
  
  # validates_presence_of :otu_id, message: 'Taxon is not specified', if:  proc { |attributes| attributes['otu_id'].nil?  ( attributes['otu_attributes'] && (!attributes['otu_attributes']['name'] || !attributes['otu_attributes']['taxon_name_id']))}
  validates_presence_of :geographic_area_id, message: 'geographic area is not selected'

  # Might not be able to do these for nested attributes
  validates :geographic_area, presence: true
  validates :otu, presence: true

  validates_uniqueness_of :geographic_area_id, scope: [:project_id, :otu_id], message: 'record for this source/otu combination already exists'

  validate :new_records_include_citation
  
  scope :with_otu_id, -> (otu_id) { where(otu_id: otu_id) }
  scope :with_geographic_area_id, -> (geographic_area_id) { where(geographic_area_id: geographic_area_id) }
  scope :with_geographic_area_array, -> (geographic_area_array) { where('geographic_area_id IN (?)', geographic_area_array) }
  scope :with_is_absent, -> { where('is_absent = true') }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }

  soft_validate(:sv_conflicting_geographic_area, set: :conflicting_geographic_area)

  def self.find_for_autocomplete(params)
    term = params[:term]
    include(:geographic_area, :otu).
        where(geographic_areas: {name: term}, otus: {name: term}).with_project_id(params[:project_id])
  end

  # @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

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

  # @return [Hash]
  #   this asserted distribution as a GeoJSON feature
  def to_geo_json_feature
    {
      'type'       => 'Feature',
      'geometry'   => RGeo::GeoJSON.encode(self.geographic_area.geographic_items.first.geo_object),
      'properties' => {
        'asserted_distribution' => {
          'id' => self.id
        }
      }
    }
  end

  protected

  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

  def new_records_include_otu

  end
  

  def sv_conflicting_geographic_area
    ga = self.geographic_area
    unless ga.nil?
      if self.is_absent == true
        presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id) # this returns an array, not a single GA so test below is not right
        soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
      else
        areas    = [ga.level0_id, ga.level1_id, ga.level2_id].compact
        presence = AssertedDistribution.with_is_absent.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 options [Hash] of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return an array of AssertedDistributions.new()
  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

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

- (Integer) source_id

the source ID

Returns:

  • (Integer)


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

class AssertedDistribution < ActiveRecord::Base
  include Housekeeping
  include Shared::Notable
  include SoftValidation
  include Shared::IsData
  include Shared::Taggable
  include Shared::DataAttributes
  include Shared::Citable

  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions 

  belongs_to :otu, inverse_of: :asserted_distributions
  belongs_to :geographic_area, inverse_of: :asserted_distributions

  accepts_nested_attributes_for :otu, allow_destroy: false, reject_if: proc { |attributes| attributes['name'].blank? && attributes['taxon_name_id'].blank?  }
  
  # validates_presence_of :otu_id, message: 'Taxon is not specified', if:  proc { |attributes| attributes['otu_id'].nil?  ( attributes['otu_attributes'] && (!attributes['otu_attributes']['name'] || !attributes['otu_attributes']['taxon_name_id']))}
  validates_presence_of :geographic_area_id, message: 'geographic area is not selected'

  # Might not be able to do these for nested attributes
  validates :geographic_area, presence: true
  validates :otu, presence: true

  validates_uniqueness_of :geographic_area_id, scope: [:project_id, :otu_id], message: 'record for this source/otu combination already exists'

  validate :new_records_include_citation
  
  scope :with_otu_id, -> (otu_id) { where(otu_id: otu_id) }
  scope :with_geographic_area_id, -> (geographic_area_id) { where(geographic_area_id: geographic_area_id) }
  scope :with_geographic_area_array, -> (geographic_area_array) { where('geographic_area_id IN (?)', geographic_area_array) }
  scope :with_is_absent, -> { where('is_absent = true') }
  scope :without_is_absent, -> { where('is_absent = false OR is_absent is Null') }

  soft_validate(:sv_conflicting_geographic_area, set: :conflicting_geographic_area)

  def self.find_for_autocomplete(params)
    term = params[:term]
    include(:geographic_area, :otu).
        where(geographic_areas: {name: term}, otus: {name: term}).with_project_id(params[:project_id])
  end

  # @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

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

  # @return [Hash]
  #   this asserted distribution as a GeoJSON feature
  def to_geo_json_feature
    {
      'type'       => 'Feature',
      'geometry'   => RGeo::GeoJSON.encode(self.geographic_area.geographic_items.first.geo_object),
      'properties' => {
        'asserted_distribution' => {
          'id' => self.id
        }
      }
    }
  end

  protected

  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

  def new_records_include_otu

  end
  

  def sv_conflicting_geographic_area
    ga = self.geographic_area
    unless ga.nil?
      if self.is_absent == true
        presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id) # this returns an array, not a single GA so test below is not right
        soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
      else
        areas    = [ga.level0_id, ga.level1_id, ga.level2_id].compact
        presence = AssertedDistribution.with_is_absent.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 options [Hash] of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return an array of AssertedDistributions.new()
  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

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

Class Method Details

+ (Object) find_for_autocomplete(params)



64
65
66
67
68
# File 'app/models/asserted_distribution.rb', line 64

def self.find_for_autocomplete(params)
  term = params[:term]
  include(:geographic_area, :otu).
      where(geographic_areas: {name: term}, otus: {name: term}).with_project_id(params[:project_id])
end

+ (Object) generate_download(scope) (protected)



143
144
145
146
147
148
149
150
151
152
# File 'app/models/asserted_distribution.rb', line 143

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

+ (AssertedDistribution) stub(defaults: {})

Returns used to also stub an #origin_citation, as required

Returns:



72
73
74
75
76
77
78
79
# File 'app/models/asserted_distribution.rb', line 72

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

+ (Object) stub_new(options = {}) (protected)

Returns an array of AssertedDistributions.new()

Parameters:

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

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

Returns:

  • an array of AssertedDistributions.new()



130
131
132
133
134
135
136
137
138
139
140
141
# File 'app/models/asserted_distribution.rb', line 130

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

- (Object) new_records_include_citation (protected)



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

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

- (Object) new_records_include_otu (protected)



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

def new_records_include_otu

end

- (True) requires_citation?

Returns see citable.rb

Returns:

  • (True)

    see citable.rb



83
84
85
# File 'app/models/asserted_distribution.rb', line 83

def requires_citation?
  true
end

- (Object) sv_conflicting_geographic_area (protected)



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/asserted_distribution.rb', line 114

def sv_conflicting_geographic_area
  ga = self.geographic_area
  unless ga.nil?
    if self.is_absent == true
      presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id) # this returns an array, not a single GA so test below is not right
      soft_validations.add(:geographic_area_id, "Taxon is reported as present in #{presence.first.geographic_area.name}") unless presence.empty?
    else
      areas    = [ga.level0_id, ga.level1_id, ga.level2_id].compact
      presence = AssertedDistribution.with_is_absent.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

- (Hash) to_geo_json_feature

Returns this asserted distribution as a GeoJSON feature

Returns:

  • (Hash)

    this asserted distribution as a GeoJSON feature



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

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