Sha256: a27c3f5dd4fce1f31312235e87b888e1931792c0538184b5176dec7afdb4ca9b

Contents?: true

Size: 1.19 KB

Versions: 42

Compression:

Stored size: 1.19 KB

Contents

module Virgo
  class Category < ActiveRecord::Base
    enable_redis_tracking

    extend FriendlyId

    friendly_id :name, use: :slugged

    has_many :post_categories, dependent: :destroy
    has_many :posts, ->{ where(post_type: :post) }, through: :post_categories

    belongs_to :parent, class_name: 'Virgo::Category'

    belongs_to :created_by, class_name: 'Virgo::User'

    has_many :children, class_name: 'Virgo::Category', foreign_key: :parent_id

    validates :name, presence: true, uniqueness: true

    scope :visible, ->{ where(visible: true) }

    scope :by_weight, ->{ order(navbar_weight: :asc) }

    scope :navbar, ->{ where(display_in_navbar: true).by_weight }

    scope :bottom_level, ->{
      where("virgo_categories.id NOT IN (SELECT DISTINCT virgo_categories.parent_id FROM virgo_categories WHERE virgo_categories.parent_id IS NOT NULL)")
    }

    scope :top_level, ->{
      where(parent_id: nil)
    }

    scope :with_post_count, ->{
      select("virgo_categories.*, " +
        "(SELECT COUNT(*) FROM virgo_post_categories WHERE virgo_post_categories.category_id = virgo_categories.id) AS post_count"
      )
    }

    def self.COLUMNS
      find_by(name: "Columns")
    end
  end
end

Version data entries

42 entries across 42 versions & 1 rubygems

Version Path
virgo-0.3.17 app/models/virgo/category.rb
virgo-0.3.16 app/models/virgo/category.rb
virgo-0.3.15 app/models/virgo/category.rb
virgo-0.3.14 app/models/virgo/category.rb
virgo-0.3.13 app/models/virgo/category.rb
virgo-0.3.12 app/models/virgo/category.rb
virgo-0.3.11 app/models/virgo/category.rb
virgo-0.3.10 app/models/virgo/category.rb
virgo-0.3.9 app/models/virgo/category.rb
virgo-0.3.8 app/models/virgo/category.rb
virgo-0.3.7 app/models/virgo/category.rb
virgo-0.3.6 app/models/virgo/category.rb
virgo-0.3.4 app/models/virgo/category.rb
virgo-0.3.3 app/models/virgo/category.rb
virgo-0.3.2 app/models/virgo/category.rb
virgo-0.3.1 app/models/virgo/category.rb
virgo-0.3 app/models/virgo/category.rb
virgo-0.2.9 app/models/virgo/category.rb
virgo-0.2.8 app/models/virgo/category.rb
virgo-0.2.7 app/models/virgo/category.rb