Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
21 changes: 12 additions & 9 deletions app/models/fmu.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#

class Fmu < ApplicationRecord
has_paper_trail
has_paper_trail skip: %i[geometry]
acts_as_paranoid

include EsriShapefileUpload
Expand All @@ -46,13 +46,13 @@ class Fmu < ApplicationRecord
accepts_nested_attributes_for :operators
accepts_nested_attributes_for :fmu_operator, reject_if: proc { |attributes| attributes["operator_id"].blank? }

before_validation :update_geojson_properties

validates :name, presence: true
validates :forest_type, presence: true
validates :geojson, geojson: true, if: :geojson_changed?

after_save :update_geometry, if: :saved_change_to_geojson?
after_create :update_geometry, if: :geojson
after_update :update_geometry, if: :saved_change_to_geojson?
after_save :update_geojson_properties

# TODO Redo all of those
scope :filter_by_countries, ->(country_ids) { where(country_id: country_ids.split(",")) }
Expand Down Expand Up @@ -82,14 +82,12 @@ def fetch_all(options)
end

def update_geojson_properties
return if geojson.blank?

fmu_type_label = begin
ForestType::TYPES[forest_type.to_sym][:geojson_label]
rescue
""
end
geojson["properties"] = (geojson["properties"] || {}).merge({
new_properties = {
"id" => id,
"fmu_name" => name,
"iso3_fmu" => country&.iso,
Expand All @@ -106,7 +104,12 @@ def update_geojson_properties
"observations" => active_observations.reload.uniq.count,
"forest_type" => forest_type,
"fmu_type_label" => fmu_type_label # old one deprecated, to be removed in the future
})
}
# we want to omit tracking those changes in properties in the paper trail
# we only will track changes to geojson->geometry via upload
self.class.unscoped.where(id: id).where.not(geojson: nil).update_all(
["geojson = jsonb_set(geojson, '{properties}', COALESCE(geojson->'properties', '{}') || ?::jsonb, true)", new_properties.to_json]
)
end

def properties
Expand Down Expand Up @@ -146,6 +149,6 @@ def update_geometry
end

def update_centroid
self.class.unscoped.where(id: id).update_all("geojson = jsonb_set(geojson, '{properties,centroid}', ST_AsGeoJSON(st_centroid(geometry))::jsonb, true)")
self.class.unscoped.where(id: id).update_all("geojson = jsonb_set(jsonb_set(geojson, '{properties}', COALESCE(geojson -> 'properties', '{}'), true), '{properties,centroid}', ST_AsGeoJSON(st_centroid(geometry))::jsonb, true)")
end
end
2 changes: 1 addition & 1 deletion app/models/observation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ def update_fmu_geojson
return unless fmu_id

fmu.update_geojson_properties
fmu.save
fmu.touch # to update cache
end

def assign_observers_from_report
Expand Down
2 changes: 1 addition & 1 deletion app/models/operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#

class Operator < ApplicationRecord
has_paper_trail
has_paper_trail skip: %i[country_doc_rank country_operators]

mount_base64_uploader :logo, LogoUploader
attr_accessor :delete_logo
Expand Down
Loading
Loading