Skip to content

Commit

Permalink
Merge pull request #2414 from MushroomObserver/jdc-harden-nil-notes
Browse files Browse the repository at this point in the history
Prevent Error for nil Notes
  • Loading branch information
JoeCohen authored Sep 28, 2024
2 parents 7798836 + 56bbe46 commit 4f6c265
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 8 deletions.
19 changes: 11 additions & 8 deletions app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,10 @@ def self.no_notes
{}
end

def notes
self[:notes] || {}
end

# no_notes persisted in the db
def self.no_notes_persisted
no_notes.to_yaml
Expand Down Expand Up @@ -1172,14 +1176,13 @@ def turn_off_specimen_if_no_more_records
##############################################################################

# Which agent created this observation?
enum source:
{
mo_website: 1,
mo_android_app: 2,
mo_iphone_app: 3,
mo_api: 4,
mo_inat_import: 5
}
enum :source, {
mo_website: 1,
mo_android_app: 2,
mo_iphone_app: 3,
mo_api: 4,
mo_inat_import: 5
}

# Message to use to credit the agent which created this observation.
# Intended to be used with .tpl to render as HTML:
Expand Down
13 changes: 13 additions & 0 deletions test/models/observation_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -917,6 +917,19 @@ def test_notes_parts_values
assert_equal("pine", obs.notes_part_value("Nearby trees"))
end

def test_notes_nil
User.current = mary
obs = Observation.create!(name_id: names(:fungi).id, when_str: "2020-07-05")

assert_nothing_raised do
obs.notes[:Collector]
rescue StandardError => e
flunk(
"It shouldn't throw \"#{e.message}\" when reading part of a nil Note"
)
end
end

def test_make_sure_no_observations_are_misspelled
good = names(:peltigera)
bad = names(:petigera)
Expand Down

0 comments on commit 4f6c265

Please sign in to comment.