Module: Cowtech::RubyOnRails::Models::Mongoid::Cowtech::ClassMethods
- Defined in:
- app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb
Overview
Instance Method Summary collapse
- #[](what, only_id = false) ⇒ Object
- #__finalize(record, only_id = false) ⇒ Object
- #__index_find(oid) ⇒ Object
- #__safe_index_find(what) ⇒ Object
-
#criteria(*args) ⇒ Object
Overrides for paranoia module to allow find associations on deleted documents.
- #find_or_create(oid, attributes = nil) ⇒ Object
- #not_deleted ⇒ Object
- #per_page ⇒ Object
- #random ⇒ Object
- #safe_find(oid) ⇒ Object
- #valid_object_id?(oid) ⇒ Boolean
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_deleted ⇒ Object
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_page ⇒ Object
41 42 43 |
# File 'app/models/cowtech/ruby_on_rails/models/mongoid/cowtech.rb', line 41 def per_page 25 end |
#random ⇒ Object
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
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 |