Class: RangedLotCategory

Inherits:
ApplicationRecord show all
Includes:
Housekeeping, Shared::IsData, SoftValidation
Defined in:
app/models/ranged_lot_category.rb

Overview

A RangedLotCategory is an estimate used to assert that a CollectoinObject contains somewhere between a minimum and maximum number of individuals, as asserted by a curator of that CollectionObject. When a RangedLotCategory is assigned to a CollectionObject then no CollectionObject#total can be provided.

Constant Summary

Constant Summary

Constants included from SoftValidation

SoftValidation::ANCESTORS_WITH_SOFT_VALIDATIONS

Instance Attribute 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) maximum_value

TODO:

Returns:

  • (Integer)


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

class RangedLotCategory < ApplicationRecord
  include Housekeeping
  include Shared::IsData
  include SoftValidation

  validates_presence_of :name, :minimum_value
  validate :validate_values
  validates :minimum_value, numericality: {greater_than: 0, only_integer: true}, allow_nil: false
  validates :maximum_value, numericality: {greater_than: 1, only_integer: true}, allow_nil: false

  validates_uniqueness_of :name, scope: [:project_id]

  has_many :ranged_lots, inverse_of: :ranged_lot_category, dependent: :restrict_with_error

  soft_validate(:sv_range_does_not_overlap)

  protected

  # @return [Object]
  def validate_values
    errors.add(:maximum_value, 'minimum value must be less than maximum value') if self.minimum_value && self.maximum_value && (self.minimum_value >= self.maximum_value)
  end

  # @return [Object]
  def sv_range_does_not_overlap
    soft_validations.add(:minimum_value, 'The range of values overlaps with another defined range of values.') if RangedLotCategory.where('minimum_value >= ? and maximum_value <= ? and project_id = ?', minimum_value, maximum_value, project_id)
  end

end

- (Integer) minimum_value

TODO:

Returns:

  • (Integer)


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

class RangedLotCategory < ApplicationRecord
  include Housekeeping
  include Shared::IsData
  include SoftValidation

  validates_presence_of :name, :minimum_value
  validate :validate_values
  validates :minimum_value, numericality: {greater_than: 0, only_integer: true}, allow_nil: false
  validates :maximum_value, numericality: {greater_than: 1, only_integer: true}, allow_nil: false

  validates_uniqueness_of :name, scope: [:project_id]

  has_many :ranged_lots, inverse_of: :ranged_lot_category, dependent: :restrict_with_error

  soft_validate(:sv_range_does_not_overlap)

  protected

  # @return [Object]
  def validate_values
    errors.add(:maximum_value, 'minimum value must be less than maximum value') if self.minimum_value && self.maximum_value && (self.minimum_value >= self.maximum_value)
  end

  # @return [Object]
  def sv_range_does_not_overlap
    soft_validations.add(:minimum_value, 'The range of values overlaps with another defined range of values.') if RangedLotCategory.where('minimum_value >= ? and maximum_value <= ? and project_id = ?', minimum_value, maximum_value, project_id)
  end

end

- (String) name

TODO:

Returns:

  • (String)


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

class RangedLotCategory < ApplicationRecord
  include Housekeeping
  include Shared::IsData
  include SoftValidation

  validates_presence_of :name, :minimum_value
  validate :validate_values
  validates :minimum_value, numericality: {greater_than: 0, only_integer: true}, allow_nil: false
  validates :maximum_value, numericality: {greater_than: 1, only_integer: true}, allow_nil: false

  validates_uniqueness_of :name, scope: [:project_id]

  has_many :ranged_lots, inverse_of: :ranged_lot_category, dependent: :restrict_with_error

  soft_validate(:sv_range_does_not_overlap)

  protected

  # @return [Object]
  def validate_values
    errors.add(:maximum_value, 'minimum value must be less than maximum value') if self.minimum_value && self.maximum_value && (self.minimum_value >= self.maximum_value)
  end

  # @return [Object]
  def sv_range_does_not_overlap
    soft_validations.add(:minimum_value, 'The range of values overlaps with another defined range of values.') if RangedLotCategory.where('minimum_value >= ? and maximum_value <= ? and project_id = ?', minimum_value, maximum_value, project_id)
  end

end

- (Integer) project_id

the project ID

Returns:

  • (Integer)


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

class RangedLotCategory < ApplicationRecord
  include Housekeeping
  include Shared::IsData
  include SoftValidation

  validates_presence_of :name, :minimum_value
  validate :validate_values
  validates :minimum_value, numericality: {greater_than: 0, only_integer: true}, allow_nil: false
  validates :maximum_value, numericality: {greater_than: 1, only_integer: true}, allow_nil: false

  validates_uniqueness_of :name, scope: [:project_id]

  has_many :ranged_lots, inverse_of: :ranged_lot_category, dependent: :restrict_with_error

  soft_validate(:sv_range_does_not_overlap)

  protected

  # @return [Object]
  def validate_values
    errors.add(:maximum_value, 'minimum value must be less than maximum value') if self.minimum_value && self.maximum_value && (self.minimum_value >= self.maximum_value)
  end

  # @return [Object]
  def sv_range_does_not_overlap
    soft_validations.add(:minimum_value, 'The range of values overlaps with another defined range of values.') if RangedLotCategory.where('minimum_value >= ? and maximum_value <= ? and project_id = ?', minimum_value, maximum_value, project_id)
  end

end

Instance Method Details

- (Object) sv_range_does_not_overlap (protected)

Returns:

  • (Object)


45
46
47
# File 'app/models/ranged_lot_category.rb', line 45

def sv_range_does_not_overlap
  soft_validations.add(:minimum_value, 'The range of values overlaps with another defined range of values.') if RangedLotCategory.where('minimum_value >= ? and maximum_value <= ? and project_id = ?', minimum_value, maximum_value, project_id)
end

- (Object) validate_values (protected)

Returns:

  • (Object)


40
41
42
# File 'app/models/ranged_lot_category.rb', line 40

def validate_values
  errors.add(:maximum_value, 'minimum value must be less than maximum value') if self.minimum_value && self.maximum_value && (self.minimum_value >= self.maximum_value)
end