diff options
-rw-r--r-- | activerecord/lib/active_record/base.rb | 37 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 62 |
2 files changed, 86 insertions, 13 deletions
diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 3f57aaed8f..b178419657 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -604,6 +604,16 @@ module ActiveRecord #: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 @@ module ActiveRecord #:nodoc: @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 @@ module ActiveRecord #:nodoc: # 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 @@ module ActiveRecord #:nodoc: 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 @@ module ActiveRecord #:nodoc: 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 @@ module ActiveRecord #: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: diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 3a84e40f57..062cac7d52 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1476,6 +1476,24 @@ class BasicsTest < ActiveRecord::TestCase 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 @@ class BasicsTest < ActiveRecord::TestCase 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 @@ class BasicsTest < ActiveRecord::TestCase 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 |