<commit_before>require 'spec_helper'

describe Secretary::TracksAssociation do
  describe '::tracks_association' do
    it "raises an error if the model isn't versioned" do
      -> {
        User.tracks_association
      }.should raise_error Secretary::NotVersionedError
    end

    it 'adds the associations to the versioned attributes' do
      Person.versioned_attributes.should include "animals"
      Person.versioned_attributes.should include "hobbies"
    end
  end


  describe 'dirty association' do
    let(:person) { create :person }
    let(:animal) { create :animal }

    it 'sets associations_were' do
      person.animals << animal
      person.animals_were.should eq []
    end

    it 'marks the association as changed when the association is changed' do
      person.animals << animal
      person.animals_changed?.should eq true
    end

    it 'clears out the dirty association after commit' do
      person.animals << animal
      person.animals_changed?.should eq true
      person.save!
      person.animals_changed?.should eq false
    end
  end


  describe "adding to association collections" do
    let(:person) { create :person }
    let(:animal) { build :animal, name: "Bob", color: "dog" }

    it "creates a new version when adding" do
      person.animals = [animal]
      person.save!

      person.versions.count.should eq 2
      version = person.versions.last
      version.object_changes["animals"][0].should eq []

      version.object_changes["animals"][1].should eq [
        {"name" => "Bob", "color" => "dog"}
      ]
    end

    it 'makes a version when adding' do
      person  = create :person
      animal  = build :animal, name: "Bryan", color: 'lame'

      person.animals << animal
      person.save!
      person.versions.count.should eq 2

      versions = person.versions.order('version_number').to_a
      versions.last.object_changes["animals"][0].should eq []
      versions.last.object_changes["animals"][1].should eq [{
        "name" => "Bryan",
        "color" => "lame"
      }]
    end

    it 'makes a version when removing' do
      person  = build :person
      animal  = build :animal, name: "Bryan", color: 'lame'
      person.animals = [animal]
      person.save!
      person.versions.count.should eq 1

      person.animals = []
      person.save!
      person.versions.count.should eq 2

      versions = person.versions.order('version_number').to_a
      versions.last.object_changes["animals"][0].should eq [{
        "name" => "Bryan",
        "color" => "lame"
      }]
      versions.last.object_changes["animals"][1].should eq []
    end
  end


  describe 'with accepts_nested_attributes_for' do
    let(:animal) { create :animal, name: "Henry", color: "blind" }
    let(:person) { create :person }

    it 'adds a new version when adding to collection' do
      animals_attributes = [
        {
          "name" => "George",
          "color" => "yes"
        }
      ]

      person.animals_attributes = animals_attributes
      person.save!
      person.versions.count.should eq 2

      version = person.versions.order('version_number').last
      version.object_changes["animals"][0].should eq []
      version.object_changes["animals"][1].should eq [{
        "name" => "George",
        "color" => "yes"
      }]
    end

    it 'adds a new version when changing something in collection', focus: true do
      animals_attributes = [
        {
          "id" => animal.id,
          "name" => "Lemon"
        }
      ]

      person.animals << animal
      person.versions.count.should eq 2
      person.save!
      person.animals_attributes = animals_attributes
      person.versions.count.should eq 3

      version = person.versions.order('version_number').last
      version.object_changes["animals"][0].should eq [{
        "name" => "Henry",
        "color" => "blind"
      }]
      version.object_changes["animals"][1].should eq [{
        "name" => "Lemon",
        "color" => "blind"
      }]
    end

    it 'does not add a new version if nothing has changed' do
    end
  end
end<commit_msg>Work on tracking already-existing associated objects [ci skip]<commit_after>require 'spec_helper'

describe Secretary::TracksAssociation do
  describe '::tracks_association' do
    it "raises an error if the model isn't versioned" do
      -> {
        User.tracks_association
      }.should raise_error Secretary::NotVersionedError
    end

    it 'adds the associations to the versioned attributes' do
      Person.versioned_attributes.should include "animals"
      Person.versioned_attributes.should include "hobbies"
    end
  end


  describe 'dirty association' do
    let(:person) { create :person }
    let(:animal) { create :animal }

    it 'sets associations_were' do
      person.animals << animal
      person.animals_were.should eq []
    end

    it 'marks the association as changed when the association is changed' do
      person.animals << animal
      person.animals_changed?.should eq true
    end

    it 'clears out the dirty association after commit' do
      person.animals << animal
      person.animals_changed?.should eq true
      person.save!
      person.animals_changed?.should eq false
    end
  end


  describe "adding to association collections" do
    let(:person) { create :person }
    let(:animal) { build :animal, name: "Bob", color: "dog" }

    it "creates a new version when adding" do
      person.animals = [animal]
      person.save!

      person.versions.count.should eq 2
      version = person.versions.last
      version.object_changes["animals"][0].should eq []

      version.object_changes["animals"][1].should eq [
        {"name" => "Bob", "color" => "dog"}
      ]
    end

    it 'makes a version when adding' do
      person  = create :person
      animal  = build :animal, name: "Bryan", color: 'lame'

      person.animals << animal
      person.save!
      person.versions.count.should eq 2

      versions = person.versions.order('version_number').to_a
      versions.last.object_changes["animals"][0].should eq []
      versions.last.object_changes["animals"][1].should eq [{
        "name" => "Bryan",
        "color" => "lame"
      }]
    end

    it 'makes a version when removing' do
      person  = build :person
      animal  = build :animal, name: "Bryan", color: 'lame'
      person.animals = [animal]
      person.save!
      person.versions.count.should eq 1

      person.animals = []
      person.save!
      person.versions.count.should eq 2

      versions = person.versions.order('version_number').to_a
      versions.last.object_changes["animals"][0].should eq [{
        "name" => "Bryan",
        "color" => "lame"
      }]
      versions.last.object_changes["animals"][1].should eq []
    end
  end


  describe 'with accepts_nested_attributes_for' do
    let(:animal) { create :animal, name: "Henry", color: "blind" }
    let(:person) { create :person }

    it 'adds a new version when adding to collection' do
      animals_attributes = [
        {
          "name" => "George",
          "color" => "yes"
        }
      ]

      person.animals_attributes = animals_attributes
      person.save!
      person.versions.count.should eq 2

      version = person.versions.order('version_number').last
      version.object_changes["animals"][0].should eq []
      version.object_changes["animals"][1].should eq [{
        "name" => "George",
        "color" => "yes"
      }]
    end

    it 'adds a new version when changing something in collection', focus: true do
      animals_attributes = [
        {
          "id" => animal.id,
          "name" => "Lemon"
        }
      ]

      person.animals << animal
      person.save!
      person.versions.count.should eq 2
      person.animals_attributes = animals_attributes # this doesn't call before_add/remove callbacks
      person.versions.count.should eq 3

      version = person.versions.order('version_number').last
      version.object_changes["animals"][0].should eq [{
        "name" => "Henry",
        "color" => "blind"
      }]
      version.object_changes["animals"][1].should eq [{
        "name" => "Lemon",
        "color" => "blind"
      }]
    end

    it 'does not add a new version if nothing has changed' do
    end
  end
end
{'fraction_non_alphanumeric': 0.11598405219282348, 'fraction_numerical': 0.004107768515162498, 'mean_word_length': 2.4898819561551435, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 37, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '244451', 'n_tokens_mistral': 2524, 'n_tokens_neox': 2334, 'n_words': 645}