Class: ManyEmbeddedAssociation

Inherits:
Association show all
Includes:
EmbeddedAssociation, ManyAssociation
Defined in:
lib/yodel/models/core/associations/embedded/many_embedded_association.rb

Constant Summary

Constants inherited from Field

Field::TYPES

Instance Attribute Summary

Attributes inherited from Field

#name

Instance Method Summary collapse

Methods included from ManyAssociation

#before_destroy

Methods included from EmbeddedAssociation

#default_input_type, #fields, #fields_field, #validate

Methods included from AbstractModel

#embed_many, #embed_one, #field, #fields, #many, #modify_field, #one, #remove_field

Methods inherited from Association

#json_action

Methods inherited from Field

#default_input_type, #display?, field_from_type, from_options, #include_in_search_keywords?, #index?, #inherited?, #initialize, #method_missing, #numeric?, #required?, #searchable?, #strip_nil?, #to_json, #to_str, #unique?, #validate

Constructor Details

This class inherits a constructor from Field

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Field

Instance Method Details

#associate(embedded_record, store, record) ⇒ Object



29
30
31
32
33
# File 'lib/yodel/models/core/associations/embedded/many_embedded_association.rb', line 29

def associate(embedded_record, store, record)
  raise "Associated record must be an Embedded Record" unless embedded_record.is_a?(EmbeddedRecord)
  embedded_record.save
  store << embedded_record.values
end

#defaultObject



51
52
53
# File 'lib/yodel/models/core/associations/embedded/many_embedded_association.rb', line 51

def default
  @options['default'] || []
end

#destroy(embedded_record, parent_record) ⇒ Object



25
26
27
# File 'lib/yodel/models/core/associations/embedded/many_embedded_association.rb', line 25

def destroy(embedded_record, parent_record)
  parent_record.get(name).delete(embedded_record)
end

#from_json(value, record) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/yodel/models/core/associations/embedded/many_embedded_association.rb', line 61

def from_json(value, record)
  # TODO: change to constructing a new array here instead of removing elements at the end
  store = record.get(name)
  existing_records = store.each_with_object({}) {|record, ids| ids[record.id.to_s] = record}
  
  # update and add existing/new records
  value.each do |new_record|
    next unless new_record.is_a?(Hash)
    new_record_id = new_record['_id']
    new_record.delete('_id')
    if existing_records.key?(new_record_id)
      existing_records[new_record_id].from_json(new_record)
      existing_records.delete(new_record_id)
    else
      process_json_item(new_record, store, record).save
    end
  end
  
  # any remaining ids in existing_records were deleted
  existing_records.values.each(&:destroy)
  record.get(name)
end

#optionsObject



15
16
17
# File 'lib/yodel/models/core/associations/embedded/many_embedded_association.rb', line 15

def options
  super.merge({'type' => 'many_embedded'})
end

#save(embedded_record, parent_record) ⇒ Object



19
20
21
22
23
# File 'lib/yodel/models/core/associations/embedded/many_embedded_association.rb', line 19

def save(embedded_record, parent_record)
  if embedded_record.new?
    parent_record.get(name) << embedded_record
  end
end

#search_terms_set(record) ⇒ Object



9
10
11
12
13
# File 'lib/yodel/models/core/associations/embedded/many_embedded_association.rb', line 9

def search_terms_set(record)
  record.get(name).collect do |embedded_record|
    embedded_record.search_terms
  end.flatten
end

#typecast(value, record) ⇒ Object



55
56
57
58
59
# File 'lib/yodel/models/core/associations/embedded/many_embedded_association.rb', line 55

def typecast(value, record)
  return EmbeddedRecordArray.new(record, self, []) if value.blank?
  raise "ManyEmbedded values must be enumerable (#{name})" unless value.respond_to?(:each)
  EmbeddedRecordArray.new(record, self, all(value, record))
end

#unassociate(embedded_record, store, record) ⇒ Object



35
36
37
# File 'lib/yodel/models/core/associations/embedded/many_embedded_association.rb', line 35

def unassociate(embedded_record, store, record)
  store.delete(embedded_record)
end

#untypecast(value, record) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
# File 'lib/yodel/models/core/associations/embedded/many_embedded_association.rb', line 39

def untypecast(value, record)
  return nil if value.blank?
  raise "ManyEmbeddedAssociation values must be enumerable (#{name})" unless value.respond_to?(:each)
  
  store = record.get_raw(name)
  clear(store, record)
  value.each do |associated_record|
    associate(associated_record, store, record)
  end
  store
end