Sha256: 985f6ad342040d9630d75bf5c3e957f047dbe368fe2cba3a6815b2aa2d172a3b
Contents?: true
Size: 998 Bytes
Versions: 42
Compression:
Stored size: 998 Bytes
Contents
module WithUsages extend ActiveSupport::Concern included do has_many :usages, as: :item before_destroy :ensure_unused! organic_on :usages end def usage_in_organization(organization = Organization.current) usages_in_organization(organization).first.try(:parent_item) end def usage_in_organization_of_type(type, organization = Organization.current) item = usage_in_organization(organization) item.is_a?(type) ? item : nil end class_methods do def aggregate_of(association) super revamp "rebuild_#{association}!" do |_, this, children, hyper| old_children = this.send association added_children = children - old_children hyper.(children) this.usages.each { |it| it.index_children!(added_children) } this end end end private def ensure_unused! if usages.present? errors.add :base, :in_use, organization: usages.first.organization.name throw :abort end end end
Version data entries
42 entries across 42 versions & 2 rubygems