diff options
-rw-r--r-- | actionpack/test/fixtures/company.rb | 4 | ||||
-rw-r--r-- | activerecord/CHANGELOG.md | 1 | ||||
-rw-r--r-- | activerecord/lib/active_record/base.rb | 39 | ||||
-rw-r--r-- | activerecord/test/cases/base_test.rb | 8 | ||||
-rw-r--r-- | activerecord/test/models/company.rb | 2 |
5 files changed, 38 insertions, 16 deletions
diff --git a/actionpack/test/fixtures/company.rb b/actionpack/test/fixtures/company.rb index cd39ea7898..e29978801e 100644 --- a/actionpack/test/fixtures/company.rb +++ b/actionpack/test/fixtures/company.rb @@ -1,10 +1,10 @@ class Company < ActiveRecord::Base has_one :mascot attr_protected :rating - set_sequence_name :companies_nonstd_seq + self.sequence_name = :companies_nonstd_seq validates_presence_of :name def validate errors.add('rating', 'rating should not be 2') if rating == 2 end -end
\ No newline at end of file +end diff --git a/activerecord/CHANGELOG.md b/activerecord/CHANGELOG.md index 2e226cc55f..be2c761faf 100644 --- a/activerecord/CHANGELOG.md +++ b/activerecord/CHANGELOG.md @@ -4,6 +4,7 @@ * `set_table_name` * `set_inheritance_column` + * `set_sequence_name` Use an assignment method instead. For example, instead of `set_table_name`, use `self.table_name=`: diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 40fa010558..9f173a4bcc 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -734,16 +734,16 @@ module ActiveRecord #:nodoc: end end - # Lazy-set the sequence name to the connection's default. This method - # is only ever called once since set_sequence_name overrides it. - def sequence_name #:nodoc: - reset_sequence_name + def sequence_name + if superclass == Base + @sequence_name ||= reset_sequence_name + else + (@sequence_name ||= nil) || superclass.sequence_name + end end def reset_sequence_name #:nodoc: - default = connection.default_sequence_name(table_name, primary_key) - set_sequence_name(default) - default + self.sequence_name = connection.default_sequence_name(table_name, primary_key) end # Sets the name of the sequence to use when generating ids to the given @@ -758,12 +758,29 @@ module ActiveRecord #:nodoc: # will discover the sequence corresponding to your primary key for you. # # class Project < ActiveRecord::Base - # set_sequence_name "projectseq" # default would have been "project_seq" + # self.sequence_name = "projectseq" # default would have been "project_seq" # end - def set_sequence_name(value = nil, &block) - define_attr_method :sequence_name, value, &block + def sequence_name=(value) + @sequence_name = value.to_s + end + + def set_sequence_name(value = nil, &block) #:nodoc: + if block + ActiveSupport::Deprecation.warn( + "Calling set_sequence_name is deprecated. If you need to lazily evaluate " \ + "the sequence name, define your own `self.sequence_name` class method. You can use `super` " \ + "to get the default sequence name where you would have called `original_sequence_name`." + ) + + define_attr_method :sequence_name, value, &block + else + ActiveSupport::Deprecation.warn( + "Calling set_sequence_name is deprecated. Please use `self.sequence_name = 'the_name'` instead." + ) + + self.sequence_name = value + end end - alias :sequence_name= :set_sequence_name # Indicates whether the table associated with this class exists def table_exists? diff --git a/activerecord/test/cases/base_test.rb b/activerecord/test/cases/base_test.rb index 9fc9197aa5..3a84e40f57 100644 --- a/activerecord/test/cases/base_test.rb +++ b/activerecord/test/cases/base_test.rb @@ -1515,7 +1515,9 @@ class BasicsTest < ActiveRecord::TestCase k.sequence_name = "foo" assert_equal "foo", k.sequence_name - k.set_sequence_name "bar" + assert_deprecated do + k.set_sequence_name "bar" + end assert_equal "bar", k.sequence_name end @@ -1525,7 +1527,9 @@ class BasicsTest < ActiveRecord::TestCase orig_name = k.sequence_name if orig_name - k.set_sequence_name { original_sequence_name + "_lol" } + 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" diff --git a/activerecord/test/models/company.rb b/activerecord/test/models/company.rb index 78eb4c57ac..fe9c465c81 100644 --- a/activerecord/test/models/company.rb +++ b/activerecord/test/models/company.rb @@ -4,7 +4,7 @@ end class Company < AbstractCompany attr_protected :rating - set_sequence_name :companies_nonstd_seq + self.sequence_name = :companies_nonstd_seq validates_presence_of :name |