demarches-normaliennes/app/models/champs/carte_champ.rb
2018-11-29 08:43:51 +01:00

69 lines
1.5 KiB
Ruby

class Champs::CarteChamp < Champ
# We are not using scopes here as we want to access
# the following collections on unsaved records.
def cadastres
geo_areas.select do |area|
area.source == GeoArea.sources.fetch(:cadastre)
end
end
def quartiers_prioritaires
geo_areas.select do |area|
area.source == GeoArea.sources.fetch(:quartier_prioritaire)
end
end
def parcelles_agricoles
geo_areas.select do |area|
area.source == GeoArea.sources.fetch(:parcelle_agricole)
end
end
def cadastres?
type_de_champ&.cadastres && type_de_champ.cadastres != '0'
end
def quartiers_prioritaires?
type_de_champ&.quartiers_prioritaires && type_de_champ.quartiers_prioritaires != '0'
end
def parcelles_agricoles?
type_de_champ&.parcelles_agricoles && type_de_champ.parcelles_agricoles != '0'
end
def position
if dossier.present?
dossier.geo_position
else
lon = "2.428462"
lat = "46.538192"
zoom = "13"
{ lon: lon, lat: lat, zoom: zoom }
end
end
def geo_json
@geo_json ||= value.blank? ? [] : JSON.parse(value)
end
def user_geometry
{
type: 'Polygon',
coordinates: [
geo_json[0].map do |polygon|
[polygon['lng'], polygon['lat']]
end
]
}
end
def user_geo_area
if geo_json.present?
GeoArea.new(
geometry: user_geometry,
source: GeoArea.sources.fetch(:selection_utilisateur)
)
end
end
end