Skip to content

Commit

Permalink
Merge branch 'fix/process_groups_hide_scope' into 'main'
Browse files Browse the repository at this point in the history
Fix: process groups hide scope

See merge request decidim/decidim-module-geo!168
  • Loading branch information
Hadrien Froger committed Dec 11, 2024
2 parents ecc5e83 + f94ad75 commit 06a7a18
Show file tree
Hide file tree
Showing 31 changed files with 189 additions and 149 deletions.
13 changes: 8 additions & 5 deletions app/cells/decidim/geo/content_blocks/geo_maps_cell.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def empty_map?

def data_count
@data_count ||= ::Decidim::Geo::Api::GeoQuery.new(
current_organization,
current_user,
{ filters: filters, is_index: index? },
current_organization,
current_user,
{ filters: filters, is_index: index? },
current_locale
).results_count
end
Expand Down Expand Up @@ -66,9 +66,11 @@ def insert_map
"data-i18n" => geo_i18n.to_json
)
end

def index?
@options.has_key?(:is_index) ? @options[:is_index] : true
end

def insert_scopes_mobile
content_tag(:div, "", class: ["js-decidimgeo"])
end
Expand Down Expand Up @@ -130,15 +132,16 @@ def default_locale

def current_component_id
return "none" unless current_component

current_component.id
end

def current_component
return request.env["decidim.current_component"]
request.env["decidim.current_component"]
end

def current_participatory_space
return request.env["decidim.current_participatory_space"]
request.env["decidim.current_participatory_space"]
end

def current_component?
Expand Down
44 changes: 22 additions & 22 deletions app/controllers/decidim/geo/points_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,24 +21,24 @@ def index
)
last_modified = query.maximum(:updated_at) || 1.year.from_now
etag = "#{query.cache_key_with_version}/params-#{Digest::MD5.hexdigest(permitted_params.to_json)}"
if stale?(last_modified: last_modified.utc, etag: etag)
last_id = query.maximum(:id)
geo_scope_ids = query.select(:geo_scope_id).group(:geo_scope_id).pluck(:geo_scope_id).compact
render json: {
meta: {
fields: permitted_fields_params,
filters: filters_params,
organization: current_organization.id,
end_cursor: results.last && results.last.id,
default_locale: current_organization.default_locale,
first: first_params,
after: after_params,
has_more: results.last && results.last.id != last_id,
geo_scope_ids: geo_scope_ids || []
},
data: results
}
end
return unless stale?(last_modified: last_modified.utc, etag: etag)

last_id = query.maximum(:id)
geo_scope_ids = query.select(:geo_scope_id).group(:geo_scope_id).pluck(:geo_scope_id).compact
render json: {
meta: {
fields: permitted_fields_params,
filters: filters_params,
organization: current_organization.id,
end_cursor: results.last && results.last.id,
default_locale: current_organization.default_locale,
first: first_params,
after: after_params,
has_more: results.last && results.last.id != last_id,
geo_scope_ids: geo_scope_ids || []
},
data: results
}
end

private
Expand All @@ -61,14 +61,14 @@ def query
current_user,
{
filters: filters_params,
is_index: is_index_params
is_index: index_params?
},
locale_param
).results
end

def is_index_params
@is_index_params ||= permitted_params[:is_index] || false
def index_params?
@index_params ||= permitted_params[:is_index] || false
end

def locale_param
Expand Down Expand Up @@ -113,7 +113,7 @@ def permitted_fields_params
filtered_fields.push("id")
filtered_fields.map(&:underscore)
end
end
end

def set_default_format
request.format = :json
Expand Down
2 changes: 1 addition & 1 deletion app/forms/decidim/geo/admin/geo_config_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class GeoConfigForm < Form
attribute :only_processes, Boolean
attribute :default_geoencoded_filter, Integer
attribute :focus_zoom_level, Integer

alias organization current_organization
end
end
Expand Down
137 changes: 69 additions & 68 deletions app/jobs/decidim/geo/automatic_scope_job.rb
Original file line number Diff line number Diff line change
@@ -1,87 +1,88 @@
# 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|
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.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
model.include?(Decidim::ScopableParticipatorySpace)
end
end

private

def latitude(resource)
if location_overriden?(resource)
location(resource).latitude if location(resource) && location(resource).latitude
else
resource.latitude
# 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.reject 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

def longitude(resource)
if location_overriden?(resource)
location(resource).longitude if location(resource) && location(resource).longitude
else
resource.longitude
assigned_scope = Decidim::Scope.where(shapedata: shape).first
next unless assigned_scope

resource.update(scope: assigned_scope)
end
end
end

def location(resource)
resource.decidim_geo_space_location
end
private

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

def registry
@registry ||= Decidim::Geo::ManifestRegistry.instance
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
end
3 changes: 2 additions & 1 deletion app/jobs/decidim/geo/update_accountability_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def perform(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 @@ -58,4 +59,4 @@ def manifest_name
end
end
end
end
end
7 changes: 4 additions & 3 deletions app/jobs/decidim/geo/update_assembly_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ class UpdateAssemblyGeoIndexJob < ::ApplicationJob
alias assembly= resource=

def perform(assembly_id)
@resource_id = 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 @@ -50,7 +51,7 @@ def with_dates(decidim_geo_hash)
decidim_geo_hash[:start_date] = assembly.included_at.to_date if assembly.included_at
decidim_geo_hash[:end_date] = assembly.closing_date.to_date if assembly.closing_date
decidim_geo_hash
end
end

def remove_assembly
match = Decidim::Geo::Index.find_by(resource_id: resource_id, resource_type: manifest_name)
Expand All @@ -67,4 +68,4 @@ def manifest_name
end
end
end
end
end
3 changes: 2 additions & 1 deletion app/jobs/decidim/geo/update_debate_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def perform(debate_id)
@resource_id = debate_id
@resource = Decidim::Debates::Debate.where(id: debate_id).first
return remove_debate unless resource

sync_debate
end

Expand Down Expand Up @@ -64,4 +65,4 @@ def manifest_name
end
end
end
end
end
1 change: 1 addition & 0 deletions app/jobs/decidim/geo/update_meeting_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def perform(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
4 changes: 2 additions & 2 deletions app/jobs/decidim/geo/update_process_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def perform(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 @@ -41,7 +42,6 @@ def sync_process
rescue StandardError => e
Rails.logger.debug { "Can not index #{e}" }
raise e
false
end

def remove_process
Expand All @@ -59,4 +59,4 @@ def manifest_name
end
end
end
end
end
3 changes: 2 additions & 1 deletion app/jobs/decidim/geo/update_proposal_geo_index_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ def perform(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 @@ -70,4 +71,4 @@ def manifest_name
end
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/decidim/geo/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def as_json(options = {})
camelized.merge!(lonlat: coordinates) if camelized.has_key?("lonlat")

camelized
end
end
end
end
end
2 changes: 1 addition & 1 deletion app/models/decidim/geo/no_index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ def geo_indexed?
end
end
end
end
end
Loading

0 comments on commit 06a7a18

Please sign in to comment.