Sha256: c054e5ae29a5cea2d5a5d324fc677765b1238c269793a8c461ca345ca9309804

Contents?: true

Size: 1.98 KB

Versions: 25

Compression:

Stored size: 1.98 KB

Contents

module Api
  module ServiceController
    class Base < ::ApiController
      module RestActions
        extend ActiveSupport::Concern

        included do
          include ActionController::MimeResponds

          respond_to :json

          if respond_to?(:before_action)
            before_action :initialize_service_for_create, only: [:create]
          else
            before_filter :initialize_service_for_create, only: [:create]
          end
        end

        def create
          perform
        end

        private

        def perform
          @result = @service.perform
          respond_to do |format|
            if @result.success?
              format.json { render json: serialize_result(@result), status: :created }
            else
              format.json { render json: { errors: serialize_errors(@result.errors) }, status: 422 }
            end
          end
        end

        def initialize_service_for_create
          @service = service_class.new(*service_arguments)
        end

        def service_arguments
          # In rails 5 permitted_params is an instance of ActionController::Parameters.
          # Stragely, when calling #delete on it, it does not delete the key, so we have
          # to transform it into a hash first.
          params_hash = permitted_params.try(:to_h).presence || permitted_params
          options = params_hash.try(:delete, :options) || {}
          [params_hash, options]
        end

        def permitted_params
          raise "not implemented"
        end
      end

      module Service
        extend ActiveSupport::Concern

        def service_class
          self.class.service_class
        end
      end

      module Serialization
        private

        def serialize_result(result)
          result.as_json
        end

        def serialize_errors(errors)
          errors
        end
      end

      include RestActions
      include Service
      include Serialization
      include ApiControllerConcerns::ExceptionHandling
    end
  end
end

Version data entries

25 entries across 25 versions & 1 rubygems

Version Path
rails-add_ons-2.2.1 app/controllers/api/service_controller/base.rb
rails-add_ons-2.2.0 app/controllers/api/service_controller/base.rb
rails-add_ons-2.1.1 app/controllers/api/service_controller/base.rb
rails-add_ons-2.1.0 app/controllers/api/service_controller/base.rb
rails-add_ons-2.0.2 app/controllers/api/service_controller/base.rb
rails-add_ons-2.0.1 app/controllers/api/service_controller/base.rb
rails-add_ons-2.0.0 app/controllers/api/service_controller/base.rb
rails-add_ons-1.5.2 app/controllers/api/service_controller/base.rb
rails-add_ons-1.5.1 app/controllers/api/service_controller/base.rb
rails-add_ons-1.5.0 app/controllers/api/service_controller/base.rb
rails-add_ons-1.4.1 app/controllers/api/service_controller/base.rb
rails-add_ons-1.4.0 app/controllers/api/service_controller/base.rb
rails-add_ons-1.3.3 app/controllers/api/service_controller/base.rb
rails-add_ons-1.3.2 app/controllers/api/service_controller/base.rb
rails-add_ons-1.3.1 app/controllers/api/service_controller/base.rb
rails-add_ons-1.3.0 app/controllers/api/service_controller/base.rb
rails-add_ons-1.2.0 app/controllers/api/service_controller/base.rb
rails-add_ons-1.1.0 app/controllers/api/service_controller/base.rb
rails-add_ons-1.0.0 app/controllers/api/service_controller/base.rb
rails-add_ons-0.6.0 app/controllers/api/service_controller/base.rb