class TestAutosaveAssociationsInGeneral

Public Class Methods

name() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 36
def self.name; "Person"; end

Public Instance Methods

should_be_cool() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 40
def should_be_cool
  unless first_name == "cool"
    errors.add :first_name, "not cool"
  end
end
test_autosave_validation() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 32
def test_autosave_validation
  person = Class.new(ActiveRecord::Base) {
    self.table_name = "people"
    validate :should_be_cool, on: :create
    def self.name; "Person"; end

    private

      def should_be_cool
        unless first_name == "cool"
          errors.add :first_name, "not cool"
        end
      end
  }
  reference = Class.new(ActiveRecord::Base) {
    self.table_name = "references"
    def self.name; "Reference"; end
    belongs_to :person, autosave: true, anonymous_class: person
  }

  u = person.create!(first_name: "cool")
  u.update_attributes!(first_name: "nah") # still valid because validation only applies on 'create'
  assert reference.create!(person: u).persisted?
end
test_cyclic_autosaves_do_not_add_multiple_validations() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 73
def test_cyclic_autosaves_do_not_add_multiple_validations
  ship = ShipWithoutNestedAttributes.new
  ship.prisoners.build

  assert_not ship.valid?
  assert_equal 1, ship.errors[:name].length
end
test_should_not_add_the_same_callbacks_multiple_times_for_belongs_to() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 61
def test_should_not_add_the_same_callbacks_multiple_times_for_belongs_to
  assert_no_difference_when_adding_callbacks_twice_for Ship, :pirate
end
test_should_not_add_the_same_callbacks_multiple_times_for_has_and_belongs_to_many() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 69
def test_should_not_add_the_same_callbacks_multiple_times_for_has_and_belongs_to_many
  assert_no_difference_when_adding_callbacks_twice_for Pirate, :parrots
end
test_should_not_add_the_same_callbacks_multiple_times_for_has_many() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 65
def test_should_not_add_the_same_callbacks_multiple_times_for_has_many
  assert_no_difference_when_adding_callbacks_twice_for Pirate, :birds
end
test_should_not_add_the_same_callbacks_multiple_times_for_has_one() click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 57
def test_should_not_add_the_same_callbacks_multiple_times_for_has_one
  assert_no_difference_when_adding_callbacks_twice_for Pirate, :ship
end

Private Instance Methods

assert_no_difference_when_adding_callbacks_twice_for(model, association_name) click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 83
def assert_no_difference_when_adding_callbacks_twice_for(model, association_name)
  reflection = model.reflect_on_association(association_name)
  assert_no_difference "callbacks_for_model(#{model.name}).length" do
    model.send(:add_autosave_association_callbacks, reflection)
  end
end
callbacks_for_model(model) click to toggle source
# File activerecord/test/cases/autosave_association_test.rb, line 90
def callbacks_for_model(model)
  model.instance_variables.grep(/_callbacks$/).flat_map do |ivar|
    model.instance_variable_get(ivar)
  end
end