Sha256: 902c66e1d65cdf46b49fa3f0144b411f97fa867d924dd410f79806010907622e

Contents?: true

Size: 901 Bytes

Versions: 3

Compression:

Stored size: 901 Bytes

Contents

module Saasable::Mongoid::ScopedDocument
  @scoped_documents = []

  def self.included klass
    @scoped_documents << klass unless @scoped_documents.include? klass

    klass.extend ClassMethods
    klass.class_eval do
      # Fields
      field :saas_id, :type => BSON::ObjectId
      
      # Indexes
      index :saas_id
      index [[:saad_id, 1], [:_id, 1]]
    end
  end

  def self.scoped_documents
    @scoped_documents
  end

  def saas= a_saas
    self.saas_id = a_saas._id
  end

  def saas
    @saas ||= Saasable::Mongoid::SaasDocument.saas_document.find(self.saas_id)
  end

  module ClassMethods
    def validates_uniqueness_of(*args)
      attributes = _merge_attributes(args)
      attributes[:scope] ||= []
      attributes[:scope] << :saas_id unless attributes[:scope].include?(:saas_id)

      validates_with(Mongoid::Validations::UniquenessValidator, attributes)
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
saasable-0.3.5 lib/saasable/mongoid/scoped_document.rb
saasable-0.3.3 lib/saasable/mongoid/scoped_document.rb
saasable-0.3.2 lib/saasable/mongoid/scoped_document.rb