diff --git a/app/controllers/categories_controller.rb b/app/controllers/categories_controller.rb index d128a54..faaa442 100644 --- a/app/controllers/categories_controller.rb +++ b/app/controllers/categories_controller.rb @@ -4,7 +4,7 @@ class CategoriesController < ApplicationController # GET /categories # GET /categories.json def index - @categories = Category.all + @categories = User.find(session[:user_id]).categories end # GET /categories/1 @@ -30,7 +30,7 @@ class CategoriesController < ApplicationController respond_to do |format| if @category.save - format.html { redirect_to @category, notice: 'Category was successfully created.' } + format.html { redirect_to :categories, notice: 'Category was successfully created.' } format.json { render :show, status: :created, location: @category } else format.html { render :new } @@ -56,10 +56,13 @@ class CategoriesController < ApplicationController # DELETE /categories/1 # DELETE /categories/1.json def destroy - @category.destroy - respond_to do |format| - format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' } - format.json { head :no_content } + if @category.destroy + respond_to do |format| + format.html { redirect_to categories_url, notice: 'Category was successfully destroyed.' } + format.json { head :no_content } + end + else + redirect_to categories_url, notice: @category.errors.full_messages.first end end diff --git a/app/models/category.rb b/app/models/category.rb index 60ce423..1635f56 100644 --- a/app/models/category.rb +++ b/app/models/category.rb @@ -1,10 +1,12 @@ class Category < ApplicationRecord has_many :documents - has_many :subcategories, class_name: 'Category', foreign_key: 'parent_id', dependent: :destroy + has_many :subcategories, class_name: 'Category', foreign_key: 'parent_id' belongs_to :parent, class_name: 'Category', optional: true belongs_to :user - before_destroy :check_for_subcategories + before_destroy :check_for_subcategories, :check_for_documents + + validates :name, presence: true def get_fqcn fqcn = name @@ -25,4 +27,11 @@ class Category < ApplicationRecord throw :abort end end + + def check_for_documents + unless documents.empty? + errors.add(:base, 'Documents present') + throw :abort + end + end end diff --git a/app/models/document.rb b/app/models/document.rb index 9107288..75bdc83 100644 --- a/app/models/document.rb +++ b/app/models/document.rb @@ -4,6 +4,8 @@ class Document < ApplicationRecord has_one :content, dependent: :destroy, required: false has_and_belongs_to_many :tags + validates :category, :doc, presence: true + has_attached_file :doc, { styles: { thumb: ["200x", :png] },