Sha256: a3ea4ebb298b0928de4e14f3cf8472591c7b0aad393637f3eff2f7458893cc00

Contents?: true

Size: 1.25 KB

Versions: 1

Compression:

Stored size: 1.25 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:, 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

1 entries across 1 versions & 1 rubygems

Version Path
decidim-reporting_proposals-0.6.0 app/controllers/decidim/reporting_proposals/geolocation_controller.rb