Report abuse


			
# == Description
#
# Section is a descendent of the Kategory class. It is the basis of the Magazine section. Each section contains multiple
# Categories, which hold the content to the site.
#
# == Relationships
#
# These are called from an instance of the section. ie: Section.find(101).categories
#
#   categories            #=> All categories related to this section
#   pages                 #=> Pages are tied to the section via the category
#   quizzes               #=> All approved pages that are ready to be published and have a contentable_type of 'Quiz'
#   articles              #=> All approved pages that are ready to be published and have a contentable_type of 'Article'
#   links                 #=> All links that are associated to the categories
#   
#   featured_quiz         #=> The featured quiz
#   featured_game         #=> The featured game
#   featured_contest      #=> The featured contest
#
# For a better understanding, try viewing the source
#
# == Validations
#
# The model has the following validations:
#
#   validates_presence_of :meta_title           #=> Meta Title required for SEO
#   validates_presence_of :meta_description     #=> Meta Description required for SEO
#
# Also note that this model has inherited validations from the Kategory model
#
# == Schema Information
# Schema version: 67
#
# Table name: kategories
#
#  id               :integer       not null, primary key
#  title            :string(255)
#  slug             :string(255)
#  parent_id        :integer
#  typeof           :string(255)
#  position         :string(255)
#  meta_title       :string(255)
#  meta_description :string(255)
#  meta_keywords    :string(255)
#  ad_keyword       :string(255)
#

class Section < Kategory
  has_many :categories, :foreign_key => 'parent_id', :order => 'position'
  has_many :pages, :through => :categories

  has_many :quizzes, :through => :categories, :source => 'pages', :conditions => ['contentable_type = ? AND approved = ? AND featured_at < ?','Quiz',true,Time.now], :order => 'featured_at DESC'
  has_many :articles, :through => :categories, :source => 'pages', :conditions => ['contentable_type = ? AND approved = ? AND featured_at < ?','Article',true,Time.now], :order => 'featured_at DESC'

  has_many :links, :through => :categories

  has_one :featured_quiz, :class_name => 'FeaturedItem', :conditions => ['typeof = ?','Quiz']
  has_one :featured_game, :class_name => 'FeaturedItem', :conditions => ['typeof = ?','Game']
  has_one :featured_contest, :class_name => 'FeaturedItem', :conditions => ['typeof = ?','Contest']

  # Find the featured page for this section. If no page is found, just send a blank page.
  #
  # ie. Section.find(101).featured_page   #=> <#Page Object>
  def featured_page
    Page.approved_scope {
      @featured_page ||= pages.find(:first, :conditions => ['featured_at < ?',Time.now], :order => 'featured_at DESC') || Page.new
    }
  end

  # Check the section to make sure that the Meta Title and Meta Description are present
  # A section cannot be saved without these fields filled in
  #
  # Note that title and slug are already required in the Kategory Class that this class descends from
  validates_presence_of :meta_title, :meta_description

end