Module: SimplyStored::Couch::BelongsTo

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

Defined Under Namespace

Classes: Property

Instance Method Summary collapse

Instance Method Details

#belongs_to(name) ⇒ Object



6
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
46
# File 'lib/simply_stored/couch/belongs_to.rb', line 6

def belongs_to(name)
  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, null);
        }
      }
    }
  eos
  
  reduce_definition = <<-eos
    function(key, values) {
      return values.length;
    }
  eos
   
  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, null);
      }
    }
  eos
   
  view "association_#{self.name.underscore}_belongs_to_#{name}_with_deleted",
    :map => map_definition_with_deleted,
    :reduce => reduce_definition,
    :type => "custom",
    :include_docs => true
      
  property name, :class => SimplyStored::Couch::BelongsTo::Property
end