Sha256: bcaab5e7f195109b7dd2ffcfbbcbfef89dd640511770443ca6a167d9afab663b

Contents?: true

Size: 1.69 KB

Versions: 9

Compression:

Stored size: 1.69 KB

Contents

# frozen_string_literal: true

module Decidim
  module DecidimAwesome
    # This controller handles image uploads for the hacked Quill editor
    class EditorImagesController < DecidimAwesome::ApplicationController
      include FormFactory
      include NeedsAwesomeConfig

      # overwrite original rescue_from to ensure we print messages from ajax methods (update)
      rescue_from Decidim::ActionForbidden, with: :ajax_user_has_no_permission

      def create
        enforce_permission_to :create, :editor_image, awesome_config: awesome_config

        @form = form(EditorImageForm).from_params(form_values)

        CreateEditorImage.call(@form) do
          on(:ok) do |image|
            url = image.url
            url = "#{request.base_url}#{url}" unless url&.start_with?("http")
            render json: { url: url, message: I18n.t("decidim_awesome.editor_images.create.success", scope: "decidim") }
          end

          on(:invalid) do |_message|
            render json: { message: I18n.t("decidim_awesome.editor_images.create.error", scope: "decidim") }, status: :unprocessable_entity
          end
        end
      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

      def form_values
        {
          image: params[:image],
          author_id: current_user.id,
          path: request.referer
        }
      end
    end
  end
end

Version data entries

9 entries across 9 versions & 1 rubygems

Version Path
decidim-decidim_awesome-0.7.2 app/controllers/decidim/decidim_awesome/editor_images_controller.rb
decidim-decidim_awesome-0.7.0 app/controllers/decidim/decidim_awesome/editor_images_controller.rb
decidim-decidim_awesome-0.6.7 app/controllers/decidim/decidim_awesome/editor_images_controller.rb
decidim-decidim_awesome-0.6.6 app/controllers/decidim/decidim_awesome/editor_images_controller.rb
decidim-decidim_awesome-0.6.5 app/controllers/decidim/decidim_awesome/editor_images_controller.rb
decidim-decidim_awesome-0.6.4 app/controllers/decidim/decidim_awesome/editor_images_controller.rb
decidim-decidim_awesome-0.6.3 app/controllers/decidim/decidim_awesome/editor_images_controller.rb
decidim-decidim_awesome-0.6.2 app/controllers/decidim/decidim_awesome/editor_images_controller.rb
decidim-decidim_awesome-0.6.1 app/controllers/decidim/decidim_awesome/editor_images_controller.rb