Module: MongoMapper::Plugins::Micelaneous::ClassMethods
- Defined in:
- lib/mongo_mapper_ext/plugins/micelaneous.rb
Instance Method Summary collapse
-
#all_sequentially(&block) ⇒ Object
Sequentiall :all for big collection.
-
#belongs_to(association_id, options = {}, &extension) ⇒ Object
CounterCache belongs_to :item, :counter_cashe => true.
-
#upsert(*args) ⇒ Object
shortcut for upsert.
-
#use_database(database_alias) ⇒ Object
Connect to database_alias specified in config.
Instance Method Details
#all_sequentially(&block) ⇒ Object
Sequentiall :all for big collection
15 16 17 18 19 20 21 22 |
# File 'lib/mongo_mapper_ext/plugins/micelaneous.rb', line 15 def all_sequentially &block page, per_page = 1, 5 begin results = paginate(:page => page, :per_page => per_page, :order => '_id asc') results.each{|o| block.call o} page += 1 end until results.blank? or results.size < per_page end |
#belongs_to(association_id, options = {}, &extension) ⇒ Object
CounterCache belongs_to :item, :counter_cashe => true
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/mongo_mapper_ext/plugins/micelaneous.rb', line 49 def belongs_to association_id, ={}, &extension .should_not! :include, :counter_cashe if .delete(:counter_cache) || .delete('counter_cache') association_id = association_id.to_s association_key = "#{association_id}_id" cache_attribute = "#{name.pluralize.underscore}_count" cache_class = association_id.classify.constantize cache_class.keys.should! :include, cache_attribute increase_method_name = "increase_#{cache_class.name.underscore}_#{name.pluralize.underscore}_counter" decrease_method_name = "decrease_#{cache_class.name.underscore}_#{name.pluralize.underscore}_counter" define_method increase_method_name do cache_class.upsert self.send(association_key), :$inc => {cache_attribute => 1} end protected increase_method_name define_method decrease_method_name do cache_class.upsert self.send(association_key), :$inc => {cache_attribute => -1} end protected decrease_method_name after_create increase_method_name after_destroy decrease_method_name end super association_id, , &extension end |
#upsert(*args) ⇒ Object
shortcut for upsert
40 41 42 |
# File 'lib/mongo_mapper_ext/plugins/micelaneous.rb', line 40 def upsert *args collection.upsert *args end |
#use_database(database_alias) ⇒ Object
Connect to database_alias specified in config
28 29 30 31 32 33 34 |
# File 'lib/mongo_mapper_ext/plugins/micelaneous.rb', line 28 def use_database database_alias database_alias = database_alias.to_s MongoMapper.db_config.should! :include, database_alias self.connection MongoMapper.connections[database_alias] set_database_name MongoMapper.db_config[database_alias]['name'] end |