Skip to content

Commit

Permalink
add more validations for observation reports
Browse files Browse the repository at this point in the history
  • Loading branch information
tsubik committed Jan 23, 2024
1 parent f6858c4 commit 78f83e3
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/admin/observation_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,8 @@ def apply_filtering(chain)
attributes_table do
row :title
row :publication_date
row :observers
row :observations
row :user
row :created_at
row :updated_at
Expand Down
5 changes: 5 additions & 0 deletions app/models/observation_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ class ObservationReport < ApplicationRecord
has_many :observers, through: :observation_report_observers, after_add: :self_touch, after_remove: :self_touch
has_many :observations, dependent: :destroy

validates :attachment, presence: true
validates :observers, presence: true
validates :title, presence: true
validates :publication_date, presence: true

skip_callback :commit, :after, :remove_attachment!
after_destroy :move_attachment_to_private_directory
after_restore :move_attachment_to_public_directory
Expand Down
14 changes: 13 additions & 1 deletion spec/integration/v1/observation_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,25 @@
module V1
describe "Observation Reports", type: :request do
let(:user) { create(:user) }
let(:document_data) {
"data:application/pdf;base64,#{Base64.encode64(File.read(File.join(Rails.root, "spec", "support", "files", "doc.pdf")))}"
}
let(:observer) { create(:observer) }

it_behaves_like "jsonapi-resources", ObservationReport, {
show: {},
create: {
success_roles: %i[admin],
failure_roles: %i[user],
valid_params: -> { {title: "Report one", relationships: {user: user.id}} }
valid_params: -> {
{
title: "Report one",
"publication-date": Time.zone.today.to_s,
attachment: document_data,
relationships: {user: user.id, observers: [observer.id]}
}
},
excluded_params: %i[attachment publication-date] # workaround as comparing publication-date does not work
},
edit: {
success_roles: %i[admin],
Expand Down
12 changes: 12 additions & 0 deletions spec/models/observation_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,18 @@
expect(subject).to be_valid
end

describe "Validations" do
it { is_expected.to validate_presence_of(:title) }
it { is_expected.to validate_presence_of(:observers) }
it { is_expected.to validate_presence_of(:publication_date) }

it "validates presence of attachment" do
subject.remove_attachment!
expect(subject).not_to be_valid
expect(subject.errors.messages[:attachment]).to include("can't be blank")
end
end

describe "hooks" do
describe "sync_observation_observers" do
let!(:observer) { create(:observer) }
Expand Down

0 comments on commit 78f83e3

Please sign in to comment.