class Category < ActiveRecord::Base ##--- Associaciones has_and_belongs_to_many :products belongs_to :parent, class_name: "Category" has_many :children, -> { where "categories.status!= 0" }, class_name: "Category", foreign_key: 'parent_id' enum status: [:erased, :active, :inactive] ##--- Llevar registro de Actividad del usuario audited ##--- Validaciones previas de guardar validates :category, presence: { message: "Debe capturar el nombre de la lĂ­nea de producto." } ##--- Tipo de vistas / consultas scope :vigentes, -> { where("status != 0").order("status ASC, category ASC") } scope :activos, -> { where("status = 1").order("category ASC") } scope :activos_padre, -> { where("categories.status = 1 AND categories.parent_id = 0").order("categories.category ASC") } scope :get_parents, -> { where("categories.parent_id = 0 and categories.status != 0").order("categories.category ASC") } def descendents self_and_descendents - [self] end def self_and_descendents self.class.tree_for(self) end def self.tree_for(instance) where("#{table_name}.id IN (#{tree_sql_for(instance)})").order("#{table_name}.id") end def sales_by_category(pointsale_id) @pointsale = Pointsale.find(pointsale_id) @category_parents = Category.get_parents @sales = Pointsale.find(pointsale_id).sales.activas puts "PUNTO DE VENTA: #{@pointsale.name}" puts "CATEGORIAS: #{@category_parents.count}" puts "TOTAL DE VENTAS: #{@sales.count}" @category.each do |category| # category.children.each do |childre_of_category| # end end end def self.tree_sql_for(instance) tree_sql = <<-SQL WITH RECURSIVE search_tree(id, path) AS ( SELECT id, ARRAY[id] FROM #{table_name} WHERE id = #{instance.id} UNION ALL SELECT #{table_name}.id, path || #{table_name}.id FROM search_tree JOIN #{table_name} ON #{table_name}.parent_id = search_tree.id WHERE NOT #{table_name}.id = ANY(path) ) SELECT id FROM search_tree ORDER BY path SQL end before_save do self.parent_id = 0 if parent_id.nil? end end