Sha256: 755177d060660d327a4c91216afbf25f1944854a77664b2a031ad809798069ea

Contents?: true

Size: 669 Bytes

Versions: 24

Compression:

Stored size: 669 Bytes

Contents

class ParentDoc
  include Mongoid::Document

  embeds_many :child_docs

  field :statistic
  field :children_order, :type => Array, :default => [] # hold all the children's id
end


class ChildDoc
  include Mongoid::Document

  embedded_in :parent_doc

  attr_writer :position

  after_save :update_position

  def position
    exsited_position = parent_doc.children_order.index(id)
    exsited_position ? exsited_position + 1 : parent_doc.aspects.size
  end

  def update_position
    if @position && (@position.to_i > 0)
      parent_doc.children_order.delete(id)
      parent_doc.children_order.insert(@position.to_i - 1, id)
      parent_doc.save
    end
  end
end

Version data entries

24 entries across 24 versions & 5 rubygems

Version Path
mongoid_spacial-0.1.0 spec/models/parents.rb
mongoid_spacial-0.0.1 spec/models/parents.rb
mongoid-eager-loading-0.3.1 spec/models/parents.rb
mongoid-eager-loading-0.3.0 spec/models/parents.rb