-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch '149-improve-content-discoverability-with-dynamic-empty-…
…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
Showing
4 changed files
with
143 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |