Skip to content
This repository
Browse code

Deprecated `define_attr_method` in `ActiveModel::AttributeMethods`

This only existed to support methods like `set_table_name` in Active
Record, which are themselves being deprecated.
  • Loading branch information...
commit 8df787d42890017f182c1ac6cb082317c255a456 1 parent f3c84dc
Jon Leighton authored November 29, 2011
5  activemodel/CHANGELOG.md
Source Rendered
... ...
@@ -1,5 +1,10 @@
1 1
 ## Rails 3.2.0 (unreleased) ##
2 2
 
  3
+*   Deprecated `define_attr_method` in `ActiveModel::AttributeMethods`, because this only existed to
  4
+    support methods like `set_table_name` in Active Record, which are themselves being deprecated.
  5
+
  6
+    *Jon Leighton*
  7
+
3 8
 *   Renamed (with a deprecation the following constants):
4 9
 
5 10
         ActiveModel::Serialization     => ActiveModel::Serializable
57  activemodel/lib/active_model/attribute_methods.rb
@@ -66,46 +66,29 @@ module AttributeMethods
66 66
     end
67 67
 
68 68
     module ClassMethods
69  
-      # Defines an "attribute" method (like +inheritance_column+ or +table_name+).
70  
-      # A new (class) method will be created with the given name. If a value is
71  
-      # specified, the new method will return that value (as a string).
72  
-      # Otherwise, the given block will be used to compute the value of the
73  
-      # method.
74  
-      #
75  
-      # The original method will be aliased, with the new name being prefixed
76  
-      # with "original_". This allows the new method to access the original
77  
-      # value.
78  
-      #
79  
-      # Example:
80  
-      #
81  
-      #   class Person
82  
-      #
83  
-      #     include ActiveModel::AttributeMethods
84  
-      #
85  
-      #     cattr_accessor :primary_key
86  
-      #     cattr_accessor :inheritance_column
87  
-      #
88  
-      #     define_attr_method :primary_key, "sysid"
89  
-      #     define_attr_method( :inheritance_column ) do
90  
-      #       original_inheritance_column + "_id"
91  
-      #     end
92  
-      #
93  
-      #   end
94  
-      #
95  
-      # Provides you with:
96  
-      #
97  
-      #   Person.primary_key
98  
-      #   # => "sysid"
99  
-      #   Person.inheritance_column = 'address'
100  
-      #   Person.inheritance_column
101  
-      #   # => 'address_id'
102  
-      def define_attr_method(name, value=nil, &block)
  69
+      def define_attr_method(name, value=nil, deprecation_warning = true, &block) #:nodoc:
  70
+        # This deprecation_warning param is for internal use so that we can silence
  71
+        # the warning from Active Record, because we are implementing more specific
  72
+        # messages there instead.
  73
+        #
  74
+        # It doesn't apply to the original_#{name} method as we want to warn if
  75
+        # people are calling that regardless.
  76
+        if deprecation_warning
  77
+          ActiveSupport::Deprecation.warn("define_attr_method is deprecated and will be removed without replacement.")
  78
+        end
  79
+
103 80
         sing = singleton_class
104 81
         sing.class_eval <<-eorb, __FILE__, __LINE__ + 1
105  
-          if method_defined?('original_#{name}')
106  
-            undef :'original_#{name}'
  82
+          remove_possible_method :'original_#{name}'
  83
+          remove_possible_method :'_original_#{name}'
  84
+          alias_method :'_original_#{name}', :'#{name}'
  85
+          define_method :'original_#{name}' do
  86
+            ActiveSupport::Deprecation.warn(
  87
+              "This method is generated by ActiveModel::AttributeMethods::ClassMethods#define_attr_method, " \
  88
+              "which is deprecated and will be removed."
  89
+            )
  90
+            send(:'_original_#{name}')
107 91
           end
108  
-          alias_method :'original_#{name}', :'#{name}'
109 92
         eorb
110 93
         if block_given?
111 94
           sing.send :define_method, name, &block
23  activemodel/test/cases/attribute_methods_test.rb
@@ -134,20 +134,33 @@ def foo
134 134
   end
135 135
 
136 136
   test '#define_attr_method generates attribute method' do
137  
-    ModelWithAttributes.define_attr_method(:bar, 'bar')
  137
+    assert_deprecated do
  138
+      ModelWithAttributes.define_attr_method(:bar, 'bar')
  139
+    end
138 140
 
139 141
     assert_respond_to ModelWithAttributes, :bar
140  
-    assert_equal "original bar", ModelWithAttributes.original_bar
  142
+
  143
+    assert_deprecated do
  144
+      assert_equal "original bar", ModelWithAttributes.original_bar
  145
+    end
  146
+
141 147
     assert_equal "bar", ModelWithAttributes.bar
142  
-    ModelWithAttributes.define_attr_method(:bar)
  148
+    ActiveSupport::Deprecation.silence do
  149
+      ModelWithAttributes.define_attr_method(:bar)
  150
+    end
143 151
     assert !ModelWithAttributes.bar
144 152
   end
145 153
 
146 154
   test '#define_attr_method generates attribute method with invalid identifier characters' do
147  
-    ModelWithWeirdNamesAttributes.define_attr_method(:'c?d', 'c?d')
  155
+    ActiveSupport::Deprecation.silence do
  156
+      ModelWithWeirdNamesAttributes.define_attr_method(:'c?d', 'c?d')
  157
+    end
148 158
 
149 159
     assert_respond_to ModelWithWeirdNamesAttributes, :'c?d'
150  
-    assert_equal "original c?d", ModelWithWeirdNamesAttributes.send('original_c?d')
  160
+
  161
+    ActiveSupport::Deprecation.silence do
  162
+      assert_equal "original c?d", ModelWithWeirdNamesAttributes.send('original_c?d')
  163
+    end
151 164
     assert_equal "c?d", ModelWithWeirdNamesAttributes.send('c?d')
152 165
   end
153 166
 
4  activerecord/lib/active_record/base.rb
@@ -594,13 +594,13 @@ def deprecated_property_setter(property, value, block) #:nodoc:
594 594
             "to get the default #{property} where you would have called `original_#{property}`."
595 595
           )
596 596
 
597  
-          define_attr_method property, value, &block
  597
+          define_attr_method property, value, false, &block
598 598
         else
599 599
           ActiveSupport::Deprecation.warn(
600 600
             "Calling set_#{property} is deprecated. Please use `self.#{property} = 'the_name'` instead."
601 601
           )
602 602
 
603  
-          define_attr_method property, value
  603
+          define_attr_method property, value, false
604 604
         end
605 605
       end
606 606
 
33  activerecord/test/cases/base_test.rb
@@ -1410,23 +1410,6 @@ def test_reload_with_exclusive_scope
1410 1410
     assert_equal dev, dev.reload
1411 1411
   end
1412 1412
 
1413  
-  def test_define_attr_method_with_value
1414  
-    k = Class.new( ActiveRecord::Base )
1415  
-    k.send(:define_attr_method, :table_name, "foo")
1416  
-    assert_equal "foo", k.table_name
1417  
-  end
1418  
-
1419  
-  def test_define_attr_method_with_block
1420  
-    k = Class.new( ActiveRecord::Base ) do
1421  
-      class << self
1422  
-        attr_accessor :foo_key
1423  
-      end
1424  
-    end
1425  
-    k.foo_key = "id"
1426  
-    k.send(:define_attr_method, :foo_key) { "sys_" + original_foo_key }
1427  
-    assert_equal "sys_id", k.foo_key
1428  
-  end
1429  
-
1430 1413
   def test_set_table_name_with_value
1431 1414
     k = Class.new( ActiveRecord::Base )
1432 1415
     k.table_name = "foo"
@@ -1464,7 +1447,9 @@ def test_set_table_name_with_block
1464 1447
     k = Class.new( ActiveRecord::Base )
1465 1448
     assert_deprecated do
1466 1449
       k.set_table_name "foo"
1467  
-      k.set_table_name { original_table_name + "ks" }
  1450
+      k.set_table_name do
  1451
+        ActiveSupport::Deprecation.silence { original_table_name } + "ks"
  1452
+      end
1468 1453
     end
1469 1454
     assert_equal "fooks", k.table_name
1470 1455
   end
@@ -1510,7 +1495,9 @@ def test_set_primary_key_with_block
1510 1495
     k.primary_key = 'id'
1511 1496
 
1512 1497
     assert_deprecated do
1513  
-      k.set_primary_key { "sys_" + original_primary_key }
  1498
+      k.set_primary_key do
  1499
+        "sys_" + ActiveSupport::Deprecation.silence { original_primary_key }
  1500
+      end
1514 1501
     end
1515 1502
     assert_equal "sys_id", k.primary_key
1516 1503
   end
@@ -1547,7 +1534,9 @@ def test_set_inheritance_column_with_value
1547 1534
   def test_set_inheritance_column_with_block
1548 1535
     k = Class.new( ActiveRecord::Base )
1549 1536
     assert_deprecated do
1550  
-      k.set_inheritance_column { original_inheritance_column + "_id" }
  1537
+      k.set_inheritance_column do
  1538
+        ActiveSupport::Deprecation.silence { original_inheritance_column } + "_id"
  1539
+      end
1551 1540
     end
1552 1541
     assert_equal "type_id", k.inheritance_column
1553 1542
   end
@@ -1580,7 +1569,9 @@ def test_set_sequence_name_with_block
1580 1569
     return skip "sequences not supported by db" unless orig_name
1581 1570
 
1582 1571
     assert_deprecated do
1583  
-      k.set_sequence_name { original_sequence_name + "_lol" }
  1572
+      k.set_sequence_name do
  1573
+        ActiveSupport::Deprecation.silence { original_sequence_name } + "_lol"
  1574
+      end
1584 1575
     end
1585 1576
     assert_equal orig_name + "_lol", k.sequence_name
1586 1577
   end
4  activerecord/test/cases/locking_test.rb
@@ -243,7 +243,9 @@ def test_set_locking_column_with_block
243 243
     k.locking_column = 'foo'
244 244
 
245 245
     assert_deprecated do
246  
-      k.set_locking_column { "lock_" + original_locking_column }
  246
+      k.set_locking_column do
  247
+        "lock_" + ActiveSupport::Deprecation.silence { original_locking_column }
  248
+      end
247 249
     end
248 250
     assert_equal "lock_foo", k.locking_column
249 251
   end

0 notes on commit 8df787d

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