Sha256: 7a7c56b377f379cd12bde73699b7410b99da36bbe8e8f5dda13090b63a81c751

Contents?: true

Size: 992 Bytes

Versions: 5

Compression:

Stored size: 992 Bytes

Contents

class Market::Category < ActiveRecord::Base

  has_many :subcategories, inverse_of: :category, dependent: :destroy, class_name: '::Market::Category'
  belongs_to :category, inverse_of: :subcategories, class_name: '::Market::Category'

  validates :name, presence: true

  scope :ordered, -> { order(:position) }
  scope :top_level, -> { ordered.where(category_id: nil) }

  def full_name
    categories = [self]
    category = self
    while category.category
      category = category.category
      categories << category
    end
    categories.reverse.map(&:name).join(' -> ')
  end

  def self.for_combo(category)
    parents = top_level
    parents = parents.where.not(id: category.id) if category
    result = []
    parents.each do |top_level|
      result << top_level
      result << top_level.ordered_subcategories.order(:position).reject { |sub_category| sub_category == category }
    end
    result.flatten
  end

  def ordered_subcategories
    subcategories.ordered
  end

end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
coalla-cms-0.4.2.0 lib/generators/coalla/cms/market/templates/models/category.rb
coalla-cms-0.5.2.4 lib/generators/coalla/cms/market/templates/models/category.rb
coalla-cms-0.4.4.3 lib/generators/coalla/cms/market/templates/models/category.rb
coalla-cms-0.5.1.9 lib/generators/coalla/cms/market/templates/models/category.rb
coalla-cms-0.5.1.8 lib/generators/coalla/cms/market/templates/models/category.rb