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.2.17 spec/models/parents.rb
sig_mongoid_spacial-0.2.17 spec/models/parents.rb
cb_mongoid_spacial-0.2.16 spec/models/parents.rb
mongoid_geospatial-1.0.0 spec/models/parents.rb
mongoid_geospatial-1.0.0rc1 spec/models/parents.rb
mongoid_geospatial-1.0.0rc0 spec/models/parents.rb
mongoid_spacial-0.2.16 spec/models/parents.rb
mongoid_spacial-0.2.13 spec/models/parents.rb
mongoid_spacial-0.2.12 spec/models/parents.rb
mongoid_spacial-0.2.11 spec/models/parents.rb
mongoid_spacial-0.2.10 spec/models/parents.rb
mongoid_spacial-0.2.8 spec/models/parents.rb
mongoid_spacial-0.2.7 spec/models/parents.rb
mongoid_spacial-0.2.6 spec/models/parents.rb
mongoid_spacial-0.2.5 spec/models/parents.rb
mongoid_spacial-0.2.4 spec/models/parents.rb
mongoid_spacial-0.2.3 spec/models/parents.rb
mongoid_spacial-0.2.2 spec/models/parents.rb
mongoid_spacial-0.2.0 spec/models/parents.rb
mongoid_spacial-0.1.1 spec/models/parents.rb