Sha256: a941f40dcf2b27ab7ddc4c50a3a7ba87618d2f6abb01d699d0992a0ab8eb0fe0

Contents?: true

Size: 1.46 KB

Versions: 2

Compression:

Stored size: 1.46 KB

Contents

# frozen_string_literal: true

module Azeroth
  class RequestHandler
    # @api private
    #
    # hadler for requests to update resource
    class Update < RequestHandler
      private

      delegate :update_with, to: :options

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

      # Update a resource saving it to the database
      #
      # @return [Object] updated resource
      def perform_update
        @resource = controller.send(model.name)
        resource.tap do
          trigger_event(:save) do
            update_and_save_resource
          end
        end
      end

      # @private
      #
      # Update the resource
      #
      # This update happens by either by running update_with
      # or directly updating the attributes in the object
      #
      # @return [Object] updated resource
      def update_and_save_resource
        return resource.update(attributes) unless update_with

        controller.run(update_with)
      end

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

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
azeroth-1.0.0 lib/azeroth/request_handler/update.rb
azeroth-0.10.1 lib/azeroth/request_handler/update.rb