Skip to content

Commit

Permalink
Merge branch 'fix/demos' into 'main'
Browse files Browse the repository at this point in the history
Fix/demos

See merge request decidim/decidim-module-geo!159
  • Loading branch information
Hadrien Froger committed Oct 21, 2024
2 parents d07b70f + 6900fc2 commit 0a804ed
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 35 deletions.
8 changes: 5 additions & 3 deletions app/jobs/decidim/geo/update_accountability_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -52,7 +54,7 @@ def remove_from_index?
end

def manifest_name
accountability_result.component.manifest.name.to_s
"accountability"
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions app/jobs/decidim/geo/update_assembly_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -51,17 +53,17 @@ 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?
!assembly.visible? || !Decidim::Assembly.visible_for(nil).find(assembly.id)
end

def manifest_name
assembly.manifest.name.to_s
"assemblies"
end
end
end
Expand Down
12 changes: 7 additions & 5 deletions app/jobs/decidim/geo/update_debate_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down Expand Up @@ -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

Expand All @@ -58,7 +60,7 @@ def remove_from_index?
end

def manifest_name
debate.component.manifest.name.to_s
"debates"
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions app/jobs/decidim/geo/update_meeting_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -61,7 +63,7 @@ def remove_from_index?
end

def manifest_name
meeting.component.manifest.name.to_s
"meetings"
end
end
end
Expand Down
10 changes: 6 additions & 4 deletions app/jobs/decidim/geo/update_process_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -43,17 +45,17 @@ 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?
!process.visible?
end

def manifest_name
process.manifest.name.to_s
"participatory_processes"
end
end
end
Expand Down
8 changes: 5 additions & 3 deletions app/jobs/decidim/geo/update_proposal_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand All @@ -62,7 +64,7 @@ def remove_from_index?
end

def manifest_name
proposal.component.manifest_name.to_s
"proposals"
end
end
end
Expand Down
6 changes: 4 additions & 2 deletions app/packs/src/decidim/geo/models/GeoScope.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion app/packs/src/decidim/geo/stores/configStore.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion app/packs/src/decidim/geo/ui/Drawer.js
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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() {
Expand Down
1 change: 1 addition & 0 deletions lib/decidim/geo/generic_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module GenericSerializer

included do
attr_accessor :resource
attr_accessor :resource_id

def scope
@scope ||= if resource.class.include? Decidim::HasComponent
Expand Down
13 changes: 6 additions & 7 deletions lib/decidim/geo/geo_index_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
11 changes: 9 additions & 2 deletions lib/decidim/geo/update_geo_component.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0a804ed

Please sign in to comment.