Skip to content

Commit 0b72a04

Browse files
committed
Deprecate set_table_name in favour of self.table_name= or defining your own method.
1 parent f73f534 commit 0b72a04

File tree

21 files changed

+138
-72
lines changed

21 files changed

+138
-72
lines changed

actionpack/test/activerecord/polymorphic_routes_test.rb

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,36 @@
22
require 'fixtures/project'
33

44
class Task < ActiveRecord::Base
5-
set_table_name 'projects'
5+
self.table_name = 'projects'
66
end
77

88
class Step < ActiveRecord::Base
9-
set_table_name 'projects'
9+
self.table_name = 'projects'
1010
end
1111

1212
class Bid < ActiveRecord::Base
13-
set_table_name 'projects'
13+
self.table_name = 'projects'
1414
end
1515

1616
class Tax < ActiveRecord::Base
17-
set_table_name 'projects'
17+
self.table_name = 'projects'
1818
end
1919

2020
class Fax < ActiveRecord::Base
21-
set_table_name 'projects'
21+
self.table_name = 'projects'
2222
end
2323

2424
class Series < ActiveRecord::Base
25-
set_table_name 'projects'
25+
self.table_name = 'projects'
2626
end
2727

2828
module Blog
2929
class Post < ActiveRecord::Base
30-
set_table_name 'projects'
30+
self.table_name = 'projects'
3131
end
3232

3333
class Blog < ActiveRecord::Base
34-
set_table_name 'projects'
34+
self.table_name = 'projects'
3535
end
3636

3737
def self.use_relative_model_naming?

actionpack/test/fixtures/developer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ class Developer < ActiveRecord::Base
55
end
66

77
class DeVeLoPeR < ActiveRecord::Base
8-
set_table_name "developers"
8+
self.table_name = "developers"
99
end

activerecord/CHANGELOG.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,21 @@
11
## Rails 3.2.0 (unreleased) ##
22

3+
* Deprecated `set_table_name`. Use `self.table_name=` instead, or define your own
4+
`self.table_name` method:
5+
6+
class Project < ActiveRecord::Base
7+
self.table_name = "project"
8+
end
9+
10+
class Post < ActiveRecord::Base
11+
def self.table_name
12+
"special_" + super
13+
end
14+
end
15+
Post.table_name # => "special_posts"
16+
17+
*Jon Leighton*
18+
319
* Generated association methods are created within a separate module to allow overriding and
420
composition using `super`. For a class named `MyModel`, the module is named
521
`MyModel::GeneratedFeatureMethods`. It is included into the model class immediately after

activerecord/lib/active_record/base.rb

Lines changed: 59 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
require 'active_support/core_ext/module/introspection'
2424
require 'active_support/core_ext/object/duplicable'
2525
require 'active_support/core_ext/object/blank'
26+
require 'active_support/deprecation'
2627
require 'arel'
2728
require 'active_record/errors'
2829
require 'active_record/log_subscriber'
@@ -624,14 +625,61 @@ def serialize(attr_name, class_name = Object)
624625
# the table name guess for an Invoice class becomes "myapp_invoices".
625626
# Invoice::Lineitem becomes "myapp_invoice_lineitems".
626627
#
627-
# You can also overwrite this class method to allow for unguessable
628-
# links, such as a Mouse class with a link to a "mice" table. Example:
628+
# You can also set your own table name explicitly:
629629
#
630630
# class Mouse < ActiveRecord::Base
631-
# set_table_name "mice"
631+
# self.table_name = "mice"
632632
# end
633+
#
634+
# Alternatively, you can override the table_name method to define your
635+
# own computation. (Possibly using <tt>super</tt> to manipulate the default
636+
# table name.) Example:
637+
#
638+
# class Post < ActiveRecord::Base
639+
# def self.table_name
640+
# "special_" + super
641+
# end
642+
# end
643+
# Post.table_name # => "special_posts"
633644
def table_name
634-
reset_table_name
645+
reset_table_name unless defined?(@table_name)
646+
@table_name
647+
end
648+
649+
# Sets the table name explicitly. Example:
650+
#
651+
# class Project < ActiveRecord::Base
652+
# self.table_name = "project"
653+
# end
654+
#
655+
# You can also just define your own <tt>self.table_name</tt> method; see
656+
# the documentation for ActiveRecord::Base#table_name.
657+
def table_name=(value)
658+
@quoted_table_name = nil
659+
@arel_table = nil
660+
@table_name = value
661+
@relation = Relation.new(self, arel_table)
662+
end
663+
664+
def set_table_name(value = nil, &block) #:nodoc:
665+
if block
666+
ActiveSupport::Deprecation.warn(
667+
"Calling set_table_name is deprecated. If you need to lazily evaluate " \
668+
"the table name, define your own `self.table_name` class method. You can use `super` " \
669+
"to get the default table name where you would have called `original_table_name`."
670+
)
671+
672+
@quoted_table_name = nil
673+
define_attr_method :table_name, value, &block
674+
@arel_table = nil
675+
@relation = Relation.new(self, arel_table)
676+
else
677+
ActiveSupport::Deprecation.warn(
678+
"Calling set_table_name is deprecated. Please use `self.table_name = 'the_name'` instead."
679+
)
680+
681+
self.table_name = value
682+
end
635683
end
636684

637685
# Returns a quoted version of the table name, used to construct SQL statements.
@@ -641,9 +689,13 @@ def quoted_table_name
641689

642690
# Computes the table name, (re)sets it internally, and returns it.
643691
def reset_table_name #:nodoc:
644-
return if abstract_class?
645-
646-
self.table_name = compute_table_name
692+
if superclass.abstract_class?
693+
self.table_name = superclass.table_name || compute_table_name
694+
elsif abstract_class?
695+
self.table_name = superclass == Base ? nil : superclass.table_name
696+
else
697+
self.table_name = compute_table_name
698+
end
647699
end
648700

649701
def full_table_name_prefix #:nodoc:
@@ -668,21 +720,6 @@ def reset_sequence_name #:nodoc:
668720
default
669721
end
670722

671-
# Sets the table name. If the value is nil or false then the value returned by the given
672-
# block is used.
673-
#
674-
# class Project < ActiveRecord::Base
675-
# set_table_name "project"
676-
# end
677-
def set_table_name(value = nil, &block)
678-
@quoted_table_name = nil
679-
define_attr_method :table_name, value, &block
680-
@arel_table = nil
681-
682-
@relation = Relation.new(self, arel_table)
683-
end
684-
alias :table_name= :set_table_name
685-
686723
# Sets the name of the inheritance column to use to the given value,
687724
# or (if the value # is nil or false) to the value returned by the
688725
# given block.

activerecord/test/cases/adapters/mysql/schema_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def setup
1414
@db_name = db
1515

1616
@omgpost = Class.new(ActiveRecord::Base) do
17-
set_table_name "#{db}.#{table}"
17+
self.table_name = "#{db}.#{table}"
1818
def self.name; 'Post'; end
1919
end
2020
end

activerecord/test/cases/adapters/mysql2/schema_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def setup
1414
@db_name = db
1515

1616
@omgpost = Class.new(ActiveRecord::Base) do
17-
set_table_name "#{db}.#{table}"
17+
self.table_name = "#{db}.#{table}"
1818
def self.name; 'Post'; end
1919
end
2020
end

activerecord/test/cases/adapters/postgresql/schema_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,23 +26,23 @@ class SchemaTest < ActiveRecord::TestCase
2626
PK_TABLE_NAME = 'table_with_pk'
2727

2828
class Thing1 < ActiveRecord::Base
29-
set_table_name "test_schema.things"
29+
self.table_name = "test_schema.things"
3030
end
3131

3232
class Thing2 < ActiveRecord::Base
33-
set_table_name "test_schema2.things"
33+
self.table_name = "test_schema2.things"
3434
end
3535

3636
class Thing3 < ActiveRecord::Base
37-
set_table_name 'test_schema."things.table"'
37+
self.table_name = 'test_schema."things.table"'
3838
end
3939

4040
class Thing4 < ActiveRecord::Base
41-
set_table_name 'test_schema."Things"'
41+
self.table_name = 'test_schema."Things"'
4242
end
4343

4444
class Thing5 < ActiveRecord::Base
45-
set_table_name 'things'
45+
self.table_name = 'things'
4646
end
4747

4848
def setup

activerecord/test/cases/adapters/postgresql/view_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class ViewTest < ActiveRecord::TestCase
1414
]
1515

1616
class ThingView < ActiveRecord::Base
17-
set_table_name 'test_schema.view_things'
17+
self.table_name = 'test_schema.view_things'
1818
end
1919

2020
def setup

activerecord/test/cases/associations/eager_load_includes_full_sti_class_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
module Namespaced
66
class Post < ActiveRecord::Base
7-
set_table_name 'posts'
7+
self.table_name = 'posts'
88
has_one :tagging, :as => :taggable, :class_name => 'Tagging'
99
end
1010
end

activerecord/test/cases/associations/has_and_belongs_to_many_associations_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
require 'active_support/core_ext/string/conversions'
2424

2525
class ProjectWithAfterCreateHook < ActiveRecord::Base
26-
set_table_name 'projects'
26+
self.table_name = 'projects'
2727
has_and_belongs_to_many :developers,
2828
:class_name => "DeveloperForProjectWithAfterCreateHook",
2929
:join_table => "developers_projects",
@@ -39,7 +39,7 @@ def add_david
3939
end
4040

4141
class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
42-
set_table_name 'developers'
42+
self.table_name = 'developers'
4343
has_and_belongs_to_many :projects,
4444
:class_name => "ProjectWithAfterCreateHook",
4545
:join_table => "developers_projects",
@@ -48,7 +48,7 @@ class DeveloperForProjectWithAfterCreateHook < ActiveRecord::Base
4848
end
4949

5050
class ProjectWithSymbolsForKeys < ActiveRecord::Base
51-
set_table_name 'projects'
51+
self.table_name = 'projects'
5252
has_and_belongs_to_many :developers,
5353
:class_name => "DeveloperWithSymbolsForKeys",
5454
:join_table => :developers_projects,
@@ -57,7 +57,7 @@ class ProjectWithSymbolsForKeys < ActiveRecord::Base
5757
end
5858

5959
class DeveloperWithSymbolsForKeys < ActiveRecord::Base
60-
set_table_name 'developers'
60+
self.table_name = 'developers'
6161
has_and_belongs_to_many :projects,
6262
:class_name => "ProjectWithSymbolsForKeys",
6363
:join_table => :developers_projects,
@@ -66,7 +66,7 @@ class DeveloperWithSymbolsForKeys < ActiveRecord::Base
6666
end
6767

6868
class DeveloperWithCounterSQL < ActiveRecord::Base
69-
set_table_name 'developers'
69+
self.table_name = 'developers'
7070
has_and_belongs_to_many :projects,
7171
:class_name => "DeveloperWithCounterSQL",
7272
:join_table => "developers_projects",

0 commit comments

Comments
 (0)