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



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

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



69
70
71
# File 'lib/tenacity/orm_ext/mongoid.rb', line 69

def _t_find_all_by_associate(property, id)
  find(:all, :conditions => { property => _t_serialize(id) })
end

#_t_find_all_ids_by_associate(property, id) ⇒ Object



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

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

#_t_find_bulk(ids) ⇒ Object



58
59
60
61
62
63
# File 'lib/tenacity/orm_ext/mongoid.rb', line 58

def _t_find_bulk(ids)
  docs = find(_t_serialize_ids(ids))
  docs.respond_to?(:each) ? docs : [docs]
rescue ::Mongoid::Errors::DocumentNotFound
  []
end

#_t_find_first_by_associate(property, id) ⇒ Object



65
66
67
# File 'lib/tenacity/orm_ext/mongoid.rb', line 65

def _t_find_first_by_associate(property, id)
  find(:first, :conditions => { property => _t_serialize(id) })
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



92
93
94
95
96
97
98
# File 'lib/tenacity/orm_ext/mongoid.rb', line 92

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



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

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



83
84
85
# File 'lib/tenacity/orm_ext/mongoid.rb', line 83

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

#_t_initialize_tenacityObject



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

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