Sha256: 44fb07e0610a2ff90459b1c0f083f0efcd9a3dc2e9d89749553fb911023eac2e

Contents?: true

Size: 1.98 KB

Versions: 2

Compression:

Stored size: 1.98 KB

Contents

require_dependency "<%= module_snake %>/api/v<%= api_version %>/application_controller"
require 'authorization'

module <%= module_camel %>
  class Api::V1::<%= resource_camel.pluralize %>Controller < Api::V1::ApplicationController
    before_action :set_<%= resource_singular %>, only: [:show, :update, :destroy]

		# GET /api/1/<%= resource_plural %>
    def index
      @<%= resource_plural %> = ::<%= module_camel %>::<%= resource_camel %>.all
      render json: @<%= resource_plural %>
    end

    # GET /api/1/<%= resource_plural %>/1
    def show
    	render json: @<%= resource_singular %>
    end

    # POST /api/1/<%= resource_plural %>
    def create
      @<%= resource_singular %> = ::<%= module_camel %>::<%= resource_camel %>.new(<%= resource_singular %>_params)
      if @<%= resource_singular %>.save
        render json: @<%= resource_singular %>
      else
        render :json => {errors: @<%= resource_singular %>.errors}, status: :unprocessable_entity
      end
    end

    # PATCH/PUT /api/1/<%= resource_plural %>/1
    def update
    	if @<%= resource_singular %>.update(<%= resource_singular %>_params)
        render json: @<%= resource_singular %>
      else
        render :json => {errors: @<%= resource_singular %>.errors}, status: :unprocessable_entity
      end
    end

    # DELETE /api/1/<%= resource_plural %>/1
    def destroy
      @<%= resource_singular %>.destroy
      render json: {}
    end

    private

      # Use callbacks to share common setup or constraints between actions.

      def set_<%= resource_singular %>
        @<%= resource_singular %> = ::<%= module_camel %>::<%= resource_camel %>.find_by_id(params[:id])
        if @<%= resource_singular %>.nil?
          render :json => {errors: "<%= resource_camel %> was not found"}, status: :not_found
        end
      end

      # Only allow a trusted parameter "white list" through.

      def <%= resource_singular %>_params
        params.require(:<%= resource_singular %>).permit(:params_here)
      end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
json_voorhees-0.0.2 lib/generators/json_voorhees/engine_create_controller/templates/no_auth_controller_template.rb.erb
json_voorhees-0.0.1 lib/generators/json_voorhees/engine_create_controller/templates/no_auth_controller_template.rb.erb