Module: SimplyCouch::Model::EmbeddedIn

Included in:
ClassMethods
Defined in:
lib/simply_couch/model/embedded_in.rb

Defined Under Namespace

Classes: Property

Instance Method Summary collapse

Instance Method Details

#is_embedded_in(name, options = {}) ⇒ 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
47
# File 'lib/simply_couch/model/embedded_in.rb', line 6

def is_embedded_in(name, options = {})
  check_existing_properties(name, SimplyCouch::Model::BelongsTo::Property)
  parent = options[:class_name] || name.to_s.camelize
  self.name.property_name.pluralize

  map_definition_without_deleted = <<-eos
    function(doc) {
      if (doc['ruby_class'] == '#{parent}') {
        if(typeof(doc['']))
        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.gsub('/', '__')}_embedded_in_#{name}",
    :map_function => map_definition_without_deleted,
    :reduce_function => 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.gsub('/', '__')}_embedded_in_#{name}_with_deleted",
    :map_function => map_definition_with_deleted,
    :reduce_function => reduce_definition,
    :type => :custom,
    :include_docs => true

  properties << SimplyCouch::Model::EmbeddedIn::Property.new(self, name, options)
end