Changeset 5018
- Timestamp:
- 09/05/06 18:54:24 (2 years ago)
- Files:
-
- trunk/activerecord/CHANGELOG (modified) (1 diff)
- trunk/activerecord/lib/active_record/aggregations.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations.rb (modified) (16 diffs)
- trunk/activerecord/lib/active_record/associations/association_collection.rb (modified) (4 diffs)
- trunk/activerecord/lib/active_record/associations/association_proxy.rb (modified) (2 diffs)
- trunk/activerecord/lib/active_record/associations/belongs_to_association.rb (modified) (3 diffs)
- trunk/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb (modified) (4 diffs)
- trunk/activerecord/lib/active_record/associations/has_many_association.rb (modified) (2 diffs)
- trunk/activerecord/lib/active_record/associations/has_many_through_association.rb (modified) (4 diffs)
- trunk/activerecord/lib/active_record/associations/has_one_association.rb (modified) (4 diffs)
- trunk/activerecord/lib/active_record/base.rb (modified) (5 diffs)
- trunk/activerecord/lib/active_record/callbacks.rb (modified) (2 diffs)
- trunk/activerecord/lib/active_record/locking/pessimistic.rb (modified) (1 diff)
- trunk/activerecord/lib/active_record/validations.rb (modified) (4 diffs)
- trunk/activerecord/test/associations_test.rb (modified) (23 diffs)
- trunk/activerecord/test/base_test.rb (modified) (5 diffs)
- trunk/activerecord/test/finder_test.rb (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/activerecord/CHANGELOG
r5017 r5018 1 1 *SVN* 2 3 * Deprecated ActiveRecord::Base.new_record? in favor of ActiveRecord::Base.new? (old version still works until Rails 2.0) [DHH]4 2 5 3 * Rename AR::Base#quote so people can use that name in their models. #3628 [Koz] trunk/activerecord/lib/active_record/aggregations.rb
r5017 r5018 8 8 self.class.reflect_on_all_aggregations.to_a.each do |assoc| 9 9 instance_variable_set "@#{assoc.name}", nil 10 end unless self.new ?10 end unless self.new_record? 11 11 end 12 12 trunk/activerecord/lib/active_record/associations.rb
r5017 r5018 66 66 self.class.reflect_on_all_associations.to_a.each do |assoc| 67 67 instance_variable_set "@#{assoc.name}", nil 68 end unless self.new ?68 end unless self.new_record? 69 69 end 70 70 … … 132 132 # 133 133 # * Assigning an object to a has_one association automatically saves that object and the object being replaced (if there is one), in 134 # order to update their primary keys - except if the parent object is unsaved (new ? == true).134 # order to update their primary keys - except if the parent object is unsaved (new_record? == true). 135 135 # * If either of these saves fail (due to one of the objects being invalid) the assignment statement returns false and the assignment 136 136 # is cancelled. … … 145 145 # * If saving any of the objects being added to a collection (via #push or similar) fails, then #push returns false. 146 146 # * You can add an object to a collection without automatically saving it by using the #collection.build method (documented below). 147 # * All unsaved (new ? == true) members of the collection are automatically saved when the parent is saved.147 # * All unsaved (new_record? == true) members of the collection are automatically saved when the parent is saved. 148 148 # 149 149 # === Association callbacks … … 592 592 after_save <<-EOF 593 593 association = instance_variable_get("@#{reflection.name}") 594 if !association.nil? && (new ? || association.new? || association["#{reflection.primary_key_name}"] != id)594 if !association.nil? && (new_record? || association.new_record? || association["#{reflection.primary_key_name}"] != id) 595 595 association["#{reflection.primary_key_name}"] = id 596 596 association.save(true) … … 656 656 # belongs_to :attachable, :polymorphic => true 657 657 def belongs_to(association_id, options = {}) 658 if options.include?(:class_name) && !options.include?(:foreign_key) 659 ::ActiveSupport::Deprecation.warn( 660 "The inferred foreign_key name will change in Rails 2.0 to use the association name instead of its class name when they differ. When using :class_name in belongs_to, use the :foreign_key option to explicitly set the key name to avoid problems in the transition.", 661 caller) 662 end 663 658 664 reflection = create_belongs_to_reflection(association_id, options) 659 665 … … 665 671 association = instance_variable_get("@#{reflection.name}") 666 672 if association && association.target 667 if association.new ?673 if association.new_record? 668 674 association.save(true) 669 675 end … … 685 691 association = instance_variable_get("@#{reflection.name}") 686 692 if !association.nil? 687 if association.new ?693 if association.new_record? 688 694 association.save(true) 689 695 end … … 925 931 association = instance_variable_get("@#{association_name}") 926 932 if association.respond_to?(:loaded?) 927 if new ?933 if new_record? 928 934 association 929 935 else 930 association.select { |record| record.new ? }936 association.select { |record| record.new_record? } 931 937 end.each do |record| 932 938 errors.add "#{association_name}" unless record.valid? … … 936 942 937 943 validate method_name 938 before_save("@new_record_before_save = new ?; true")944 before_save("@new_record_before_save = new_record?; true") 939 945 940 946 after_callback = <<-end_eval … … 945 951 records_to_save = association 946 952 else 947 records_to_save = association.select { |record| record.new ? }953 records_to_save = association.select { |record| record.new_record? } 948 954 end 949 955 records_to_save.each { |record| association.send(:insert_record, record) } … … 992 998 if reflection.options[:exclusively_dependent] 993 999 reflection.options[:dependent] = :delete_all 994 #warn "The :exclusively_dependent option is deprecated. Please use :dependent => :delete_all instead.")1000 ::ActiveSupport::Deprecation.warn("The :exclusively_dependent option is deprecated and will be removed from Rails 2.0. Please use :dependent => :delete_all instead. See http://www.rubyonrails.org/deprecation for details.", caller) 995 1001 end 996 1002 … … 1452 1458 aliased_join_table_name, polymorphic_foreign_key, 1453 1459 parent.aliased_table_name, parent.primary_key, 1454 aliased_join_table_name, polymorphic_foreign_type, klass.quote (parent.active_record.base_class.name)] +1460 aliased_join_table_name, polymorphic_foreign_type, klass.quote_value(parent.active_record.base_class.name)] + 1455 1461 " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [table_name_and_alias, 1456 1462 aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key … … 1467 1473 aliased_join_table_name, options[:foreign_key] || primary_key, 1468 1474 aliased_table_name, "#{source_reflection.options[:as]}_type", 1469 klass.quote (source_reflection.active_record.base_class.name)1475 klass.quote_value(source_reflection.active_record.base_class.name) 1470 1476 ] 1471 1477 else … … 1496 1502 parent.aliased_table_name, parent.primary_key, 1497 1503 aliased_table_name, "#{reflection.options[:as]}_type", 1498 klass.quote (parent.active_record.base_class.name)1504 klass.quote_value(parent.active_record.base_class.name) 1499 1505 ] 1500 1506 when reflection.macro == :has_one && reflection.options[:as] … … 1504 1510 parent.aliased_table_name, parent.primary_key, 1505 1511 aliased_table_name, "#{reflection.options[:as]}_type", 1506 klass.quote (reflection.active_record.base_class.name)1512 klass.quote_value(reflection.active_record.base_class.name) 1507 1513 ] 1508 1514 else … … 1525 1531 aliased_table_name, 1526 1532 reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column), 1527 klass.quote (klass.name.demodulize)] unless klass.descends_from_active_record?1533 klass.quote_value(klass.name.demodulize)] unless klass.descends_from_active_record? 1528 1534 join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions] 1529 1535 join trunk/activerecord/lib/active_record/associations/association_collection.rb
r5017 r5018 24 24 raise_on_type_mismatch(record) 25 25 callback(:before_add, record) 26 result &&= insert_record(record) unless @owner.new ?26 result &&= insert_record(record) unless @owner.new_record? 27 27 @target << record 28 28 callback(:after_add, record) … … 52 52 records = flatten_deeper(records) 53 53 records.each { |record| raise_on_type_mismatch(record) } 54 records.reject! { |record| @target.delete(record) if record.new ? }54 records.reject! { |record| @target.delete(record) if record.new_record? } 55 55 return if records.empty? 56 56 … … 92 92 else 93 93 record = build(attributes) 94 record.save unless @owner.new ?94 record.save unless @owner.new_record? 95 95 record 96 96 end … … 104 104 @target.size 105 105 elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array) 106 unsaved_records = Array(@target.detect { |r| r.new ? })106 unsaved_records = Array(@target.detect { |r| r.new_record? }) 107 107 unsaved_records.size + count_records 108 108 else trunk/activerecord/lib/active_record/associations/association_proxy.rb
r5017 r5018 100 100 def set_belongs_to_association_for(record) 101 101 if @reflection.options[:as] 102 record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new ?102 record["#{@reflection.options[:as]}_id"] = @owner.id unless @owner.new_record? 103 103 record["#{@reflection.options[:as]}_type"] = @owner.class.base_class.name.to_s 104 104 else 105 record[@reflection.primary_key_name] = @owner.id unless @owner.new ?105 record[@reflection.primary_key_name] = @owner.id unless @owner.new_record? 106 106 end 107 107 end … … 126 126 127 127 def load_target 128 if !loaded? and (!@owner.new ? || foreign_key_present)128 if !loaded? and (!@owner.new_record? || foreign_key_present) 129 129 @target = find_target 130 130 end trunk/activerecord/lib/active_record/associations/belongs_to_association.rb
r5017 r5018 14 14 15 15 if record.nil? 16 if counter_cache_name && @owner[counter_cache_name] && !@owner.new ?16 if counter_cache_name && @owner[counter_cache_name] && !@owner.new_record? 17 17 @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name] 18 18 end … … 22 22 raise_on_type_mismatch(record) 23 23 24 if counter_cache_name && !@owner.new ?24 if counter_cache_name && !@owner.new_record? 25 25 @reflection.klass.increment_counter(counter_cache_name, record.id) 26 26 @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name] … … 28 28 29 29 @target = (AssociationProxy === record ? record.target : record) 30 @owner[@reflection.primary_key_name] = record.id unless record.new ?30 @owner[@reflection.primary_key_name] = record.id unless record.new_record? 31 31 @updated = true 32 32 end trunk/activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
r5017 r5018 8 8 @target = (AssociationProxy === record ? record.target : record) 9 9 10 unless record.new ?10 unless record.new_record? 11 11 @owner[@reflection.primary_key_name] = record.id 12 12 @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s trunk/activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
r5017 r5018 20 20 else 21 21 record = build(attributes) 22 insert_record(record) unless @owner.new ?22 insert_record(record) unless @owner.new_record? 23 23 record 24 24 end … … 76 76 77 77 callback(:before_add, record) 78 insert_record(record) unless @owner.new ?78 insert_record(record) unless @owner.new_record? 79 79 @target << record 80 80 callback(:after_add, record) … … 102 102 103 103 def insert_record(record) 104 if record.new ?104 if record.new_record? 105 105 return false unless record.save 106 106 end … … 119 119 else 120 120 if record.attributes.has_key?(column.name) 121 value = @owner.send(:quote , record[column.name], column)121 value = @owner.send(:quote_value, record[column.name], column) 122 122 attributes[column.name] = value unless value.nil? 123 123 end trunk/activerecord/lib/active_record/associations/has_many_association.rb
r5017 r5018 112 112 113 113 def load_target 114 if !@owner.new ? || foreign_key_present114 if !@owner.new_record? || foreign_key_present 115 115 begin 116 116 if !loaded? … … 185 185 @finder_sql = 186 186 "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " + 187 "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}"187 "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" 188 188 @finder_sql << " AND (#{conditions})" if conditions 189 189 trunk/activerecord/lib/active_record/associations/has_many_through_association.rb
r5017 r5018 50 50 return if records.empty? 51 51 through = @reflection.through_reflection 52 raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) if @owner.new ?52 raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) if @owner.new_record? 53 53 54 54 load_target … … 58 58 flatten_deeper(records).each do |associate| 59 59 raise_on_type_mismatch(associate) 60 raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new ?) && !associate.new?60 raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new_record?) && !associate.new_record? 61 61 62 62 @owner.send(@reflection.through_reflection.name).proxy_target << klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! } … … 128 128 if as = reflection.options[:as] 129 129 { "#{as}_id" => @owner.quoted_id, 130 "#{as}_type" => reflection.klass.quote (130 "#{as}_type" => reflection.klass.quote_value( 131 131 @owner.class.base_class.name.to_s, 132 132 reflection.klass.columns_hash["#{as}_type"]) } … … 165 165 polymorphic_join = "AND %s.%s = %s" % [ 166 166 @reflection.table_name, "#{@reflection.source_reflection.options[:as]}_type", 167 @owner.class.quote (@reflection.through_reflection.klass.name)167 @owner.class.quote_value(@reflection.through_reflection.klass.name) 168 168 ] 169 169 end trunk/activerecord/lib/active_record/associations/has_one_association.rb
r5017 r5018 19 19 replace(record, true) 20 20 else 21 record[@reflection.primary_key_name] = @owner.id unless @owner.new ?21 record[@reflection.primary_key_name] = @owner.id unless @owner.new_record? 22 22 self.target = record 23 23 end … … 31 31 unless @target.nil? 32 32 if dependent? && !dont_save && @target != obj 33 @target.destroy unless @target.new ?33 @target.destroy unless @target.new_record? 34 34 @owner.clear_association_cache 35 35 else 36 36 @target[@reflection.primary_key_name] = nil 37 @target.save unless @owner.new ? || @target.new?37 @target.save unless @owner.new_record? || @target.new_record? 38 38 end 39 39 end … … 49 49 @loaded = true 50 50 51 unless @owner.new ? or obj.nil? or dont_save51 unless @owner.new_record? or obj.nil? or dont_save 52 52 return (obj.save ? self : false) 53 53 else … … 70 70 @finder_sql = 71 71 "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_id = #{@owner.quoted_id} AND " + 72 "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}"72 "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote_value(@owner.class.base_class.name.to_s)}" 73 73 else 74 74 @finder_sql = "#{@reflection.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}" trunk/activerecord/lib/active_record/base.rb
r5017 r5018 184 184 # # No 'Winter' tag exists 185 185 # winter = Tag.find_or_initialize_by_name("Winter") 186 # winter.new ? # true186 # winter.new_record? # true 187 187 # 188 188 # == Saving arrays, hashes, and other non-mappable objects in text columns … … 1519 1519 1520 1520 # Returns true if this object hasn't been saved yet -- that is, a record for the object doesn't exist yet. 1521 def new ?1521 def new_record? 1522 1522 @new_record 1523 end1524 1525 # Deprecated alias for new?1526 def new_record?1527 ActiveSupport::Deprecation.warn(1528 "ActiveRecord::Base.new_record? has been deprecated and will be removed with Rails 2.0." +1529 "Please use ActiveRecord::Base.new? instead.", caller1530 )1531 1532 new?1533 1523 end 1534 1524 … … 1549 1539 # be made (since they can't be persisted). 1550 1540 def destroy 1551 unless new ?1541 unless new_record? 1552 1542 connection.delete <<-end_sql, "#{self.class.name} Destroy" 1553 1543 DELETE FROM #{self.class.table_name} … … 1718 1708 (comparison_object.instance_of?(self.class) && 1719 1709 comparison_object.id == id && 1720 !comparison_object.new ?)1710 !comparison_object.new_record?) 1721 1711 end 1722 1712 … … 1774 1764 private 1775 1765 def create_or_update 1776 if new ? then create else update end1766 if new_record? then create else update end 1777 1767 true 1778 1768 end trunk/activerecord/lib/active_record/callbacks.rb
r5017 r5018 294 294 def valid_with_callbacks? #:nodoc: 295 295 return false if callback(:before_validation) == false 296 if new ? then result = callback(:before_validation_on_create) else result = callback(:before_validation_on_update) end296 if new_record? then result = callback(:before_validation_on_create) else result = callback(:before_validation_on_update) end 297 297 return false if result == false 298 298 … … 300 300 301 301 callback(:after_validation) 302 if new ? then callback(:after_validation_on_create) else callback(:after_validation_on_update) end302 if new_record? then callback(:after_validation_on_create) else callback(:after_validation_on_update) end 303 303 304 304 return result trunk/activerecord/lib/active_record/locking/pessimistic.rb
r5017 r5018 70 70 # the locked record. 71 71 def lock!(lock = true) 72 reload(:lock => lock) unless new ?72 reload(:lock => lock) unless new_record? 73 73 self 74 74 end trunk/activerecord/lib/active_record/validations.rb
r5017 r5018 334 334 335 335 validates_each(attr_names, configuration) do |record, attr_name, value| 336 record.errors.add(attr_name, configuration[:message]) unless record.send("#{attr_name}_confirmation").nil? or value == record.send("#{attr_name}_confirmation") 336 confirm = record.send("#{attr_name}_confirmation") 337 record.errors.add(attr_name, configuration[:message]) unless value.nil? || value == confirm 337 338 end 338 339 end … … 375 376 # The first_name attribute must be in the object and it cannot be blank. 376 377 # 378 # If you want to validate the presence of a boolean field (where the real values are true and false), 379 # you will want to use validates_inclusion_of :field_name, :in => [true, false] 380 # This is due to the way Object#blank? handles boolean values. false.blank? # => true 381 # 377 382 # Configuration options: 378 383 # * <tt>message</tt> - A custom error message (default is: "can't be blank") … … 539 544 end 540 545 end 541 unless record.new ?546 unless record.new_record? 542 547 condition_sql << " AND #{record.class.table_name}.#{record.class.primary_key} <> ?" 543 548 condition_params << record.send(:id) … … 773 778 validate 774 779 775 if new ?780 if new_record? 776 781 run_validations(:validate_on_create) 777 782 validate_on_create trunk/activerecord/test/associations_test.rb
r5017 r5018 179 179 assert_equal [account_id], Account.destroyed_account_ids[firm.id] 180 180 end 181 181 182 182 def test_deprecated_exclusive_dependence 183 183 assert_deprecated(/:exclusively_dependent.*:dependent => :delete_all/) do … … 185 185 end 186 186 end 187 187 188 188 def test_exclusive_dependence 189 189 num_accounts = Account.count … … 245 245 account = firm.account.build("credit_limit" => 1000) 246 246 assert_equal account, firm.account 247 assert account.new ?247 assert account.new_record? 248 248 assert firm.save 249 249 assert_equal account, firm.account 250 assert !account.new ?250 assert !account.new_record? 251 251 end 252 252 … … 256 256 firm.account = account = Account.new("credit_limit" => 1000) 257 257 assert_equal account, firm.account 258 assert account.new ?258 assert account.new_record? 259 259 assert firm.save 260 260 assert_equal account, firm.account 261 assert !account.new ?261 assert !account.new_record? 262 262 end 263 263 … … 296 296 firm = Firm.new("name" => "GlobalMegaCorp") 297 297 firm.account = a = Account.find(1) 298 assert firm.new ?298 assert firm.new_record? 299 299 assert_equal a, firm.account 300 300 assert firm.save … … 306 306 firm = Firm.find(1) 307 307 firm.account = a = Account.new("credit_limit" => 1000) 308 assert !a.new ?308 assert !a.new_record? 309 309 assert_equal a, firm.account 310 310 assert_equal a, firm.account … … 315 315 firm = Firm.new("name" => "GlobalMegaCorp") 316 316 firm.account = a = Account.new("credit_limit" => 1000) 317 assert firm.new ?318 assert a.new ?317 assert firm.new_record? 318 assert a.new_record? 319 319 assert_equal a, firm.account 320 320 assert firm.save 321 assert !firm.new ?322 assert !a.new ?321 assert !firm.new_record? 322 assert !a.new_record? 323 323 assert_equal a, firm.account 324 324 assert_equal a, firm.account(true) … … 351 351 end 352 352 353 def test_deprecated_inferred_foreign_key 354 assert_not_deprecated { Company.belongs_to :firm } 355 assert_not_deprecated { Company.belongs_to :client, :foreign_key => "firm_id" } 356 assert_not_deprecated { Company.belongs_to :firm, :class_name => "Firm", :foreign_key => "client_of" } 357 assert_deprecated("inferred foreign_key name") { Company.belongs_to :client, :class_name => "Firm" } 358 end 359 353 360 end 354 361 … … 552 559 new_firm.clients_of_firm.push Client.new("name" => "Natural Company") 553 560 new_firm.clients_of_firm << (c = Client.new("name" => "Apple")) 554 assert new_firm.new ?555 assert c.new ?561 assert new_firm.new_record? 562 assert c.new_record? 556 563 assert_equal 2, new_firm.clients_of_firm.size 557 564 assert_equal no_of_firms, Firm.count # Firm was not saved to database. 558 565 assert_equal no_of_clients, Client.count # Clients were not saved to database. 559 566 assert new_firm.save 560 assert !new_firm.new ?561 assert !c.new ?567 assert !new_firm.new_record? 568 assert !c.new_record? 562 569 assert_equal new_firm, c.firm 563 570 assert_equal no_of_firms+1, Firm.count # Firm was saved to database. … … 570 577 firm = Firm.find(1) 571 578 assert !(firm.clients_of_firm << c = Client.new) 572 assert c.new ?579 assert c.new_record? 573 580 assert !firm.valid? 574 581 assert !firm.save 575 assert c.new ?582 assert c.new_record? 576 583 end 577 584 … … 581 588 new_firm = Firm.new("name" => "A New Firm, Inc") 582 589 new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")]) 583 assert c.new ?590 assert c.new_record? 584 591 assert !c.valid? 585 592 assert !new_firm.valid? 586 593 assert !new_firm.save 587 assert c.new ?588 assert new_firm.new ?594 assert c.new_record? 595 assert new_firm.new_record? 589 596 end 590 597 … … 592 599 new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client") 593 600 assert_equal "Another Client", new_client.name 594 assert new_client.new ?601 assert new_client.new_record? 595 602 assert_equal new_client, companies(:first_firm).clients_of_firm.last 596 603 assert companies(:first_firm).save 597 assert !new_client.new ?604 assert !new_client.new_record? 598 605 assert_equal 2, companies(:first_firm).clients_of_firm(true).size 599 606 end … … 638 645 def test_invalid_build 639 646 new_client = companies(:first_firm).clients_of_firm.build 640 assert new_client.new ?647 assert new_client.new_record? 641 648 assert !new_client.valid? 642 649 assert_equal new_client, companies(:first_firm).clients_of_firm.last 643 650 assert !companies(:first_firm).save 644 assert new_client.new ?651 assert new_client.new_record? 645 652 assert_equal 1, companies(:first_firm).clients_of_firm(true).size 646 653 end … … 649 656 force_signal37_to_load_all_clients_of_firm 650 657 new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client") 651 assert !new_client.new ?658 assert !new_client.new_record? 652 659 assert_equal new_client, companies(:first_firm).clients_of_firm.last 653 660 assert_equal new_client, companies(:first_firm).clients_of_firm(true).last … … 1026 1033 client.firm = apple 1027 1034 assert_equal apple, client.firm 1028 assert apple.new ?1035 assert apple.new_record? 1029 1036 assert client.save 1030 1037 assert apple.save 1031 assert !apple.new ?1038 assert !apple.new_record? 1032 1039 assert_equal apple, client.firm 1033 1040 assert_equal apple, client.firm(true) … … 1038 1045 firm = Firm.find(1) 1039 1046 final_cut.firm = firm 1040 assert final_cut.new ?1047 assert final_cut.new_record? 1041 1048 assert final_cut.save 1042 assert !final_cut.new ?1043 assert !firm.new ?1049 assert !final_cut.new_record? 1050 assert !firm.new_record? 1044 1051 assert_equal firm, final_cut.firm 1045 1052 assert_equal firm, final_cut.firm(true) … … 1050 1057 apple = Firm.new("name" => "Apple") 1051 1058 final_cut.firm = apple 1052 assert final_cut.new ?1053 assert apple.new ?1059 assert final_cut.new_record? 1060 assert apple.new_record? 1054 1061 assert final_cut.save 1055 assert !final_cut.new ?1056 assert !apple.new ?1062 assert !final_cut.new_record? 1063 assert !apple.new_record? 1057 1064 assert_equal apple, final_cut.firm 1058 1065 assert_equal apple, final_cut.firm(true) … … 1368 1375 aredridel = Developer.new("name" => "Aredridel") 1369 1376 aredridel.projects.concat([Project.find(1), p = Project.new("name" => "Projekt")]) 1370 assert aredridel.new ?1371 assert p.new ?1377 assert aredridel.new_record? 1378 assert p.new_record? 1372 1379 assert aredridel.save 1373 assert !aredridel.new ?1380 assert !aredridel.new_record? 1374 1381 assert_equal no_of_devels+1, Developer.count 1375 1382 assert_equal no_of_projects+1, Project.count … … 1386 1393 p = Project.new("name" => "Foomatic") 1387 1394 ken.projects.push_with_attributes( p, :joined_on => now ) 1388 assert ken.new ?1389 assert p.new ?1395 assert ken.new_record? 1396 assert p.new_record? 1390 1397 assert ken.save 1391 assert !ken.new ?1398 assert !ken.new_record? 1392 1399 assert_equal no_of_devels+1, Developer.count 1393 1400 assert_equal no_of_projects+1, Project.count … … 1422 1429 proj = devel.projects.build("name" => "Projekt") 1423 1430 assert_equal devel.projects.last, proj 1424 assert proj.new ?1431 assert proj.new_record? 1425 1432 devel.save 1426 assert !proj.new ?1433 assert !proj.new_record? 1427 1434 assert_equal devel.projects.last, proj 1428 1435 assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated … … 1434 1441 proj2 = devel.projects.build(:name => "Lie in it") 1435 1442 assert_equal devel.projects.last, proj2 1436 assert proj2.new ?1443 assert proj2.new_record? 1437 1444 devel.save 1438 assert !devel.new ?1439 assert !proj2.new ?1445 assert !devel.new_record? 1446 assert !proj2.new_record? 1440 1447 assert_equal devel.projects.last, proj2 1441 1448 assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated … … 1446 1453 proj = devel.projects.create("name" => "Projekt") 1447 1454 assert_equal devel.projects.last, proj 1448 assert !proj.new ?1455 assert !proj.new_record? 1449 1456 assert_equal Developer.find(1).projects.sort_by(&:id).last, proj # prove join table is updated 1450 1457 end … … 1455 1462 proj2 = devel.projects.create(:name => "Lie in it") 1456 1463 assert_equal devel.projects.last, proj2 1457 assert proj2.new ?1464 assert proj2.new_record? 1458 1465 devel.save 1459 assert !devel.new ?1460 assert !proj2.new ?1466 assert !devel.new_record? 1467 assert !proj2.new_record? 1461 1468 assert_equal devel.projects.last, proj2 1462 1469 assert_equal Developer.find_by_name("Marcel").projects.last, proj2 # prove join table is updated trunk/activerecord/test/base_test.rb
r5017 r5018 808 808 assert_nothing_raised { cloned_topic = topic.clone } 809 809 assert_equal topic.title, cloned_topic.title 810 assert cloned_topic.new ?810 assert cloned_topic.new_record? 811 811 812 812 # test if the attributes have been cloned … … 823 823 824 824 cloned_topic.save 825 assert !cloned_topic.new ?825 assert !cloned_topic.new_record? 826 826 assert cloned_topic.id != topic.id 827 827 end … … 835 835 assert_kind_of DeveloperSalary, clone.salary 836 836 assert_equal dev.salary.amount, clone.salary.amount 837 assert clone.new ?837 assert clone.new_record? 838 838 839 839 # test if the attributes have been cloned … … 843 843 844 844 assert clone.save 845 assert !clone.new ?845 assert !clone.new_record? 846 846 assert clone.id != dev.id 847 847 end … … 1340 1340 xml = [ topics(:first), topics(:second) ].to_xml(:indent => 0, :skip_instruct => true, :include => :replies) 1341 1341 assert xml.include?(%(<replies><reply>)) 1342 end 1343 1344 def test_array_to_xml_including_methods 1345 xml = [ topics(:first), topics(:second) ].to_xml(:indent => 0, :skip_instruct => true, :methods => [ :topic_id ]) 1346 assert xml.include?(%(<topic-id type="integer">#{topics(:first).topic_id}</topic-id>)) 1347 assert xml.include?(%(<topic-id type="integer">#{topics(:second).topic_id}</topic-id>)) 1342 1348 end 1343 1349 trunk/activerecord/test/finder_test.rb
r5017 r5018 346 346 assert_equal number_of_companies + 1, Company.count 347 347 assert_equal sig38, Company.find_or_create_by_name("38signals") 348 assert !sig38.new ?348 assert !sig38.new_record? 349 349 end 350 350 … … 354 354 assert_equal number_of_topics + 1, Topic.count 355 355 assert_equal another, Topic.find_or_create_by_title_and_author_name("Another topic", "John") 356 assert !another.new ?356 assert !another.new_record? 357 357 end 358 358 … … 360 360 sig38 = Company.find_or_initialize_by_name("38signals") 361 361 assert_equal "38signals", sig38.name 362 assert sig38.new ?362 assert sig38.new_record? 363 363 end 364 364 … … 367 367 assert_equal "Another topic", another.title 368 368 assert_equal "John", another.author_name 369 assert another.new ?369 assert another.new_record? 370 370 end 371 371