Sha256: 284b8e6c796472eee64b8a418d7c47d9acb0ab5b4027089faa02720997fc469e

Contents?: true

Size: 1.8 KB

Versions: 4

Compression:

Stored size: 1.8 KB

Contents

# Category Rollup 是分类的层级关系。

class Unidom::Category::CategoryRollup < ActiveRecord::Base

  self.table_name = 'unidom_category_rollups'

  include Unidom::Common::Concerns::ModelExtension

  validates :distance, presence: true, numericality: { integer_only: true, greater_than: 0 }

  belongs_to :ancestor_category,   class_name: 'Unidom::Category::Category'
  belongs_to :descendant_category, class_name: 'Unidom::Category::Category'

  scope :apart, ->(distance = 1) { where distance: distance }

  scope :ancestor_category_is,   ->(category) { where ancestor_category_id:   (category.respond_to?(:id) ? category.id : category) }
  scope :descendant_category_is, ->(category) { where descendant_category_id: (category.respond_to?(:id) ? category.id : category) }

  after_create ->(record) {
    return unless 1==record.distance
    ancestor_category = record.ancestor_category_id
    ancestor_category_rollups_1 = self.class.descendant_category_is(record.ancestor_category_id).alive
    ancestor_category_rollups_1.each do |ancestor_category_rollup|
      self.class.apart(1+ancestor_category_rollup.distance).
        ancestor_category_is(ancestor_category_rollup.ancestor_category_id).
        descendant_category_is(record.descendant_category_id).
        first_or_create! elemental: ancestor_category_rollup.elemental, from_date: ancestor_category_rollup.from_date
    end
  }

  #def self.roll_up!(ancestor_category, descendant_category, opened_at = Time.now)
  #  self.descendant_category_is(descendant_category).ancestor_category_is(ancestor_category).valid_at.alive.first_or_create! opened_at: opened_at
  #end

  def self.roll_up!(descendant_category, into: nil, at: Time.now)
    self.descendant_category_is(descendant_category).ancestor_category_is(into).valid_at.alive.first_or_create! opened_at: at
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
unidom-category-1.3 app/models/unidom/category/category_rollup.rb
unidom-category-1.2 app/models/unidom/category/category_rollup.rb
unidom-category-1.1 app/models/unidom/category/category_rollup.rb
unidom-category-1.0 app/models/unidom/category/category_rollup.rb