Sha256: 004da45176f035353286592c6bd208f383a6ea39d6ad896ea1f0deb74c8324ae

Contents?: true

Size: 522 Bytes

Versions: 27

Compression:

Stored size: 522 Bytes

Contents

# frozen_string_literal: true

class ChildDoc
  include Mongoid::Document

  embedded_in :parent_doc

  attr_writer :position

  after_save :update_position

  def position
    existing_position = parent_doc.children_order.index(id)
    existing_position ? existing_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

27 entries across 27 versions & 1 rubygems

Version Path
mongoid-8.1.9 spec/support/models/child_doc.rb
mongoid-8.0.9 spec/support/models/child_doc.rb
mongoid-8.1.8 spec/support/models/child_doc.rb
mongoid-8.1.7 spec/support/models/child_doc.rb
mongoid-8.1.6 spec/support/models/child_doc.rb
mongoid-8.0.8 spec/support/models/child_doc.rb
mongoid-8.1.5 spec/support/models/child_doc.rb
mongoid-8.1.4 spec/support/models/child_doc.rb
mongoid-8.0.7 spec/support/models/child_doc.rb
mongoid-8.1.3 spec/support/models/child_doc.rb
mongoid-8.1.2 spec/support/models/child_doc.rb
mongoid-8.0.6 spec/support/models/child_doc.rb
mongoid-7.5.4 spec/support/models/child_doc.rb
mongoid-8.1.1 spec/support/models/child_doc.rb
mongoid-8.0.5 spec/support/models/child_doc.rb
mongoid-8.1.0 spec/support/models/child_doc.rb
mongoid-7.5.3 spec/support/models/child_doc.rb
mongoid-8.0.4 spec/support/models/child_doc.rb
mongoid-8.0.3 spec/support/models/child_doc.rb
mongoid-7.5.2 spec/support/models/child_doc.rb