Class: AssertedDistribution

Inherits:
ApplicationRecord show all
Includes:
DwcExtensions, Housekeeping, Shared::Citations, Shared::Confidences, Shared::DataAttributes, Shared::IsData, Shared::IsDwcOccurrence, Shared::Notes, Shared::Tags, 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?

Methods included from ActiverecordUtilities

#trim_attributes

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

class AssertedDistribution < ApplicationRecord
  include Housekeeping
  include Shared::Notes
  include SoftValidation
  include Shared::IsData
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::Citations
  include Shared::Confidences

  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions

  belongs_to :otu, inverse_of: :asserted_distributions
  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

  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)

  # @param [ActionController::Parameters] params
  # @return [Scope]
  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

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

  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 [Nil]
  def new_records_include_otu

  end

  # @return [Boolean]
  def sv_conflicting_geographic_area
    ga = self.geographic_area
    unless ga.nil?
      if self.is_absent == true # this returns an array, not a single GA so test below is not right
        presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id)
        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 [Hash] options of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return [Array] an array of AssertedDistributions
  def self.stub_new(options = {})
    options.symbolize_keys!
    result = []
    options[:geographic_areas].each do |ga|
      result.push(
        AssertedDistribution.new(otu_id:                     options[:otu_id],
                                 geographic_area:            ga,
                                 origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end

end

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

class AssertedDistribution < ApplicationRecord
  include Housekeeping
  include Shared::Notes
  include SoftValidation
  include Shared::IsData
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::Citations
  include Shared::Confidences

  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions

  belongs_to :otu, inverse_of: :asserted_distributions
  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

  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)

  # @param [ActionController::Parameters] params
  # @return [Scope]
  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

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

  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 [Nil]
  def new_records_include_otu

  end

  # @return [Boolean]
  def sv_conflicting_geographic_area
    ga = self.geographic_area
    unless ga.nil?
      if self.is_absent == true # this returns an array, not a single GA so test below is not right
        presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id)
        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 [Hash] options of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return [Array] an array of AssertedDistributions
  def self.stub_new(options = {})
    options.symbolize_keys!
    result = []
    options[:geographic_areas].each do |ga|
      result.push(
        AssertedDistribution.new(otu_id:                     options[:otu_id],
                                 geographic_area:            ga,
                                 origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end

end

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

class AssertedDistribution < ApplicationRecord
  include Housekeeping
  include Shared::Notes
  include SoftValidation
  include Shared::IsData
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::Citations
  include Shared::Confidences

  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions

  belongs_to :otu, inverse_of: :asserted_distributions
  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

  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)

  # @param [ActionController::Parameters] params
  # @return [Scope]
  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

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

  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 [Nil]
  def new_records_include_otu

  end

  # @return [Boolean]
  def sv_conflicting_geographic_area
    ga = self.geographic_area
    unless ga.nil?
      if self.is_absent == true # this returns an array, not a single GA so test below is not right
        presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id)
        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 [Hash] options of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return [Array] an array of AssertedDistributions
  def self.stub_new(options = {})
    options.symbolize_keys!
    result = []
    options[:geographic_areas].each do |ga|
      result.push(
        AssertedDistribution.new(otu_id:                     options[:otu_id],
                                 geographic_area:            ga,
                                 origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end

end

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

class AssertedDistribution < ApplicationRecord
  include Housekeeping
  include Shared::Notes
  include SoftValidation
  include Shared::IsData
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::Citations
  include Shared::Confidences

  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions

  belongs_to :otu, inverse_of: :asserted_distributions
  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

  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)

  # @param [ActionController::Parameters] params
  # @return [Scope]
  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

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

  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 [Nil]
  def new_records_include_otu

  end

  # @return [Boolean]
  def sv_conflicting_geographic_area
    ga = self.geographic_area
    unless ga.nil?
      if self.is_absent == true # this returns an array, not a single GA so test below is not right
        presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id)
        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 [Hash] options of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return [Array] an array of AssertedDistributions
  def self.stub_new(options = {})
    options.symbolize_keys!
    result = []
    options[:geographic_areas].each do |ga|
      result.push(
        AssertedDistribution.new(otu_id:                     options[:otu_id],
                                 geographic_area:            ga,
                                 origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end

end

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

class AssertedDistribution < ApplicationRecord
  include Housekeeping
  include Shared::Notes
  include SoftValidation
  include Shared::IsData
  include Shared::Tags
  include Shared::DataAttributes
  include Shared::Citations
  include Shared::Confidences

  include Shared::IsDwcOccurrence
  include AssertedDistribution::DwcExtensions

  belongs_to :otu, inverse_of: :asserted_distributions
  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

  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)

  # @param [ActionController::Parameters] params
  # @return [Scope]
  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

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

  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 [Nil]
  def new_records_include_otu

  end

  # @return [Boolean]
  def sv_conflicting_geographic_area
    ga = self.geographic_area
    unless ga.nil?
      if self.is_absent == true # this returns an array, not a single GA so test below is not right
        presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id)
        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 [Hash] options of e.g., {otu_id: 5, source_id: 5, geographic_areas: Array of {GeographicArea}}
  # @return [Array] an array of AssertedDistributions
  def self.stub_new(options = {})
    options.symbolize_keys!
    result = []
    options[:geographic_areas].each do |ga|
      result.push(
        AssertedDistribution.new(otu_id:                     options[:otu_id],
                                 geographic_area:            ga,
                                 origin_citation_attributes: {source_id: options[:source_id]})
      )
    end
    result
  end

end

Class Method Details

+ (Scope) find_for_autocomplete(params)

Parameters:

  • params (ActionController::Parameters)

Returns:

  • (Scope)


70
71
72
73
74
75
# File 'app/models/asserted_distribution.rb', line 70

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

+ (AssertedDistribution) stub(defaults: {})

Returns used to also stub an #origin_citation, as required

Parameters:

  • defaults (Hash)

Returns:



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

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

+ (Array) stub_new(options = {}) (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



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

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) geographic_item



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

def geographic_item
  geographic_area.default_geographic_item
end

- (Boolean) new_records_include_citation (protected)

Returns:

  • (Boolean)


115
116
117
118
119
# File 'app/models/asserted_distribution.rb', line 115

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

- (Nil) new_records_include_otu (protected)

Returns:

  • (Nil)


122
123
124
# File 'app/models/asserted_distribution.rb', line 122

def new_records_include_otu

end

- (True) requires_citation?

Returns see citable.rb

Returns:

  • (True)

    see citable.rb



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

def requires_citation?
  true
end

- (Boolean) sv_conflicting_geographic_area (protected)

Returns:

  • (Boolean)


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

def sv_conflicting_geographic_area
  ga = self.geographic_area
  unless ga.nil?
    if self.is_absent == true # this returns an array, not a single GA so test below is not right
      presence = AssertedDistribution.without_is_absent.with_geographic_area_id(self.geographic_area_id)
      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

rubocop:disable Style/StringHashKeys

Returns:

  • (Hash)

    GeoJSON feature



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

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