Permalink
Browse files

Make sure the original_foo accessor still works (but deprecated) if w…

…e are using self.foo=
  • Loading branch information...
1 parent e51ecfa commit 4aad289428bf46cf4a13f159819f1993f8fc978f @jonleighton jonleighton committed Nov 29, 2011
Showing with 86 additions and 13 deletions.
  1. +31 −6 activerecord/lib/active_record/base.rb
  2. +55 −7 activerecord/test/cases/base_test.rb
@@ -604,6 +604,16 @@ def deprecated_property_setter(property, value, block) #:nodoc:
end
end
+ def deprecated_original_property_getter(property) #:nodoc:
+ ActiveSupport::Deprecation.warn("original_#{property} is deprecated. Define self.#{property} and call super instead.")
+
+ if !instance_variable_defined?("@original_#{property}") && respond_to?("reset_#{property}")
+ send("reset_#{property}")
+ else
+ instance_variable_get("@original_#{property}")
+ end
+ end
+
# Guesses the table name (in forced lower-case) based on the name of the class in the
# inheritance hierarchy descending directly from ActiveRecord::Base. So if the hierarchy
# looks like: Reply < Message < ActiveRecord::Base, then Message is used
@@ -664,6 +674,10 @@ def table_name
@table_name
end
+ def original_table_name #:nodoc:
+ deprecated_original_property_getter :table_name
+ end
+
# Sets the table name explicitly. Example:
#
# class Project < ActiveRecord::Base
@@ -673,10 +687,11 @@ def table_name
# You can also just define your own <tt>self.table_name</tt> method; see
# the documentation for ActiveRecord::Base#table_name.
def table_name=(value)
- @table_name = value
- @quoted_table_name = nil
- @arel_table = nil
- @relation = Relation.new(self, arel_table)
+ @original_table_name = @table_name if defined?(@table_name)
+ @table_name = value
+ @quoted_table_name = nil
+ @arel_table = nil
+ @relation = Relation.new(self, arel_table)
end
def set_table_name(value = nil, &block) #:nodoc:
@@ -715,9 +730,14 @@ def inheritance_column
end
end
+ def original_inheritance_column #:nodoc:
+ deprecated_original_property_getter :inheritance_column
+ end
+
# Sets the value of inheritance_column
def inheritance_column=(value)
- @inheritance_column = value.to_s
+ @original_inheritance_column = inheritance_column
+ @inheritance_column = value.to_s
end
def set_inheritance_column(value = nil, &block) #:nodoc:
@@ -732,6 +752,10 @@ def sequence_name
end
end
+ def original_sequence_name #:nodoc:
+ deprecated_original_property_getter :sequence_name
+ end
+
def reset_sequence_name #:nodoc:
self.sequence_name = connection.default_sequence_name(table_name, primary_key)
end
@@ -751,7 +775,8 @@ def reset_sequence_name #:nodoc:
# self.sequence_name = "projectseq" # default would have been "project_seq"
# end
def sequence_name=(value)
- @sequence_name = value.to_s
+ @original_sequence_name = @sequence_name if defined?(@sequence_name)
+ @sequence_name = value.to_s
end
def set_sequence_name(value = nil, &block) #:nodoc:
@@ -1476,6 +1476,24 @@ def k.table_name; super + "ks"; end
assert_equal "foosks", k.table_name
end
+ def test_original_table_name
+ k = Class.new(ActiveRecord::Base)
+ def k.name; "Foo"; end
+ k.table_name = "bar"
+
+ assert_deprecated do
+ assert_equal "foos", k.original_table_name
+ end
+
+ k = Class.new(ActiveRecord::Base)
+ k.table_name = "omg"
+ k.table_name = "wtf"
+
+ assert_deprecated do
+ assert_equal "omg", k.original_table_name
+ end
+ end
+
def test_set_primary_key_with_value
k = Class.new( ActiveRecord::Base )
k.primary_key = "foo"
@@ -1510,6 +1528,16 @@ def test_set_inheritance_column_with_block
assert_equal "type_id", k.inheritance_column
end
+ def test_original_inheritance_column
+ k = Class.new(ActiveRecord::Base)
+ def k.name; "Foo"; end
+ k.inheritance_column = "omg"
+
+ assert_deprecated do
+ assert_equal "type", k.original_inheritance_column
+ end
+ end
+
def test_set_sequence_name_with_value
k = Class.new( ActiveRecord::Base )
k.sequence_name = "foo"
@@ -1525,14 +1553,34 @@ def test_set_sequence_name_with_block
k = Class.new( ActiveRecord::Base )
k.table_name = "projects"
orig_name = k.sequence_name
+ return skip "sequences not supported by db" unless orig_name
- if orig_name
- assert_deprecated do
- k.set_sequence_name { original_sequence_name + "_lol" }
- end
- assert_equal orig_name + "_lol", k.sequence_name
- else
- skip "sequences not supported by db"
+ assert_deprecated do
+ k.set_sequence_name { original_sequence_name + "_lol" }
+ end
+ assert_equal orig_name + "_lol", k.sequence_name
+ end
+
+ def test_original_sequence_name
+ k = Class.new(ActiveRecord::Base)
+ k.table_name = "projects"
+ orig_name = k.sequence_name
+ return skip "sequences not supported by db" unless orig_name
+
+ k = Class.new(ActiveRecord::Base)
+ k.table_name = "projects"
+ k.sequence_name = "omg"
+
+ assert_deprecated do
+ assert_equal orig_name, k.original_sequence_name
+ end
+
+ k = Class.new(ActiveRecord::Base)
+ k.table_name = "projects"
+ k.sequence_name = "omg"
+ k.sequence_name = "wtf"
+ assert_deprecated do
+ assert_equal "omg", k.original_sequence_name
end
end

0 comments on commit 4aad289

Please sign in to comment.