Skip to content

Commit

Permalink
Merge branch '149-improve-content-discoverability-with-dynamic-empty-…
Browse files Browse the repository at this point in the history
…state-cta-s-2' into 'main'

Resolve "improve content discoverability with dynamic empty state CTA's"

See merge request decidim/decidim-module-geo!161
  • Loading branch information
Hadrien Froger committed Oct 30, 2024
2 parents 0146399 + 31c94fd commit 7e64045
Show file tree
Hide file tree
Showing 4 changed files with 143 additions and 20 deletions.
87 changes: 87 additions & 0 deletions app/jobs/decidim/geo/automatic_scope_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# frozen_string_literal: true

module Decidim
module Geo
class AutomaticScopeJob < ::ApplicationJob
queue_as :default

def perform
# Select registred spaces
spaces = registry.active_manifests do |registry|
registry.select do |_manifest_name, options|
model = options[:model]
model.include?(Decidim::ScopableParticipatorySpace)
end
end
# Select registred components manifest name
component_options = registry.active_manifests do |registry|
registry.select do |_manifest_name, options|
model = options[:model]
model.include?(Decidim::HasComponent) && model.include?(Decidim::ScopableResource)
end
end
component_manifests = component_options.keys
# Select all the components where space's scope is nil
# and component's scope is nil, and get the component Ids.
components_without_scopes = spaces.map do |manifest_name, space_options|
space_options[:model].where(scope: nil).select(:id).map do |space|
Decidim::Component.where(
participatory_space_type: space_options[:model].name,
participatory_space_id: space.id,
manifest_name: component_manifests
).to_a.select do |component|
!component.settings.scope_id
end
end.flatten
end.flatten
# loop over all the resources that have no scopes
component_options.map do |manifest_name, options|
model = options[:model]
model.where(component: components_without_scopes, scope: nil).map do |resource|
lat = latitude(resource)
lon = longitude(resource)
next unless lat && lon
point_coord = RGeo::Geographic.spherical_factory(srid: 2056).point(lon, lat)
shape = Decidim::Geo::Shapedata.where('ST_Contains(geom, ?)', point_coord).first
next unless shape
assigned_scope = Decidim::Scope.where(shapedata: shape).first
next unless assigned_scope
resource.update(scope: assigned_scope)
end
end
end

private

def latitude(resource)
if location_overriden?(resource)
location(resource).latitude if location(resource) && location(resource).latitude
else
resource.latitude
end
end

def longitude(resource)
if location_overriden?(resource)
location(resource).longitude if location(resource) && location(resource).longitude
else
resource.longitude
end
end

def location(resource)
resource.decidim_geo_space_location
end

def location_overriden?(resource)
resource.respond_to?(:decidim_geo_space_location)
end

def registry
@registry ||= Decidim::Geo::ManifestRegistry.instance
end

end
end
end

34 changes: 17 additions & 17 deletions app/packs/src/decidim/geo/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,23 +108,23 @@ async function displayMap() {
pointsLayer.addLayer(marker);
});
}
// if (
// pointInMap.length > 0 &&
// !fetchesRunning &&
// !selectedScope &&
// !selectedPoint
// ) {
// const boundingBox = L.featureGroup(pointInMap.map(({ marker }) => marker)).getBounds();
// if(map.getBounds().contains(boundingBox)){
// return;
// }
// map.fitBounds(
// boundingBox,
// {
// padding: configStore.getState().isFullScreen ? [8, 8] : [16, 16]
// }
// );
// }
if (
pointInMap.length > 0 &&
!fetchesRunning &&
!selectedScope &&
!selectedPoint
) {
const boundingBox = L.featureGroup(pointInMap.map(({ marker }) => marker)).getBounds();
if(map.getBounds().contains(boundingBox)){
return;
}
// map.fitBounds(
// boundingBox,
// {
// padding: configStore.getState().isFullScreen ? [8, 8] : [16, 16]
// }
// );
}
}
);

Expand Down
6 changes: 3 additions & 3 deletions app/packs/src/decidim/geo/ui/DrawerListItem/proposals.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ const proposals = (node) => {
const listCard = L.DomUtil.create("li", "decidimGeo__drawer__listCard decidimGeo__drawer__listCard--proposals");
const info = L.DomUtil.create(
"div",
"decidimGeo__drawer__listCardInfo",
"decidimGeo__drawer__listCardInfo decidimGeo__drawer__listCardInfo--large",
listCard
);
const metadatas = L.DomUtil.create("div", "decidimGeo__drawer__metas", info);
Expand Down Expand Up @@ -45,8 +45,8 @@ const proposals = (node) => {
node.shortDescription[locale] || node.shortDescription[defaultLocale];
}
if (hasImage) {
const image = L.DomUtil.create("img", "decidimGeo__drawer__listCardImg", listCard);
image.src = node.imageUrl;
// const image = L.DomUtil.create("img", "decidimGeo__drawer__listCardImg", listCard);
// image.src = node.imageUrl;
}

return listCard;
Expand Down
36 changes: 36 additions & 0 deletions lib/tasks/decidim_geo_assign_geo_scopes_tasks.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require "decidim/gem_manager"

namespace :decidim_geo do
namespace :automatic_scopes do
desc "Assign Scopes automatically"
task export: :environment do
raise "Decidim gem is not installed" if decidim_path.nil?
raise "Decidim-Geo is not installed" unless Gem.loaded_specs.has_key?(gem_name)


end


def decidim_geo_path
@decidim_geo_path ||= Pathname.new(decidim_geo_gemspec.full_gem_path) if Gem.loaded_specs.has_key?(gem_name)
end

def decidim_geo_gemspec
@decidim_geo_gemspec ||= Gem.loaded_specs[gem_name]
end

def rails_app_path
@rails_app_path ||= Rails.root
end

def system!(command)
system("cd #{rails_app_path} && #{command}") || abort("\n== Command #{command} failed ==")
end

def gem_name
"decidim-decidim_geo"
end
end
end

0 comments on commit 7e64045

Please sign in to comment.