Module: Decidim::Geo::QueryExtension

Defined in:
lib/decidim/api/query_extension.rb

Overview

GeoQueryExtension

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(type) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/decidim/api/query_extension.rb', line 6

def self.included(type)
  def geo_manifests
    { 
      "Decidim::Meetings::Meeting": true, 
      "Decidim::Proposals::Proposal": true, 
      "Decidim::Assembly": true,
      "Decidim::ParticipatoryProcess": true,
      "Decidim::Debates::Debate": false
    }
  end

  def supported_geo_components 
    geo_manifests.keys
  end


  type.field :geo_shapefiles, [Decidim::Geo::GeoShapefileType], description: "Return's information about the shapefiles", null: true do
    argument :title, [String], required: false
  end

  type.field :geo_shapedata, [Decidim::Geo::GeoShapedataType], description: "Return's datas from shapefile", null: true do
    argument :name, [String], required: false
  end

  type.field :geo_config, Decidim::Geo::GeoConfigType, description: "Return's information about the geo config", null: true

  type.field :geo_scope, [Decidim::Geo::GeoScopeApiType], description: "Return's scopes with shapedatas", null: true do
    argument :id, type: [Integer], required: false
  end

  type.field :geo_datasource, Decidim::Geo::GeoDatasourceType.connection_type, null: true do
    argument :filters, [Decidim::Geo::GeoDatasourceInputFilter], "This argument let's you filter the results", required: false
    argument :locale, type: String, required: false
  end
end

Instance Method Details

#geo_configObject



102
103
104
# File 'lib/decidim/api/query_extension.rb', line 102

def geo_config
  Decidim::Geo::GeoConfig.geo_config_default
end

#geo_datasource(**kwargs) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/decidim/api/query_extension.rb', line 42

def geo_datasource(**kwargs)
  
  locale = kwargs[:locale] || I18n.locale
  return nofilter_datasource(locale) unless kwargs[:filters].present?
  
  scopes = kwargs[:filters].select {|f| f[:scope_filter].present?}
  resource_type = kwargs[:filters].find {|f| f[:resource_type_filter].present?}
  processes = kwargs[:filters].select {|f| f[:process_filter].present?}
  assemblies = kwargs[:filters].select {|f| f[:assembly_filter].present?}
  @time = kwargs[:filters].find {|f| f[:time_filter].present?} 
  @geoencoded = kwargs[:filters].find {|f| f[:geoencoded_filter].present?}
  search_params = {locale: locale, class_name: supported_geo_components}
  filtered_by_spaces = assemblies.length > 0 || processes.length > 0
  filtered_by_scopes = scopes.length > 0
  if scopes.length > 0
    # Search only in a given scope
    search_params = search_params.merge({scope_ids: scopes.map {|scope| scope.scope_filter.scope_id }})
  end

  if resource_type
    # search only for a resource type
    class_name = resource_type.resource_type_filter.resource_type
    search_params = search_params.merge({class_name: class_name}) unless class_name == "all"
  elsif assemblies.length > 0 && processes.length == 0
    search_params = search_params.merge({class_name: supported_geo_components.select {|k| k != :"Decidim::ParticipatoryProcess"}})
  elsif processes.length > 0 && assemblies.length == 0
    search_params = search_params.merge({class_name: supported_geo_components.select {|k| k != :"Decidim::Assembly"}})
  end
  search_results = filtered_query_for(**search_params)

  if assemblies.length > 0
    # The results must be within an assembly
    search_results = search_results.where(
      decidim_participatory_space_type: "Decidim::Assembly",
      decidim_participatory_space_id: assemblies.map{|assembly| assembly.assembly_filter.assembly_id }
    )
  end

  if processes.length > 0
    # The results must be within a process
    search_results = search_results.where(
      decidim_participatory_space_type: "Decidim::ParticipatoryProcess",
      decidim_participatory_space_id: processes.map {|process| process.process_filter.process_id }
    )
  end

  fetch_results(search_results)
end

#geo_manifestsObject



7
8
9
10
11
12
13
14
15
# File 'lib/decidim/api/query_extension.rb', line 7

def geo_manifests
  { 
    "Decidim::Meetings::Meeting": true, 
    "Decidim::Proposals::Proposal": true, 
    "Decidim::Assembly": true,
    "Decidim::ParticipatoryProcess": true,
    "Decidim::Debates::Debate": false
  }
end

#geo_scope(id: []) ⇒ Object



106
107
108
109
# File 'lib/decidim/api/query_extension.rb', line 106

def geo_scope(id: [])
  return Decidim::Scope.all if id.empty?
  Decidim::Scope.where(id: id)
end

#geo_shapedata(name: nil) ⇒ Object



96
97
98
99
100
# File 'lib/decidim/api/query_extension.rb', line 96

def geo_shapedata(name: nil)
  return Decidim::Geo::Shapedata.where("data->>'NAME' in (?)", name) if name.present?

  Decidim::Geo::Shapedata.all
end

#geo_shapefiles(title: nil) ⇒ Object



91
92
93
94
# File 'lib/decidim/api/query_extension.rb', line 91

def geo_shapefiles(title: nil)
  return Decidim::Geo::Shapefile.where(title: title) if title.present?
  Decidim::Geo::Shapefile.all
end

#supported_geo_componentsObject



17
18
19
# File 'lib/decidim/api/query_extension.rb', line 17

def supported_geo_components 
  geo_manifests.keys
end