Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/observation multiple evidences #480

Merged
merged 23 commits into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
42e1974
create observation document - observation join table
tsubik Dec 22, 2023
fa336a2
move evidence type to observation document and name it as document_type
tsubik Jan 3, 2024
4f2804f
fix migration
tsubik Jan 5, 2024
7e29cff
add observation_report reference to observation documents
tsubik Jan 5, 2024
70a3f5e
API changes for multiple evidences
tsubik Jan 9, 2024
0ba3e6e
fix observation document uploader
tsubik Jan 9, 2024
fa1d577
remove evidence_type from obs histories as this is no needed
tsubik Jan 11, 2024
66ee048
update migration saving old evidence_type as evidence_type_v0 in pape…
tsubik Jan 11, 2024
1d35c7b
fixing specs
tsubik Jan 11, 2024
f08477c
update model annotations
tsubik Jan 25, 2024
1bbfc41
add observation-report-id to observation document resource to not hav…
tsubik Jan 26, 2024
03151e8
add deleted_at to obs-documents-observations join table, update gener…
tsubik Jan 26, 2024
e6cab37
admin observation report show page: add documents/evidences
tsubik Jan 26, 2024
4d4dc1f
fix not visible documents on index page after moving observation to r…
tsubik Jan 26, 2024
8004ba0
change how admin can add and attach evidences to observations
tsubik Mar 20, 2024
00050a2
tweak observation form, show only relevant evidence inputs based on e…
tsubik Mar 20, 2024
db41407
fix problem with evidences that does not belong to report somehow
tsubik Mar 21, 2024
4a776f6
fix issue with uploader, validate evidence attachment
tsubik Mar 21, 2024
f8f947b
validate name for observation evidence
tsubik Mar 21, 2024
04a9c2d
fixing specs
tsubik Mar 21, 2024
077bc37
remove user from observation documents as it is not even used
tsubik Mar 21, 2024
d822b2f
fix permissions
tsubik Mar 21, 2024
8e24cc9
user reference was removed from observation document
tsubik Mar 21, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
move evidence type to observation document and name it as document_type
  • Loading branch information
tsubik committed Mar 25, 2024
commit fa336a243321b7c5b8d6cba083a7cb1ee4dc081c
4 changes: 4 additions & 0 deletions app/admin/observation_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
csv do
column :id
column :name
column :document_type
column :user do |od|
od.user&.name
end
Expand All @@ -37,6 +38,7 @@
column :id
column :observations
column :name
column :document_type
column :attachment do |o|
link_to o&.name, o.attachment&.url if o.attachment&.url
end
Expand Down Expand Up @@ -73,6 +75,7 @@
f.input :observations, input_html: {disabled: true}
f.input :user, input_html: {disabled: true}
f.input :name
f.input :document_type
f.input :attachment, as: :file, hint: f.object&.attachment&.file&.filename

f.actions
Expand All @@ -83,6 +86,7 @@
attributes_table do
row :id
row :observations
row :document_type
row :attachment do |o|
link_to o&.name, o.attachment&.url if o.attachment&.url
end
Expand Down
2 changes: 1 addition & 1 deletion app/importers/observations_importer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
class ObservationsImporter < FileDataImport::BaseImporter
PERMITTED_ATTRIBUTES = %i[
observation_type publication_date pv is_active location_accuracy
lat lng fmu_id evidence_type location_information actions_taken
lat lng fmu_id location_information actions_taken
evidence_on_report validation_status is_physical_place user_id
observer_ids
].freeze
Expand Down
29 changes: 12 additions & 17 deletions app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
# law_id :integer
# location_information :string
# is_physical_place :boolean default(TRUE), not null
# evidence_type :integer
# location_accuracy :integer
# evidence_on_report :string
# hidden :boolean default(FALSE), not null
Expand All @@ -50,18 +49,18 @@ class Observation < ApplicationRecord
translates :details, :concern_opinion, :litigation_status, touch: true, versioning: :paper_trail, paranoia: true
active_admin_translates :details, :concern_opinion, :litigation_status

enum evidence_type: {
"No Evidence" => 0, "Uploaded documents" => 1, "Evidence presented in the report" => 2
}
enum observation_type: {"operator" => 0, "government" => 1}
enum validation_status: {"Created" => 0, "Ready for QC" => 1, "QC in progress" => 2, "Approved" => 3,
"Rejected" => 4, "Needs revision" => 5, "Ready for publication" => 6,
"Published (no comments)" => 7, "Published (not modified)" => 8,
"Published (modified)" => 9}
enum evidence_type: {"Government Documents" => 0, "Company Documents" => 1, "Photos" => 2,
"Testimony from local communities" => 3, "Other" => 4, "Evidence presented in the report" => 5,
"Maps" => 6}
enum location_accuracy: {"Estimated location" => 0, "GPS coordinates extracted from photo" => 1,
"Accurate GPS coordinates" => 2}

validate_enum_attributes :observation_type, :evidence_type, :location_accuracy
validate_enum_attributes :evidence_type, :observation_type, :location_accuracy

STATUS_TRANSITIONS = {
monitor: {
Expand Down Expand Up @@ -127,7 +126,7 @@ class Observation < ApplicationRecord

validates :lat, numericality: {greater_than_or_equal_to: -90, less_than_or_equal_to: 90, allow_blank: true}
validates :lng, numericality: {greater_than_or_equal_to: -180, less_than_or_equal_to: 180, allow_blank: true}
validate :evidence_presented_in_the_report
validates :evidence_on_report, presence: true, if: -> { evidence_type == "Evidence presented in the report" }
validate :status_changes, if: -> { user_type.present? }

validates :observers, presence: true
Expand All @@ -137,6 +136,7 @@ class Observation < ApplicationRecord
validates :admin_comment, presence: true, if: -> { validation_status == "Needs revision" }

before_validation :assign_observers_from_report, if: :observation_report_changed?
before_validation :nullify_evidence_on_report, if: -> { evidence_type != "Evidence presented in the report" }

before_save :set_active_status
before_save :nullify_fmu_and_coordinates, unless: :is_physical_place
Expand All @@ -152,7 +152,7 @@ class Observation < ApplicationRecord

after_save :create_history, if: :saved_changes?

after_save :remove_documents, if: -> { evidence_type == "Evidence presented in the report" }
after_save :remove_documents, if: -> { evidence_type != "Uploaded documents" }
after_save :update_fmu_geojson

after_commit :notify_about_creation, on: :create
Expand Down Expand Up @@ -221,6 +221,10 @@ def published?

private

def nullify_evidence_on_report
self.evidence_on_report = nil
end

def nullify_fmu_and_coordinates
self.lat = nil
self.lng = nil
Expand Down Expand Up @@ -296,17 +300,8 @@ def status_changes
"Invalid validation change for #{@user_type}. Can't move from '#{validation_status_was}' to '#{validation_status}'")
end

def evidence_presented_in_the_report
if evidence_type == "Evidence presented in the report" && evidence_on_report.blank?
errors.add(:evidence_on_report, "You must add information on where to find the evidence on the report")
end
if evidence_type != "Evidence presented in the report" && evidence_on_report.present?
errors.add(:evidence_on_report, "This field can only be present when the evidence is presented on the report")
end
end

def remove_documents
ObservationDocument.where(observation_id: id).destroy_all
self.observation_documents = []
end

# If the observation is for an fmu, it updates its geojson with the new count
Expand Down
7 changes: 6 additions & 1 deletion app/models/observation_document.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,15 @@

class ObservationDocument < ApplicationRecord
has_paper_trail
acts_as_paranoid
mount_base64_uploader :attachment, ObservationDocumentUploader
include MoveableAttachment

acts_as_paranoid
enum document_type: {
"Government Documents" => 0, "Company Documents" => 1, "Photos" => 2,
"Testimony from local communities" => 3, "Other" => 4, "Maps" => 5
}
validate_enum_attributes :document_type

# TODO: only nils in the database, maybe that should be removed?
belongs_to :user, inverse_of: :observation_documents, touch: true, optional: true
Expand Down
2 changes: 1 addition & 1 deletion app/uploaders/observation_document_uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def filename
else
[
model.id,
(model.observation.evidence_type || "other").parameterize
(model.document_type || "other").parameterize
].join("-")
end

Expand Down
2 changes: 2 additions & 0 deletions app/views/active_admin/resource/_attributes_table.html.arb
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ panel I18n.t("active_admin.observations_page.details") do
render partial: "map", locals: {center: [r.lng, r.lat], geojson: r.fmu&.geojson, bbox: r.fmu&.bbox}
end
end
row :evidence_type
row :evidence_on_report
row :actions_taken
row :concern_opinion
row :observation_report
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class AddDocumentTypeToObservationDocument < ActiveRecord::Migration[7.0]
def up
add_column :observation_documents, :document_type, :integer, default: 0, null: false
# 5 is evidence on report in observations table
# changing Maps from 6 to 5
query = <<~SQL
UPDATE observation_documents SET document_type = odo.evidence_type, updated_at = NOW()
FROM (
SELECT DISTINCT ON (observation_document_id) observation_document_id, evidence_type
FROM observation_documents_observations
INNER JOIN observations ON observations.id = observation_documents_observations.observation_id
WHERE evidence_type <> 5 AND evidence_type IS NOT NULL
) odo
WHERE observation_documents.id = odo.observation_document_id
SQL
execute(query)
execute("UPDATE observation_documents SET document_type = 5 WHERE document_type = 6")
execute("UPDATE observations SET evidence_type = 2 WHERE evidence_type = 5") # update for evidence on report
execute("UPDATE observations SET evidence_type = 1 WHERE EXISTS (SELECT * FROM observation_documents_observations where observation_id = observations.id)") # update for docuements
execute("UPDATE observations SET evidence_type = 0 WHERE evidence_type is null") # for the rest
end

def down
query = <<~SQL
UPDATE observations SET evidence_type = odo.document_type, updated_at = NOW()
FROM (
SELECT DISTINCT ON (observation_id) observation_id, document_type
FROM observation_documents_observations
INNER JOIN observation_documents ON observation_documents.id = observation_documents_observations.observation_document_id
) odo
WHERE observations.id = odo.observation_id
SQL
execute(query)
execute("UPDATE observations SET evidence_type = 6 WHERE evidence_type = 5")
execute("UPDATE observations SET evidence_type = 5 WHERE evidence_type is null and coalesce(evidence_on_report, '') <> ''")
remove_column :observation_documents, :document_type
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.0].define(version: 2023_12_22_154005) do
ActiveRecord::Schema[7.0].define(version: 2023_12_22_171303) do
# These are extensions that must be enabled in order to support this database
enable_extension "address_standardizer"
enable_extension "address_standardizer_data_us"
Expand Down Expand Up @@ -391,6 +391,7 @@
t.datetime "updated_at", precision: nil, null: false
t.integer "user_id"
t.datetime "deleted_at", precision: nil
t.integer "document_type", default: 0, null: false
t.index ["deleted_at"], name: "index_observation_documents_on_deleted_at"
t.index ["name"], name: "index_observation_documents_on_name"
t.index ["user_id"], name: "index_observation_documents_on_user_id"
Expand Down