Module: SimplyStored::Couch::BelongsTo

Includes:
Properties
Included in:
ClassMethods
Defined in:
lib/simply_stored/couch/belongs_to.rb

Defined Under Namespace

Classes: Property

Instance Method Summary collapse

Methods included from Properties

#check_existing_properties

Instance Method Details

#belongs_to(name, options = {}) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/simply_stored/couch/belongs_to.rb', line 7

def belongs_to(name, options = {})
  check_existing_properties(name, SimplyStored::Couch::BelongsTo::Property)

  map_definition_without_deleted = <<-eos
    function(doc) { 
      if (doc['ruby_class'] == '#{self.to_s}' && doc['#{name.to_s}_id'] != null) {
        if (doc['#{soft_delete_attribute}'] && doc['#{soft_delete_attribute}'] != null){
          // "soft" deleted
        }else{
          emit([doc.#{name.to_s}_id, doc.created_at], 1);
        }
      }
    }
  eos
  
  reduce_definition = "_sum"
   
  view "association_#{self.name.underscore}_belongs_to_#{name}",
    :map => map_definition_without_deleted,
    :reduce => reduce_definition,
    :type => "custom",
    :include_docs => true
    
  map_definition_with_deleted = <<-eos
    function(doc) { 
      if (doc['ruby_class'] == '#{self.to_s}' && doc['#{name.to_s}_id'] != null) {
        emit([doc.#{name.to_s}_id, doc.created_at], 1);
      }
    }
  eos
   
  view "association_#{self.name.underscore}_belongs_to_#{name}_with_deleted",
    :map => map_definition_with_deleted,
    :reduce => reduce_definition,
    :type => "custom",
    :include_docs => true
      
  properties << SimplyStored::Couch::BelongsTo::Property.new(self, name, options)
end