Files
nairobi/app/models/geozone.rb
Matheus Miranda de13e789dd Add polygon geographies to Budgets' map
Note that in the budgets wizard test we now create district with no
associated geozone, so the text "all city" will appear in the districts
table too, meaning we can't use `within "section", text: "All city" do`
anymore since it would result in an ambiguous match.

Co-Authored-By: Julian Herrero <microweb10@gmail.com>
Co-Authored-By: Javi Martín <javim@elretirao.net>
2023-05-31 16:56:15 +02:00

47 lines
1.1 KiB
Ruby

class Geozone < ApplicationRecord
include Graphqlable
has_many :proposals
has_many :debates
has_many :users
has_many :headings, class_name: "Budget::Heading", dependent: :nullify
validates :name, presence: true
validates :geojson, geojson_format: true
scope :public_for_api, -> { all }
def self.names
Geozone.pluck(:name)
end
def safe_to_destroy?
Geozone.reflect_on_all_associations(:has_many).all? do |association|
association.klass.where(geozone: self).empty?
end
end
def outline_points
normalized_coordinates.map { |longlat| [longlat.last, longlat.first] }
end
private
def normalized_coordinates
if geojson.present?
if geojson.match(/"coordinates"\s*:\s*\[{4}/)
coordinates.reduce([], :concat).reduce([], :concat)
elsif geojson.match(/"coordinates"\s*:\s*\[{3}/)
coordinates.reduce([], :concat)
else
coordinates
end
else
[]
end
end
def coordinates
JSON.parse(geojson)["geometry"]["coordinates"]
end
end