Sha256: 2177201524bd9e7b2f00ea45344f9aac9fc33cd6fc304e0554d49830d6e1a0ce

Contents?: true

Size: 1.43 KB

Versions: 3

Compression:

Stored size: 1.43 KB

Contents

# frozen_string_literal: true

module EIVO
  module Concerns
    module Resource
      extend ::ActiveSupport::Concern

      included do
        before_action :set_default_serializer_options
      end

      def show
        @object ||= collection
        render_success serializer.new(@object, @serializer_options)
      end

      def create
        @object ||= collection.new(object_params_create)
        if @object.save
          render_success serializer.new(@object, @serializer_options)
        else
          render_model_errors @object.errors
        end
      end

      def update
        @object ||= collection
        if @object.update(object_params_update)
          render_success serializer.new(@object, @serializer_options)
        else
          render_model_errors @object.errors
        end
      end

      def destroy
        @object ||= collection

        if @object.destroy
          render_success
        else
          render_model_errors @object.errors
        end
      end

    protected
      
      def collection
        raise NotImplementedError
      end

      def serializer
        raise NotImplementedError
      end

      def object_params
        raise NotImplementedError
      end

      def object_params_create
        object_params
      end

      def object_params_update
        object_params
      end
    
      def set_default_serializer_options
        @serializer_options ||= {}
      end

    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
eivo-rails-api-0.0.6 app/controllers/eivo/concerns/resource.rb
eivo-rails-api-0.0.5 app/controllers/eivo/concerns/resource.rb
eivo-rails-api-0.0.4 app/controllers/eivo/concerns/resource.rb