Sha256: 9eb81ced54efba09ee2b9029af4a60bbb4423ad25a933e866ed8747ec144e105

Contents?: true

Size: 697 Bytes

Versions: 5

Compression:

Stored size: 697 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, :inverse_of => :child_docs

  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

5 entries across 5 versions & 2 rubygems

Version Path
mongoid-eager-loading-0.2.0 spec/models/parents.rb
mongoid-eager-loading-0.1.2 spec/models/parents.rb
mongoid-eager-loading-0.1.1 spec/models/parents.rb
mongoid-eager-loading-0.1.0 spec/models/parents.rb
mongoid-locomotive-2.0.0.beta9 spec/models/parents.rb