lib/saasable/mongoid/scoped_document.rb in saasable-5.0.5 vs lib/saasable/mongoid/scoped_document.rb in saasable-5.1.0

- old
+ new

@@ -1,30 +1,38 @@ +# frozen_string_literal: true + module Saasable::Mongoid::ScopedDocument - def self.included klass + def self.included(klass) klass.extend ClassMethods klass.class_eval do # Fields - field :saas_id, type: BSON::ObjectId, default: ->{ Saasable::Mongoid::SaasDocument.active_saas._id } + field :saas_id, type: BSON::ObjectId, default: -> { Saasable::Mongoid::SaasDocument.active_saas._id } # Default scope - default_scope ->{ Saasable::Mongoid::SaasDocument.active_saas ? where(saas_id: Saasable::Mongoid::SaasDocument.active_saas._id) : all } + default_scope do + if Saasable::Mongoid::SaasDocument.active_saas + where(saas_id: Saasable::Mongoid::SaasDocument.active_saas._id) + else + all + end + end # Indexes - index({saas_id: 1}) + index(saas_id: 1) index({saas_id: 1, _id: 1}, unique: true) class << self alias_method_chain :index, :saasable end end end - def saas= a_saas + def saas=(a_saas) self.saas_id = a_saas._id end def saas - @saas ||= Saasable::Mongoid::SaasDocument.saas_document.find(self.saas_id) + @saas ||= Saasable::Mongoid::SaasDocument.saas_document.find(saas_id) if saas_id end module ClassMethods def validates_uniqueness_of(*args) attributes = _merge_attributes(args)