Sha256: 6148754291a3301c01e39db624183b6d373806ae162f7e2b7c2543039b7497fe

Contents?: true

Size: 1.21 KB

Versions: 5

Compression:

Stored size: 1.21 KB

Contents

class OneEmbeddedAssociation < Association
  include EmbeddedAssociation
  include OneAssociation
  
  # remove OneAssociation'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).search_terms
  end
  
  def default
    @options['default'] || EmbeddedRecord.new(self, nil).default_values
  end
  
  def options
    super.merge({'type' => 'one_embedded'})
  end
  
  def save(embedded_record, parent_record)
    # noop
  end

  def destroy(embedded_record, parent_record)
    embedded_record.initialize(self, parent_record)
    parent_record.changed!(name)
  end
  
  def untypecast(value, record)
    return {} unless value.is_a?(EmbeddedRecord)
    value.save
    value.values
  end
  
  private
    def associated(store, record)
      store = {} unless store.is_a?(Hash)
      EmbeddedRecord.new(self, record, store, false)
    end
    
    def associate(embedded_record, store, record)
      raise "Associated record must be an Embedded Record" unless embedded_record.is_a?(EmbeddedRecord)
      embedded_record.save
      record.set(name, embedded_record)
    end
end

Field::TYPES['one_embedded'] = OneEmbeddedAssociation

Version data entries

5 entries across 5 versions & 1 rubygems

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