Sha256: a172b3a09d4c0ac587e567802ab5452a58d94cb139f6e92241a3f7ceacbc5acf

Contents?: true

Size: 1.71 KB

Versions: 14

Compression:

Stored size: 1.71 KB

Contents

require_dependency 'notee/application_controller'

module Notee
  class CategoriesController < ApplicationController
    before_action :set_category, only: [:show, :update, :destroy]

    def index
      @categories = Category.where(is_deleted: false)
      render json: { status: 'success', categories: @categories }
    end

    def show
      render json: { status: 'success', category: @category }
    end

    def create
      @category = Category.new(category_params)
      respond_to do |format|
        if @category.save
          format.json { render json: @category, status: 200 }
        else
          format.json { render json: @category.errors, status: :unprocessable_entity }
        end
      end
    end

    def update
      respond_to do |format|
        Category.skip_callback(:save, :before, :set_slug)
        if @category.update(category_params)
          Category.set_callback(:save, :before, :set_slug)
          format.json { render json: @category, status: 200 }
        else
          Category.set_callback(:save, :before, :set_slug)
          format.json { render json: @category.errors, status: :unprocessable_entity }
        end
      end
    end

    def destroy
      respond_to do |format|
        if @category.update(slug: nil, is_deleted: true)
          Category.before_destroy_parent(@category.id)
          format.json { render json: @category, status: 200 }
        else
          format.json { render json: @category.errors, status: :unprocessable_entity }
        end
      end
    end

    private

    def category_params
      params.require(:category).permit(:name, :slug, :parent_id, :is_private)
    end

    def set_category
      @category = Category.find_by(id: params[:id])
    end
  end
end

Version data entries

14 entries across 14 versions & 1 rubygems

Version Path
notee-1.1.2.2 app/controllers/notee/categories_controller.rb
notee-1.1.2.1 app/controllers/notee/categories_controller.rb
notee-1.1.2 app/controllers/notee/categories_controller.rb
notee-1.1.1 app/controllers/notee/categories_controller.rb
notee-1.1.0 app/controllers/notee/categories_controller.rb
notee-1.0.8 app/controllers/notee/categories_controller.rb
notee-1.0.7 app/controllers/notee/categories_controller.rb
notee-1.0.6 app/controllers/notee/categories_controller.rb
notee-1.0.5 app/controllers/notee/categories_controller.rb
notee-1.0.4 app/controllers/notee/categories_controller.rb
notee-1.0.3 app/controllers/notee/categories_controller.rb
notee-1.0.2 app/controllers/notee/categories_controller.rb
notee-1.0.1 app/controllers/notee/categories_controller.rb
notee-1.0.0 app/controllers/notee/categories_controller.rb