diff --git a/app/jobs/decidim/geo/update_accountability_geo_index_job.rb b/app/jobs/decidim/geo/update_accountability_geo_index_job.rb index 02ebf58..ef4fe86 100644 --- a/app/jobs/decidim/geo/update_accountability_geo_index_job.rb +++ b/app/jobs/decidim/geo/update_accountability_geo_index_job.rb @@ -11,7 +11,9 @@ class UpdateAccountabilityGeoIndexJob < ::ApplicationJob alias accountability_result= resource= def perform(accountability_result_id) - @resource = Decidim::Accountability::Result.find(accountability_result_id) + @resource_id = accountability_result_id + @resource = Decidim::Accountability::Result.where(id: accountability_result_id).first + return remove_accountability_result unless resource sync_accountability_result end @@ -43,7 +45,7 @@ def sync_accountability_result end def remove_accountability_result - match = Decidim::Geo::Index.find_by(resource_id: accountability_result.id, resource_type: manifest_name) + match = Decidim::Geo::Index.find_by(resource_id: resource_id, resource_type: manifest_name) match.destroy if match end @@ -52,7 +54,7 @@ def remove_from_index? end def manifest_name - accountability_result.component.manifest.name.to_s + "accountability" end end end diff --git a/app/jobs/decidim/geo/update_assembly_geo_index_job.rb b/app/jobs/decidim/geo/update_assembly_geo_index_job.rb index 8da790a..5d76a8f 100644 --- a/app/jobs/decidim/geo/update_assembly_geo_index_job.rb +++ b/app/jobs/decidim/geo/update_assembly_geo_index_job.rb @@ -11,7 +11,9 @@ class UpdateAssemblyGeoIndexJob < ::ApplicationJob alias assembly= resource= def perform(assembly_id) - @resource = Decidim::Assembly.find(assembly_id) + @resource_id = assembly_id + @resource = Decidim::Assembly.where(id: assembly_id).first + return remove_assembly unless resource sync_assembly end @@ -51,9 +53,9 @@ def with_dates(decidim_geo_hash) end def remove_assembly - match = Decidim::Geo::Index.find_by(resource_id: assembly.id, resource_type: manifest_name) + match = Decidim::Geo::Index.find_by(resource_id: resource_id, resource_type: manifest_name) match.destroy if match - decidim_geo_update_components! + decidim_geo_update_components!(Decidim::Assembly) end def remove_from_index? @@ -61,7 +63,7 @@ def remove_from_index? end def manifest_name - assembly.manifest.name.to_s + "assemblies" end end end diff --git a/app/jobs/decidim/geo/update_debate_geo_index_job.rb b/app/jobs/decidim/geo/update_debate_geo_index_job.rb index 50cc2eb..e2c36ec 100644 --- a/app/jobs/decidim/geo/update_debate_geo_index_job.rb +++ b/app/jobs/decidim/geo/update_debate_geo_index_job.rb @@ -11,14 +11,16 @@ class UpdateDebateGeoIndexJob < ::ApplicationJob alias debate= resource= def perform(meeting_id) - @resource = Decidim::Debates::Debate.find(meeting_id) + @resource_id = meeting + @resource = Decidim::Debates::Debate.where(id: meeting_id).first + return remove_debate unless resource sync_meeting end private def sync_meeting - return remove_meeting if remove_from_index? + return remove_debate if remove_from_index? upsert_index(debate.id, manifest_name, with_scope( @@ -48,8 +50,8 @@ def end_date resource.closed_at.to_date if resource.closed_at end - def remove_meeting - match = Decidim::Geo::Index.find_by(resource_id: debate.id, resource_type: manifest_name) + def remove_debate + match = Decidim::Geo::Index.find_by(resource_id: resource_id, resource_type: manifest_name) match.destroy if match end @@ -58,7 +60,7 @@ def remove_from_index? end def manifest_name - debate.component.manifest.name.to_s + "debates" end end end diff --git a/app/jobs/decidim/geo/update_meeting_geo_index_job.rb b/app/jobs/decidim/geo/update_meeting_geo_index_job.rb index 2d059c2..6b75888 100644 --- a/app/jobs/decidim/geo/update_meeting_geo_index_job.rb +++ b/app/jobs/decidim/geo/update_meeting_geo_index_job.rb @@ -11,7 +11,9 @@ class UpdateMeetingGeoIndexJob < ::ApplicationJob alias meeting= resource= def perform(meeting_id) - @resource = Decidim::Meetings::Meeting.find(meeting_id) + @resource_id = meeting_id + @resource = Decidim::Meetings::Meeting.where(id: meeting_id).first + return remove_meeting unless resource sync_meeting end @@ -52,7 +54,7 @@ def with_coords(decidim_geo_hash) end def remove_meeting - match = Decidim::Geo::Index.find_by(resource_id: meeting.id, resource_type: manifest_name) + match = Decidim::Geo::Index.find_by(resource_id: resource_id, resource_type: manifest_name) match.destroy if match end @@ -61,7 +63,7 @@ def remove_from_index? end def manifest_name - meeting.component.manifest.name.to_s + "meetings" end end end diff --git a/app/jobs/decidim/geo/update_process_geo_index_job.rb b/app/jobs/decidim/geo/update_process_geo_index_job.rb index deea4eb..8e347c5 100644 --- a/app/jobs/decidim/geo/update_process_geo_index_job.rb +++ b/app/jobs/decidim/geo/update_process_geo_index_job.rb @@ -11,7 +11,9 @@ class UpdateProcessGeoIndexJob < ::ApplicationJob alias process= resource= def perform(process_id) - @resource = Decidim::ParticipatoryProcess.find(process_id) + @resource_id = process_id + @resource = Decidim::ParticipatoryProcess.where(id: process_id).first + return remove_process unless resource sync_process end @@ -43,9 +45,9 @@ def sync_process end def remove_process - match = Decidim::Geo::Index.find_by(resource_id: process.id, resource_type: manifest_name) + match = Decidim::Geo::Index.find_by(resource_id: resource_id, resource_type: manifest_name) match.destroy if match - decidim_geo_update_components! + decidim_geo_update_components!(Decidim::ParticipatoryProcess) end def remove_from_index? @@ -53,7 +55,7 @@ def remove_from_index? end def manifest_name - process.manifest.name.to_s + "participatory_processes" end end end diff --git a/app/jobs/decidim/geo/update_proposal_geo_index_job.rb b/app/jobs/decidim/geo/update_proposal_geo_index_job.rb index e63331b..f065922 100644 --- a/app/jobs/decidim/geo/update_proposal_geo_index_job.rb +++ b/app/jobs/decidim/geo/update_proposal_geo_index_job.rb @@ -11,7 +11,9 @@ class UpdateProposalGeoIndexJob < ::ApplicationJob alias proposal= resource= def perform(proposal_id) - @resource = Decidim::Proposals::Proposal.find(proposal_id) + @resource_id = proposal_id + @resource = Decidim::Proposals::Proposal.where(id: proposal_id).first + return remove_proposal unless resource sync_proposal end @@ -53,7 +55,7 @@ def space_wrapper end def remove_proposal - match = Decidim::Geo::Index.find_by(resource_id: proposal.id, resource_type: manifest_name) + match = Decidim::Geo::Index.find_by(resource_id: resource_id, resource_type: manifest_name) match.destroy if match end @@ -62,7 +64,7 @@ def remove_from_index? end def manifest_name - proposal.component.manifest_name.to_s + "proposals" end end end diff --git a/app/packs/src/decidim/geo/models/GeoScope.js b/app/packs/src/decidim/geo/models/GeoScope.js index e44534a..b0e4b80 100644 --- a/app/packs/src/decidim/geo/models/GeoScope.js +++ b/app/packs/src/decidim/geo/models/GeoScope.js @@ -19,6 +19,7 @@ export default class GeoScope { } isEmpty(points = undefined) { + if (!points) points = pointStore.getState().points; if (this.data.geom === null) return true; const currentScopeId = this.id; @@ -96,7 +97,8 @@ export default class GeoScope { } remove() { const { map } = configStore.getState(); - map.removeLayer(this.layer); + if(this.layer) + map.removeLayer(this.layer); } init(mapLayer) { this.markers_group = this.markersForScope(); @@ -131,7 +133,7 @@ export default class GeoScope { if (isLoading) return; // Points are loaded, we can check if the the layer is empty, // and thus remove it. - if (this.isEmpty()) { + if (this.isEmpty() && this.layer) { if (map.hasLayer(this.layer) && mapReady) { map.removeLayer(this.layer); } diff --git a/app/packs/src/decidim/geo/stores/configStore.js b/app/packs/src/decidim/geo/stores/configStore.js index c0de8bf..f439083 100644 --- a/app/packs/src/decidim/geo/stores/configStore.js +++ b/app/packs/src/decidim/geo/stores/configStore.js @@ -57,9 +57,10 @@ const store = createStore( map_config: mapConfig.map_config, i18n: mapConfig.i18n, images: mapConfig.images || {}, - isIndex: mapConfig.is_index === "true", + isIndex: mapConfig.is_index === "true" || mapConfig.is_index === true, activeManifests: mapConfig.active_manifests })); + const { setDefaultFilters, setFilters, toFilterOptions } = filterStore.getState(); setDefaultFilters(mapConfig.filters); setFilters(mapConfig.filters); diff --git a/app/packs/src/decidim/geo/ui/Drawer.js b/app/packs/src/decidim/geo/ui/Drawer.js index 42c213c..6e29161 100644 --- a/app/packs/src/decidim/geo/ui/Drawer.js +++ b/app/packs/src/decidim/geo/ui/Drawer.js @@ -2,7 +2,6 @@ import createClasses from "./createClasses"; import geoStore from "../stores/geoStore"; import pointStore from "../stores/pointStore"; import configStore from "../stores/configStore"; -import filterStore from "../stores/filterStore"; import dropdownFilterStore from "../stores/dropdownFilterStore"; import { meetings as meetingDetails, fallback as fallbackDetails } from "./DrawerDetail"; @@ -100,6 +99,7 @@ export default class Drawer { const viewBtn = L.DomUtil.create("a", "decidimGeo__drawer__viewBtn", listCard); viewBtn.textContent = this.i18n()["decidim_geo.actions.view"]; viewBtn.href = node.resourceUrl; + this.cardList.scrollTo({top: 0}) return listCard; } repaint() { diff --git a/lib/decidim/geo/generic_serializer.rb b/lib/decidim/geo/generic_serializer.rb index e4d6bd6..1f70112 100644 --- a/lib/decidim/geo/generic_serializer.rb +++ b/lib/decidim/geo/generic_serializer.rb @@ -7,6 +7,7 @@ module GenericSerializer included do attr_accessor :resource + attr_accessor :resource_id def scope @scope ||= if resource.class.include? Decidim::HasComponent diff --git a/lib/decidim/geo/geo_index_methods.rb b/lib/decidim/geo/geo_index_methods.rb index 6ae0b01..9798c71 100644 --- a/lib/decidim/geo/geo_index_methods.rb +++ b/lib/decidim/geo/geo_index_methods.rb @@ -22,8 +22,7 @@ def upsert_index(resource_id, resource_type, decidim_geo_hash) index.resource_id = resource_id index.resource_type = resource_type index.save - decidim_geo_update_components! - end + end def with_scope(decidim_geo_hash) return decidim_geo_hash unless scope @@ -55,19 +54,19 @@ def has_coordinates? latitude && longitude end - def decidim_geo_update_components! + def decidim_geo_update_components!(space_klass) return unless resource.class.include? Decidim::ScopableParticipatorySpace - decidim_geo_linked_components.each(&:update_decidim_geo_index) + decidim_geo_linked_components(space_klass).each(&:update_decidim_geo_index) end - def decidim_geo_linked_components + def decidim_geo_linked_components(space_klass) @decidim_geo_linked_components ||= begin registry = Decidim::Geo::ManifestRegistry.instance active_manifest_names = registry.active_manifests(&:keys) Decidim::Component.where( - participatory_space_id: resource.id, - participatory_space_type: resource.class.name, + participatory_space_id: resource_id, + participatory_space_type: space_klass.name, manifest_name: active_manifest_names ) end diff --git a/lib/decidim/geo/update_geo_component.rb b/lib/decidim/geo/update_geo_component.rb index bd5fbc7..9df20bb 100644 --- a/lib/decidim/geo/update_geo_component.rb +++ b/lib/decidim/geo/update_geo_component.rb @@ -9,12 +9,19 @@ module UpdateGeoComponent included do after_commit :update_decidim_geo_index - + before_save :set_decidim_geo_trigger_changes + def set_decidim_geo_trigger_changes + @decidim_geo_trigger_changes = !changes.empty? + end def update_decidim_geo_index + return unless @decidim_geo_trigger_changes component_id = id registry = Decidim::Geo::ManifestRegistry.instance model_klass = registry.model_for(manifest_name) - model_klass.where(component: component_id).each(&:update_decidim_geo_index) + + attached = model_klass.where(component: component_id) + Decidim::Geo::Index.where(component_id: component_id, resource_type: manifest_name).where.not(resource_id: attached.ids).each {|ind| ind.destroy } + attached.each(&:update_decidim_geo_index) rescue StandardError => e Rails.logger.debug { "ERROR: manifest #{manifest_name} not supported" } end