Sha256: e65959f4457eb58ec1035f65093e092ed2c38ce9ff1c8794d4bdeae8f98b3533

Contents?: true

Size: 1009 Bytes

Versions: 1

Compression:

Stored size: 1009 Bytes

Contents

##
# Categorizing 是分类(category)和被分类项(categorized)之间的关联关系。

module Unidom
  module Category

    class Categorizing < ApplicationRecord

      self.table_name = 'unidom_categorizings'

      include Unidom::Common::Concerns::ModelExtension

      belongs_to :category,    class_name:  'Unidom::Category::Category'
      belongs_to :categorized, polymorphic: true

      scope :category_is,    ->(category)    { where category_id: to_id(category) }
      scope :categorized_is, ->(categorized) { where categorized: categorized     }

      def self.categorize!(categorized, into: nil, at: Time.now)

        assert_present! :categorized, categorized
        assert_present! :into,        into
        assert_present! :at,          at

        categorized_is(categorized).category_is(into).valid_at.alive.first_or_create! elemental: true, opened_at: at

      end

    end unless Unidom::Common::Neglection.namespace_neglected? 'Unidom::Category::Categorizing'

  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
unidom-category-2.0.1 app/models/unidom/category/categorizing.rb