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