Sha256: c8934b26077c04919d7a06e4776ff5ebee6d48ab11eb018cd7c86f5294b42c61

Contents?: true

Size: 1.37 KB

Versions: 37

Compression:

Stored size: 1.37 KB

Contents

# frozen_string_literal: true

module Decidim
  class EditorImagesController < Decidim::ApplicationController
    include FormFactory

    # 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

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

      CreateEditorImage.call(@form) do
        on(:ok) do |image|
          render json: { url: image.attached_uploader(:file).path, message: I18n.t("success", scope: "decidim.editor_images.create") }
        end

        on(:invalid) do |_message|
          render json: { message: I18n.t("error", scope: "decidim.editor_images.create") }, 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
      {
        file: params[:image],
        author_id: current_user.id
      }
    end

    def permission_scope
      :admin
    end
  end
end

Version data entries

37 entries across 37 versions & 1 rubygems

Version Path
decidim-core-0.29.1 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.28.4 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.27.9 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.29.0 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.28.3 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.27.8 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.29.0.rc4 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.29.0.rc3 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.29.0.rc2 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.29.0.rc1 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.28.2 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.27.7 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.28.1 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.27.6 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.26.10 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.26.9 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.28.0 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.27.5 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.28.0.rc5 app/controllers/decidim/editor_images_controller.rb
decidim-core-0.28.0.rc4 app/controllers/decidim/editor_images_controller.rb