Sha256: ed8409dbd98534a6649afce5839022f4c4dbd4d47d15b7b52b36aa29a65a59f4

Contents?: true

Size: 1.79 KB

Versions: 41

Compression:

Stored size: 1.79 KB

Contents

class CamaleonCms::Category < CamaleonCms::TermTaxonomy
  alias_attribute :site_id, :term_group
  alias_attribute :post_type_id, :status

  default_scope { where(taxonomy: :category) }
  scope :no_empty, -> { where('count > 0') } # return all categories that contains at least one post
  scope :empty, -> { where(count: [0, nil]) } # return all categories that does not contain any post
  # scope :parents, -> { where("term_taxonomy.parent_id IS NULL") }

  cama_define_common_relationships('Category')
  has_many :posts, foreign_key: :objectid, through: :term_relationships, source: :objects
  has_many :children, class_name: 'CamaleonCms::Category', foreign_key: :parent_id,
    dependent: :destroy
  belongs_to :parent, class_name: 'CamaleonCms::Category', foreign_key: :parent_id
  belongs_to :post_type_parent, class_name: 'CamaleonCms::PostType', foreign_key: :parent_id,
    inverse_of: :categories
  belongs_to :site, class_name: 'CamaleonCms::Site', foreign_key: :site_id

  before_save :set_site
  before_destroy :set_posts_in_default_category

  # return the post type of this category
  def post_type
    cama_fetch_cache('post_type') do
      ctg = self
      begin
        pt = ctg.post_type_parent
        ctg = ctg.parent
      end while ctg.present?
      pt
    end
  end

  private

  def set_site
    pt = self.post_type
    self.site_id = pt.site_id unless site_id.present?
    self.status = pt.id unless status.present?
  end

  # rescue all posts to assign into default category if they don't have any category assigned
  def set_posts_in_default_category
    category_default = self.post_type.default_category
    return if category_default == self
    self.posts.each do |post|
      if post.categories.where.not(id: id).blank?
        post.assign_category(category_default.id)
      end
    end
  end
end

Version data entries

41 entries across 41 versions & 2 rubygems

Version Path
camaleon_cms-2.5.3.1 app/models/camaleon_cms/category.rb
camaleon_cms-2.5.3 app/models/camaleon_cms/category.rb
camaleon_cms-2.5.2 app/models/camaleon_cms/category.rb
camaleon_cms_rails6-2.5.0 app/models/camaleon_cms/category.rb
camaleon_cms-2.5.1 app/models/camaleon_cms/category.rb
camaleon_cms-2.5.0 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.6.9 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.6.8 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.6.7 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.6.6 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.6.5 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.6.4 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.6.3 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.6.2 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.6.1 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.6.0 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.5.14 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.5.13 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.5.12 app/models/camaleon_cms/category.rb
camaleon_cms-2.4.5.11 app/models/camaleon_cms/category.rb