Sha256: e1c3c3c56891c923f0a4ab4a7a6e9c426be804053221fc1abb4cb15f76e83bd0

Contents?: true

Size: 1.96 KB

Versions: 21

Compression:

Stored size: 1.96 KB

Contents

module Comee
  module Core
    module Common
      extend ActiveSupport::Concern

      included do
        before_action :set_clazz
        before_action :set_object, only: %i[show update]
      end

      def index
        data = if block_given?
                 yield
               else
                 @clazz.all
               end
        page = params[:page]
        if page
          render json: {success: true, data: serialize(data), page: page, total: @clazz.count}
        else
          render json: {success: true, data: serialize(data)}
        end
      end

      def show
        render json: {success: true, data: serialize(@obj)}
      end

      def create
        obj = if block_given?
                yield
              else
                @clazz.new(model_params)
              end
        if obj.save
          render json: {success: true, data: serialize(obj)}, status: :created
        else
          render json: {success: false, error: obj.errors.full_messages[0]}, status: :unprocessable_entity
        end
      rescue StandardError => e
        render json: {success: false, error: e.message}
      end

      def update
        obj = if block_given?
                yield
              else
                obj = @obj
              end
        if obj.update(model_params)
          render json: {success: true, data: serialize(obj)}
        else
          render json: {success: false, error: obj.errors.full_messages[0]}, status: :unprocessable_entity
        end
      rescue StandardError => e
        render json: {success: false, error: e.message}
      end

      private

      def serialize(data)
        ActiveModelSerializers::SerializableResource.new(data)
      end

      def set_clazz
        @clazz = "Comee::Core::#{controller_name.classify}".constantize
      end

      def set_object
        @obj = @clazz.find(params[:id])
      end

      # This class should be overridden by respective child controllers
      def model_params; end
    end
  end
end

Version data entries

21 entries across 21 versions & 1 rubygems

Version Path
comee_core-0.1.82 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.81 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.80 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.79 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.78 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.77 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.76 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.75 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.74 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.73 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.72 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.71 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.70 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.69 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.68 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.67 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.66 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.65 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.64 app/controllers/concerns/comee/core/common.rb
comee_core-0.1.63 app/controllers/concerns/comee/core/common.rb