Class: CollectingEvent

Inherits:
ActiveRecord::Base
  • Object
show all
Includes:
Housekeeping, Shared::Citable, Shared::Identifiable, Shared::Notable, SoftValidation
Defined in:
app/models/collecting_event.rb

Overview

A collecting event is the unique combination of who, where, when, and how.

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?

Instance Attribute Details

#document_labelString

A print-ready expanded/clarified version of a verbatim_label intended to clarify interpretation of that label. To be used, for example, when reporting Holotype labels.



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

class CollectingEvent < ActiveRecord::Base
  include Housekeeping
  include Shared::Citable
  include Shared::Identifiable
  include Shared::Notable
  include SoftValidation

  belongs_to :geographic_area, inverse_of: :collecting_events
  has_many :collector_roles, class_name: 'Collector', as: :role_object
  has_many :collectors, through: :collector_roles, source: :person

  before_validation :check_verbatim_geolocation_uncertainty,
                    :check_date_range,
                    :check_elevation_range

  validates_uniqueness_of :md5_of_verbatim_label, scope: [:project_id]
  validates_presence_of :verbatim_longitude, if: '!verbatim_latitude.blank?'
  validates_presence_of :verbatim_latitude, if: '!verbatim_longitude.blank?'

  # TODO: factor these out (see also TaxonDetermination, Source::Bibtex)
  validates_numericality_of :start_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :start_date_month,
    in: 1..12, 
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :start_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.start_date_year, a.start_date_month).end_of_month.day },
    :unless => 'start_date_year.nil? || start_date_month.nil?',
    message: '%{value} is not a valid start_date_day for the month provided'
  validates_numericality_of :end_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :end_date_month,
    in: 1..12,
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :end_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.end_date_year, a.end_date_month).end_of_month.day },
    :unless => 'end_date_year.nil? || end_date_month.nil?',
    message: '%{value} is not a valid end_date_day for the month provided'

  soft_validate(:sv_minimally_check_for_a_label)

  def verbatim_label=(value)
    write_attribute(:verbatim_label, value)
    write_attribute(:md5_of_verbatim_label, Utilities::Strings.generate_md5(value))
  end

  def has_start_date?
    !start_date_day.blank? && !start_date_month.blank? && !start_date_year.blank?
  end

  def has_end_date?
    !end_date_day.blank? && !end_date_month.blank? && !end_date_year.blank?
  end

  def end_date
    has_end_date? && Utilities::Dates.nomenclature_date(end_date_day, end_date_month, end_date_year)  
  end

  def start_date
    has_start_date? && Utilities::Dates.nomenclature_date(start_date_day, start_date_month, start_date_year)  
  end 

  protected

  def check_verbatim_geolocation_uncertainty
    errors.add(:verbatim_geolocation_uncertainty, 'Provide both verbatim_latitude and verbatim_longitude if you provide verbatim_uncertainty.') if !verbatim_geolocation_uncertainty.blank? && verbatim_longitude.blank? && verbatim_latitude.blank?
  end

  def check_date_range
    errors.add(:base, 'End date is earlier than start date.') if has_start_date? && has_end_date? && (start_date > end_date)
  end

  def check_elevation_range
    errors.add(:maximum_elevation,  'Maximum elevation is lower than minimum elevation.') if !minimum_elevation.blank? && !maximum_elevation.blank? && maximum_elevation < minimum_elevation
  end

  def sv_minimally_check_for_a_label
    [:verbatim_label, :print_label, :document_label, :field_notes].each do |v|
      return true if !self.send(v).blank?
    end 

    soft_validations.add(:base, 'At least one label type, or field notes, should be provided.')
  end

end

#field_notesString

Any/all field notes that this collecting event was derived from, or that supplement this collecting event.



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

class CollectingEvent < ActiveRecord::Base
  include Housekeeping
  include Shared::Citable
  include Shared::Identifiable
  include Shared::Notable
  include SoftValidation

  belongs_to :geographic_area, inverse_of: :collecting_events
  has_many :collector_roles, class_name: 'Collector', as: :role_object
  has_many :collectors, through: :collector_roles, source: :person

  before_validation :check_verbatim_geolocation_uncertainty,
                    :check_date_range,
                    :check_elevation_range

  validates_uniqueness_of :md5_of_verbatim_label, scope: [:project_id]
  validates_presence_of :verbatim_longitude, if: '!verbatim_latitude.blank?'
  validates_presence_of :verbatim_latitude, if: '!verbatim_longitude.blank?'

  # TODO: factor these out (see also TaxonDetermination, Source::Bibtex)
  validates_numericality_of :start_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :start_date_month,
    in: 1..12, 
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :start_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.start_date_year, a.start_date_month).end_of_month.day },
    :unless => 'start_date_year.nil? || start_date_month.nil?',
    message: '%{value} is not a valid start_date_day for the month provided'
  validates_numericality_of :end_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :end_date_month,
    in: 1..12,
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :end_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.end_date_year, a.end_date_month).end_of_month.day },
    :unless => 'end_date_year.nil? || end_date_month.nil?',
    message: '%{value} is not a valid end_date_day for the month provided'

  soft_validate(:sv_minimally_check_for_a_label)

  def verbatim_label=(value)
    write_attribute(:verbatim_label, value)
    write_attribute(:md5_of_verbatim_label, Utilities::Strings.generate_md5(value))
  end

  def has_start_date?
    !start_date_day.blank? && !start_date_month.blank? && !start_date_year.blank?
  end

  def has_end_date?
    !end_date_day.blank? && !end_date_month.blank? && !end_date_year.blank?
  end

  def end_date
    has_end_date? && Utilities::Dates.nomenclature_date(end_date_day, end_date_month, end_date_year)  
  end

  def start_date
    has_start_date? && Utilities::Dates.nomenclature_date(start_date_day, start_date_month, start_date_year)  
  end 

  protected

  def check_verbatim_geolocation_uncertainty
    errors.add(:verbatim_geolocation_uncertainty, 'Provide both verbatim_latitude and verbatim_longitude if you provide verbatim_uncertainty.') if !verbatim_geolocation_uncertainty.blank? && verbatim_longitude.blank? && verbatim_latitude.blank?
  end

  def check_date_range
    errors.add(:base, 'End date is earlier than start date.') if has_start_date? && has_end_date? && (start_date > end_date)
  end

  def check_elevation_range
    errors.add(:maximum_elevation,  'Maximum elevation is lower than minimum elevation.') if !minimum_elevation.blank? && !maximum_elevation.blank? && maximum_elevation < minimum_elevation
  end

  def sv_minimally_check_for_a_label
    [:verbatim_label, :print_label, :document_label, :field_notes].each do |v|
      return true if !self.send(v).blank?
    end 

    soft_validations.add(:base, 'At least one label type, or field notes, should be provided.')
  end

end

A print-formatted ready represenatation of this collecting event. !! Do not assume that this remains static, it can change over time with user needs.



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

class CollectingEvent < ActiveRecord::Base
  include Housekeeping
  include Shared::Citable
  include Shared::Identifiable
  include Shared::Notable
  include SoftValidation

  belongs_to :geographic_area, inverse_of: :collecting_events
  has_many :collector_roles, class_name: 'Collector', as: :role_object
  has_many :collectors, through: :collector_roles, source: :person

  before_validation :check_verbatim_geolocation_uncertainty,
                    :check_date_range,
                    :check_elevation_range

  validates_uniqueness_of :md5_of_verbatim_label, scope: [:project_id]
  validates_presence_of :verbatim_longitude, if: '!verbatim_latitude.blank?'
  validates_presence_of :verbatim_latitude, if: '!verbatim_longitude.blank?'

  # TODO: factor these out (see also TaxonDetermination, Source::Bibtex)
  validates_numericality_of :start_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :start_date_month,
    in: 1..12, 
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :start_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.start_date_year, a.start_date_month).end_of_month.day },
    :unless => 'start_date_year.nil? || start_date_month.nil?',
    message: '%{value} is not a valid start_date_day for the month provided'
  validates_numericality_of :end_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :end_date_month,
    in: 1..12,
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :end_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.end_date_year, a.end_date_month).end_of_month.day },
    :unless => 'end_date_year.nil? || end_date_month.nil?',
    message: '%{value} is not a valid end_date_day for the month provided'

  soft_validate(:sv_minimally_check_for_a_label)

  def verbatim_label=(value)
    write_attribute(:verbatim_label, value)
    write_attribute(:md5_of_verbatim_label, Utilities::Strings.generate_md5(value))
  end

  def has_start_date?
    !start_date_day.blank? && !start_date_month.blank? && !start_date_year.blank?
  end

  def has_end_date?
    !end_date_day.blank? && !end_date_month.blank? && !end_date_year.blank?
  end

  def end_date
    has_end_date? && Utilities::Dates.nomenclature_date(end_date_day, end_date_month, end_date_year)  
  end

  def start_date
    has_start_date? && Utilities::Dates.nomenclature_date(start_date_day, start_date_month, start_date_year)  
  end 

  protected

  def check_verbatim_geolocation_uncertainty
    errors.add(:verbatim_geolocation_uncertainty, 'Provide both verbatim_latitude and verbatim_longitude if you provide verbatim_uncertainty.') if !verbatim_geolocation_uncertainty.blank? && verbatim_longitude.blank? && verbatim_latitude.blank?
  end

  def check_date_range
    errors.add(:base, 'End date is earlier than start date.') if has_start_date? && has_end_date? && (start_date > end_date)
  end

  def check_elevation_range
    errors.add(:maximum_elevation,  'Maximum elevation is lower than minimum elevation.') if !minimum_elevation.blank? && !maximum_elevation.blank? && maximum_elevation < minimum_elevation
  end

  def sv_minimally_check_for_a_label
    [:verbatim_label, :print_label, :document_label, :field_notes].each do |v|
      return true if !self.send(v).blank?
    end 

    soft_validations.add(:base, 'At least one label type, or field notes, should be provided.')
  end

end

#verbatim_labelString

A verbatim representation of label that defined this collecting event, typically, but not exclusively, used for retroactive data capture.



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

class CollectingEvent < ActiveRecord::Base
  include Housekeeping
  include Shared::Citable
  include Shared::Identifiable
  include Shared::Notable
  include SoftValidation

  belongs_to :geographic_area, inverse_of: :collecting_events
  has_many :collector_roles, class_name: 'Collector', as: :role_object
  has_many :collectors, through: :collector_roles, source: :person

  before_validation :check_verbatim_geolocation_uncertainty,
                    :check_date_range,
                    :check_elevation_range

  validates_uniqueness_of :md5_of_verbatim_label, scope: [:project_id]
  validates_presence_of :verbatim_longitude, if: '!verbatim_latitude.blank?'
  validates_presence_of :verbatim_latitude, if: '!verbatim_longitude.blank?'

  # TODO: factor these out (see also TaxonDetermination, Source::Bibtex)
  validates_numericality_of :start_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :start_date_month,
    in: 1..12, 
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :start_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.start_date_year, a.start_date_month).end_of_month.day },
    :unless => 'start_date_year.nil? || start_date_month.nil?',
    message: '%{value} is not a valid start_date_day for the month provided'
  validates_numericality_of :end_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :end_date_month,
    in: 1..12,
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :end_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.end_date_year, a.end_date_month).end_of_month.day },
    :unless => 'end_date_year.nil? || end_date_month.nil?',
    message: '%{value} is not a valid end_date_day for the month provided'

  soft_validate(:sv_minimally_check_for_a_label)

  def verbatim_label=(value)
    write_attribute(:verbatim_label, value)
    write_attribute(:md5_of_verbatim_label, Utilities::Strings.generate_md5(value))
  end

  def has_start_date?
    !start_date_day.blank? && !start_date_month.blank? && !start_date_year.blank?
  end

  def has_end_date?
    !end_date_day.blank? && !end_date_month.blank? && !end_date_year.blank?
  end

  def end_date
    has_end_date? && Utilities::Dates.nomenclature_date(end_date_day, end_date_month, end_date_year)  
  end

  def start_date
    has_start_date? && Utilities::Dates.nomenclature_date(start_date_day, start_date_month, start_date_year)  
  end 

  protected

  def check_verbatim_geolocation_uncertainty
    errors.add(:verbatim_geolocation_uncertainty, 'Provide both verbatim_latitude and verbatim_longitude if you provide verbatim_uncertainty.') if !verbatim_geolocation_uncertainty.blank? && verbatim_longitude.blank? && verbatim_latitude.blank?
  end

  def check_date_range
    errors.add(:base, 'End date is earlier than start date.') if has_start_date? && has_end_date? && (start_date > end_date)
  end

  def check_elevation_range
    errors.add(:maximum_elevation,  'Maximum elevation is lower than minimum elevation.') if !minimum_elevation.blank? && !maximum_elevation.blank? && maximum_elevation < minimum_elevation
  end

  def sv_minimally_check_for_a_label
    [:verbatim_label, :print_label, :document_label, :field_notes].each do |v|
      return true if !self.send(v).blank?
    end 

    soft_validations.add(:base, 'At least one label type, or field notes, should be provided.')
  end

end

#verbatim_localityString

A string, typically sliced from verbatim_label, that represents the locality, including any modifiers (2 mi NE).



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

class CollectingEvent < ActiveRecord::Base
  include Housekeeping
  include Shared::Citable
  include Shared::Identifiable
  include Shared::Notable
  include SoftValidation

  belongs_to :geographic_area, inverse_of: :collecting_events
  has_many :collector_roles, class_name: 'Collector', as: :role_object
  has_many :collectors, through: :collector_roles, source: :person

  before_validation :check_verbatim_geolocation_uncertainty,
                    :check_date_range,
                    :check_elevation_range

  validates_uniqueness_of :md5_of_verbatim_label, scope: [:project_id]
  validates_presence_of :verbatim_longitude, if: '!verbatim_latitude.blank?'
  validates_presence_of :verbatim_latitude, if: '!verbatim_longitude.blank?'

  # TODO: factor these out (see also TaxonDetermination, Source::Bibtex)
  validates_numericality_of :start_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :start_date_month,
    in: 1..12, 
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :start_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.start_date_year, a.start_date_month).end_of_month.day },
    :unless => 'start_date_year.nil? || start_date_month.nil?',
    message: '%{value} is not a valid start_date_day for the month provided'
  validates_numericality_of :end_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :end_date_month,
    in: 1..12,
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :end_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.end_date_year, a.end_date_month).end_of_month.day },
    :unless => 'end_date_year.nil? || end_date_month.nil?',
    message: '%{value} is not a valid end_date_day for the month provided'

  soft_validate(:sv_minimally_check_for_a_label)

  def verbatim_label=(value)
    write_attribute(:verbatim_label, value)
    write_attribute(:md5_of_verbatim_label, Utilities::Strings.generate_md5(value))
  end

  def has_start_date?
    !start_date_day.blank? && !start_date_month.blank? && !start_date_year.blank?
  end

  def has_end_date?
    !end_date_day.blank? && !end_date_month.blank? && !end_date_year.blank?
  end

  def end_date
    has_end_date? && Utilities::Dates.nomenclature_date(end_date_day, end_date_month, end_date_year)  
  end

  def start_date
    has_start_date? && Utilities::Dates.nomenclature_date(start_date_day, start_date_month, start_date_year)  
  end 

  protected

  def check_verbatim_geolocation_uncertainty
    errors.add(:verbatim_geolocation_uncertainty, 'Provide both verbatim_latitude and verbatim_longitude if you provide verbatim_uncertainty.') if !verbatim_geolocation_uncertainty.blank? && verbatim_longitude.blank? && verbatim_latitude.blank?
  end

  def check_date_range
    errors.add(:base, 'End date is earlier than start date.') if has_start_date? && has_end_date? && (start_date > end_date)
  end

  def check_elevation_range
    errors.add(:maximum_elevation,  'Maximum elevation is lower than minimum elevation.') if !minimum_elevation.blank? && !maximum_elevation.blank? && maximum_elevation < minimum_elevation
  end

  def sv_minimally_check_for_a_label
    [:verbatim_label, :print_label, :document_label, :field_notes].each do |v|
      return true if !self.send(v).blank?
    end 

    soft_validations.add(:base, 'At least one label type, or field notes, should be provided.')
  end

end

#verbatim_longitudeString

A string, typically sliced from verbatim_label, that represents the latitude. Is used to derive mappable values, but does not get mapped itself.



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

class CollectingEvent < ActiveRecord::Base
  include Housekeeping
  include Shared::Citable
  include Shared::Identifiable
  include Shared::Notable
  include SoftValidation

  belongs_to :geographic_area, inverse_of: :collecting_events
  has_many :collector_roles, class_name: 'Collector', as: :role_object
  has_many :collectors, through: :collector_roles, source: :person

  before_validation :check_verbatim_geolocation_uncertainty,
                    :check_date_range,
                    :check_elevation_range

  validates_uniqueness_of :md5_of_verbatim_label, scope: [:project_id]
  validates_presence_of :verbatim_longitude, if: '!verbatim_latitude.blank?'
  validates_presence_of :verbatim_latitude, if: '!verbatim_longitude.blank?'

  # TODO: factor these out (see also TaxonDetermination, Source::Bibtex)
  validates_numericality_of :start_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :start_date_month,
    in: 1..12, 
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :start_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.start_date_year, a.start_date_month).end_of_month.day },
    :unless => 'start_date_year.nil? || start_date_month.nil?',
    message: '%{value} is not a valid start_date_day for the month provided'
  validates_numericality_of :end_date_year,
    only_integer: true, greater_than: 0,
    less_than_or_equal_to: Time.now.year,
    allow_nil: true,
    message: 'start date year must be an integer greater than 0'
  validates_inclusion_of :end_date_month,
    in: 1..12,
    allow_nil: true,
    message: ' start date month'
  validates_numericality_of :end_date_day,
    allow_nil: true,
    only_integer: true,
    greater_than: 0,
    less_than_or_equal_to: Proc.new { |a| Time.utc(a.end_date_year, a.end_date_month).end_of_month.day },
    :unless => 'end_date_year.nil? || end_date_month.nil?',
    message: '%{value} is not a valid end_date_day for the month provided'

  soft_validate(:sv_minimally_check_for_a_label)

  def verbatim_label=(value)
    write_attribute(:verbatim_label, value)
    write_attribute(:md5_of_verbatim_label, Utilities::Strings.generate_md5(value))
  end

  def has_start_date?
    !start_date_day.blank? && !start_date_month.blank? && !start_date_year.blank?
  end

  def has_end_date?
    !end_date_day.blank? && !end_date_month.blank? && !end_date_year.blank?
  end

  def end_date
    has_end_date? && Utilities::Dates.nomenclature_date(end_date_day, end_date_month, end_date_year)  
  end

  def start_date
    has_start_date? && Utilities::Dates.nomenclature_date(start_date_day, start_date_month, start_date_year)  
  end 

  protected

  def check_verbatim_geolocation_uncertainty
    errors.add(:verbatim_geolocation_uncertainty, 'Provide both verbatim_latitude and verbatim_longitude if you provide verbatim_uncertainty.') if !verbatim_geolocation_uncertainty.blank? && verbatim_longitude.blank? && verbatim_latitude.blank?
  end

  def check_date_range
    errors.add(:base, 'End date is earlier than start date.') if has_start_date? && has_end_date? && (start_date > end_date)
  end

  def check_elevation_range
    errors.add(:maximum_elevation,  'Maximum elevation is lower than minimum elevation.') if !minimum_elevation.blank? && !maximum_elevation.blank? && maximum_elevation < minimum_elevation
  end

  def sv_minimally_check_for_a_label
    [:verbatim_label, :print_label, :document_label, :field_notes].each do |v|
      return true if !self.send(v).blank?
    end 

    soft_validations.add(:base, 'At least one label type, or field notes, should be provided.')
  end

end

Instance Method Details

#end_dateObject



95
96
97
# File 'app/models/collecting_event.rb', line 95

def end_date
  has_end_date? && Utilities::Dates.nomenclature_date(end_date_day, end_date_month, end_date_year)  
end

#has_end_date?Boolean



91
92
93
# File 'app/models/collecting_event.rb', line 91

def has_end_date?
  !end_date_day.blank? && !end_date_month.blank? && !end_date_year.blank?
end

#has_start_date?Boolean



87
88
89
# File 'app/models/collecting_event.rb', line 87

def has_start_date?
  !start_date_day.blank? && !start_date_month.blank? && !start_date_year.blank?
end

#start_dateObject



99
100
101
# File 'app/models/collecting_event.rb', line 99

def start_date
  has_start_date? && Utilities::Dates.nomenclature_date(start_date_day, start_date_month, start_date_year)  
end