Sha256: de3181bd5a04030e549915752ef56c0345ba7ace154ba98d2aa67e0c0793832f

Contents?: true

Size: 1.61 KB

Versions: 5

Compression:

Stored size: 1.61 KB

Contents

# frozen_string_literal: true

module Azeroth
  class RequestHandler
    # @api private
    #
    # hadler for requests to create resources
    class Create < RequestHandler
      private

      delegate :build_with, to: :options

      # @private
      #
      # Creates and return an instance of the model
      #
      # creation uses the method +"#{model.name}_params"+ to
      # fetch all allowed attributes for creation
      #
      # @return [Object]
      def resource
        @resource ||= build_and_save_resource
      end

      # @private
      #
      # build resource for create and save it
      #
      # @return [Object]
      def build_and_save_resource
        @resource = build_resource
        controller.instance_variable_set("@#{model.name}", resource)

        trigger_event(:save) do
          resource.tap(&:save)
        end
      end

      # @private
      #
      # build resource without saving it
      #
      # when +build_with+ option is given, the proc/method
      # is called instead of collection.build
      #
      # @return [Object] resource built
      def build_resource
        return collection.build(attributes) unless build_with

        case build_with
        when Proc
          controller.instance_eval(&build_with)
        else
          controller.send(build_with)
        end
      end

      # @private
      #
      # Response status
      #
      # For success, returns +:created+, for
      # validation errors, it returns +:unprocessable_entity+
      #
      # @return [Symbol]
      def status
        resource.valid? ? :created : :unprocessable_entity
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
azeroth-0.8.0 lib/azeroth/request_handler/create.rb
azeroth-0.7.4 lib/azeroth/request_handler/create.rb
azeroth-0.7.3 lib/azeroth/request_handler/create.rb
azeroth-0.7.2 lib/azeroth/request_handler/create.rb
azeroth-0.7.1 lib/azeroth/request_handler/create.rb