class ProductCategory < Category
  # FIXME: do not allow category with products or inputs to be destroyed
  has_many :products
  has_many :inputs

  attr_accessible :name, :parent, :environment

  scope :unique, :select => 'DISTINCT ON (path) categories.*'
  scope :by_enterprise, -> enterprise {
    distinct.joins(:products).
    where('products.profile_id = ?', enterprise.id)
  }
  scope :by_environment, -> environment {
    where 'environment_id = ?', environment.id
  }
  scope :unique_by_level, -> level {
    select "DISTINCT ON (filtered_category) split_part(path, '/', #{level}) AS filtered_category, categories.*"
  }

  def all_products
    Product.where(product_category_id: (all_children << self).map(&:id))
  end

  def self.menu_categories(top_category, env)
    top_category ? top_category.children : top_level_for(env).select{|c|c.kind_of?(ProductCategory)}
  end

end

class ProductCategory < Category
  # FIXME: do not allow category with products or inputs to be destroyed
  has_many :products
  has_many :inputs

  attr_accessible :name, :parent, :environment

  scope :unique, :select => 'DISTINCT ON (path) categories.*'
  scope :by_enterprise, -> enterprise {
    joins(:products).
    where('products.profile_id = ?', enterprise.id)
  }
  scope :by_environment, -> environment {
    where 'environment_id = ?', environment.id
  }
  scope :unique_by_level, -> level {
    select "DISTINCT ON (filtered_category) split_part(path, '/', #{level}) AS filtered_category, categories.*"
  }

  def all_products
    Product.where(product_category_id: (all_children << self).map(&:id))
  end

  def self.menu_categories(top_category, env)
    top_category ? top_category.children : top_level_for(env).select{|c|c.kind_of?(ProductCategory)}
  end

end

Revert "return distinct elements in by_enterprise scope"
Revert "return distinct elements in by_enterprise scope"

This reverts commit 3057b70bfe7e25e4dee80cae10c408238f040a02, which
caused several tests to fail.
