Sha256: 435dc56570e8a524df1720ca07ac52f31a7f27d6029b2717c835bc75b0c0ba6e

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

# frozen_string_literal: true

module Decidim
  module ReportingProposals
    class GeolocationController < Decidim::ReportingProposals::ApplicationController
      # overwrite original rescue_from to ensure we print messages from ajax methods (update)
      rescue_from Decidim::ActionForbidden, with: :ajax_user_has_no_permission

      def locate
        enforce_permission_to :locate, :geolocation

        unless Decidim::Map.configured?
          return render(json: { message: I18n.t("unconfigured", scope: "decidim.application.geocoding"), found: false }, status: :unprocessable_entity)
        end

        geocoder = Decidim::Map.utility(:geocoding, organization: current_organization)
        address = geocoder.address([params[:latitude], params[:longitude]])
        render json: { address: address, found: address.present? }
      end

      private

      # Rescue ajax calls and print the update.js view which prints the info on the message ajax form
      # Only if the request is AJAX, otherwise behave as Decidim standards
      def ajax_user_has_no_permission
        return user_has_no_permission unless request.xhr?

        render json: { message: I18n.t("actions.unauthorized", scope: "decidim.core") }, status: :unprocessable_entity
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
decidim-reporting_proposals-0.5.2 app/controllers/decidim/reporting_proposals/geolocation_controller.rb
decidim-reporting_proposals-0.5.1 app/controllers/decidim/reporting_proposals/geolocation_controller.rb
decidim-reporting_proposals-0.5.0 app/controllers/decidim/reporting_proposals/geolocation_controller.rb
decidim-reporting_proposals-0.4.2 app/controllers/decidim/reporting_proposals/geolocation_controller.rb
decidim-reporting_proposals-0.2.0 app/controllers/decidim/reporting_proposals/geolocation_controller.rb