Skip to content
This repository
Browse code

Deprecated ActiveRecord::Base.new_record? in favor of ActiveRecord::B…

…ase.new? (old version still works until Rails 2.0) [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5017 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
commit 9d7e6432b22a11e96ad7a3cab7510fac7b3927d6 1 parent d48322e
David Heinemeier Hansson authored
2  activerecord/CHANGELOG
... ...
@@ -1,5 +1,7 @@
1 1
 *SVN*
2 2
 
  3
+* Deprecated ActiveRecord::Base.new_record? in favor of ActiveRecord::Base.new? (old version still works until Rails 2.0) [DHH]
  4
+
3 5
 * Rename AR::Base#quote so people can use that name in their models. #3628 [Koz]
4 6
 
5 7
 * Add deprecation warning for inferred foreign key. #6029 [Josh Susser]
2  activerecord/lib/active_record/aggregations.rb
@@ -7,7 +7,7 @@ def self.included(base)
7 7
     def clear_aggregation_cache #:nodoc:
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_record?
  10
+      end unless self.new?
11 11
     end
12 12
 
13 13
     # Active Record implements aggregation through a macro-like class method called +composed_of+ for representing attributes 
38  activerecord/lib/active_record/associations.rb
@@ -65,7 +65,7 @@ def self.included(base)
65 65
     def clear_association_cache #:nodoc:
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_record?
  68
+      end unless self.new?
69 69
     end
70 70
     
71 71
     # Associations are a set of macro-like class methods for tying objects together through foreign keys. They express relationships like 
@@ -131,7 +131,7 @@ def clear_association_cache #:nodoc:
131 131
     # === One-to-one associations
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_record? == true).
  134
+    #   order to update their primary keys - except if the parent object is unsaved (new? == 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.
137 137
     # * If you wish to assign an object to a has_one association without saving it, use the #association.build method (documented below).
@@ -144,7 +144,7 @@ def clear_association_cache #:nodoc:
144 144
     #   (the owner of the collection) is not yet stored in the database.
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_record? == true) members of the collection are automatically saved when the parent is saved.
  147
+    # * All unsaved (new? == true) members of the collection are automatically saved when the parent is saved.
148 148
     #
149 149
     # === Association callbacks
150 150
     #
@@ -591,7 +591,7 @@ def has_one(association_id, options = {})
591 591
         module_eval do
592 592
           after_save <<-EOF
593 593
             association = instance_variable_get("@#{reflection.name}")
594  
-            if !association.nil? && (new_record? || association.new_record? || association["#{reflection.primary_key_name}"] != id)
  594
+            if !association.nil? && (new? || association.new? || association["#{reflection.primary_key_name}"] != id)
595 595
               association["#{reflection.primary_key_name}"] = id
596 596
               association.save(true)
597 597
             end
@@ -655,12 +655,6 @@ def has_one(association_id, options = {})
655 655
       #              :conditions => 'discounts > #{payments_count}'
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  
-        
664 658
         reflection = create_belongs_to_reflection(association_id, options)
665 659
         
666 660
         if reflection.options[:polymorphic]
@@ -670,7 +664,7 @@ def belongs_to(association_id, options = {})
670 664
             before_save <<-EOF
671 665
               association = instance_variable_get("@#{reflection.name}")
672 666
               if association && association.target
673  
-                if association.new_record?
  667
+                if association.new?
674 668
                   association.save(true)
675 669
                 end
676 670
                 
@@ -690,7 +684,7 @@ def belongs_to(association_id, options = {})
690 684
             before_save <<-EOF
691 685
               association = instance_variable_get("@#{reflection.name}")
692 686
               if !association.nil? 
693  
-                if association.new_record?
  687
+                if association.new?
694 688
                   association.save(true)
695 689
                 end
696 690
                 
@@ -930,10 +924,10 @@ def add_multiple_associated_save_callbacks(association_name)
930 924
           define_method(method_name) do
931 925
             association = instance_variable_get("@#{association_name}")
932 926
             if association.respond_to?(:loaded?)
933  
-              if new_record?
  927
+              if new?
934 928
                 association
935 929
               else
936  
-                association.select { |record| record.new_record? }
  930
+                association.select { |record| record.new? }
937 931
               end.each do |record|
938 932
                 errors.add "#{association_name}" unless record.valid?
939 933
               end
@@ -941,7 +935,7 @@ def add_multiple_associated_save_callbacks(association_name)
941 935
           end
942 936
 
943 937
           validate method_name
944  
-          before_save("@new_record_before_save = new_record?; true")
  938
+          before_save("@new_record_before_save = new?; true")
945 939
 
946 940
           after_callback = <<-end_eval
947 941
             association = instance_variable_get("@#{association_name}")
@@ -950,7 +944,7 @@ def add_multiple_associated_save_callbacks(association_name)
950 944
               if @new_record_before_save
951 945
                 records_to_save = association
952 946
               else
953  
-                records_to_save = association.select { |record| record.new_record? }
  947
+                records_to_save = association.select { |record| record.new? }
954 948
               end
955 949
               records_to_save.each { |record| association.send(:insert_record, record) }
956 950
               association.send(:construct_sql)   # reconstruct the SQL queries now that we know the owner's id
@@ -997,7 +991,7 @@ def configure_dependency_for_has_many(reflection)
997 991
 
998 992
           if reflection.options[:exclusively_dependent]
999 993
             reflection.options[:dependent] = :delete_all
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)
  994
+            #warn "The :exclusively_dependent option is deprecated.  Please use :dependent => :delete_all instead.")
1001 995
           end
1002 996
 
1003 997
           # See HasManyAssociation#delete_records.  Dependent associations
@@ -1457,7 +1451,7 @@ def association_join
1457 1451
                           table_alias_for(through_reflection.klass.table_name, aliased_join_table_name),
1458 1452
                           aliased_join_table_name, polymorphic_foreign_key,
1459 1453
                           parent.aliased_table_name, parent.primary_key,
1460  
-                          aliased_join_table_name, polymorphic_foreign_type, klass.quote_value(parent.active_record.base_class.name)] +
  1454
+                          aliased_join_table_name, polymorphic_foreign_type, klass.quote(parent.active_record.base_class.name)] +
1461 1455
                         " LEFT OUTER JOIN %s ON %s.%s = %s.%s " % [table_name_and_alias,
1462 1456
                           aliased_table_name, primary_key, aliased_join_table_name, options[:foreign_key] || reflection.klass.to_s.classify.foreign_key
1463 1457
                         ]
@@ -1472,7 +1466,7 @@ def association_join
1472 1466
                             aliased_table_name, "#{source_reflection.options[:as]}_id", 
1473 1467
                             aliased_join_table_name, options[:foreign_key] || primary_key,
1474 1468
                             aliased_table_name, "#{source_reflection.options[:as]}_type", 
1475  
-                            klass.quote_value(source_reflection.active_record.base_class.name)
  1469
+                            klass.quote(source_reflection.active_record.base_class.name)
1476 1470
                           ]
1477 1471
                         else
1478 1472
                           case source_reflection.macro
@@ -1501,7 +1495,7 @@ def association_join
1501 1495
                         aliased_table_name, "#{reflection.options[:as]}_id",
1502 1496
                         parent.aliased_table_name, parent.primary_key,
1503 1497
                         aliased_table_name, "#{reflection.options[:as]}_type",
1504  
-                        klass.quote_value(parent.active_record.base_class.name)
  1498
+                        klass.quote(parent.active_record.base_class.name)
1505 1499
                       ]
1506 1500
                     when reflection.macro == :has_one && reflection.options[:as]
1507 1501
                       " LEFT OUTER JOIN %s ON %s.%s = %s.%s AND %s.%s = %s " % [
@@ -1509,7 +1503,7 @@ def association_join
1509 1503
                         aliased_table_name, "#{reflection.options[:as]}_id",
1510 1504
                         parent.aliased_table_name, parent.primary_key,
1511 1505
                         aliased_table_name, "#{reflection.options[:as]}_type",
1512  
-                        klass.quote_value(reflection.active_record.base_class.name)
  1506
+                        klass.quote(reflection.active_record.base_class.name)
1513 1507
                       ]
1514 1508
                     else
1515 1509
                       foreign_key = options[:foreign_key] || reflection.active_record.name.foreign_key
@@ -1530,7 +1524,7 @@ def association_join
1530 1524
               join << %(AND %s.%s = %s ) % [
1531 1525
                 aliased_table_name, 
1532 1526
                 reflection.active_record.connection.quote_column_name(reflection.active_record.inheritance_column), 
1533  
-                klass.quote_value(klass.name.demodulize)] unless klass.descends_from_active_record?
  1527
+                klass.quote(klass.name.demodulize)] unless klass.descends_from_active_record?
1534 1528
               join << "AND #{interpolate_sql(sanitize_sql(reflection.options[:conditions]))} " if reflection.options[:conditions]
1535 1529
               join
1536 1530
             end
8  activerecord/lib/active_record/associations/association_collection.rb
@@ -23,7 +23,7 @@ def <<(*records)
23 23
           flatten_deeper(records).each do |record|
24 24
             raise_on_type_mismatch(record)
25 25
             callback(:before_add, record)
26  
-            result &&= insert_record(record) unless @owner.new_record?
  26
+            result &&= insert_record(record) unless @owner.new?
27 27
             @target << record
28 28
             callback(:after_add, record)
29 29
           end
@@ -51,7 +51,7 @@ def sum(*args, &block)
51 51
       def delete(*records)
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_record? }
  54
+        records.reject! { |record| @target.delete(record) if record.new? }
55 55
         return if records.empty?
56 56
         
57 57
         @owner.transaction do
@@ -91,7 +91,7 @@ def create(attributes = {})
91 91
           attributes.collect { |attr| create(attr) }
92 92
         else
93 93
           record = build(attributes)
94  
-          record.save unless @owner.new_record?
  94
+          record.save unless @owner.new?
95 95
           record
96 96
         end
97 97
       end
@@ -103,7 +103,7 @@ def size
103 103
         if loaded? && !@reflection.options[:uniq]
104 104
           @target.size
105 105
         elsif !loaded? && !@reflection.options[:uniq] && @target.is_a?(Array)
106  
-          unsaved_records = Array(@target.detect { |r| r.new_record? })
  106
+          unsaved_records = Array(@target.detect { |r| r.new? })
107 107
           unsaved_records.size + count_records
108 108
         else
109 109
           count_records
6  activerecord/lib/active_record/associations/association_proxy.rb
@@ -99,10 +99,10 @@ def extract_options_from_args!(args)
99 99
 
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_record?
  102
+            record["#{@reflection.options[:as]}_id"]   = @owner.id unless @owner.new?
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_record?
  105
+            record[@reflection.primary_key_name] = @owner.id unless @owner.new?
106 106
           end
107 107
         end
108 108
 
@@ -125,7 +125,7 @@ def method_missing(method, *args, &block)
125 125
         end
126 126
 
127 127
         def load_target
128  
-          if !loaded? and (!@owner.new_record? || foreign_key_present)
  128
+          if !loaded? and (!@owner.new? || foreign_key_present)
129 129
             @target = find_target
130 130
           end
131 131
 
6  activerecord/lib/active_record/associations/belongs_to_association.rb
@@ -13,7 +13,7 @@ def replace(record)
13 13
         counter_cache_name = @reflection.counter_cache_column
14 14
 
15 15
         if record.nil?
16  
-          if counter_cache_name && @owner[counter_cache_name] && !@owner.new_record?
  16
+          if counter_cache_name && @owner[counter_cache_name] && !@owner.new?
17 17
             @reflection.klass.decrement_counter(counter_cache_name, @owner[@reflection.primary_key_name]) if @owner[@reflection.primary_key_name]
18 18
           end
19 19
 
@@ -21,13 +21,13 @@ def replace(record)
21 21
         else
22 22
           raise_on_type_mismatch(record)
23 23
 
24  
-          if counter_cache_name && !@owner.new_record?
  24
+          if counter_cache_name && !@owner.new?
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]
27 27
           end
28 28
 
29 29
           @target = (AssociationProxy === record ? record.target : record)
30  
-          @owner[@reflection.primary_key_name] = record.id unless record.new_record?
  30
+          @owner[@reflection.primary_key_name] = record.id unless record.new?
31 31
           @updated = true
32 32
         end
33 33
 
2  activerecord/lib/active_record/associations/belongs_to_polymorphic_association.rb
@@ -7,7 +7,7 @@ def replace(record)
7 7
         else
8 8
           @target = (AssociationProxy === record ? record.target : record)
9 9
 
10  
-          unless record.new_record?
  10
+          unless record.new?
11 11
             @owner[@reflection.primary_key_name] = record.id
12 12
             @owner[@reflection.options[:foreign_type]] = record.class.base_class.name.to_s
13 13
           end
8  activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
@@ -19,7 +19,7 @@ def create(attributes = {})
19 19
           attributes.collect { |attr| create(attr) }
20 20
         else
21 21
           record = build(attributes)
22  
-          insert_record(record) unless @owner.new_record?
  22
+          insert_record(record) unless @owner.new?
23 23
           record
24 24
         end
25 25
       end
@@ -75,7 +75,7 @@ def push_with_attributes(record, join_attributes = {})
75 75
         join_attributes.each { |key, value| record[key.to_s] = value }
76 76
 
77 77
         callback(:before_add, record)
78  
-        insert_record(record) unless @owner.new_record?
  78
+        insert_record(record) unless @owner.new?
79 79
         @target << record
80 80
         callback(:after_add, record)
81 81
 
@@ -101,7 +101,7 @@ def count_records
101 101
         end
102 102
 
103 103
         def insert_record(record)
104  
-          if record.new_record?
  104
+          if record.new?
105 105
             return false unless record.save
106 106
           end
107 107
 
@@ -118,7 +118,7 @@ def insert_record(record)
118 118
                   attributes[column.name] = record.quoted_id
119 119
                 else
120 120
                   if record.attributes.has_key?(column.name)
121  
-                    value = @owner.send(:quote_value, record[column.name], column)
  121
+                    value = @owner.send(:quote, record[column.name], column)
122 122
                     attributes[column.name] = value unless value.nil?
123 123
                   end
124 124
               end
4  activerecord/lib/active_record/associations/has_many_association.rb
@@ -111,7 +111,7 @@ def method_missing(method, *args, &block)
111 111
         end
112 112
 
113 113
         def load_target
114  
-          if !@owner.new_record? || foreign_key_present
  114
+          if !@owner.new? || foreign_key_present
115 115
             begin
116 116
               if !loaded?
117 117
                 if @target.is_a?(Array) && @target.any?
@@ -184,7 +184,7 @@ def construct_sql
184 184
             when @reflection.options[:as]
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_value(@owner.class.base_class.name.to_s)}"
  187
+                "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}"
188 188
               @finder_sql << " AND (#{conditions})" if conditions
189 189
             
190 190
             else
8  activerecord/lib/active_record/associations/has_many_through_association.rb
@@ -49,7 +49,7 @@ def reset
49 49
       def <<(*records)
50 50
         return if records.empty?
51 51
         through = @reflection.through_reflection
52  
-        raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) if @owner.new_record?
  52
+        raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) if @owner.new?
53 53
 
54 54
         load_target
55 55
 
@@ -57,7 +57,7 @@ def <<(*records)
57 57
         klass.transaction do
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_record?) && !associate.new_record?
  60
+            raise ActiveRecord::HasManyThroughCantAssociateNewRecords.new(@owner, through) unless associate.respond_to?(:new?) && !associate.new?
61 61
 
62 62
             @owner.send(@reflection.through_reflection.name).proxy_target << klass.with_scope(:create => construct_join_attributes(associate)) { klass.create! }
63 63
             @target << associate
@@ -127,7 +127,7 @@ def construct_join_attributes(associate)
127 127
         def construct_quoted_owner_attributes(reflection)
128 128
           if as = reflection.options[:as]
129 129
             { "#{as}_id" => @owner.quoted_id,
130  
-              "#{as}_type" => reflection.klass.quote_value(
  130
+              "#{as}_type" => reflection.klass.quote(
131 131
                 @owner.class.base_class.name.to_s,
132 132
                 reflection.klass.columns_hash["#{as}_type"]) }
133 133
           else
@@ -164,7 +164,7 @@ def construct_joins(custom_joins = nil)
164 164
             if @reflection.source_reflection.options[:as]
165 165
               polymorphic_join = "AND %s.%s = %s" % [
166 166
                 @reflection.table_name, "#{@reflection.source_reflection.options[:as]}_type",
167  
-                @owner.class.quote_value(@reflection.through_reflection.klass.name)
  167
+                @owner.class.quote(@reflection.through_reflection.klass.name)
168 168
               ]
169 169
             end
170 170
           end
10  activerecord/lib/active_record/associations/has_one_association.rb
@@ -18,7 +18,7 @@ def build(attributes = {}, replace_existing = true)
18 18
         if replace_existing
19 19
           replace(record, true) 
20 20
         else
21  
-          record[@reflection.primary_key_name] = @owner.id unless @owner.new_record?
  21
+          record[@reflection.primary_key_name] = @owner.id unless @owner.new?
22 22
           self.target = record
23 23
         end
24 24
 
@@ -30,11 +30,11 @@ def replace(obj, dont_save = false)
30 30
 
31 31
         unless @target.nil?
32 32
           if dependent? && !dont_save && @target != obj
33  
-            @target.destroy unless @target.new_record?
  33
+            @target.destroy unless @target.new?
34 34
             @owner.clear_association_cache
35 35
           else
36 36
             @target[@reflection.primary_key_name] = nil
37  
-            @target.save unless @owner.new_record? || @target.new_record?
  37
+            @target.save unless @owner.new? || @target.new?
38 38
           end
39 39
         end
40 40
 
@@ -48,7 +48,7 @@ def replace(obj, dont_save = false)
48 48
 
49 49
         @loaded = true
50 50
 
51  
-        unless @owner.new_record? or obj.nil? or dont_save
  51
+        unless @owner.new? or obj.nil? or dont_save
52 52
           return (obj.save ? self : false)
53 53
         else
54 54
           return (obj.nil? ? nil : self)
@@ -69,7 +69,7 @@ def construct_sql
69 69
             when @reflection.options[:as]
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_value(@owner.class.base_class.name.to_s)}"          
  72
+                "#{@reflection.klass.table_name}.#{@reflection.options[:as]}_type = #{@owner.class.quote @owner.class.base_class.name.to_s}"          
73 73
             else
74 74
               @finder_sql = "#{@reflection.table_name}.#{@reflection.primary_key_name} = #{@owner.quoted_id}"
75 75
           end
20  activerecord/lib/active_record/base.rb
@@ -183,7 +183,7 @@ def initialize(errors)
183 183
   #
184 184
   #   # No 'Winter' tag exists
185 185
   #   winter = Tag.find_or_initialize_by_name("Winter")
186  
-  #   winter.new_record? # true
  186
+  #   winter.new? # true
187 187
   #
188 188
   # == Saving arrays, hashes, and other non-mappable objects in text columns
189 189
   #
@@ -1518,9 +1518,19 @@ def id=(value)
1518 1518
       end
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_record?
  1521
+      def new?
1522 1522
         @new_record
1523 1523
       end
  1524
+      
  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.", caller
  1530
+        )
  1531
+        
  1532
+        new?
  1533
+      end
1524 1534
 
1525 1535
       # * No record exists: Creates a new record with values matching those of the object attributes.
1526 1536
       # * A record does exist: Updates the record with values matching those of the object attributes.
@@ -1538,7 +1548,7 @@ def save!
1538 1548
       # Deletes the record in the database and freezes this instance to reflect that no changes should
1539 1549
       # be made (since they can't be persisted).
1540 1550
       def destroy
1541  
-        unless new_record?
  1551
+        unless new?
1542 1552
           connection.delete <<-end_sql, "#{self.class.name} Destroy"
1543 1553
             DELETE FROM #{self.class.table_name}
1544 1554
             WHERE #{self.class.primary_key} = #{quoted_id}
@@ -1707,7 +1717,7 @@ def ==(comparison_object)
1707 1717
         comparison_object.equal?(self) ||
1708 1718
           (comparison_object.instance_of?(self.class) && 
1709 1719
             comparison_object.id == id && 
1710  
-            !comparison_object.new_record?)
  1720
+            !comparison_object.new?)
1711 1721
       end
1712 1722
 
1713 1723
       # Delegates to ==
@@ -1763,7 +1773,7 @@ def readonly! #:nodoc:
1763 1773
 
1764 1774
     private
1765 1775
       def create_or_update
1766  
-        if new_record? then create else update end
  1776
+        if new? then create else update end
1767 1777
         true
1768 1778
       end
1769 1779
 
4  activerecord/lib/active_record/callbacks.rb
@@ -293,13 +293,13 @@ def after_validation_on_update()  end
293 293
 
294 294
     def valid_with_callbacks? #:nodoc:
295 295
       return false if callback(:before_validation) == false
296  
-      if new_record? then result = callback(:before_validation_on_create) else result = callback(:before_validation_on_update) end
  296
+      if new? then result = callback(:before_validation_on_create) else result = callback(:before_validation_on_update) end
297 297
       return false if result == false
298 298
 
299 299
       result = valid_without_callbacks?
300 300
 
301 301
       callback(:after_validation)
302  
-      if new_record? then callback(:after_validation_on_create) else callback(:after_validation_on_update) end
  302
+      if new? then callback(:after_validation_on_create) else callback(:after_validation_on_update) end
303 303
 
304 304
       return result
305 305
     end
2  activerecord/lib/active_record/locking/pessimistic.rb
@@ -69,7 +69,7 @@ module Pessimistic
69 69
       # or pass true for "FOR UPDATE" (the default, an exclusive row lock).  Returns
70 70
       # the locked record.
71 71
       def lock!(lock = true)
72  
-        reload(:lock => lock) unless new_record?
  72
+        reload(:lock => lock) unless new?
73 73
         self
74 74
       end
75 75
     end
11  activerecord/lib/active_record/validations.rb
@@ -333,8 +333,7 @@ def validates_confirmation_of(*attr_names)
333 333
         attr_accessor *(attr_names.map { |n| "#{n}_confirmation" })
334 334
 
335 335
         validates_each(attr_names, configuration) do |record, attr_name, value|
336  
-          confirm = record.send("#{attr_name}_confirmation")
337  
-          record.errors.add(attr_name, configuration[:message]) unless value.nil? || value == confirm
  336
+          record.errors.add(attr_name, configuration[:message]) unless record.send("#{attr_name}_confirmation").nil? or value == record.send("#{attr_name}_confirmation")
338 337
         end
339 338
       end
340 339
 
@@ -375,10 +374,6 @@ def validates_acceptance_of(*attr_names)
375 374
       #
376 375
       # The first_name attribute must be in the object and it cannot be blank.
377 376
       #      
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  
-      #
382 377
       # Configuration options:
383 378
       # * <tt>message</tt> - A custom error message (default is: "can't be blank")
384 379
       # * <tt>on</tt> - Specifies when this validation is active (default is :save, other options :create, :update)
@@ -543,7 +538,7 @@ def validates_uniqueness_of(*attr_names)
543 538
               condition_params << scope_value
544 539
             end
545 540
           end
546  
-          unless record.new_record?
  541
+          unless record.new?
547 542
             condition_sql << " AND #{record.class.table_name}.#{record.class.primary_key} <> ?"
548 543
             condition_params << record.send(:id)
549 544
           end
@@ -777,7 +772,7 @@ def valid?
777 772
       run_validations(:validate)
778 773
       validate
779 774
 
780  
-      if new_record?
  775
+      if new?
781 776
         run_validations(:validate_on_create)
782 777
         validate_on_create
783 778
       else
107  activerecord/test/associations_test.rb
@@ -178,13 +178,13 @@ def test_dependence
178 178
     assert_equal num_accounts - 1, Account.count
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
184 184
       Firm.has_many :deprecated_exclusively_dependent_clients, :class_name => 'Client', :exclusively_dependent => true
185 185
     end
186 186
   end
187  
-
  187
+  
188 188
   def test_exclusive_dependence
189 189
     num_accounts = Account.count
190 190
     firm = ExclusivelyDependentFirm.find(9)
@@ -244,10 +244,10 @@ def test_build_before_child_saved
244 244
 
245 245
     account = firm.account.build("credit_limit" => 1000)
246 246
     assert_equal account, firm.account
247  
-    assert account.new_record?
  247
+    assert account.new?
248 248
     assert firm.save
249 249
     assert_equal account, firm.account
250  
-    assert !account.new_record?
  250
+    assert !account.new?
251 251
   end
252 252
 
253 253
   def test_build_before_either_saved
@@ -255,10 +255,10 @@ def test_build_before_either_saved
255 255
 
256 256
     firm.account = account = Account.new("credit_limit" => 1000)
257 257
     assert_equal account, firm.account
258  
-    assert account.new_record?
  258
+    assert account.new?
259 259
     assert firm.save
260 260
     assert_equal account, firm.account
261  
-    assert !account.new_record?
  261
+    assert !account.new?
262 262
   end
263 263
 
264 264
   def test_failing_build_association
@@ -295,7 +295,7 @@ def test_dependence_with_missing_association
295 295
   def test_assignment_before_parent_saved
296 296
     firm = Firm.new("name" => "GlobalMegaCorp")
297 297
     firm.account = a = Account.find(1)
298  
-    assert firm.new_record?
  298
+    assert firm.new?
299 299
     assert_equal a, firm.account
300 300
     assert firm.save
301 301
     assert_equal a, firm.account
@@ -305,7 +305,7 @@ def test_assignment_before_parent_saved
305 305
   def test_assignment_before_child_saved
306 306
     firm = Firm.find(1)
307 307
     firm.account = a = Account.new("credit_limit" => 1000)
308  
-    assert !a.new_record?
  308
+    assert !a.new?
309 309
     assert_equal a, firm.account
310 310
     assert_equal a, firm.account
311 311
     assert_equal a, firm.account(true)
@@ -314,12 +314,12 @@ def test_assignment_before_child_saved
314 314
   def test_assignment_before_either_saved
315 315
     firm = Firm.new("name" => "GlobalMegaCorp")
316 316
     firm.account = a = Account.new("credit_limit" => 1000)
317  
-    assert firm.new_record?
318  
-    assert a.new_record?
  317
+    assert firm.new?
  318
+    assert a.new?
319 319
     assert_equal a, firm.account
320 320
     assert firm.save
321  
-    assert !firm.new_record?
322  
-    assert !a.new_record?
  321
+    assert !firm.new?
  322
+    assert !a.new?
323 323
     assert_equal a, firm.account
324 324
     assert_equal a, firm.account(true)
325 325
   end
@@ -350,13 +350,6 @@ def test_save_still_works_after_accessing_nil_has_one
350 350
     end    
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  
-
360 353
 end
361 354
 
362 355
 
@@ -558,14 +551,14 @@ def test_adding_before_save
558 551
     new_firm = Firm.new("name" => "A New Firm, Inc")
559 552
     new_firm.clients_of_firm.push Client.new("name" => "Natural Company")
560 553
     new_firm.clients_of_firm << (c = Client.new("name" => "Apple"))
561  
-    assert new_firm.new_record?
562  
-    assert c.new_record?
  554
+    assert new_firm.new?
  555
+    assert c.new?
563 556
     assert_equal 2, new_firm.clients_of_firm.size
564 557
     assert_equal no_of_firms, Firm.count      # Firm was not saved to database.
565 558
     assert_equal no_of_clients, Client.count  # Clients were not saved to database.
566 559
     assert new_firm.save
567  
-    assert !new_firm.new_record?
568  
-    assert !c.new_record?
  560
+    assert !new_firm.new?
  561
+    assert !c.new?
569 562
     assert_equal new_firm, c.firm
570 563
     assert_equal no_of_firms+1, Firm.count      # Firm was saved to database.
571 564
     assert_equal no_of_clients+2, Client.count  # Clients were saved to database.
@@ -576,10 +569,10 @@ def test_adding_before_save
576 569
   def test_invalid_adding
577 570
     firm = Firm.find(1)
578 571
     assert !(firm.clients_of_firm << c = Client.new)
579  
-    assert c.new_record?
  572
+    assert c.new?
580 573
     assert !firm.valid?
581 574
     assert !firm.save
582  
-    assert c.new_record?
  575
+    assert c.new?
583 576
   end
584 577
 
585 578
   def test_invalid_adding_before_save
@@ -587,21 +580,21 @@ def test_invalid_adding_before_save
587 580
     no_of_clients = Client.count
588 581
     new_firm = Firm.new("name" => "A New Firm, Inc")
589 582
     new_firm.clients_of_firm.concat([c = Client.new, Client.new("name" => "Apple")])
590  
-    assert c.new_record?
  583
+    assert c.new?
591 584
     assert !c.valid?
592 585
     assert !new_firm.valid?
593 586
     assert !new_firm.save
594  
-    assert c.new_record?
595  
-    assert new_firm.new_record?
  587
+    assert c.new?
  588
+    assert new_firm.new?
596 589
   end
597 590
 
598 591
   def test_build
599 592
     new_client = companies(:first_firm).clients_of_firm.build("name" => "Another Client")
600 593
     assert_equal "Another Client", new_client.name
601  
-    assert new_client.new_record?
  594
+    assert new_client.new?
602 595
     assert_equal new_client, companies(:first_firm).clients_of_firm.last
603 596
     assert companies(:first_firm).save
604  
-    assert !new_client.new_record?
  597
+    assert !new_client.new?
605 598
     assert_equal 2, companies(:first_firm).clients_of_firm(true).size
606 599
   end
607 600
 
@@ -644,18 +637,18 @@ def test_create_without_loading_association
644 637
 
645 638
   def test_invalid_build
646 639
     new_client = companies(:first_firm).clients_of_firm.build
647  
-    assert new_client.new_record?
  640
+    assert new_client.new?
648 641
     assert !new_client.valid?
649 642
     assert_equal new_client, companies(:first_firm).clients_of_firm.last
650 643
     assert !companies(:first_firm).save
651  
-    assert new_client.new_record?
  644
+    assert new_client.new?
652 645
     assert_equal 1, companies(:first_firm).clients_of_firm(true).size
653 646
   end
654 647
   
655 648
   def test_create
656 649
     force_signal37_to_load_all_clients_of_firm
657 650
     new_client = companies(:first_firm).clients_of_firm.create("name" => "Another Client")
658  
-    assert !new_client.new_record?
  651
+    assert !new_client.new?
659 652
     assert_equal new_client, companies(:first_firm).clients_of_firm.last
660 653
     assert_equal new_client, companies(:first_firm).clients_of_firm(true).last
661 654
   end
@@ -1032,10 +1025,10 @@ def test_assignment_before_parent_saved
1032 1025
     apple = Firm.new("name" => "Apple")
1033 1026
     client.firm = apple
1034 1027
     assert_equal apple, client.firm
1035  
-    assert apple.new_record?
  1028
+    assert apple.new?
1036 1029
     assert client.save
1037 1030
     assert apple.save
1038  
-    assert !apple.new_record?
  1031
+    assert !apple.new?
1039 1032
     assert_equal apple, client.firm
1040 1033
     assert_equal apple, client.firm(true)
1041 1034
   end
@@ -1044,10 +1037,10 @@ def test_assignment_before_child_saved
1044 1037
     final_cut = Client.new("name" => "Final Cut")
1045 1038
     firm = Firm.find(1)
1046 1039
     final_cut.firm = firm
1047  
-    assert final_cut.new_record?
  1040
+    assert final_cut.new?
1048 1041
     assert final_cut.save
1049  
-    assert !final_cut.new_record?
1050  
-    assert !firm.new_record?
  1042
+    assert !final_cut.new?
  1043
+    assert !firm.new?
1051 1044
     assert_equal firm, final_cut.firm
1052 1045
     assert_equal firm, final_cut.firm(true)
1053 1046
   end
@@ -1056,11 +1049,11 @@ def test_assignment_before_either_saved
1056 1049
     final_cut = Client.new("name" => "Final Cut")
1057 1050
     apple = Firm.new("name" => "Apple")
1058 1051
     final_cut.firm = apple
1059  
-    assert final_cut.new_record?
1060  
-    assert apple.new_record?
  1052
+    assert final_cut.new?
  1053
+    assert apple.new?
1061 1054
     assert final_cut.save
1062  
-    assert !final_cut.new_record?
1063  
-    assert !apple.new_record?
  1055
+    assert !final_cut.new?
  1056
+    assert !apple.new?
1064 1057
     assert_equal apple, final_cut.firm
1065 1058
     assert_equal apple, final_cut.firm(true)
1066 1059
   end
@@ -1374,10 +1367,10 @@ def test_habtm_adding_before_save
1374 1367
     no_of_projects = Project.count
1375 1368
     aredridel = Developer.new("name" => "Aredridel")
1376 1369
     aredridel.projects.concat([Project.find(1), p = Project.new("name" => "Projekt")])
1377  
-    assert aredridel.new_record?
1378  
-    assert p.new_record?
  1370
+    assert aredridel.new?
  1371
+    assert p.new?
1379 1372
     assert aredridel.save
1380  
-    assert !aredridel.new_record?
  1373
+    assert !aredridel.new?
1381 1374
     assert_equal no_of_devels+1, Developer.count
1382 1375
     assert_equal no_of_projects+1, Project.count
1383 1376
     assert_equal 2, aredridel.projects.size
@@ -1392,10 +1385,10 @@ def test_habtm_adding_before_save_with_join_attributes
1392 1385
     ken.projects.push_with_attributes( Project.find(1), :joined_on => now )
1393 1386
     p = Project.new("name" => "Foomatic")
1394 1387
     ken.projects.push_with_attributes( p, :joined_on => now )
1395  
-    assert ken.new_record?
1396  
-    assert p.new_record?
  1388
+    assert ken.new?
  1389
+    assert p.new?
1397 1390
     assert ken.save
1398  
-    assert !ken.new_record?
  1391
+    assert !ken.new?
1399 1392
     assert_equal no_of_devels+1, Developer.count
1400 1393
     assert_equal no_of_projects+1, Project.count
1401 1394
     assert_equal 2, ken.projects.size
@@ -1428,9 +1421,9 @@ def test_build
1428 1421
     devel = Developer.find(1)
1429 1422
     proj = devel.projects.build("name" => "Projekt")
1430 1423
     assert_equal devel.projects.last, proj
1431  
-    assert proj.new_record?
  1424
+    assert proj.new?
1432 1425
     devel.save
1433  
-    assert !proj.new_record?
  1426
+    assert !proj.new?
1434 1427
     assert_equal devel.projects.last, proj
1435 1428
     assert_equal Developer.find(1).projects.sort_by(&:id).last, proj  # prove join table is updated
1436 1429
   end
@@ -1440,10 +1433,10 @@ def test_build_by_new_record
1440 1433
     proj1 = devel.projects.build(:name => "Make bed")
1441 1434
     proj2 = devel.projects.build(:name => "Lie in it")
1442 1435
     assert_equal devel.projects.last, proj2
1443  
-    assert proj2.new_record?
  1436
+    assert proj2.new?
1444 1437
     devel.save
1445  
-    assert !devel.new_record?
1446  
-    assert !proj2.new_record?
  1438
+    assert !devel.new?
  1439
+    assert !proj2.new?
1447 1440
     assert_equal devel.projects.last, proj2
1448 1441
     assert_equal Developer.find_by_name("Marcel").projects.last, proj2  # prove join table is updated
1449 1442
   end
@@ -1452,7 +1445,7 @@ def test_create
1452 1445
     devel = Developer.find(1)
1453 1446
     proj = devel.projects.create("name" => "Projekt")
1454 1447
     assert_equal devel.projects.last, proj
1455  
-    assert !proj.new_record?
  1448
+    assert !proj.new?
1456 1449
     assert_equal Developer.find(1).projects.sort_by(&:id).last, proj  # prove join table is updated
1457 1450
   end
1458 1451
   
@@ -1461,10 +1454,10 @@ def test_create_by_new_record
1461 1454
     proj1 = devel.projects.create(:name => "Make bed")
1462 1455
     proj2 = devel.projects.create(:name => "Lie in it")
1463 1456
     assert_equal devel.projects.last, proj2
1464  
-    assert proj2.new_record?
  1457
+    assert proj2.new?
1465 1458
     devel.save
1466  
-    assert !devel.new_record?
1467  
-    assert !proj2.new_record?
  1459
+    assert !devel.new?
  1460
+    assert !proj2.new?
1468 1461
     assert_equal devel.projects.last, proj2
1469 1462
     assert_equal Developer.find_by_name("Marcel").projects.last, proj2  # prove join table is updated
1470 1463
   end
14  activerecord/test/base_test.rb
@@ -807,7 +807,7 @@ def test_clone
807 807
     cloned_topic = nil
808 808
     assert_nothing_raised { cloned_topic = topic.clone }
809 809
     assert_equal topic.title, cloned_topic.title
810  
-    assert cloned_topic.new_record?
  810
+    assert cloned_topic.new?
811 811
 
812 812
     # test if the attributes have been cloned
813 813
     topic.title = "a" 
@@ -822,7 +822,7 @@ def test_clone
822 822
     assert_equal "b", topic.title["a"]
823 823
 
824 824
     cloned_topic.save
825  
-    assert !cloned_topic.new_record?
  825
+    assert !cloned_topic.new?
826 826
     assert cloned_topic.id != topic.id
827 827
   end
828 828
 
@@ -834,7 +834,7 @@ def test_clone_with_aggregate_of_same_name_as_attribute
834 834
     assert_nothing_raised { clone = dev.clone }
835 835
     assert_kind_of DeveloperSalary, clone.salary
836 836
     assert_equal dev.salary.amount, clone.salary.amount
837  
-    assert clone.new_record?
  837
+    assert clone.new?
838 838
 
839 839
     # test if the attributes have been cloned
840 840
     original_amount = clone.salary.amount
@@ -842,7 +842,7 @@ def test_clone_with_aggregate_of_same_name_as_attribute
842 842
     assert_equal original_amount, clone.salary.amount
843 843
 
844 844
     assert clone.save
845  
-    assert !clone.new_record?
  845
+    assert !clone.new?
846 846
     assert clone.id != dev.id
847 847
   end
848 848
 
@@ -1340,12 +1340,6 @@ def test_array_to_xml_including_has_many_association
1340 1340
     xml = [ topics(:first), topics(:second) ].to_xml(:indent => 0, :skip_instruct => true, :include => :replies)
1341 1341
     assert xml.include?(%(<replies><reply>))
1342 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>))
1348  
-  end
1349 1343
   
1350 1344
   def test_array_to_xml_including_has_one_association
1351 1345
     xml = [ companies(:first_firm), companies(:rails_core) ].to_xml(:indent => 0, :skip_instruct => true, :include => :account)
8  activerecord/test/finder_test.rb
@@ -345,7 +345,7 @@ def test_find_or_create_from_one_attribute
345 345
     sig38 = Company.find_or_create_by_name("38signals")
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_record?
  348
+    assert !sig38.new?
349 349
   end
350 350
 
351 351
   def test_find_or_create_from_two_attributes
@@ -353,20 +353,20 @@ def test_find_or_create_from_two_attributes
353 353
     another = Topic.find_or_create_by_title_and_author_name("Another topic","John")
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_record?
  356
+    assert !another.new?
357 357
   end
358 358
   
359 359
   def test_find_or_initialize_from_one_attribute
360 360
     sig38 = Company.find_or_initialize_by_name("38signals")
361 361
     assert_equal "38signals", sig38.name
362  
-    assert sig38.new_record?
  362
+    assert sig38.new?
363 363
   end
364 364
   
365 365
   def test_find_or_initialize_from_two_attributes
366 366
     another = Topic.find_or_initialize_by_title_and_author_name("Another topic","John")
367 367
     assert_equal "Another topic", another.title
368 368
     assert_equal "John", another.author_name
369  
-    assert another.new_record?
  369
+    assert another.new?
370 370
   end
371 371
 
372 372
   def test_find_with_bad_sql

0 notes on commit 9d7e643

Please sign in to comment.
Something went wrong with that request. Please try again.