Sha256: c7f4e0b80ed3276d54e98a48532ca5b1f1cc3f45349de4e7d27e642bac385f18

Contents?: true

Size: 778 Bytes

Versions: 2

Compression:

Stored size: 778 Bytes

Contents

class Usage < ApplicationRecord
  belongs_to :organization

  belongs_to :item, polymorphic: true
  belongs_to :parent_item, polymorphic: true

  scope :in_organization, ->(organization = Organization.current) { where(organization_id: organization.id) }

  before_save :set_slug
  before_destroy :destroy_children_usages!

  def self.destroy_all_where(query)
    where(query).destroy_all
  end

  def self.destroy_usages_for(record)
    destroy_all_where(parent_item: record)
  end

  def destroy_children_usages!
    item.children.each { |child| Usage.destroy_all_where(item: child, organization: organization) }
  end

  def index_children!(children)
    children.each { |it| it.index_usage! organization }
  end

  private

  def set_slug
    self.slug = item.slug
  end
end

Version data entries

2 entries across 2 versions & 2 rubygems

Version Path
mumuki-laboratory-7.1.0 vendor/bundle/ruby/2.3.0/bundler/gems/mumuki-domain-925adf85cca8/app/models/usage.rb
mumuki-domain-7.1.0 app/models/usage.rb