Sha256: e440983715cef17575d91b516d2de7b85fef0c9225d056077f6d3da4f123fbc4
Contents?: true
Size: 1.7 KB
Versions: 14
Compression:
Stored size: 1.7 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 render json: {success: true, data: serialize(data)} 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 => 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 => 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
14 entries across 14 versions & 1 rubygems