Module: Cowtech::RubyOnRails::Models::Mongoid::Cowtech::ClassMethods

Defined in:
app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb

Overview

Uncomment for numeric ID included do

include Mongoid::Sequence
identity type: Integer
sequence :_id

end

Instance Method Summary collapse

Instance Method Details

#[](what, only_id = false) ⇒ Object



22
23
24
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 22

def [](what, only_id = false)
	self.__finalize(self.__safe_index_find(what), only_id)
end

#__finalize(record, only_id = false) ⇒ Object



66
67
68
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 66

def __finalize(record, only_id = false)
	record ? (only_id ? record.id : record) : nil
end

#__index_find(oid) ⇒ Object



60
61
62
63
64
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 60

def __index_find(oid)
	oid.blank? ? nil : self.find(BSON::ObjectId(oid))
rescue ::Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId
	nil
end

#__safe_index_find(what) ⇒ Object



70
71
72
73
74
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 70

def __safe_index_find(what)
	self.__index_find(what)
rescue ::Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId
	nil
end

#criteria(*args) ⇒ Object

Overrides for paranoia module to allow find associations on deleted documents



46
47
48
49
50
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 46

def criteria(*args)
	rv = super
	rv.selector = {}
	rv
end

#find_or_create(oid, attributes = nil) ⇒ Object



26
27
28
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 26

def find_or_create(oid, attributes = nil)
	self.safe_find(oid) || self.new(attributes)
end

#not_deletedObject



52
53
54
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 52

def not_deleted
	where(:deleted_at.exists => false)
end

#per_pageObject



41
42
43
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 41

def per_page
	25
end

#randomObject



36
37
38
39
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 36

def random
	c = self.count
	c != 0 ? self.skip(rand(c)).first : nil
end

#safe_find(oid) ⇒ Object



30
31
32
33
34
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 30

def safe_find(oid)
	rv = oid.blank? ? nil : self.find(BSON::ObjectId(oid))
rescue ::Mongoid::Errors::DocumentNotFound, BSON::InvalidObjectId
	nil
end

#valid_object_id?(oid) ⇒ Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 56

def valid_object_id?(oid)
	oid.blank? || BSON::ObjectId.legal?(oid)
end