Sha256: af3eb55f882a5e4c4852a137a8d94cee03e34c93e8ec3e6060ed3b8681ccc90f

Contents?: true

Size: 1.69 KB

Versions: 4

Compression:

Stored size: 1.69 KB

Contents

class ManyEmbeddedAssociation < Association
  include EmbeddedAssociation
  include ManyAssociation
  
  # remove ManyAssociation's destroy behaviour since embedded
  # records are destroyed as part of the parent anyway
  undef :before_destroy
  
  def search_terms_set(record)
    record.get(name).collect do |embedded_record|
      embedded_record.search_terms
    end.flatten
  end
  
  def options
    super.merge({'type' => 'many_embedded'})
  end
  
  def save(embedded_record, parent_record)
    if embedded_record.new?
      parent_record.get(name) << embedded_record
    end
  end

  def destroy(embedded_record, parent_record)
    parent_record.get(name).delete(embedded_record)
  end
  
  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
  
  def unassociate(embedded_record, store, record)
    store.delete(embedded_record)
  end
  
  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
  
  def default
    @options['default'] || []
  end
  
  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
end

Field::TYPES['many_embedded'] = ManyEmbeddedAssociation

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
yodel-0.0.4 lib/yodel/models/core/associations/embedded/many_embedded_association.rb
yodel-0.0.3 lib/yodel/models/core/associations/embedded/many_embedded_association.rb
yodel-0.0.2 lib/yodel/models/core/associations/embedded/many_embedded_association.rb
yodel-0.0.1 lib/yodel/models/core/associations/embedded/many_embedded_association.rb