123456789_123456789_123456789_123456789_123456789_

Module: Mongoid::Association::Nested::Buildable

Relationships & Source Files
Extension / Inclusion / Inheritance Descendants
Included In:
Defined in: lib/mongoid/association/nested/nested_buildable.rb

Overview

Mixin module containing common functionality used to perform #accepts_nested_attributes_for attribute assignment on associations.

Instance Attribute Summary

Instance Method Summary

Instance Attribute Details

#allow_destroy?true | false (readonly)

Determines if destroys are allowed for this document.

Examples:

Do we allow a destroy?

builder.allow_destroy?

Returns:

  • (true | false)

    True if the allow destroy option was set.

[ GitHub ]

  
# File 'lib/mongoid/association/nested/nested_buildable.rb', line 21

def allow_destroy?
  options[:allow_destroy] || false
end

#association (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/nested/nested_buildable.rb', line 13

attr_accessor :attributes, :existing, :association, :options

#attributes (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/nested/nested_buildable.rb', line 13

attr_accessor :attributes, :existing, :association, :options

#existing (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/nested/nested_buildable.rb', line 13

attr_accessor :attributes, :existing, :association, :options

#options (rw)

[ GitHub ]

  
# File 'lib/mongoid/association/nested/nested_buildable.rb', line 13

attr_accessor :attributes, :existing, :association, :options

#update_only?true | false (readonly)

Determines if only updates can occur. Only valid for one-to-one associations.

Examples:

Is this update only?

builder.update_only?

Returns:

  • (true | false)

    True if the update_only option was set.

[ GitHub ]

  
# File 'lib/mongoid/association/nested/nested_buildable.rb', line 52

def update_only?
  options[:update_only] || false
end

Instance Method Details

#convert_id(klass, id) ⇒ BSON::ObjectId | String | Object

Convert an id to its appropriate type.

Examples:

Convert the id.

builder.convert_id(Person, "4d371b444835d98b8b000010")

Parameters:

  • klass (Class)

    The class we’re trying to convert for.

  • id (String)

    The id, usually coming from the form.

Returns:

[ GitHub ]

  
# File 'lib/mongoid/association/nested/nested_buildable.rb', line 65

def convert_id(klass, id)
  klass.using_object_ids? ? BSON::ObjectId.mongoize(id) : id
end

#delete_id(hash) ⇒ Object (private)

Deletes the id key from the given hash.

Examples:

Delete an id value.

delete_id({ "_id" => 1 })

Parameters:

  • hash (Hash)

    The hash from which to delete.

Returns:

  • (Object)

    The deleted value, or nil.

[ GitHub ]

  
# File 'lib/mongoid/association/nested/nested_buildable.rb', line 92

def delete_id(hash)
  hash.delete('_id') || hash.delete(:_id) || hash.delete('id') || hash.delete(:id)
end

#extract_id(hash) ⇒ Object (private)

Get the id attribute from the given hash, whether it’s prefixed with an underscore or is a symbol.

Examples:

Get the id.

extract_id({ _id: 1 })

Parameters:

  • hash (Hash)

    The hash from which to extract.

Returns:

  • (Object)

    The value of the id.

[ GitHub ]

  
# File 'lib/mongoid/association/nested/nested_buildable.rb', line 80

def extract_id(hash)
  hash['_id'] || hash[:_id] || hash['id'] || hash[:id]
end

#reject?(document, attrs) ⇒ true | false

Returns the reject if option defined with the macro.

Examples:

Is there a reject proc?

builder.reject?

Parameters:

  • document (Document)

    The parent document of the association

  • attrs (Hash)

    The attributes to check for rejection.

Returns:

  • (true | false)

    True and call proc or method if rejectable, false if not.

[ GitHub ]

  
# File 'lib/mongoid/association/nested/nested_buildable.rb', line 34

def reject?(document, attrs)
  case callback = options[:reject_if]
    when Symbol
      document.method(callback).arity == 0 ? document.send(callback) : document.send(callback, attrs)
    when Proc
      callback.call(attrs)
    else
      false
  end
end