Sha256: 72e5fb7c9859ac6e8660c6dad34498c429ffe4068a77efdb7fac184c5898de50
Contents?: true
Size: 1.91 KB
Versions: 1
Compression:
Stored size: 1.91 KB
Contents
require_dependency "papercat/application_controller" module Papercat module Api class BaseController < ApplicationController before_action :check_xhr! before_action :set_collection, only: [:index] before_action :set_record, only: [:show, :update, :destroy] respond_to :json rescue_from ActiveRecord::RecordNotFound do |exception| render nothing: true, :status => :not_found end def index respond_with(@collection, location: collection_path) end def show respond_with(@record, location: record_path) end def create @record = model.new(model_params) @record.save respond_with(@record, location: record_path) end def update @record.update(model_params) respond_with(@record, location: record_path) end def destroy @record.destroy respond_with(@record, location: collection_path) end protected def collection_name @collection_name ||= self.class.name.demodulize.gsub(/Controller/,'').downcase end def model_name @model_name ||= collection_name.singularize end def model @model ||= ('Papercat::' + model_name.classify).constantize end def permitted_attributes end def record_path return collection_path unless @record.persisted? public_send :"api_#{model_name}_path", @record end def collection_path public_send :"api_#{collection_name}_path" end private def set_collection @collection = model.all end def set_record @record = model.find_by!(id: params[:id]) end def model_params params.require(model_name).permit(permitted_attributes) end def check_xhr! render nothing: true, satus: 400 and return unless request.xhr? end end end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
papercat-0.0.2 | app/controllers/papercat/api/base_controller.rb |