Sha256: 5288efdb7d0b94cd1a4b3f5e3d4b86a2c933ac971c7ad234f65a222c4eb39d1a

Contents?: true

Size: 1.58 KB

Versions: 1

Compression:

Stored size: 1.58 KB

Contents

require 'spec_helper'

describe Mongoid::History::Tracker do
  before :each do
    class ModelOne
      include Mongoid::Document
      include Mongoid::History::Trackable

      field :name, type: String
      belongs_to :user, inverse_of: :model_ones
      embeds_many :emb_ones

      track_history
    end

    class EmbOne
      include Mongoid::Document
      include Mongoid::History::Trackable

      field :name
      embeds_many :emb_twos, store_as: :ems
      embedded_in :model_one

      track_history
    end

    class EmbTwo
      include Mongoid::Document
      include Mongoid::History::Trackable

      field :name
      embedded_in :emb_one

      track_history scope: :model_one
    end

    class User
      include Mongoid::Document

      has_many :model_ones, dependent: :destroy, inverse_of: :user
    end
  end

  after :each do
    Object.send(:remove_const, :ModelOne)
    Object.send(:remove_const, :EmbOne)
    Object.send(:remove_const, :EmbTwo)
    Object.send(:remove_const, :User)
  end

  let(:user) { User.create! }

  it 'should be able to track history for nested embedded documents' do
    model = ModelOne.create!(name: 'm1name', user: user, modifier: user)
    embedded1 = model.emb_ones.create!(name: 'e1name', modifier: user)
    embedded2 = embedded1.emb_twos.create!(name: 'e2name', modifier: user)

    embedded2.update_attributes!(name: 'a new name')

    model.history_tracks[-1].undo! user
    expect(embedded1.reload.name).to eq('e1name')
    expect(embedded2.reload.name).to eq('e2name')
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
mongoid-history-0.8.5 spec/integration/nested_embedded_documents_spec.rb