Newer
Older
# == Schema Information
#
# Table name: communication_website_categories
#
# id :uuid not null, primary key
# description :text
# created_at :datetime not null
# updated_at :datetime not null
# communication_website_id :uuid not null
# university_id :uuid not null
#
# Indexes
#
# idx_communication_website_post_cats_on_communication_website_id (communication_website_id)
# index_communication_website_categories_on_parent_id (parent_id)
# index_communication_website_categories_on_university_id (university_id)
#
# Foreign Keys
#
# fk_rails_... (communication_website_id => communication_websites.id)
# fk_rails_... (parent_id => communication_website_categories.id)
# fk_rails_... (university_id => universities.id)
#
class Communication::Website::Category < ApplicationRecord
has_one :imported_category,
class_name: 'Communication::Website::Imported::Category',
dependent: :destroy
belongs_to :university
belongs_to :website,
foreign_key: :communication_website_id
belongs_to :parent,
class_name: 'Communication::Website::Category',
optional: true
belongs_to :program,
class_name: 'Education::Program',
optional: true
has_one :imported_category,
class_name: 'Communication::Website::Imported::Category',
dependent: :destroy
has_many :children,
class_name: 'Communication::Website::Category',
foreign_key: :parent_id,
dependent: :destroy
has_and_belongs_to_many :posts,
class_name: 'Communication::Website::Post',
join_table: 'communication_website_categories_posts',
foreign_key: 'communication_website_category_id',
association_foreign_key: 'communication_website_post_id'
validates :slug, uniqueness: { scope: :communication_website_id }
scope :ordered, -> { order(:position) }
before_create :set_position
new_record? ? website.list_of_categories
: website.list_of_categories.reject! { |p| p[:id] == id }
def path
# used in menu_item#jekyll_target
"/#{website.posts_github_directory}/#{slug}"
end
"#{website.posts_github_directory}/#{slug}.html"
end
def github_manifest
super << {
identifier: "collection_item",
data: -> (github_file) { ApplicationController.render(
template: "admin/communication/website/categories/jekyll_collection",
formats: [:yml],
layout: false,
last_element = website.categories.where(parent_id: parent_id).ordered.last
unless last_element.nil?
self.position = last_element.position + 1
else
self.position = 1
end
end
def slug_unavailable?(slug)
self.class.unscoped.where(communication_website_id: self.communication_website_id, slug: slug).where.not(id: self.id).exists?
end