Module: Tenacity::OrmExt::Mongoid::ClassMethods

Includes:
Helpers
Defined in:
lib/tenacity/orm_ext/mongoid.rb

Overview

:nodoc:

Instance Method Summary collapse

Methods included from Helpers

#_t_serialize_id_for_sql, #_t_serialize_ids, #id_class_for

Instance Method Details

#_t_delete(ids, run_callbacks = true) ⇒ Object



113
114
115
116
117
118
119
120
# File 'lib/tenacity/orm_ext/mongoid.rb', line 113

def _t_delete(ids, run_callbacks=true)
  docs = _t_find_bulk(ids)
  if run_callbacks
    docs.each { |doc| doc.destroy }
  else
    docs.each { |doc| doc.delete }
  end
end

#_t_find(id) ⇒ Object



52
53
54
55
56
# File 'lib/tenacity/orm_ext/mongoid.rb', line 52

def _t_find(id)
  (id.nil? || id.to_s.strip == "") ? nil : find(_t_serialize(id))
rescue ::Mongoid::Errors::DocumentNotFound
  nil
end

#_t_find_all_by_associate(property, id) ⇒ Object



78
79
80
81
82
83
84
# File 'lib/tenacity/orm_ext/mongoid.rb', line 78

def _t_find_all_by_associate(property, id)
  if respond_to?(:where)
    where(property => _t_serialize(id)).all
  else
    all(:conditions => { property => _t_serialize(id) })
  end
end

#_t_find_all_ids_by_associate(property, id) ⇒ Object



86
87
88
89
# File 'lib/tenacity/orm_ext/mongoid.rb', line 86

def _t_find_all_ids_by_associate(property, id)
  results = collection.find(property => _t_serialize(id))
  results.map { |r| r['_id'] }
end

#_t_find_bulk(ids) ⇒ Object



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/tenacity/orm_ext/mongoid.rb', line 58

def _t_find_bulk(ids)
  if respond_to?(:where)
    ids = [ids].flatten
    docs = where(:_id.in => _t_serialize_ids(ids)).all
  else
    docs = find(_t_serialize_ids(ids))
  end
  docs.respond_to?(:each) ? docs : [docs]
rescue ::Mongoid::Errors::DocumentNotFound
  []
end

#_t_find_first_by_associate(property, id) ⇒ Object



70
71
72
73
74
75
76
# File 'lib/tenacity/orm_ext/mongoid.rb', line 70

def _t_find_first_by_associate(property, id)
  if respond_to?(:where)
    where(property => _t_serialize(id)).first
  else
    first(:conditions => { property => _t_serialize(id) })
  end
end

#_t_id_typeObject



48
49
50
# File 'lib/tenacity/orm_ext/mongoid.rb', line 48

def _t_id_type
  String
end

#_t_initialize_belongs_to_association(association) ⇒ Object



105
106
107
108
109
110
111
# File 'lib/tenacity/orm_ext/mongoid.rb', line 105

def _t_initialize_belongs_to_association(association)
  unless self.respond_to?(association.foreign_key)
    field association.foreign_key, :type => id_class_for(association)
    field association.polymorphic_type, :type => String if association.polymorphic?
    after_destroy { |record| record._t_cleanup_belongs_to_association(association) }
  end
end

#_t_initialize_has_many_association(association) ⇒ Object



100
101
102
103
# File 'lib/tenacity/orm_ext/mongoid.rb', line 100

def _t_initialize_has_many_association(association)
  after_save { |record| self.class._t_save_associates(record, association) }
  before_destroy { |record| record._t_cleanup_has_many_association(association) }
end

#_t_initialize_has_one_association(association) ⇒ Object



96
97
98
# File 'lib/tenacity/orm_ext/mongoid.rb', line 96

def _t_initialize_has_one_association(association)
  before_destroy { |record| record._t_cleanup_has_one_association(association) }
end

#_t_initialize_tenacityObject



91
92
93
94
# File 'lib/tenacity/orm_ext/mongoid.rb', line 91

def _t_initialize_tenacity
  before_save { |record| record._t_verify_associates_exist }
  after_save { |record| record._t_save_autosave_associations }
end